loginsrv

Unnamed repository; edit this file 'description' to name the repository.
git clone git@jamesshield.xyz:repos/loginsrv.git
Log | Files | Refs | README | LICENSE

backend_test.go (1760B)


      1 package htpasswd
      2 
      3 import (
      4 	. "github.com/stretchr/testify/assert"
      5 	"github.com/tarent/loginsrv/login"
      6 	"os"
      7 	"strings"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func TestSetupOneFile(t *testing.T) {
     13 	p, exist := login.GetProvider(ProviderName)
     14 	True(t, exist)
     15 	NotNil(t, p)
     16 
     17 	files := writeTmpfile(testfile)
     18 	backend, err := p(map[string]string{
     19 		"file": files[0],
     20 	})
     21 
     22 	NoError(t, err)
     23 	Equal(t,
     24 		[]File{File{files[0], modTime(files[0])}},
     25 		backend.(*Backend).auth.filenames)
     26 }
     27 
     28 func TestSetupTwoFiles(t *testing.T) {
     29 	p, exist := login.GetProvider(ProviderName)
     30 	True(t, exist)
     31 	NotNil(t, p)
     32 
     33 	filenames := writeTmpfile(testfile, testfile)
     34 
     35 	var morphed []File
     36 	for _, curFile := range filenames {
     37 		morphed = append(morphed, File{curFile, modTime(curFile)})
     38 	}
     39 	backend, err := p(map[string]string{
     40 		"file": strings.Join(filenames, ";"),
     41 	})
     42 
     43 	NoError(t, err)
     44 	Equal(t,
     45 		morphed,
     46 		backend.(*Backend).auth.filenames)
     47 }
     48 
     49 func TestSetup_Error(t *testing.T) {
     50 	p, exist := login.GetProvider(ProviderName)
     51 	True(t, exist)
     52 	NotNil(t, p)
     53 
     54 	_, err := p(map[string]string{})
     55 	Error(t, err)
     56 }
     57 
     58 func TestSimpleBackend_Authenticate(t *testing.T) {
     59 	backend, err := NewBackend(writeTmpfile(testfile))
     60 	NoError(t, err)
     61 
     62 	authenticated, userInfo, err := backend.Authenticate("bob-bcrypt", "secret")
     63 	True(t, authenticated)
     64 	Equal(t, "bob-bcrypt", userInfo.Sub)
     65 	NoError(t, err)
     66 
     67 	authenticated, userInfo, err = backend.Authenticate("bob-bcrypt", "fooo")
     68 	False(t, authenticated)
     69 	Equal(t, "", userInfo.Sub)
     70 	NoError(t, err)
     71 
     72 	authenticated, userInfo, err = backend.Authenticate("", "")
     73 	False(t, authenticated)
     74 	Equal(t, "", userInfo.Sub)
     75 	NoError(t, err)
     76 }
     77 
     78 func modTime(f string) time.Time {
     79 	fileInfo, err := os.Stat(f)
     80 	if err != nil {
     81 		panic(err)
     82 	}
     83 	return fileInfo.ModTime()
     84 }