Search in sources :

Example 16 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClansController method me.

@ApiOperation("Grab data about yourself and the clan")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success with JSON { player: {id: ?, login: ?}, clan: { id: ?, name: ?, tag: ?}}"), @ApiResponse(code = 400, message = "Bad Request") })
@RequestMapping(path = "/me", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public MeResult me(Authentication authentication) {
    Player player = playerService.getPlayer(authentication);
    Clan clan = player.getClan();
    ClanResult clanResult = null;
    if (clan != null) {
        clanResult = ClanResult.of(clan);
    }
    return new MeResult(PlayerResult.of(player), clanResult);
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) MeResult(com.faforever.api.clan.result.MeResult) ClanResult(com.faforever.api.clan.result.ClanResult) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class GlobalControllerExceptionHandlerTest method testConstraintViolationException.

@Test
public void testConstraintViolationException() {
    ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
    final Validator validator = vf.getValidator();
    final Player player = new Player();
    player.setId(1);
    player.setLogin("Player");
    final Clan clan = new Clan();
    clan.setName("Clan");
    clan.setLeader(player);
    clan.setFounder(player);
    clan.setMemberships(Collections.emptyList());
    final Set<ConstraintViolation<Clan>> constraintViolations = validator.validate(clan);
    final ConstraintViolationException ex = new ConstraintViolationException(COMMON_MESSAGE, constraintViolations);
    ErrorResponse response = instance.processConstraintViolationException(ex);
    assertEquals(3, response.getErrors().size());
    final ErrorResult errorResult = response.getErrors().get(0);
    assertEquals(ErrorCode.VALIDATION_FAILED.getTitle(), errorResult.getTitle());
    assertEquals(String.valueOf(ErrorCode.VALIDATION_FAILED.getCode()), errorResult.getAppCode());
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Test(org.junit.Test)

Example 18 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClanServiceTest method acceptPlayerInvitationTokenWrongPlayer.

@Test
public void acceptPlayerInvitationTokenWrongPlayer() throws IOException {
    String stringToken = "1234";
    Player newMember = new Player();
    newMember.setId(2);
    Clan clan = ClanFactory.builder().build();
    Player otherPlayer = new Player();
    otherPlayer.setId(3);
    long expire = System.currentTimeMillis() + 1000 * 3;
    Jwt jwtToken = Mockito.mock(Jwt.class);
    when(jwtToken.getClaims()).thenReturn(String.format("{\"expire\":%s,\"newMember\":{\"id\":%s},\"clan\":{\"id\":%s}}", expire, newMember.getId(), clan.getId()));
    when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);
    when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
    when(playerRepository.findById(newMember.getId())).thenReturn(Optional.of(newMember));
    when(playerService.getPlayer(any())).thenReturn(otherPlayer);
    try {
        instance.acceptPlayerInvitationToken(stringToken, null);
        fail();
    } catch (ApiException e) {
        assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
    }
    verify(clanMembershipRepository, Mockito.never()).save(any(ClanMembership.class));
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Jwt(org.springframework.security.jwt.Jwt) ClanMembership(com.faforever.api.data.domain.ClanMembership) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 19 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClanServiceTest method createClanWithSameTag.

@Test
public void createClanWithSameTag() {
    String clanName = "My cool Clan";
    String tag = "123";
    String description = "A cool clan for testing";
    Player creator = new Player();
    creator.setId(1);
    when(clanRepository.findOneByName(clanName)).thenReturn(Optional.empty());
    when(clanRepository.findOneByTag(tag)).thenReturn(Optional.of(new Clan()));
    try {
        instance.create(clanName, tag, description, creator);
        fail();
    } catch (ApiException e) {
        assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_TAG_EXISTS));
    }
    ArgumentCaptor<Clan> clanCaptor = ArgumentCaptor.forClass(Clan.class);
    verify(clanRepository, Mockito.times(0)).save(clanCaptor.capture());
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 20 with Clan

use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.

the class ClanServiceTest method acceptPlayerInvitationToken.

@Test
public void acceptPlayerInvitationToken() throws IOException {
    String stringToken = "1234";
    Clan clan = ClanFactory.builder().build();
    Player newMember = new Player();
    newMember.setId(2);
    long expire = System.currentTimeMillis() + 1000 * 3;
    Jwt jwtToken = Mockito.mock(Jwt.class);
    when(jwtToken.getClaims()).thenReturn(String.format("{\"expire\":%s,\"newMember\":{\"id\":%s},\"clan\":{\"id\":%s}}", expire, newMember.getId(), clan.getId()));
    when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);
    when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
    when(playerRepository.findById(newMember.getId())).thenReturn(Optional.of(newMember));
    when(playerService.getPlayer(any())).thenReturn(newMember);
    instance.acceptPlayerInvitationToken(stringToken, null);
    ArgumentCaptor<ClanMembership> captor = ArgumentCaptor.forClass(ClanMembership.class);
    verify(clanMembershipRepository, Mockito.times(1)).save(captor.capture());
    assertEquals(newMember.getId(), captor.getValue().getPlayer().getId());
    assertEquals(clan.getId(), captor.getValue().getClan().getId());
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Jwt(org.springframework.security.jwt.Jwt) ClanMembership(com.faforever.api.data.domain.ClanMembership) Test(org.junit.Test)

Aggregations

Clan (com.faforever.api.data.domain.Clan)20 Player (com.faforever.api.data.domain.Player)17 Test (org.junit.Test)15 ApiException (com.faforever.api.error.ApiException)10 ClanMembership (com.faforever.api.data.domain.ClanMembership)7 Jwt (org.springframework.security.jwt.Jwt)5 ProgrammingError (com.faforever.api.error.ProgrammingError)4 InvitationResult (com.faforever.api.clan.result.InvitationResult)3 Error (com.faforever.api.error.Error)3 SneakyThrows (lombok.SneakyThrows)3 AbstractIntegrationTest (com.faforever.api.AbstractIntegrationTest)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 WithUserDetails (org.springframework.security.test.context.support.WithUserDetails)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ClanResult (com.faforever.api.clan.result.ClanResult)1 MeResult (com.faforever.api.clan.result.MeResult)1 FafApiProperties (com.faforever.api.config.FafApiProperties)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1