Skip to content

Commit

Permalink
Server side invite migration check for new teams
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed May 17, 2024
1 parent fa4d987 commit 5135c65
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
21 changes: 13 additions & 8 deletions teams-server/src/main/java/teams/api/TeamController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
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;
import teams.domain.*;
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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion teams-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ config:
supported_language_codes: en,nl

features:
invite-migration-on: true
invite-migration-on: false

security:
user:
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5135c65

Please sign in to comment.