Search in sources :

Example 1 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class VotingServiceTest method notSaveVoteIfUserVotedAlready.

@Test
public void notSaveVoteIfUserVotedAlready() {
    Vote vote = new Vote();
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setBeginOfVoteTime(OffsetDateTime.now());
    votingSubject.setEndOfVoteTime(OffsetDateTime.MAX);
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(false);
    votingQuestion.setMaxAnswers(1);
    votingSubject.setVotingQuestions(Collections.singleton(votingQuestion));
    vote.setVotingSubject(votingSubject);
    Player player = new Player();
    when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.of(new Vote()));
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    try {
        instance.saveVote(vote, player);
    } catch (ApiException e) {
        assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.VOTED_TWICE)));
    }
    verify(voteRepository, never()).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 2 with ApiException

use of com.faforever.api.error.ApiException 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 3 with ApiException

use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.

the class ClanServiceTest method acceptPlayerInvitationTokenExpire.

@Test
public void acceptPlayerInvitationTokenExpire() throws IOException {
    String stringToken = "1234";
    long expire = System.currentTimeMillis();
    Jwt jwtToken = Mockito.mock(Jwt.class);
    when(jwtToken.getClaims()).thenReturn(String.format("{\"expire\":%s}", expire));
    when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);
    try {
        instance.acceptPlayerInvitationToken(stringToken, null);
        fail();
    } catch (ApiException e) {
        assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE));
    }
    verify(clanMembershipRepository, Mockito.never()).save(any(ClanMembership.class));
}
Also used : Jwt(org.springframework.security.jwt.Jwt) ClanMembership(com.faforever.api.data.domain.ClanMembership) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 4 with ApiException

use of com.faforever.api.error.ApiException 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 ApiException

use of com.faforever.api.error.ApiException 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

ApiException (com.faforever.api.error.ApiException)48 Error (com.faforever.api.error.Error)29 Player (com.faforever.api.data.domain.Player)18 Test (org.junit.Test)18 ProgrammingError (com.faforever.api.error.ProgrammingError)13 SneakyThrows (lombok.SneakyThrows)11 Clan (com.faforever.api.data.domain.Clan)10 ClanMembership (com.faforever.api.data.domain.ClanMembership)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ArrayList (java.util.ArrayList)6 ZipInputStream (java.util.zip.ZipInputStream)6 Map (com.faforever.api.config.FafApiProperties.Map)5 Vote (com.faforever.api.data.domain.Vote)5 VotingSubject (com.faforever.api.data.domain.VotingSubject)5 NotFoundApiException (com.faforever.api.error.NotFoundApiException)5 Jwt (org.springframework.security.jwt.Jwt)5 LuaValue (org.luaj.vm2.LuaValue)4