1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-21 06:09:42 +02:00
documize/domain/auth/ldap/public_test.go

124 lines
3.2 KiB
Go
Raw Permalink Normal View History

2018-08-30 16:01:36 +01:00
// Copyright 2016 Documize IntestConfigPublicLDAP. <legal@documize.com>. All rights reserved.
2018-08-28 10:19:22 +01:00
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package ldap
import (
"testing"
lm "github.com/documize/community/model/auth"
)
// Works against https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
2018-08-30 16:01:36 +01:00
var testConfigPublicLDAP = lm.LDAPConfig{
ServerType: lm.ServerTypeLDAP,
ServerHost: "ldap.forumsys.com",
ServerPort: 389,
2018-09-01 14:50:37 +01:00
EncryptionType: lm.EncryptionTypeNone,
2018-08-30 16:01:36 +01:00
BaseDN: "dc=example,dc=com",
BindDN: "cn=read-only-admin,dc=example,dc=com",
BindPassword: "password",
UserFilter: "",
GroupFilter: "",
AttributeUserRDN: "uid",
AttributeUserFirstname: "givenName",
AttributeUserLastname: "sn",
AttributeUserEmail: "mail",
AttributeUserDisplayName: "",
AttributeUserGroupName: "",
AttributeGroupMember: "uniqueMember",
}
2018-08-28 10:19:22 +01:00
func TestUserFilter_PublicLDAP(t *testing.T) {
2018-08-30 16:01:36 +01:00
testConfigPublicLDAP.UserFilter = "(|(objectClass=person)(objectClass=user)(objectClass=inetOrgPerson))"
2018-08-28 10:19:22 +01:00
e, err := executeUserFilter(testConfigPublicLDAP)
2018-08-28 10:19:22 +01:00
if err != nil {
t.Error("unable to exeucte user filter", err.Error())
2018-08-28 10:19:22 +01:00
return
}
if len(e) == 0 {
t.Error("Received ZERO LDAP search entries")
2018-08-28 10:19:22 +01:00
return
}
t.Logf("LDAP search entries found: %d", len(e))
for _, u := range e {
2018-08-30 16:01:36 +01:00
t.Logf("[%s] %s (%s %s) @ %s\n",
u.RemoteID, u.CN, u.Firstname, u.Lastname, u.Email)
2018-08-28 10:19:22 +01:00
}
}
2018-09-01 14:50:37 +01:00
func TestGroupFilter_PublicLDAP(t *testing.T) {
2018-08-30 16:01:36 +01:00
testConfigPublicLDAP.GroupFilter = "(|(ou=mathematicians)(ou=chemists))"
2018-09-01 14:50:37 +01:00
e, err := executeGroupFilter(testConfigPublicLDAP)
if err != nil {
2018-09-01 14:50:37 +01:00
t.Error("unable to exeucte group filter", err.Error())
return
}
2018-09-01 14:50:37 +01:00
if len(e) == 0 {
t.Error("Received zero group LDAP search entries")
return
}
2018-09-01 14:50:37 +01:00
t.Logf("LDAP group search entries found: %d", len(e))
2018-09-01 14:50:37 +01:00
for _, u := range e {
t.Logf("[%s] %s (%s %s) @ %s\n",
u.RemoteID, u.CN, u.Firstname, u.Lastname, u.Email)
}
}
func TestAuthenticate_PublicLDAP(t *testing.T) {
l, err := connect(testConfigPublicLDAP)
if err != nil {
2018-08-30 16:01:36 +01:00
t.Error("Error: unable to dial LDAP server: ", err.Error())
return
}
defer l.Close()
2018-09-04 17:19:26 +01:00
user, ok, err := authenticate(l, testConfigPublicLDAP, "newton", "password")
if err != nil {
t.Error("error during LDAP authentication: ", err.Error())
return
}
if !ok {
t.Error("failed LDAP authentication")
2018-09-04 17:19:26 +01:00
return
}
2018-09-04 17:19:26 +01:00
t.Log("Authenticated", user.Email)
}
func TestNotAuthenticate_PublicLDAP(t *testing.T) {
l, err := connect(testConfigPublicLDAP)
if err != nil {
t.Error("Error: unable to dial LDAP server: ", err.Error())
return
}
defer l.Close()
2018-09-04 17:19:26 +01:00
_, ok, err := authenticate(l, testConfigPublicLDAP, "junk", "junk")
if err != nil {
t.Error("error during LDAP authentication: ", err.Error())
return
}
if ok {
t.Error("incorrect LDAP authentication")
2018-09-04 17:19:26 +01:00
return
}
t.Log("Not authenticated")
}