use of io.gravitee.management.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class GroupMemberResourceTest method shouldUpdateApiRole.
@Test
public void shouldUpdateApiRole() {
initUPDATEmock();
MemberRoleEntity apiRole = new MemberRoleEntity();
apiRole.setRoleScope(io.gravitee.management.model.permissions.RoleScope.API);
apiRole.setRoleName("CUSTOM");
GroupMembership groupMembership = new GroupMembership();
groupMembership.setId(USERNAME);
groupMembership.setRoles(Collections.singletonList(apiRole));
final Response response = target().request().post(Entity.json(groupMembership));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.API);
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.APPLICATION);
verify(membershipService, times(1)).addOrUpdateMember(any(), any(), any());
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(USERNAME, null), new MembershipService.MembershipRole(RoleScope.API, "CUSTOM"));
}
use of io.gravitee.management.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class GroupMemberResourceTest method shouldUpdateApplicationRole.
@Test
public void shouldUpdateApplicationRole() {
initUPDATEmock();
MemberRoleEntity appRole = new MemberRoleEntity();
appRole.setRoleScope(io.gravitee.management.model.permissions.RoleScope.APPLICATION);
appRole.setRoleName("CUSTOM");
GroupMembership groupMembership = new GroupMembership();
groupMembership.setId(USERNAME);
groupMembership.setRoles(Collections.singletonList(appRole));
final Response response = target().request().post(Entity.json(groupMembership));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.API);
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.APPLICATION);
verify(membershipService, times(1)).addOrUpdateMember(any(), any(), any());
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(USERNAME, null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "CUSTOM"));
}
use of io.gravitee.management.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class OAuth2AuthenticationResourceTest method shouldConnectNewUserWithGroupsMappingFromUserInfo.
@Test
public void shouldConnectNewUserWithGroupsMappingFromUserInfo() throws Exception {
// -- MOCK
// mock environment
mockDefaultEnvironment();
mockGroupsMapping();
// mock oauth2 exchange authorisation code for access token
mockExchangeAuthorizationCodeForAccessToken();
// mock oauth2 user info call
mockUserInfo(okJson(IOUtils.toString(read("/oauth2/json/user_info_response_body.json"), Charset.defaultCharset())));
// mock DB find user by name
when(userService.findByUsername("janedoe@example.com", false)).thenThrow(new UserNotFoundException("janedoe@example.com"));
// mock create user
NewExternalUserEntity newExternalUserEntity = mockNewExternalUserEntity();
UserEntity createdUser = mockUserEntity();
mockUserCreation(newExternalUserEntity, createdUser, true);
// mock group search and association
when(groupService.findByName("Example group")).thenReturn(Collections.singletonList(mockGroupEntity("group_id_1", "Example group")));
when(groupService.findByName("soft user")).thenReturn(Collections.singletonList(mockGroupEntity("group_id_2", "soft user")));
when(groupService.findByName("Others")).thenReturn(Collections.singletonList(mockGroupEntity("group_id_3", "Others")));
when(groupService.findByName("Api consumer")).thenReturn(Collections.singletonList(mockGroupEntity("group_id_4", "Api consumer")));
RoleEntity roleApiUser = mockRoleEntity(io.gravitee.management.model.permissions.RoleScope.API, "USER");
RoleEntity roleApplicationAdmin = mockRoleEntity(io.gravitee.management.model.permissions.RoleScope.APPLICATION, "ADMIN");
when(roleService.findDefaultRoleByScopes(RoleScope.API, RoleScope.APPLICATION)).thenReturn(Arrays.asList(roleApiUser, roleApplicationAdmin));
when(membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_1"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.API, "USER"))).thenReturn(mockMemberEntity());
when(membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_2"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.API, "USER"))).thenReturn(mockMemberEntity());
when(membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_2"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "ADMIN"))).thenReturn(mockMemberEntity());
when(membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_4"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.API, "USER"))).thenReturn(mockMemberEntity());
when(membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_4"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "ADMIN"))).thenReturn(mockMemberEntity());
// mock DB update user picture
UpdateUserEntity updateUserEntity = mockUpdateUserPicture(createdUser);
// mock DB user connect
when(userService.connect("janedoe@example.com")).thenReturn(createdUser);
// -- CALL
AbstractAuthenticationResource.Payload payload = createPayload("the_client_id", "http://localhost/callback", "CoDe", "StAtE");
;
Response response = target().request().post(json(payload));
// -- VERIFY
verify(userService, times(1)).findByUsername("janedoe@example.com", false);
verify(userService, times(1)).create(refEq(newExternalUserEntity), eq(true));
verify(userService, times(1)).update(refEq(updateUserEntity));
verify(userService, times(1)).connect("janedoe@example.com");
// verify group creations
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_1"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.API, "USER"));
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_1"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "ADMIN"));
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_2"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.API, "USER"));
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_2"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "ADMIN"));
verify(membershipService, times(0)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_3"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.API, "USER"));
verify(membershipService, times(0)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_3"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "ADMIN"));
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_4"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.API, "USER"));
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, "group_id_4"), new MembershipService.MembershipUser("janedoe@example.com", null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "ADMIN"));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
// verify response body
verifyUserInResponseBody(response);
// verify jwt token
verifyJwtToken(response);
}
use of io.gravitee.management.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class GroupMemberResourceTest method shouldUpdateApiAndApplicationRole.
@Test
public void shouldUpdateApiAndApplicationRole() {
initUPDATEmock();
MemberRoleEntity apiRole = new MemberRoleEntity();
apiRole.setRoleScope(io.gravitee.management.model.permissions.RoleScope.API);
apiRole.setRoleName("CUSTOM_API");
MemberRoleEntity appRole = new MemberRoleEntity();
appRole.setRoleScope(io.gravitee.management.model.permissions.RoleScope.APPLICATION);
appRole.setRoleName("CUSTOM_APP");
GroupMembership groupMembership = new GroupMembership();
groupMembership.setId(USERNAME);
groupMembership.setRoles(Arrays.asList(apiRole, appRole));
final Response response = target().request().post(Entity.json(groupMembership));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.API);
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.APPLICATION);
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(USERNAME, null), new MembershipService.MembershipRole(RoleScope.API, "CUSTOM_API"));
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(USERNAME, null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "CUSTOM_APP"));
}
use of io.gravitee.management.service.MembershipService in project gravitee-management-rest-api by gravitee-io.
the class GroupMemberResourceTest method shouldAddMemberWithCustomApplicationRoleAndCustomApiRole.
@Test
public void shouldAddMemberWithCustomApplicationRoleAndCustomApiRole() {
initADDmock();
MemberRoleEntity apiRole = new MemberRoleEntity();
apiRole.setRoleScope(io.gravitee.management.model.permissions.RoleScope.API);
apiRole.setRoleName("CUSTOM_API");
MemberRoleEntity appRole = new MemberRoleEntity();
appRole.setRoleScope(io.gravitee.management.model.permissions.RoleScope.APPLICATION);
appRole.setRoleName("CUSTOM_APP");
GroupMembership groupMembership = new GroupMembership();
groupMembership.setId(USERNAME);
groupMembership.setRoles(Arrays.asList(apiRole, appRole));
final Response response = target().request().post(Entity.json(groupMembership));
assertEquals(HttpStatusCode.OK_200, response.getStatus());
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.API);
verify(roleService, never()).findDefaultRoleByScopes(RoleScope.APPLICATION);
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(USERNAME, null), new MembershipService.MembershipRole(RoleScope.API, "CUSTOM_API"));
verify(membershipService, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(USERNAME, null), new MembershipService.MembershipRole(RoleScope.APPLICATION, "CUSTOM_APP"));
}
Aggregations