Skip to content

Commit

Permalink
Make failure assertions strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen2112 committed Feb 9, 2022
1 parent f9b2bf9 commit 49abcd3
Showing 1 changed file with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public void testSingleBindPattern()
.setUserBindSearchPatterns("uid=${USER}," + organization.getDistinguishedName()));

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("alice", "invalid"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: Invalid credentials");
assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("unknown", "alice-pass"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(RuntimeException.class)
.hasMessageMatching("Access Denied: Invalid credentials");
assertEquals(ldapAuthenticator.createAuthenticatedPrincipal("alice", "alice-pass"), new BasicPrincipal("alice"));
}
}
Expand Down Expand Up @@ -114,11 +116,16 @@ public void testGroupMembership()
.setGroupAuthorizationSearchPattern(format("(&(objectClass=groupOfNames)(cn=group_*)(member=uid=${USER},%s))", organization.getDistinguishedName())));

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("alice", "invalid"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: Invalid credentials");

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("unknown", "alice-pass"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: Invalid credentials");

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("bob", "bob-pass"))
.isInstanceOf(AccessDeniedException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: User \\[bob] not a member of an authorized group");

openLdapServer.addUserToGroup(alice, group);
assertEquals(ldapAuthenticator.createAuthenticatedPrincipal("alice", "alice-pass"), new BasicPrincipal("alice"));
Expand All @@ -139,7 +146,8 @@ public void testInvalidBindPassword()
.setBindPassword("invalid-password"));

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("alice", "alice-pass"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: Invalid credentials");
}
}

Expand All @@ -160,32 +168,38 @@ public void testDistinguishedNameLookup()
.setBindPassword("admin"));

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("unknown_user", "invalid"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: User \\[unknown_user] not a member of an authorized group");

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("alice", "invalid"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: User \\[alice] not a member of an authorized group");
ldapAuthenticator.invalidateCache();

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("alice", "alice-pass"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: User \\[alice] not a member of an authorized group");
ldapAuthenticator.invalidateCache();

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("bob", "bob-pass"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: User \\[bob] not a member of an authorized group");
ldapAuthenticator.invalidateCache();

openLdapServer.addUserToGroup(alice, group);
assertEquals(ldapAuthenticator.createAuthenticatedPrincipal("alice", "alice-pass"), new BasicPrincipal("alice"));
ldapAuthenticator.invalidateCache();

assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("alice", "invalid"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: Invalid credentials");
ldapAuthenticator.invalidateCache();

// Now group authorization filter will return multiple entries
openLdapServer.addUserToGroup(bob, group);
assertThatThrownBy(() -> ldapAuthenticator.createAuthenticatedPrincipal("alice", "alice-pass"))
.isInstanceOf(RuntimeException.class);
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: Multiple group membership results for user \\[alice].*");
ldapAuthenticator.invalidateCache();
}
}
Expand Down

0 comments on commit 49abcd3

Please sign in to comment.