/
mimsrv_test.go
54 lines (46 loc) · 1.25 KB
/
mimsrv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
53
54
package main
import (
"bytes"
"io/ioutil"
"os"
"testing"
"github.com/jimmc/mimsrv/auth"
)
const (
maxClockSkewSeconds = 2
)
func TestPasswordFile(t *testing.T) {
pwfile := "testdata/password.tmp"
bakfile := pwfile + "~"
defer os.Remove(pwfile)
defer os.Remove(bakfile)
_ = os.Remove(pwfile) // Pre-clean
authHandler := auth.NewHandler(&auth.Config{
Prefix: "/auth/",
PasswordFilePath: pwfile,
MaxClockSkewSeconds: maxClockSkewSeconds,
})
err := authHandler.CreatePasswordFile()
if err != nil {
t.Errorf("Should have created password file %s: %v", pwfile, err)
}
err = authHandler.CreatePasswordFile()
if err == nil {
t.Errorf("CreatePasswordFile should fail when file exists")
}
err = authHandler.UpdatePassword("user1", "pw1")
if err != nil {
t.Fatalf("Error updating password file %s: %v", pwfile, err)
}
pwgot, err := ioutil.ReadFile(pwfile)
if err != nil {
t.Fatalf("Failed to read temp password file: %v", err)
}
pwwant, err := ioutil.ReadFile("testdata/pw1-golden.txt")
if err != nil {
t.Fatalf("Failed to read reference password file: %v", err)
}
if !bytes.Equal(pwgot, pwwant) {
t.Errorf("password file contents don't match, got '%s', want '%s'", pwgot, pwwant)
}
}