Skip to content

Commit 9fc2a56

Browse files
committed
Added integration tests / coverage improvement
1 parent 99336b3 commit 9fc2a56

File tree

7 files changed

+105
-8
lines changed

7 files changed

+105
-8
lines changed

server/src/main/java/invite/manage/LocalManage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ public List<String> idpEntityIdentifiersByServiceEntityId(List<String> serviceEn
9292
return allIdentityProviders.stream()
9393
.filter(idp -> {
9494
List<Map<String, String>> allowedEntities = (List<Map<String, String>>) idp.getOrDefault("allowedEntities", emptyList());
95-
return allowedEntities.stream()
95+
boolean allowedAll = (boolean) idp.getOrDefault("allowedall", false);
96+
return allowedAll || allowedEntities.stream()
9697
.map(entity -> entity.get("name"))
9798
.anyMatch(name -> serviceEntityIdentifiers.contains(name));
9899
})
99-
.map(idp -> (String) ((Map) idp.get("data")).get("entityid"))
100+
.map(idp -> (String) idp.get("entityid"))
100101
.toList();
101102
}
102103

server/src/main/java/invite/manage/RemoteManage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ public List<String> idpEntityIdentifiersByServiceEntityId(List<String> serviceEn
6262
return emptyList();
6363
}
6464
Map<String, Object> body = Map.of(
65-
"allowedEntities.name",serviceEntityIdentifiers,
66-
"REQUESTED_ATTRIBUTES", List.of("entityid")
65+
"allowedEntities.name", serviceEntityIdentifiers,
66+
"allowedall", true,
67+
"LOGICAL_OPERATOR_IS_AND", false,
68+
"REQUESTED_ATTRIBUTES", List.of("entityid")
6769
);
6870
String manageUrl = String.format("%s/manage/api/internal/search/%s", url, EntityType.SAML20_IDP.collectionName());
6971
List<Map<String, Object>> providers = restTemplate.postForObject(manageUrl, body, List.class);
7072
if (providers != null) {
7173
LOG.debug(String.format("Got %d results for idpEntityIdentifiersByServiceEntityId", providers.size()));
7274
}
73-
return providers.stream().map(m -> (String)((Map)m.get("data")).get("entityid")).toList();
75+
return providers.stream().map(m -> (String) ((Map) m.get("data")).get("entityid")).toList();
7476

7577
}
7678

server/src/test/java/invite/AbstractMailTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"spring.security.oauth2.client.provider.oidcng.user-info-uri=http://localhost:8081/user-info",
2525
"spring.security.oauth2.client.provider.oidcng.jwk-set-uri=http://localhost:8081/jwk-set",
2626
"email.enabled=true",
27+
"spring.task.scheduling.enabled=false",
2728
"manage.url: http://localhost:8081",
2829
"manage.enabled: true"
2930
})
@@ -52,7 +53,7 @@ protected MimeMessageParser mailMessage() {
5253
return parser.parse();
5354
}
5455

55-
protected List<MimeMessageParser> allMailMessages(int expectedLength) throws Exception {
56+
protected List<MimeMessageParser> allMailMessages(int expectedLength) {
5657
await().until(() -> greenMail.getReceivedMessages().length == expectedLength);
5758
MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
5859
return Stream.of(receivedMessages)

server/src/test/java/invite/AbstractTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"manage.url: http://localhost:8081",
8181
"myconext.uri: http://localhost:8081/myconext/api/invite/provision-eduid",
8282
"manage.enabled: true",
83+
"spring.task.scheduling.enabled=false",
8384
"spring.jpa.properties.hibernate.format_sql=false",
8485
"spring.jpa.show-sql=false"
8586
})

