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

import (
	"dfss/dfssc/common"
	"fmt"
	"os"
	"testing"
ElyKar's avatar
ElyKar committed
8 9 10

	"github.com/spf13/viper"
	"github.com/stretchr/testify/assert"
Caro Axel's avatar
Caro Axel committed
11 12
)

ElyKar's avatar
ElyKar committed
13 14 15 16
func mockb(fca, fcert, addrPort string) *viper.Viper {
	return common.MockViper("file_ca", fca, "file_cert", fcert, "platform_addrport", addrPort)
}

Caro Axel's avatar
Caro Axel committed
17
func TestAuthenticationValidation(t *testing.T) {
ElyKar's avatar
ElyKar committed
18
	_, err := NewAuthManager("dummy", "token", mockb("fca", fcert, addrPort))
19
	assert.True(t, err != nil, "Email is invalid")
Caro Axel's avatar
Caro Axel committed
20 21 22

	f, _ := os.Create(fcert)
	_ = f.Close()
ElyKar's avatar
ElyKar committed
23
	_, err = NewAuthManager("mail@mail.mail", "token", mockb("fca", fcert, addrPort))
24
	assert.True(t, err != nil, "Cert file already there")
Caro Axel's avatar
Caro Axel committed
25 26

	_ = os.Remove(fcert)
ElyKar's avatar
ElyKar committed
27
	_, err = NewAuthManager("mail@mail.mail", "token", mockb("fca", fcert, addrPort))
28
	assert.True(t, err != nil, "CA file not there")
Caro Axel's avatar
Caro Axel committed
29

ElyKar's avatar
ElyKar committed
30
	_, err = NewAuthManager("mail@mail.mail", "token", mockb(fca, fcert, addrPort))
Caro Axel's avatar
Caro Axel committed
31 32 33 34
	assert.Equal(t, err, nil)
}

func ExampleAuthenticate() {
ElyKar's avatar
ElyKar committed
35
	manager, err := NewAuthManager("mail@mail.mail", "token", mockb(fca, fcert, addrPort))
Caro Axel's avatar
Caro Axel committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	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
}