use of com.faforever.api.error.ProgrammingError in project faf-java-api by FAForever.
the class ClanServiceTest method acceptPlayerInvitationTokenInvalidPlayer.
@Test
public void acceptPlayerInvitationTokenInvalidPlayer() throws IOException {
String stringToken = "1234";
Clan clan = ClanFactory.builder().build();
long expire = System.currentTimeMillis() + 1000 * 3;
Jwt jwtToken = Mockito.mock(Jwt.class);
when(jwtToken.getClaims()).thenReturn(String.format("{\"expire\":%s,\"newMember\":{\"id\":2},\"clan\":{\"id\":%s}}", expire, clan.getId()));
when(jwtService.decodeAndVerify(any())).thenReturn(jwtToken);
when(clanRepository.findById(clan.getId())).thenReturn(Optional.of(clan));
try {
instance.acceptPlayerInvitationToken(stringToken, null);
fail();
} catch (ProgrammingError e) {
assertEquals("ClanMember does not exist: 2", e.getMessage());
}
verify(clanMembershipRepository, Mockito.never()).save(any(ClanMembership.class));
}
use of com.faforever.api.error.ProgrammingError in project faf-java-api by FAForever.
the class ClanService method acceptPlayerInvitationToken.
@SneakyThrows
void acceptPlayerInvitationToken(String stringToken, Authentication authentication) {
Jwt token = jwtService.decodeAndVerify(stringToken);
InvitationResult invitation = objectMapper.readValue(token.getClaims(), InvitationResult.class);
if (invitation.isExpired()) {
throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE));
}
final Integer clanId = invitation.getClan().getId();
Player player = playerService.getPlayer(authentication);
Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
Player newMember = playerRepository.findById(invitation.getNewMember().getId()).orElseThrow(() -> new ProgrammingError("ClanMember does not exist: " + invitation.getNewMember().getId()));
if (player.getId() != newMember.getId()) {
throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
}
if (newMember.getClan() != null) {
throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN));
}
ClanMembership membership = new ClanMembership();
membership.setClan(clan);
membership.setPlayer(newMember);
clanMembershipRepository.save(membership);
}
use of com.faforever.api.error.ProgrammingError in project faf-java-api by FAForever.
the class AvatarService method readImageDimensions.
private Dimension readImageDimensions(InputStream imageInputStream, String imageFileName) throws IOException {
final String fileExtension = com.google.common.io.Files.getFileExtension(imageFileName);
final Iterator<ImageReader> imageReadersBySuffix = ImageIO.getImageReadersBySuffix(fileExtension);
if (imageReadersBySuffix.hasNext()) {
final ImageReader imageReader = imageReadersBySuffix.next();
try {
imageReader.setInput(new MemoryCacheImageInputStream(imageInputStream));
final int width = imageReader.getWidth(imageReader.getMinIndex());
final int height = imageReader.getHeight(imageReader.getMinIndex());
return new Dimension(width, height);
} finally {
imageReader.dispose();
}
} else {
throw new ProgrammingError("Unsupported image format. Could not read dimensions.");
}
}
Aggregations