Search in sources :

Example 46 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class UsersResourceTest method shouldFinalizeRegistration.

@Test
public void shouldFinalizeRegistration() {
    // init
    FinalizeRegistrationInput input = new FinalizeRegistrationInput().token("token").password("P4s5vv0Rd").firstname("Firstname").lastname("LASTNAME");
    RegisterUserEntity registerUserEntity = new RegisterUserEntity();
    doReturn(registerUserEntity).when(userMapper).convert(input);
    doReturn(new UserEntity()).when(userService).finalizeRegistration(registerUserEntity);
    // test
    final Response response = target("registration/_finalize").request().post(Entity.json(input));
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    Mockito.verify(userMapper).convert(input);
    Mockito.verify(userService).finalizeRegistration(registerUserEntity);
}
Also used : Response(javax.ws.rs.core.Response) RegisterUserEntity(io.gravitee.rest.api.model.RegisterUserEntity) RegisterUserEntity(io.gravitee.rest.api.model.RegisterUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 47 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipCommandHandlerTest method handleWithAdminRole.

@Test
public void handleWithAdminRole() {
    MembershipPayload membershipPayload = new MembershipPayload();
    membershipPayload.setUserId("user#1");
    membershipPayload.setOrganizationId("orga#1");
    membershipPayload.setReferenceType(MembershipReferenceType.ENVIRONMENT.name());
    membershipPayload.setReferenceId("env#1");
    membershipPayload.setRole("ENVIRONMENT_PRIMARY_OWNER");
    MembershipCommand command = new MembershipCommand(membershipPayload);
    UserEntity user = new UserEntity();
    user.setId(UUID.random().toString());
    RoleEntity role = new RoleEntity();
    role.setId(UUID.random().toString());
    role.setScope(RoleScope.ENVIRONMENT);
    role.setName("ADMIN");
    when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
    when(roleService.findByScopeAndName(RoleScope.ENVIRONMENT, "ADMIN")).thenReturn(Optional.of(role));
    TestObserver<MembershipReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertNoErrors();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
    ArgumentCaptor<MembershipService.MembershipReference> membershipReference = ArgumentCaptor.forClass(MembershipService.MembershipReference.class);
    ArgumentCaptor<MembershipService.MembershipMember> membershipMember = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
    ArgumentCaptor<List<MembershipService.MembershipRole>> membershipRoles = ArgumentCaptor.forClass(List.class);
    verify(membershipService).updateRolesToMemberOnReference(membershipReference.capture(), membershipMember.capture(), membershipRoles.capture(), eq(COCKPIT_SOURCE), eq(false));
    assertEquals(MembershipReferenceType.ENVIRONMENT, membershipReference.getValue().getType());
    assertEquals(membershipPayload.getReferenceId(), membershipReference.getValue().getId());
    assertEquals(MembershipMemberType.USER, membershipMember.getValue().getMemberType());
    assertEquals(user.getId(), membershipMember.getValue().getMemberId());
    assertTrue(membershipRoles.getValue().size() == 1 && membershipRoles.getValue().stream().allMatch(membershipRole -> membershipRole.getScope() == role.getScope() && membershipRole.getName().equals(role.getName())));
}
Also used : MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) UserEntity(io.gravitee.rest.api.model.UserEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipService(io.gravitee.rest.api.service.MembershipService) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) List(java.util.List) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Example 48 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipCommandHandlerTest method handleWithUnknownRole.

@Test
public void handleWithUnknownRole() {
    MembershipPayload membershipPayload = new MembershipPayload();
    membershipPayload.setUserId("user#1");
    membershipPayload.setOrganizationId("orga#1");
    membershipPayload.setReferenceType(MembershipReferenceType.ENVIRONMENT.name());
    membershipPayload.setReferenceId("env#1");
    membershipPayload.setRole("UNKNOWN");
    MembershipCommand command = new MembershipCommand(membershipPayload);
    UserEntity user = new UserEntity();
    user.setId(UUID.random().toString());
    RoleEntity role = new RoleEntity();
    role.setId(UUID.random().toString());
    when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
    when(roleService.findByScopeAndName(RoleScope.ENVIRONMENT, "UNKNOWN")).thenReturn(Optional.empty());
    TestObserver<MembershipReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertNoErrors();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.ERROR));
    verifyZeroInteractions(membershipService);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) UserEntity(io.gravitee.rest.api.model.UserEntity) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Example 49 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class UserCommandHandlerTest method handleUpdate.

