Skip to content

Commit

Permalink
fix: send mail to opted in users for trial instance in MembershipService
Browse files Browse the repository at this point in the history
  • Loading branch information
ytvnr committed Mar 27, 2024
1 parent b4c480a commit 008b59f
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 6 deletions.
Expand Up @@ -52,6 +52,8 @@
import io.gravitee.rest.api.model.common.Pageable;
import io.gravitee.rest.api.model.common.PageableImpl;
import io.gravitee.rest.api.model.pagedresult.Metadata;
import io.gravitee.rest.api.model.parameters.Key;
import io.gravitee.rest.api.model.parameters.ParameterReferenceType;
import io.gravitee.rest.api.model.permissions.RoleScope;
import io.gravitee.rest.api.model.permissions.SystemRole;
import io.gravitee.rest.api.model.providers.User;
Expand All @@ -64,6 +66,7 @@
import io.gravitee.rest.api.service.GroupService;
import io.gravitee.rest.api.service.IdentityService;
import io.gravitee.rest.api.service.MembershipService;
import io.gravitee.rest.api.service.ParameterService;
import io.gravitee.rest.api.service.RoleService;
import io.gravitee.rest.api.service.UserService;
import io.gravitee.rest.api.service.builder.EmailNotificationBuilder;
Expand Down Expand Up @@ -137,6 +140,7 @@ public class MembershipServiceImpl extends AbstractService implements Membership
private final EventManager eventManager;

private final PrimaryOwnerService primaryOwnerService;
private ParameterService parameterService;

