Skip to content

Commit

Permalink
make tests more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
saltyblu committed Jun 28, 2017
1 parent 34b41ac commit 28cacd9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
50 changes: 26 additions & 24 deletions htpasswd/auth_test.go
Expand Up @@ -21,7 +21,7 @@ bob-foo:{fooo}sdcsdcsdc/BfQ=
`

func TestAuth_Hashes(t *testing.T) {
auth, err := NewAuth([]string{writeTmpfile(testfile)})
auth, err := NewAuth(writeTmpfile(testfile))
NoError(t, err)

testUsers := []string{"bob-md5", "bob-bcrypt", "bob-sha"}
Expand All @@ -39,10 +39,9 @@ func TestAuth_Hashes(t *testing.T) {
}

func TestAuth_ReloadFile(t *testing.T) {
file := writeTmpfile(`bob:$apr1$IDZSCL/o$N68zaFDDRivjour94OVeB.`)
filenames := []string{file}
files := writeTmpfile(`bob:$apr1$IDZSCL/o$N68zaFDDRivjour94OVeB.`)

auth, err := NewAuth(filenames)
auth, err := NewAuth(files)
NoError(t, err)

authenticated, err := auth.Authenticate("bob", "secret")
Expand All @@ -56,7 +55,7 @@ func TestAuth_ReloadFile(t *testing.T) {
// The refresh is time based, so we have to wait a second, here
time.Sleep(time.Second)

err = ioutil.WriteFile(file, []byte(`alice:$apr1$IDZSCL/o$N68zaFDDRivjour94OVeB.`), 06644)
err = ioutil.WriteFile(files[0], []byte(`alice:$apr1$IDZSCL/o$N68zaFDDRivjour94OVeB.`), 06644)
NoError(t, err)

authenticated, err = auth.Authenticate("bob", "secret")
Expand All @@ -69,10 +68,9 @@ func TestAuth_ReloadFile(t *testing.T) {
}

func TestAuth_ReloadFileDeleted(t *testing.T) {
file := writeTmpfile(`bob:$apr1$IDZSCL/o$N68zaFDDRivjour94OVeB.`)
filenames := []string{file}
files := writeTmpfile(`bob:$apr1$IDZSCL/o$N68zaFDDRivjour94OVeB.`)

auth, err := NewAuth(filenames)
auth, err := NewAuth(files)
NoError(t, err)

authenticated, err := auth.Authenticate("bob", "secret")
Expand All @@ -87,7 +85,7 @@ func TestAuth_ReloadFileDeleted(t *testing.T) {
time.Sleep(time.Second)

// delete file and load auth from "cache"
_ = os.Remove(file)
_ = os.Remove(files[0])

authenticated, err = auth.Authenticate("bob", "secret")
NoError(t, err)
Expand All @@ -96,7 +94,7 @@ func TestAuth_ReloadFileDeleted(t *testing.T) {
}

func TestAuth_UnknownUser(t *testing.T) {
auth, err := NewAuth([]string{writeTmpfile(testfile)})
auth, err := NewAuth(writeTmpfile(testfile))
NoError(t, err)

authenticated, err := auth.Authenticate("unknown", "secret")
Expand All @@ -110,16 +108,16 @@ func TestAuth_ErrorOnMissingFile(t *testing.T) {
}

func TestAuth_ErrorOnInvalidFileContents(t *testing.T) {
_, err := NewAuth([]string{writeTmpfile("foo bar bazz")})
_, err := NewAuth(writeTmpfile("foo bar bazz"))
Error(t, err)

_, err = NewAuth([]string{writeTmpfile("foo:bar\nfoo:bar:bazz")})
_, err = NewAuth(writeTmpfile("foo:bar\nfoo:bar:bazz"))
Error(t, err)
}

func TestAuth_BadMD5Format(t *testing.T) {
// missing $ separator in md5 hash
a, err := NewAuth([]string{writeTmpfile("foo:$apr1$IDZSCL/oN68zaFDDRivjour94OVeB.")})
a, err := NewAuth(writeTmpfile("foo:$apr1$IDZSCL/oN68zaFDDRivjour94OVeB."))
NoError(t, err)

authenticated, err := a.Authenticate("foo", "secret")
Expand All @@ -128,23 +126,27 @@ func TestAuth_BadMD5Format(t *testing.T) {
}

func TestAuth_Hashes_UnknownAlgoError(t *testing.T) {
auth, err := NewAuth([]string{writeTmpfile(testfile)})
auth, err := NewAuth(writeTmpfile(testfile))
NoError(t, err)

authenticated, err := auth.Authenticate("bob-foo", "secret")
Error(t, err)
False(t, authenticated)
}

func writeTmpfile(contents string) string {
f, err := ioutil.TempFile("", "loginsrv_htpasswdtest")
if err != nil {
panic(err)
func writeTmpfile(contents ...string) []string {
var names []string
for _, curContent := range contents {
f, err := ioutil.TempFile("", "loginsrv_htpasswdtest")
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.WriteString(curContent)
if err != nil {
panic(err)
}
names = append(names, f.Name())
}
defer f.Close()
_, err = f.WriteString(contents)
if err != nil {
panic(err)
}
return f.Name()
return names
}
14 changes: 7 additions & 7 deletions htpasswd/backend_test.go
Expand Up @@ -12,14 +12,14 @@ func TestSetupOneFile(t *testing.T) {
True(t, exist)
NotNil(t, p)

file := writeTmpfile(testfile)
files := writeTmpfile(testfile)
backend, err := p(map[string]string{
"file": file,
"file": files[0],
})

NoError(t, err)
Equal(t,
[]File{File{name: file}},
[]File{File{name: files[0]}},
backend.(*Backend).auth.filenames)
}

Expand All @@ -28,7 +28,7 @@ func TestSetupTwoFiles(t *testing.T) {
True(t, exist)
NotNil(t, p)

filenames := []string{writeTmpfile(testfile), writeTmpfile(testfile)}
filenames := writeTmpfile(testfile, testfile)

var morphed []File
for _, curFile := range filenames {
Expand All @@ -49,8 +49,8 @@ func TestSetupTwoConfigs(t *testing.T) {
True(t, exist)
NotNil(t, p)

configFiles := []string{writeTmpfile(testfile), writeTmpfile(testfile)}
configFile := []string{writeTmpfile(testfile), writeTmpfile(testfile)}
configFiles := writeTmpfile(testfile, testfile)
configFile := writeTmpfile(testfile, testfile)

var morphed []File
for _, curFile := range append(configFiles, configFile...) {
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestSetup_Error(t *testing.T) {
}

func TestSimpleBackend_Authenticate(t *testing.T) {
backend, err := NewBackend([]string{writeTmpfile(testfile)})
backend, err := NewBackend(writeTmpfile(testfile))
NoError(t, err)

authenticated, userInfo, err := backend.Authenticate("bob-bcrypt", "secret")
Expand Down

0 comments on commit 28cacd9

Please sign in to comment.