Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit 217ada4

Browse files
authored
Merge pull request #17 from tyrannosaurus-becks/fix-login-test
Fix login test
2 parents 05ce750 + 6016512 commit 217ada4

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

path_login_test.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package kerberos
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67
"testing"
78

89
"github.com/hashicorp/vault/logical"
10+
"github.com/ory/dockertest"
11+
"gopkg.in/ldap.v3"
912
)
1013

1114
func setupTestBackend(t *testing.T) (logical.Backend, logical.Storage) {
@@ -34,6 +37,23 @@ func setupTestBackend(t *testing.T) (logical.Backend, logical.Storage) {
3437
func TestLogin(t *testing.T) {
3538
b, storage := setupTestBackend(t)
3639

40+
cleanup, connURL := prepareLDAPTestContainer(t)
41+
defer cleanup()
42+
43+
ldapReq := &logical.Request{
44+
Operation: logical.UpdateOperation,
45+
Path: "config/ldap",
46+
Storage: storage,
47+
Data: map[string]interface{}{
48+
"url": connURL,
49+
},
50+
}
51+
52+
resp, err := b.HandleRequest(context.Background(), ldapReq)
53+
if err != nil || (resp != nil && resp.IsError()) {
54+
t.Fatalf("err: %s resp: %#v\n", err, resp)
55+
}
56+
3757
data := map[string]interface{}{
3858
"authorization": "",
3959
}
@@ -45,11 +65,70 @@ func TestLogin(t *testing.T) {
4565
Data: data,
4666
}
4767

48-
resp, err := b.HandleRequest(context.Background(), req)
68+
resp, err = b.HandleRequest(context.Background(), req)
4969
if err != nil || resp == nil {
5070
t.Fatalf("err: %s resp: %#v\n", err, resp)
5171
}
5272
if !resp.IsError() && !strings.HasPrefix(resp.Error().Error(), "Missing or invalid authorization") {
5373
t.Fatalf("err: %s resp: %#v\n", err, resp)
5474
}
5575
}
76+
77+
func prepareLDAPTestContainer(t *testing.T) (cleanup func(), retURL string) {
78+
pool, err := dockertest.NewPool("")
79+
if err != nil {
80+
t.Fatalf("Failed to connect to docker: %s", err)
81+
}
82+
83+
runOpts := &dockertest.RunOptions{
84+
Repository: "osixia/openldap",
85+
Tag: "latest",
86+
Env: []string{"LDAP_TLS=false"},
87+
}
88+
resource, err := pool.RunWithOptions(runOpts)
89+
if err != nil {
90+
t.Fatalf("Could not start local MSSQL docker container: %s", err)
91+
}
92+
93+
cleanup = func() {
94+
if err := pool.Purge(resource); err != nil {
95+
t.Fatalf("Failed to cleanup local container: %s", err)
96+
}
97+
}
98+
99+
ldapAddr := fmt.Sprintf("localhost:%s", resource.GetPort("389/tcp"))
100+
retURL = "ldap://" + ldapAddr
101+
102+
// exponential backoff-retry
103+
if err = pool.Retry(func() error {
104+
conn, err := ldap.Dial("tcp", ldapAddr)
105+
if err != nil {
106+
return err
107+
}
108+
defer conn.Close()
109+
110+
if err := conn.Bind("cn=admin,dc=example,dc=org", "admin"); err != nil {
111+
return err
112+
}
113+
114+
searchRequest := ldap.NewSearchRequest(
115+
"dc=example,dc=org",
116+
ldap.ScopeWholeSubtree,
117+
ldap.NeverDerefAliases,
118+
0,
119+
0,
120+
false,
121+
"(&(objectClass=*))",
122+
[]string{"dn", "cn"},
123+
nil,
124+
)
125+
if _, err := conn.Search(searchRequest); err != nil {
126+
return err
127+
}
128+
return nil
129+
}); err != nil {
130+
t.Fatalf("Could not connect to ldap auth docker container: %s", err)
131+
}
132+
133+
return
134+
}

0 commit comments

Comments
 (0)