server/src/test/java/invite/cron/ResourceCleanerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void cleanUsersWithoutActivity() throws JsonProcessingException {
2323
markUser(GUEST_SUB);
2424

2525
stubForManageProvisioning(List.of("1"));
26-
//Because there are RemoteProvisionedGroups
26+
//Because there are no RemoteProvisionedGroups
2727
stubForCreateScimRole();
2828
stubForCreateScimUser();
2929
stubForUpdateScimRole();
@@ -38,7 +38,7 @@ void cleanUsersWithoutRoles() throws JsonProcessingException {
3838
deleteUserRoles(GUEST_SUB);
3939

4040
stubForManageProvisioning(List.of("1"));
41-
//Because there are RemoteProvisionedGroups
41+
//Because there are no RemoteProvisionedGroups
4242
stubForCreateScimRole();
4343
stubForCreateScimUser();
4444
stubForUpdateScimRole();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package invite.cron;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import invite.model.Authority;
5+
import invite.model.Role;
6+
import invite.model.User;
7+
import invite.model.UserRole;
8+
import invite.provision.ProvisioningService;
9+
import invite.provision.scim.OperationType;
10+
import invite.repository.UserRepository;
11+
import invite.repository.UserRoleRepository;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.io.Serializable;
15+
import java.time.Instant;
16+
import java.time.Period;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
import static invite.AbstractTest.GUEST_SUB;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.mockito.ArgumentMatchers.any;
23+
import static org.mockito.Mockito.*;
24+
import static org.mockito.Mockito.when;
25+
26+
class ResourceCleanerUnitTest {
27+
28+
private final UserRepository userRepository = mock(UserRepository.class);
29+
private final UserRoleRepository userRoleRepository = mock(UserRoleRepository.class);
30+
private final ProvisioningService provisioningService = mock(ProvisioningService.class);
31+
32+
private final ResourceCleaner subject = new ResourceCleaner(userRepository,
33+
userRoleRepository,
34+
provisioningService,
35+
5,
36+
true);
37+
38+
39+
@Test
40+
void cleanUsers() {
41+
List<User> users = List.of(new User(), new User());
42+
when(userRepository.findByLastActivityBefore(any(Instant.class)))
43+
.thenReturn(users);
44+
when(userRepository.findNonSuperUserWithoutUserRoles())
45+
.thenReturn(users);
46+
when(userRoleRepository.findByEndDateBeforeAndExpiryNotifications(any(Instant.class),eq(1)))
47+
.thenReturn(List.of(new UserRole("Inviter", new User(), new Role(), Authority.INVITER)));
48+
49+
doThrow(new RuntimeException())
50+
.when(provisioningService)
51+
.deleteUserRequest(any(User.class));
52+
doThrow(new RuntimeException())
53+
.when(provisioningService)
54+
.updateGroupRequest(any(UserRole.class), eq(OperationType.Remove));
55+
56+
Map<String, List<? extends Serializable>> results = subject.doClean();
57+
assertEquals(2, results.get("DeletedNonActiveUsers").size());
58+
assertEquals(2, results.get("DeletedOrphanUsers").size());
59+
assertEquals(1, results.get("DeletedExpiredUserRoles").size());
60+
}
61+
62+
@Test
63+
void cleanUsersWithoutRoles() {
64+
}
65+
66+
@Test
67+
void cleanUserRoles() {
68+
}
69+
70+
71+
}

server/src/test/java/invite/manage/RemoteManageTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ void providersByIdIn() throws JsonProcessingException {
5959
assertEquals(providers, remoteProviders);
6060
}
6161

62+
@Test
63+
void idpEntityIdentifiersByServiceEntityId() throws JsonProcessingException {
64+
List<String> serviceEntityIdentifiers = List.of("https://network", "https://cloud");
65+
List<String> idpEntityIdentifiers = localManage.idpEntityIdentifiersByServiceEntityId(serviceEntityIdentifiers);
66+
List<Map<String, Map<String, String>>> idpProviders = idpEntityIdentifiers.stream().map(idpEntityId -> Map.of("data", Map.of("entityid", idpEntityId))).toList();
67+
String body = objectMapper.writeValueAsString(idpProviders);
68+
stubFor(post(urlPathMatching("/manage/api/internal/search/saml20_idp")).willReturn(aResponse()
69+
.withHeader("Content-Type", "application/json")
70+
.withBody(body)));
71+
List<String> idpEntityIdentifiersRemote = manage.idpEntityIdentifiersByServiceEntityId(serviceEntityIdentifiers);
72+
assertEquals(idpEntityIdentifiers, idpEntityIdentifiersRemote);
73+
}
74+
75+
@Test
76+
void idpEntityIdentifiersByServiceEntityIdEmpty() {
77+
List<String> serviceEntityIdentifiers = List.of();
78+
List<String> idpEntityIdentifiers = localManage.idpEntityIdentifiersByServiceEntityId(serviceEntityIdentifiers);
79+
List<String> idpEntityIdentifiersRemote = manage.idpEntityIdentifiersByServiceEntityId(serviceEntityIdentifiers);
80+
assertEquals(idpEntityIdentifiers, idpEntityIdentifiersRemote);
81+
}
82+
6283
@Test
6384
void providerByIdExceptionHandling() {
6485
stubFor(get(urlPathMatching("/manage/api/internal/metadata/saml20_sp/1")).willReturn(aResponse()

0 commit comments

Comments
 (0)