From 5135c65f30cf184ce34304dfc8987282f303b043 Mon Sep 17 00:00:00 2001 From: Okke Harsta Date: Fri, 17 May 2024 16:34:37 +0200 Subject: [PATCH] Server side invite migration check for new teams --- .../main/java/teams/api/TeamController.java | 21 ++++++---- .../src/main/resources/application.yml | 2 +- .../TeamControllerInviteMigrationTest.java | 39 +++++++++++++++++++ .../api/validations/TeamValidatorTest.java | 2 +- 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 teams-server/src/test/java/teams/api/TeamControllerInviteMigrationTest.java diff --git a/teams-server/src/main/java/teams/api/TeamController.java b/teams-server/src/main/java/teams/api/TeamController.java index 1da534a9..1d9f9356 100644 --- a/teams-server/src/main/java/teams/api/TeamController.java +++ b/teams-server/src/main/java/teams/api/TeamController.java @@ -1,10 +1,7 @@ package teams.api; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import teams.api.validations.TeamValidator; @@ -12,12 +9,9 @@ import teams.exception.NotAllowedException; import teams.exception.ResourceNotFoundException; -import javax.mail.MessagingException; import javax.servlet.http.HttpServletRequest; -import java.io.IOException; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import static java.lang.String.format; @@ -28,7 +22,15 @@ public class TeamController extends ApiController implements TeamValidator { public static final int AUTOCOMPLETE_LIMIT = 11; - private TeamMatcher teamMatcher = new TeamMatcher(); + private final TeamMatcher teamMatcher = new TeamMatcher(); + private final boolean inviteMigrationOn; + + + public TeamController(@Value("${features.invite-migration-on}") + boolean inviteMigrationOn) { + this.inviteMigrationOn = inviteMigrationOn; + } + @GetMapping("api/teams/my-teams") public MyTeams myTeams(FederatedUser federatedUser) { @@ -126,7 +128,10 @@ public boolean teamExistsByName(@RequestParam("name") String name) { @PreAuthorize("hasRole('ADMIN')") @PostMapping("api/teams/teams") - public Object createTeam(@Validated @RequestBody NewTeamProperties teamProperties, FederatedUser federatedUser) throws IOException, MessagingException { + public Object createTeam(@Validated @RequestBody NewTeamProperties teamProperties, FederatedUser federatedUser) { + if (this.inviteMigrationOn) { + throw new NotAllowedException("Migration to invite is on"); + } Team team = doCreateTeam(teamProperties, federatedUser); Person person = federatedUser.getPerson(); diff --git a/teams-server/src/main/resources/application.yml b/teams-server/src/main/resources/application.yml index 7385189c..e17dd8b7 100644 --- a/teams-server/src/main/resources/application.yml +++ b/teams-server/src/main/resources/application.yml @@ -37,7 +37,7 @@ config: supported_language_codes: en,nl features: - invite-migration-on: true + invite-migration-on: false security: user: diff --git a/teams-server/src/test/java/teams/api/TeamControllerInviteMigrationTest.java b/teams-server/src/test/java/teams/api/TeamControllerInviteMigrationTest.java new file mode 100644 index 00000000..52674999 --- /dev/null +++ b/teams-server/src/test/java/teams/api/TeamControllerInviteMigrationTest.java @@ -0,0 +1,39 @@ +package teams.api; + +import io.restassured.http.ContentType; +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; +import teams.AbstractApplicationTest; +import teams.domain.*; +import teams.exception.DuplicateTeamNameException; +import teams.exception.IllegalMembershipException; +import teams.exception.NotAllowedException; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import static io.restassured.RestAssured.given; +import static org.apache.http.HttpStatus.*; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, +properties = "features.invite-migration-on=true") +public class TeamControllerInviteMigrationTest extends AbstractApplicationTest { + + @Test + public void createTeamBadRequest() { + given() + .body(new NewTeamProperties("new team name", "Team champions ", null, true, true,false, + null, Role.ADMIN.name(), null, Language.DUTCH)) + .header(CONTENT_TYPE, "application/json") + .when() + .post("api/teams/teams") + .then() + .statusCode(SC_BAD_REQUEST); + + } + +} diff --git a/teams-server/src/test/java/teams/api/validations/TeamValidatorTest.java b/teams-server/src/test/java/teams/api/validations/TeamValidatorTest.java index b493e25a..1cbf9340 100644 --- a/teams-server/src/test/java/teams/api/validations/TeamValidatorTest.java +++ b/teams-server/src/test/java/teams/api/validations/TeamValidatorTest.java @@ -15,7 +15,7 @@ public class TeamValidatorTest implements Seed { - private TeamValidator subject = new TeamController(); + private final TeamValidator subject = new TeamController(false); @Test public void onlyAdminAllowed() throws Exception {