-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmeta_store_auth_test.go
91 lines (80 loc) · 2.17 KB
/
meta_store_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
90
91
package main
import (
"fmt"
"github.com/nmcclain/ldap"
"os"
"os/exec"
"strings"
"testing"
)
var ()
func TestNewLdapConnection(t *testing.T) {
setupMetaAuth()
defer tearDownMetaAuth()
lh, err := ldapHost()
if err != nil {
t.Errorf("Unable to process LDAP host %s", err.Error())
}
if lh.Host != "localhost:1389" {
t.Errorf("Wrong ldap host. expected localhost but got %s", lh.Host)
}
_, err = NewLdapConnection()
if err != nil {
t.Errorf("Errored trying to connect to ldap, %s", err.Error())
}
lbind := LdapBind(testUser, testPass)
if !lbind {
t.Errorf("Failed to bind as %s", testUser)
}
}
func TestLdapBind(t *testing.T) {
setupMetaAuth()
defer tearDownMetaAuth()
lbind := LdapBind(testUser, testPass)
if !lbind {
t.Errorf("Failed to bind as %s", testUser)
}
lbind = LdapBind(testUser, "badpass")
if lbind {
t.Errorf("Bound as %s but it should have failed", testUser)
}
}
func TestLdapSearch(t *testing.T) {
setupMetaAuth()
defer tearDownMetaAuth()
fltr := fmt.Sprintf("(&(objectClass=%s)(%s=%s))", Config.Ldap.UserObjectClass, Config.Ldap.UserCn, testUser)
base := fmt.Sprintf("%s=%s,%s", Config.Ldap.UserCn, testUser, Config.Ldap.Base)
search := &ldap.SearchRequest{
BaseDN: base,
Filter: fltr,
}
lsearch, err := LdapSearch(search)
if err != nil {
t.Errorf("Failed looking for user %s error: %s", testUser, err.Error())
}
found := false
for _, e := range lsearch.Entries {
if strings.Contains(e.DN, testUser) {
found = true
}
}
if !found {
t.Errorf("Failed to find user %s error: %s", testUser, err.Error())
}
}
func tearDownMetaAuth() error {
// Set back to defaults
Config.Ldap = &LdapConfig{Enabled: false, Server: "ldap://localhost:1389", Base: "dc=testers,c=test,o=company",
UserObjectClass: "objectclass=person", UserCn: "uid"}
exec.Command("pkill test_ldap_server").Run()
return nil
}
func setupMetaAuth() error {
Config.Ldap = &LdapConfig{Enabled: true, Server: "ldap://localhost:1389", Base: "o=company",
UserObjectClass: "posixaccount", UserCn: "uid", BindPass: "admin"}
rme := exec.Command("test_ldap_server/test_ldap_server")
wd, _ := os.Getwd()
rme.Dir = wd
rme.Start() // run and forget about it
return nil
}