public MembershipServiceImpl(
@Autowired @Lazy IdentityService identityService,
Expand All @@ -153,7 +157,8 @@ public MembershipServiceImpl(
@Autowired @Lazy ApiGroupService apiGroupService,
@Autowired @Lazy ApiRepository apiRepository,
@Autowired @Lazy GroupService groupService,
@Autowired AuditService auditService
@Autowired AuditService auditService,
@Autowired ParameterService parameterService
) {
this.identityService = identityService;
this.userService = userService;
Expand All @@ -170,6 +175,7 @@ public MembershipServiceImpl(
this.apiRepository = apiRepository;
this.groupService = groupService;
this.auditService = auditService;
this.parameterService = parameterService;
}

@Override
Expand Down Expand Up @@ -319,7 +325,14 @@ private MemberEntity _addRoleToMemberOnReference(
reference.getId()
);
if (emailNotification != null) {
emailService.sendAsyncEmailNotification(executionContext, emailNotification);
final boolean isTrialInstance = parameterService.findAsBoolean(
executionContext,
Key.TRIAL_INSTANCE,
ParameterReferenceType.SYSTEM
);
if (!isTrialInstance || userEntity.optedIn()) {
emailService.sendAsyncEmailNotification(executionContext, emailNotification);
}
}
}

Expand Down
Expand Up @@ -31,11 +31,14 @@
import io.gravitee.rest.api.model.MembershipReferenceType;
import io.gravitee.rest.api.model.RoleEntity;
import io.gravitee.rest.api.model.UserEntity;
import io.gravitee.rest.api.model.parameters.Key;
import io.gravitee.rest.api.model.parameters.ParameterReferenceType;
import io.gravitee.rest.api.model.permissions.RoleScope;
import io.gravitee.rest.api.service.AuditService;
import io.gravitee.rest.api.service.EmailService;
import io.gravitee.rest.api.service.GroupService;
import io.gravitee.rest.api.service.MembershipService;
import io.gravitee.rest.api.service.ParameterService;
import io.gravitee.rest.api.service.RoleService;
import io.gravitee.rest.api.service.UserService;
import io.gravitee.rest.api.service.common.GraviteeContext;
Expand Down Expand Up @@ -81,6 +84,9 @@ public class MembershipService_AddRoleToMemberOnReferenceTest {
@Mock
private AuditService auditService;

@Mock
private ParameterService parameterService;

@BeforeEach
public void setUp() throws Exception {
membershipService =
Expand All @@ -99,7 +105,8 @@ public void setUp() throws Exception {
null,
null,
groupService,
auditService
auditService,
parameterService
);
}

Expand Down Expand Up @@ -186,6 +193,7 @@ public void shouldAddPrimaryOwnerApiGroupMembership() throws Exception {
)
.thenReturn(Set.of(newMembership), Collections.emptySet());
when(membershipRepository.create(any())).thenReturn(newMembership);
when(parameterService.findAsBoolean(any(), eq(Key.TRIAL_INSTANCE), eq(ParameterReferenceType.SYSTEM))).thenReturn(false);

membershipService.addRoleToMemberOnReference(
GraviteeContext.getExecutionContext(),
Expand All @@ -207,6 +215,59 @@ public void shouldAddPrimaryOwnerApiGroupMembership() throws Exception {
verify(emailService, times(1)).sendAsyncEmailNotification(eq(GraviteeContext.getExecutionContext()), any());
}

@Test
public void shouldAddPrimaryOwnerApiGroupMembershipAndNotSendEmailForNonOptedInUserInTrialInstance() throws Exception {
RoleEntity role = mock(RoleEntity.class);
when(role.getScope()).thenReturn(RoleScope.API);
when(role.getName()).thenReturn("PRIMARY_OWNER");
when(role.getId()).thenReturn("API_PRIMARY_OWNER");
when(roleService.findByScopeAndName(RoleScope.API, "PRIMARY_OWNER", GraviteeContext.getCurrentOrganization()))
.thenReturn(Optional.of(role));

UserEntity userEntity = new UserEntity();
userEntity.setId("my name");
userEntity.setEmail("me@mail.com");
when(userService.findById(GraviteeContext.getExecutionContext(), userEntity.getId())).thenReturn(userEntity);

Membership newMembership = new Membership();
newMembership.setReferenceType(io.gravitee.repository.management.model.MembershipReferenceType.API);
newMembership.setRoleId("API_PRIMARY_OWNER");
newMembership.setReferenceId(API_ID);
newMembership.setMemberId(GROUP_ID);
newMembership.setMemberType(io.gravitee.repository.management.model.MembershipMemberType.GROUP);
when(groupService.findById(GraviteeContext.getExecutionContext(), GROUP_ID)).thenReturn(mock(GroupEntity.class));
when(
membershipRepository.findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(
userEntity.getId(),
io.gravitee.repository.management.model.MembershipMemberType.USER,
io.gravitee.repository.management.model.MembershipReferenceType.GROUP,
GROUP_ID
)
)
.thenReturn(Set.of(newMembership), Collections.emptySet());
when(membershipRepository.create(any())).thenReturn(newMembership);
when(parameterService.findAsBoolean(any(), eq(Key.TRIAL_INSTANCE), eq(ParameterReferenceType.SYSTEM))).thenReturn(true);

membershipService.addRoleToMemberOnReference(
GraviteeContext.getExecutionContext(),
new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID),
new MembershipService.MembershipMember("my name", null, MembershipMemberType.USER),
new MembershipService.MembershipRole(RoleScope.API, "PRIMARY_OWNER")
);

verify(userService, times(1)).findById(GraviteeContext.getExecutionContext(), userEntity.getId());
verify(membershipRepository, times(2))
.findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(
userEntity.getId(),
io.gravitee.repository.management.model.MembershipMemberType.USER,
io.gravitee.repository.management.model.MembershipReferenceType.GROUP,
GROUP_ID
);
verify(membershipRepository, times(1)).create(any());
verify(membershipRepository, never()).update(any());
verify(emailService, times(0)).sendAsyncEmailNotification(eq(GraviteeContext.getExecutionContext()), any());
}

@Test
public void shouldDisallowAddUnknownRoleOnApi() throws Exception {
when(roleService.findByScopeAndName(any(), any(), any())).thenReturn(Optional.empty());
Expand Down
Expand Up @@ -107,7 +107,8 @@ public void setUp() throws Exception {
null,
apiRepository,
null,
auditService
auditService,
null
);

mockRole();
Expand Down
Expand Up @@ -81,6 +81,7 @@ public void setUp() throws Exception {
null,
null,
null,
null,
null
);
when(roleService.findByScopeAndName(RoleScope.API, PRIMARY_OWNER.name(), ORG_ID))
Expand Down
Expand Up @@ -78,6 +78,7 @@ public void setUp() throws Exception {
null,
null,
null,
null,
null
);
}
Expand Down
Expand Up @@ -101,6 +101,7 @@ public void setUp() throws Exception {
null,
mockApiRepository,
mockGroupService,
null,
null
);
}
Expand Down
Expand Up @@ -98,6 +98,7 @@ public void setUp() throws Exception {
null,
apiRepository,
null,
null,
null
);
}
Expand Down
Expand Up @@ -81,6 +81,7 @@ public void setUp() throws Exception {
null,
null,
null,
null,
null
);
}
Expand Down
Expand Up @@ -68,6 +68,7 @@ public void setUp() throws Exception {
null,
null,
null,
null,
null
);
}
Expand Down
Expand Up @@ -72,6 +72,7 @@ public void setUp() throws Exception {
null,
apiRepository,
null,
null,
null
);
}
Expand Down
Expand Up @@ -114,7 +114,8 @@ public void setUp() throws TechnicalException {
apiGroupService,
apiRepository,
null,
auditService
auditService,
null
);
newPrimaryOwnerRole.setId(USER_ROLE_ID);
newPrimaryOwnerRole.setName(USER_ROLE_NAME);
Expand Down
Expand Up @@ -101,7 +101,8 @@ public void setUp() throws Exception {
null,
apiRepository,
null,
auditService
auditService,
null
);
mockApi();
}
Expand Down

0 comments on commit 008b59f

Please sign in to comment.