authentication_test.go 1.31 KB
Newer Older
Caro Axel's avatar
Caro Axel committed
1 2 3 4 5
package user

import (
	"dfss/dfssc/common"
	"fmt"
6
	"github.com/stretchr/testify/assert"
Caro Axel's avatar
Caro Axel committed
7 8 9 10 11 12
	"os"
	"testing"
)

func TestAuthenticationValidation(t *testing.T) {
	_, err := NewAuthManager("fca", fcert, addrPort, "dummy", "token")
13
	assert.True(t, err != nil, "Email is invalid")
Caro Axel's avatar
Caro Axel committed
14 15 16 17

	f, _ := os.Create(fcert)
	_ = f.Close()
	_, err = NewAuthManager("fca", fcert, addrPort, "mail@mail.mail", "token")
18
	assert.True(t, err != nil, "Cert file already there")
Caro Axel's avatar
Caro Axel committed
19 20 21

	_ = os.Remove(fcert)
	_, err = NewAuthManager("fca", fcert, addrPort, "mail@mail.mail", "token")
22
	assert.True(t, err != nil, "CA file not there")
Caro Axel's avatar
Caro Axel committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

	_, err = NewAuthManager(fca, fcert, addrPort, "mail@mail.mail", "token")
	assert.Equal(t, err, nil)
}

func ExampleAuthenticate() {
	manager, err := NewAuthManager(fca, fcert, addrPort, "mail@mail.mail", "token")
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("Manager successfully created")

	err = manager.Authenticate()
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("Manager said authentication went fine")

	if b := common.FileExists(fcert); !b {
		fmt.Println("The cert file was not saved to disk")
	} else {
		fmt.Println("The cert file was saved to disk")
		_ = os.Remove(fcert)
	}

	// Output:
	// Manager successfully created
	// Manager said authentication went fine
	// The cert file was saved to disk
}