summaryrefslogtreecommitdiff
path: root/internal/authentication/ldap_user_provider_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/authentication/ldap_user_provider_test.go')
-rw-r--r--internal/authentication/ldap_user_provider_test.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/internal/authentication/ldap_user_provider_test.go b/internal/authentication/ldap_user_provider_test.go
index fc77b7131..cabc60d6d 100644
--- a/internal/authentication/ldap_user_provider_test.go
+++ b/internal/authentication/ldap_user_provider_test.go
@@ -134,3 +134,111 @@ func TestShouldEscapeUserInput(t *testing.T) {
ldapClient.getUserAttribute(mockConn, "john=abc", "dn")
}
+
+func createSearchResultWithAttributes(attributes ...*ldap.EntryAttribute) *ldap.SearchResult {
+ return &ldap.SearchResult{
+ Entries: []*ldap.Entry{
+ &ldap.Entry{Attributes: attributes},
+ },
+ }
+}
+
+func createSearchResultWithAttributeValues(values ...string) *ldap.SearchResult {
+ return createSearchResultWithAttributes(&ldap.EntryAttribute{
+ Values: values,
+ })
+}
+
+func TestShouldNotCrashWhenGroupsAreNotRetrievedFromLDAP(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ defer ctrl.Finish()
+
+ mockFactory := NewMockLDAPConnectionFactory(ctrl)
+ mockConn := NewMockLDAPConnection(ctrl)
+
+ ldapClient := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
+ URL: "ldap://127.0.0.1:389",
+ User: "cn=admin,dc=example,dc=com",
+ Password: "password",
+ UsersFilter: "uid={0}",
+ AdditionalUsersDN: "ou=users",
+ BaseDN: "dc=example,dc=com",
+ }, mockFactory)
+
+ mockFactory.EXPECT().
+ Dial(gomock.Eq("tcp"), gomock.Eq("127.0.0.1:389")).
+ Return(mockConn, nil).Times(2)
+
+ mockConn.EXPECT().
+ Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
+ Return(nil).
+ Times(2)
+
+ mockConn.EXPECT().
+ Close().Times(2)
+
+ searchGroups := mockConn.EXPECT().
+ Search(gomock.Any()).
+ Return(createSearchResultWithAttributes(), nil)
+ searchUserDN := mockConn.EXPECT().
+ Search(gomock.Any()).
+ Return(createSearchResultWithAttributeValues("uid=john,dc=example,dc=com"), nil)
+ searchEmails := mockConn.EXPECT().
+ Search(gomock.Any()).
+ Return(createSearchResultWithAttributeValues("test@example.com"), nil)
+
+ gomock.InOrder(searchGroups, searchUserDN, searchEmails)
+
+ details, err := ldapClient.GetDetails("john")
+ require.NoError(t, err)
+
+ assert.ElementsMatch(t, details.Groups, []string{})
+ assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
+}
+
+func TestShouldNotCrashWhenEmailsAreNotRetrievedFromLDAP(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ defer ctrl.Finish()
+
+ mockFactory := NewMockLDAPConnectionFactory(ctrl)
+ mockConn := NewMockLDAPConnection(ctrl)
+
+ ldapClient := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
+ URL: "ldap://127.0.0.1:389",
+ User: "cn=admin,dc=example,dc=com",
+ Password: "password",
+ UsersFilter: "uid={0}",
+ AdditionalUsersDN: "ou=users",
+ BaseDN: "dc=example,dc=com",
+ }, mockFactory)
+
+ mockFactory.EXPECT().
+ Dial(gomock.Eq("tcp"), gomock.Eq("127.0.0.1:389")).
+ Return(mockConn, nil).Times(2)
+
+ mockConn.EXPECT().
+ Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
+ Return(nil).
+ Times(2)
+
+ mockConn.EXPECT().
+ Close().Times(2)
+
+ searchGroups := mockConn.EXPECT().
+ Search(gomock.Any()).
+ Return(createSearchResultWithAttributeValues("group1", "group2"), nil)
+ searchUserDN := mockConn.EXPECT().
+ Search(gomock.Any()).
+ Return(createSearchResultWithAttributeValues("uid=john,dc=example,dc=com"), nil)
+ searchEmails := mockConn.EXPECT().
+ Search(gomock.Any()).
+ Return(createSearchResultWithAttributes(), nil)
+
+ gomock.InOrder(searchGroups, searchUserDN, searchEmails)
+
+ details, err := ldapClient.GetDetails("john")
+ require.NoError(t, err)
+
+ assert.ElementsMatch(t, details.Groups, []string{"group1", "group2"})
+ assert.ElementsMatch(t, details.Emails, []string{})
+}