From 0680a54e42a822da3e4d0ff62b03581b2edb049c Mon Sep 17 00:00:00 2001 From: Okke Harsta Date: Tue, 8 Oct 2024 13:33:54 +0200 Subject: [PATCH] Use created Instant from teams during migration --- server/src/main/java/access/teams/Membership.java | 2 ++ server/src/main/java/access/teams/Person.java | 2 ++ .../src/main/java/access/teams/TeamsController.java | 4 ++-- .../test/java/access/teams/TeamsControllerTest.java | 12 ++++++++++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/access/teams/Membership.java b/server/src/main/java/access/teams/Membership.java index c0112099..cb7d7515 100644 --- a/server/src/main/java/access/teams/Membership.java +++ b/server/src/main/java/access/teams/Membership.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import java.io.Serializable; +import java.time.Instant; @Getter @NoArgsConstructor @@ -13,5 +14,6 @@ public class Membership implements Serializable { private Person person; private Role role; + private Instant created; } diff --git a/server/src/main/java/access/teams/Person.java b/server/src/main/java/access/teams/Person.java index 34fc860e..59c1750e 100644 --- a/server/src/main/java/access/teams/Person.java +++ b/server/src/main/java/access/teams/Person.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import java.io.Serializable; +import java.time.Instant; @Getter @NoArgsConstructor @@ -15,4 +16,5 @@ public class Person implements Serializable { private String name; private String email; private String schacHomeOrganization; + private Instant created; } diff --git a/server/src/main/java/access/teams/TeamsController.java b/server/src/main/java/access/teams/TeamsController.java index 3f35fa4c..a77fc3be 100644 --- a/server/src/main/java/access/teams/TeamsController.java +++ b/server/src/main/java/access/teams/TeamsController.java @@ -129,7 +129,7 @@ private void provision(Role role, Membership membership) { newUser.setName(person.getName()); newUser.setEmail(person.getEmail()); newUser.setSchacHomeOrganization(person.getSchacHomeOrganization()); - newUser.setCreatedAt(now); + newUser.setCreatedAt(person.getCreated()); newUser.setLastActivity(now); newUser.nameInvariant(); return userRepository.save(newUser); @@ -144,7 +144,7 @@ private void provision(Role role, Membership membership) { userRole.setInviter("teams_migration"); userRole.setUser(user); userRole.setRole(role); - userRole.setCreatedAt(now); + userRole.setCreatedAt(membership.getCreated()); userRole.setEndDate(now.plus(DEFAULT_EXPIRY_DAYS, ChronoUnit.DAYS)); access.teams.Role teamsRole = membership.getRole(); userRole.setAuthority(mapAuthority(teamsRole)); diff --git a/server/src/test/java/access/teams/TeamsControllerTest.java b/server/src/test/java/access/teams/TeamsControllerTest.java index 7d85f4d0..268f69d4 100644 --- a/server/src/test/java/access/teams/TeamsControllerTest.java +++ b/server/src/test/java/access/teams/TeamsControllerTest.java @@ -11,6 +11,8 @@ import io.restassured.http.ContentType; import org.junit.jupiter.api.Test; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -70,7 +72,9 @@ void migrateTeam() throws JsonProcessingException { users.forEach(user -> assertEquals(team.getName(), user.getUserRoles().iterator().next().getRole().getName())); userRoles.forEach((key, teamsRole) -> { User user = users.stream().filter(u -> u.getName().equals(key)).findFirst().get(); + assertTrue(Instant.now().minus(28, ChronoUnit.DAYS).isAfter(user.getCreatedAt())); UserRole userRole = user.getUserRoles().stream().findFirst().get(); + assertTrue(Instant.now().minus(13, ChronoUnit.DAYS).isAfter(userRole.getCreatedAt())); Authority authority = userRole.getAuthority(); assertEquals(mapAuthority(teamsRole), authority); if (teamsRole.equals(Role.MEMBER) || teamsRole.equals(Role.OWNER)) { @@ -104,7 +108,11 @@ private List getMemberships() { "urn:collab:person:surfnet.nl:" + entry.getKey().toLowerCase().replaceAll(" ", "."), entry.getKey(), entry.getKey().replaceAll(" ", "") + "@example.com", - "example.com"), entry.getValue())) + "example.com", + Instant.now().minus(30, ChronoUnit.DAYS)), + entry.getValue(), + Instant.now().minus(15, ChronoUnit.DAYS) + )) .collect(Collectors.toList()); } @@ -182,7 +190,7 @@ void migrateTeamInvalidPerson() { void migrateTeamInvalidSchacHome() { List applications = List.of(new Application("999", EntityType.SAML20_SP)); Person person = new Person(); - Membership membership = new Membership(person, Role.MANAGER); + Membership membership = new Membership(person, Role.MANAGER, Instant.now()); List memberships = List.of(membership); Team team = new Team( "nl:surfnet:diensten:test",