Search in sources :

Example 36 with UserEntity

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

the class EmailNotifierServiceTest method shouldHaveATemplateForApplicationHooksWithFreemarker.

@Test
public void shouldHaveATemplateForApplicationHooksWithFreemarker() throws TemplateException {
    GenericNotificationConfig cfg = new GenericNotificationConfig();
    cfg.setConfig("test@mail.com, ${api.primaryOwner.email} ");
    ApiEntity api = new ApiEntity();
    api.setName("api-name");
    UserEntity userEntity = new UserEntity();
    userEntity.setEmail("primary@owner.com");
    api.setPrimaryOwner(new PrimaryOwnerEntity(userEntity));
    PlanEntity plan = new PlanEntity();
    plan.setId("plan-id");
    plan.setName("plan-name");
    Map<String, Object> params = new HashMap<>();
    params.put((NotificationParamsBuilder.PARAM_API), api);
    params.put((NotificationParamsBuilder.PARAM_PLAN), plan);
    when(notificationTemplateService.resolveInlineTemplateWithParam(anyString(), anyString(), any())).thenReturn("primary@owner.com");
    for (ApplicationHook hook : ApplicationHook.values()) {
        reset(mockEmailService);
        service.trigger(hook, cfg, params);
        verify(mockEmailService, times(1)).sendAsyncEmailNotification(argThat(notification -> notification.getTo() != null && notification.getTo().length == 2 && notification.getTo()[0].equals("test@mail.com") && notification.getTo()[1].equals("primary@owner.com")), any());
        verify(mockEmailService, never()).sendEmailNotification(any());
    }
}
Also used : InjectMocks(org.mockito.InjectMocks) CoreMatchers(org.hamcrest.CoreMatchers) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) GenericNotificationConfig(io.gravitee.repository.management.model.GenericNotificationConfig) Mock(org.mockito.Mock) TemplateException(freemarker.template.TemplateException) PlanEntity(io.gravitee.rest.api.model.PlanEntity) io.gravitee.rest.api.service.notification(io.gravitee.rest.api.service.notification) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Test(org.junit.Test) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) EmailNotifierServiceImpl(io.gravitee.rest.api.service.notifiers.impl.EmailNotifierServiceImpl) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Map(java.util.Map) Assert(org.junit.Assert) Collections(java.util.Collections) UserEntity(io.gravitee.rest.api.model.UserEntity) HashMap(java.util.HashMap) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) GenericNotificationConfig(io.gravitee.repository.management.model.GenericNotificationConfig) PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 37 with UserEntity

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

the class MembershipService_GetMembersTest method shouldGetMembersWithoutMembership.

@Test
public void shouldGetMembersWithoutMembership() throws Exception {
    Membership membership = new Membership();
    membership.setReferenceId(API_ID);
    membership.setCreatedAt(new Date());
    membership.setUpdatedAt(membership.getCreatedAt());
    membership.setReferenceType(MembershipReferenceType.API);
    membership.setRoleId("API_PRIMARY_OWNER");
    membership.setMemberId("user-id");
    membership.setMemberType(MembershipMemberType.USER);
    UserEntity userEntity = new UserEntity();
    userEntity.setId(membership.getMemberId());
    userEntity.setFirstname("John");
    userEntity.setLastname("Doe");
    RoleEntity po = mock(RoleEntity.class);
    po.setName(SystemRole.PRIMARY_OWNER.name());
    List<String> memberIds = Collections.singletonList(membership.getMemberId());
    Set<UserEntity> userEntities = Collections.singleton(userEntity);
    when(roleService.findById("API_PRIMARY_OWNER")).thenReturn(po);
    when(membershipRepository.findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), null)).thenReturn(Collections.singleton(membership));
    when(userService.findByIds(memberIds, false)).thenReturn(userEntities);
    Set<MemberEntity> members = membershipService.getMembersByReferenceAndRole(io.gravitee.rest.api.model.MembershipReferenceType.API, API_ID, null);
    Assert.assertNotNull(members);
    Assert.assertFalse("members must not be empty", members.isEmpty());
    verify(membershipRepository, times(1)).findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), null);
    verify(userService, times(1)).findByIds(memberIds, false);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 38 with UserEntity

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

the class MetadataServiceTest method checkMetadataFormat_userWithoutEmail.

@Test
public void checkMetadataFormat_userWithoutEmail() throws TemplateException {
    when(this.notificationTemplateService.resolveInlineTemplateWithParam(anyString(), any(Reader.class), any())).thenReturn("");
    UserEntity userEntity = new UserEntity();
    PrimaryOwnerEntity primaryOwnerEntity = new PrimaryOwnerEntity(userEntity);
    ApiEntity apiEntity = new ApiEntity();
    apiEntity.setPrimaryOwner(primaryOwnerEntity);
    metadataService.checkMetadataFormat(MetadataFormat.MAIL, "${(api.primaryOwner.email)!''}", API, apiEntity);
}
Also used : PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Reader(java.io.Reader) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 39 with UserEntity

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

the class MemberMapper method convert.

public Member convert(MemberEntity member, UriInfo uriInfo) {
    final Member memberItem = new Member();
    memberItem.setCreatedAt(member.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    UserEntity userEntity = userService.findById(member.getId());
    User memberUser = userMapper.convert(userEntity);
    memberUser.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), userEntity.getId()), userEntity.getUpdatedAt()));
    memberItem.setUser(memberUser);
    if (member.getRoles() != null && !member.getRoles().isEmpty()) {
        memberItem.setRole(member.getRoles().get(0).getName());
    }
    memberItem.setUpdatedAt(member.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    return memberItem;
}
Also used : User(io.gravitee.rest.api.portal.rest.model.User) Member(io.gravitee.rest.api.portal.rest.model.Member) UserEntity(io.gravitee.rest.api.model.UserEntity)

Example 40 with UserEntity

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

the class CurrentUserResourceTest method setCurrentUserDetails.

private void setCurrentUserDetails(final UserDetails userDetails) {
    final Authentication authentication = mock(Authentication.class);
    final UserEntity userEntity = new UserEntity();
    userEntity.setId(ID);
    userEntity.setRoles(Collections.emptySet());
    userEntity.setFirstConnectionAt(new Date());
    userEntity.setCreatedAt(new Date());
    userEntity.setUpdatedAt(new Date());
    userEntity.setLastConnectionAt(new Date());
    when(authentication.getPrincipal()).thenReturn(userDetails);
    when(userService.findByIdWithRoles(USER_NAME)).thenReturn(userEntity);
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
    SecurityContextHolder.setContext(new SecurityContextImpl(authentication));
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Authentication(org.springframework.security.core.Authentication) UserEntity(io.gravitee.rest.api.model.UserEntity) Date(java.util.Date)

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