Skip to content

Commit 6b31009

Browse files
author
Pedro Igor
committed
Adding test for roles granted to groups.
1 parent 361190c commit 6b31009

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

modules/base/impl/src/test/java/org/picketlink/http/test/SecurityInitializer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.picketlink.idm.RelationshipManager;
2525
import org.picketlink.idm.credential.Digest;
2626
import org.picketlink.idm.credential.Password;
27+
import org.picketlink.idm.model.IdentityType;
2728
import org.picketlink.idm.model.Partition;
2829
import org.picketlink.idm.model.basic.Grant;
2930
import org.picketlink.idm.model.basic.Group;
@@ -106,6 +107,11 @@ private void configureDefaultPartition() {
106107

107108
// add an user as a member of a group
108109
addMember(defaultUser, administratorGroup);
110+
111+
// user must inherit roles from group Administrators, in this case role Administrator
112+
Role administratorRole = addRole("Administrator", realm, this.partitionManager);
113+
114+
grantRole(administratorGroup, administratorRole);
109115
}
110116

111117
public void addPartition(Partition partition) {
@@ -185,10 +191,10 @@ public Group addGroup(String groupName, Partition partition, PartitionManager pa
185191
return group;
186192
}
187193

188-
public void grantRole(User user, Role role) {
194+
public void grantRole(IdentityType assignee, Role role) {
189195
RelationshipManager relationshipManager = getRelationshipManager();
190196

191-
relationshipManager.add(new Grant(user, role));
197+
relationshipManager.add(new Grant(assignee, role));
192198
}
193199

194200
/**

modules/base/impl/src/test/java/org/picketlink/http/test/authorization/RoleBasedAuthorizationTestCase.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,25 @@ public void testOnlyManagers() throws Exception {
7979

8080
this.securityFilter.doFilter(this.request, this.response, this.filterChain);
8181

82-
doAnswer(new Answer() {
83-
@Override
84-
public Object answer(InvocationOnMock invocation) throws Throwable {
85-
assertEquals("/onlyManagerRole", picketLinkRequest.get().getServletPath());
86-
return null;
87-
}
88-
}).when(this.filterChain).doFilter(this.request, this.response);
82+
verify(this.filterChain, times(1)).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
83+
}
84+
85+
@Test
86+
public void testOnlyAdministrator() throws Exception {
87+
when(this.request.getServletPath()).thenReturn("/formProtectedUri/" + FormAuthenticationConfiguration.DEFAULT_AUTHENTICATION_URI);
88+
when(this.request.getParameter(FormAuthenticationScheme.J_USERNAME)).thenReturn("picketlink");
89+
when(this.request.getParameter(FormAuthenticationScheme.J_PASSWORD)).thenReturn("picketlink");
90+
91+
this.securityFilter.doFilter(this.request, this.response, this.filterChain);
92+
verify(this.filterChain, times(0)).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
93+
verify(this.response).sendRedirect(CONTEXT_PATH);
94+
95+
when(this.request.getServletPath()).thenReturn("/admin/manage");
96+
reset(this.response);
97+
98+
this.securityFilter.doFilter(this.request, this.response, this.filterChain);
99+
100+
verify(this.filterChain, times(1)).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
89101
}
90102

91103
@Test
@@ -120,7 +132,10 @@ public void configureHttpSecurity(@Observes SecurityConfigurationEvent event) {
120132
.role("Manager")
121133
.forPath("/onlyCustomerRole")
122134
.authorizeWith()
123-
.role("Customer");
135+
.role("Customer")
136+
.forPath("/admin/*")
137+
.authorizeWith()
138+
.role("Administrator");
124139
}
125140
}
126141
}

0 commit comments

Comments
 (0)