Search in sources :

Example 1 with Clan

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

the class ClanServiceTest method generatePlayerInvitationTokenFromNonLeader.

@Test
public void generatePlayerInvitationTokenFromNonLeader() throws IOException {
    Player requester = new Player();
    requester.setId(1);
    Player newMember = new Player();
    newMember.setId(2);
    Player leader = new Player();
    leader.setId(3);
    Clan clan = ClanFactory.builder().leader(leader).build();
    when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
    try {
        instance.generatePlayerInvitationToken(requester, newMember.getId(), clan.getId());
        fail();
    } catch (ApiException e) {
        assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_NOT_LEADER));
    }
    verify(jwtService, Mockito.never()).sign(any());
}
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 2 with Clan

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

the class ClanServiceTest method generatePlayerInvitationToken.

@Test
public void generatePlayerInvitationToken() throws IOException {
    Player requester = new Player();
    requester.setId(1);
    Player newMember = new Player();
    newMember.setId(2);
    Clan clan = ClanFactory.builder().leader(requester).build();
    FafApiProperties props = new FafApiProperties();
    when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
    when(playerRepository.findById(newMember.getId())).thenReturn(Optional.of(newMember));
    when(fafApiProperties.getClan()).thenReturn(props.getClan());
    instance.generatePlayerInvitationToken(requester, newMember.getId(), clan.getId());
    ArgumentCaptor<InvitationResult> captor = ArgumentCaptor.forClass(InvitationResult.class);
    verify(jwtService, Mockito.times(1)).sign(captor.capture());
    assertThat("expire", captor.getValue().getExpire(), greaterThan(System.currentTimeMillis()));
    assertEquals(newMember.getId(), captor.getValue().getNewMember().getId());
    assertEquals(newMember.getLogin(), captor.getValue().getNewMember().getLogin());
    assertEquals(clan.getId(), captor.getValue().getClan().getId());
    assertEquals(clan.getTag(), captor.getValue().getClan().getTag());
    assertEquals(clan.getName(), captor.getValue().getClan().getName());
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) InvitationResult(com.faforever.api.clan.result.InvitationResult) FafApiProperties(com.faforever.api.config.FafApiProperties) Test(org.junit.Test)

Example 3 with Clan

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

the class ClanServiceTest method createClanSuccessful.

@Test
public void createClanSuccessful() {
    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.empty());
    instance.create(clanName, tag, description, creator);
    ArgumentCaptor<Clan> clanCaptor = ArgumentCaptor.forClass(Clan.class);
    verify(clanRepository, Mockito.times(1)).save(clanCaptor.capture());
    assertEquals(clanName, clanCaptor.getValue().getName());
    assertEquals(tag, clanCaptor.getValue().getTag());
    assertEquals(description, clanCaptor.getValue().getDescription());
    assertEquals(creator, clanCaptor.getValue().getLeader());
    assertEquals(creator, clanCaptor.getValue().getFounder());
    assertEquals(1, clanCaptor.getValue().getMemberships().size());
    assertEquals(creator, clanCaptor.getValue().getMemberships().get(0).getPlayer());
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Test(org.junit.Test)

Example 4 with Clan

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

the class ClanServiceTest method generatePlayerInvitationTokenInvalidPlayer.

@Test
public void generatePlayerInvitationTokenInvalidPlayer() throws IOException {
    Player requester = new Player();
    requester.setId(1);
    Clan clan = ClanFactory.builder().leader(requester).build();
    when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
    try {
        instance.generatePlayerInvitationToken(requester, 42, clan.getId());
        fail();
    } catch (ApiException e) {
        assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_GENERATE_LINK_PLAYER_NOT_FOUND));
    }
    verify(jwtService, Mockito.never()).sign(any());
}
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 5 with Clan

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

the class ClanServiceTest method acceptPlayerInvitationTokenPlayerIAlreadyInAClan.

@Test
public void acceptPlayerInvitationTokenPlayerIAlreadyInAClan() throws IOException {
    String stringToken = "1234";
    Clan clan = ClanFactory.builder().build();
    Player newMember = new Player();
    newMember.setId(2);
    newMember.setClanMemberships(Collections.singleton(new ClanMembership().setClan(clan).setPlayer(newMember)));
    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);
    try {
        instance.acceptPlayerInvitationToken(stringToken, null);
        fail();
    } catch (ApiException e) {
        assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN));
    }
    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)

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