@Test
public void handleUpdate() {
    UserPayload userPayload = new UserPayload();
    UserCommand command = new UserCommand(userPayload);
    final String sourceId = "user#1";
    userPayload.setId(sourceId);
    userPayload.setOrganizationId("orga#1");
    userPayload.setUsername("New Username");
    userPayload.setFirstName("New Firstname");
    userPayload.setLastName("New Lastname");
    userPayload.setPicture("https://gravitee.io/my-new-picture");
    userPayload.setEmail("my-new-email@gravitee.io");
    HashMap<String, Object> additionalInformation = new HashMap<>();
    additionalInformation.put("info1", "new_value1");
    additionalInformation.put("new_info3", "new_value3");
    userPayload.setAdditionalInformation(additionalInformation);
    UserEntity existingCockpitUser = new UserEntity();
    existingCockpitUser.setId("apim_user#1");
    existingCockpitUser.setSourceId(sourceId);
    existingCockpitUser.setSource("cockpit");
    existingCockpitUser.setFirstname("Firstname");
    existingCockpitUser.setLastname("Lastname");
    existingCockpitUser.setEmail("email@gravitee.io");
    existingCockpitUser.setPicture("https://gravitee.io/picture");
    HashMap<String, Object> customFields = new HashMap<>();
    customFields.put("info1", "value1");
    customFields.put("info2", "value2");
    existingCockpitUser.setCustomFields(customFields);
    when(userService.findBySource("cockpit", sourceId, false)).thenReturn(existingCockpitUser);
    when(userService.update(eq("apim_user#1"), argThat(updatedUser -> updatedUser.getFirstname().equals(userPayload.getFirstName()) && updatedUser.getLastname().equals(userPayload.getLastName()) && updatedUser.getEmail().equals(userPayload.getEmail()) && updatedUser.getPicture().equals(userPayload.getPicture()) && updatedUser.getCustomFields().get("info1").equals(additionalInformation.get("info1")) && updatedUser.getCustomFields().get("new_info3").equals(additionalInformation.get("new_info3")) && updatedUser.getCustomFields().get(PICTURE).equals(userPayload.getPicture()) && updatedUser.getCustomFields().get(SUB).equals(userPayload.getUsername())))).thenReturn(new UserEntity());
    TestObserver<UserReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) PICTURE(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE) SUB(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB) Mock(org.mockito.Mock) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Command(io.gravitee.cockpit.api.command.Command) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply) UserService(io.gravitee.rest.api.service.UserService) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) UserEntity(io.gravitee.rest.api.model.UserEntity) Before(org.junit.Before) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) HashMap(java.util.HashMap) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Test(org.junit.Test)

Example 50 with UserEntity

use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class UserCommandHandlerTest method handleCreation.

@Test
public void handleCreation() {
    UserPayload userPayload = new UserPayload();
    UserCommand command = new UserCommand(userPayload);
    final String sourceId = "user#1";
    userPayload.setId(sourceId);
    userPayload.setOrganizationId("orga#1");
    userPayload.setUsername("Username");
    userPayload.setFirstName("Firstname");
    userPayload.setLastName("Lastname");
    userPayload.setPicture("https://gravitee.io/my-picture");
    userPayload.setEmail("email@gravitee.io");
    HashMap<String, Object> additionalInformation = new HashMap<>();
    additionalInformation.put("info1", "value1");
    additionalInformation.put("info2", "value2");
    userPayload.setAdditionalInformation(additionalInformation);
    when(userService.findBySource("cockpit", sourceId, false)).thenThrow(new UserNotFoundException(sourceId));
    when(userService.create(argThat(newUser -> newUser.getSourceId().equals(userPayload.getId()) && newUser.getSource().equals("cockpit") && newUser.getFirstname().equals(userPayload.getFirstName()) && newUser.getLastname().equals(userPayload.getLastName()) && newUser.getEmail().equals(userPayload.getEmail()) && newUser.getPicture().equals(userPayload.getPicture()) && newUser.getCustomFields().get("info1").equals(additionalInformation.get("info1")) && newUser.getCustomFields().get("info2").equals(additionalInformation.get("info2")) && newUser.getCustomFields().get(PICTURE).equals(userPayload.getPicture()) && newUser.getCustomFields().get(SUB).equals(userPayload.getUsername())), eq(false))).thenReturn(new UserEntity());
    TestObserver<UserReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) ArgumentMatchers(org.mockito.ArgumentMatchers) PICTURE(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE) SUB(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB) Mock(org.mockito.Mock) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Command(io.gravitee.cockpit.api.command.Command) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply) UserService(io.gravitee.rest.api.service.UserService) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) UserEntity(io.gravitee.rest.api.model.UserEntity) Before(org.junit.Before) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) HashMap(java.util.HashMap) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Test(org.junit.Test)

Aggregations

UserEntity (io.gravitee.rest.api.model.UserEntity)57 Test (org.junit.Test)36 User (io.gravitee.rest.api.portal.rest.model.User)14 Response (javax.ws.rs.core.Response)14 RoleEntity (io.gravitee.rest.api.model.RoleEntity)13 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)12 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)12 Date (java.util.Date)10 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)9 UserService (io.gravitee.rest.api.service.UserService)9 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)8 List (java.util.List)8 Instant (java.time.Instant)7 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)6 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 MembershipService (io.gravitee.rest.api.service.MembershipService)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5