-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_test.go
89 lines (73 loc) · 1.8 KB
/
auth_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ldap
import (
"errors"
"os"
"strings"
"testing"
)
func TestAuthWithReaduser(t *testing.T) {
wantSAMAccountName := "testuser"
wantCN := "Test User"
l, err := getWorkingLdap()
if err != nil {
t.Error(err)
return
}
user, err := l.CheckPasswordForSAMAccountName(wantSAMAccountName, "testuser")
if err != nil {
t.Error(err)
return
}
if !strings.EqualFold(user.CN(), wantCN) {
t.Errorf("expected CN %q, got %q", wantCN, user.CN())
}
if !strings.EqualFold(user.SAMAccountName, wantSAMAccountName) {
t.Errorf("expected CN %q, got %q", wantSAMAccountName, user.SAMAccountName)
}
}
func TestAuthWithNonexistantUser(t *testing.T) {
l, err := getWorkingLdap()
if err != nil {
t.Error(err)
return
}
_, err = l.CheckPasswordForSAMAccountName("invalidtestuser", "thisshouldfail")
if err != ErrUserNotFound {
t.Errorf("expected error %q, got %q", ErrUserNotFound, err)
}
}
func TestAuthWithInvalidCredentials(t *testing.T) {
l, err := getWorkingLdap()
if err != nil {
t.Error(err)
return
}
_, err = l.CheckPasswordForSAMAccountName("testuser", "thisshouldfail")
if err == nil {
t.Errorf("expected error, got %q", err)
}
}
func getWorkingLdap() (*LDAP, error) {
server, found := os.LookupEnv("LDAP_SERVER")
if !found {
return nil, errors.New("LDAP_SERVER not set")
}
baseDN, found := os.LookupEnv("LDAP_BASE_DN")
if !found {
return nil, errors.New("LDAP_BASE_DN not set")
}
readUser, found := os.LookupEnv("LDAP_READ_USER")
if !found {
return nil, errors.New("LDAP_READ_USER not set")
}
readPassword, found := os.LookupEnv("LDAP_READ_PASSWORD")
if !found {
return nil, errors.New("LDAP_READ_PASSWORD not set")
}
config := Config{
Server: server,
BaseDN: baseDN,
IsActiveDirectory: true,
}
return New(config, readUser, readPassword)
}