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);
}
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());
}
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));
}
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());
}
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));
}
Aggregations