Skip to content

Commit

Permalink
Test email of type CLIENT_STATUS is sent
Browse files Browse the repository at this point in the history
when updating client status, if owners and/or contacts
are defined
  • Loading branch information
rmiccoli committed Jul 16, 2024
1 parent c89bbee commit dd93076
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public IamEmailNotification createClientStatusChangedMessageFor(ClientDetailsEnt
List<String> emails = new ArrayList<>(recipients);

if (emails.isEmpty()) {
LOG.warn("No emails to send notification to for client id {}", client.getClientId());
LOG.warn("No email to send notification to for client {}", client.getClientId());
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static it.infn.mw.iam.api.common.client.AuthorizationGrantType.REDELEGATE;
import static it.infn.mw.iam.api.common.client.AuthorizationGrantType.REFRESH_TOKEN;
import static it.infn.mw.iam.api.common.client.TokenEndpointAuthenticationMethod.client_secret_basic;
import static it.infn.mw.iam.core.IamNotificationType.CLIENT_STATUS;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
Expand All @@ -36,7 +37,9 @@

import java.text.ParseException;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.ConstraintViolationException;

Expand Down Expand Up @@ -68,7 +71,9 @@
import it.infn.mw.iam.api.scim.model.ScimUser;
import it.infn.mw.iam.authn.util.Authorities;
import it.infn.mw.iam.persistence.model.IamAccount;
import it.infn.mw.iam.persistence.model.IamEmailNotification;
import it.infn.mw.iam.persistence.repository.IamAccountRepository;
import it.infn.mw.iam.persistence.repository.IamEmailNotificationRepository;
import it.infn.mw.iam.test.util.annotation.IamNoMvcTest;


Expand All @@ -89,6 +94,9 @@ public class ClientManagementServiceTests {
@Autowired
private IamAccountRepository accountRepo;

@Autowired
private IamEmailNotificationRepository notificationRepo;

@Autowired
private Clock clock;

Expand Down Expand Up @@ -406,30 +414,55 @@ public void testCodeChallengeValidation() {
}

@Test
public void testClientStatusChange() {
public void testClientStatusChangeAndSendEmailToOwner() {
List<IamEmailNotification> emails = new ArrayList<IamEmailNotification>();
assertEquals(notificationRepo.findByNotificationType(CLIENT_STATUS), emails);

managementService.updateClientStatus("client", false, "userUUID");
RegisteredClientDTO client = managementService.retrieveClientByClientId("client").get();

IamEmailNotification e = new IamEmailNotification();
e.setId(1L);
e.setType(CLIENT_STATUS);
e.setUuid(client.getClientId());
emails.add(e);

assertEquals(notificationRepo.findByNotificationType(CLIENT_STATUS), emails);
assertFalse(client.isActive());
assertTrue(client.getStatusChangedOn().equals(Date.from(clock.instant())));
assertEquals("userUUID", client.getStatusChangedBy());
}

@Test
public void testClientStatusChangeWithContacts() {
public void testClientStatusChangeAndSendEmailToContact() {
List<IamEmailNotification> emails = new ArrayList<IamEmailNotification>();
assertEquals(notificationRepo.findByNotificationType(CLIENT_STATUS), emails);

managementService.updateClientStatus("device-code-client", false, "userUUID");
RegisteredClientDTO client = managementService.retrieveClientByClientId("device-code-client").get();

IamEmailNotification e = new IamEmailNotification();
e.setId(1L);
e.setType(CLIENT_STATUS);
e.setUuid(client.getClientId());
emails.add(e);

assertEquals(notificationRepo.findByNotificationType(CLIENT_STATUS), emails);
assertFalse(client.isActive());
assertTrue(client.getStatusChangedOn().equals(Date.from(clock.instant())));
assertEquals("userUUID", client.getStatusChangedBy());
}

@Test
public void testClientStatusChangeWithoutOwners() {
public void testClientStatusChangeAndNoEmailSent() {
List<IamEmailNotification> emails = new ArrayList<IamEmailNotification>();
assertEquals(notificationRepo.findByNotificationType(CLIENT_STATUS), emails);

// client-cred has no contact and no owner
managementService.updateClientStatus("client-cred", false, "userUUID");
RegisteredClientDTO client = managementService.retrieveClientByClientId("client-cred").get();

assertEquals(notificationRepo.findByNotificationType(CLIENT_STATUS), emails);
assertFalse(client.isActive());
assertTrue(client.getStatusChangedOn().equals(Date.from(clock.instant())));
assertEquals("userUUID", client.getStatusChangedBy());
Expand Down

0 comments on commit dd93076

Please sign in to comment.