use of com.faforever.api.clan.result.InvitationResult 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());
}
use of com.faforever.api.clan.result.InvitationResult 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.clan.result.InvitationResult in project faf-java-api by FAForever.
the class ClanService method generatePlayerInvitationToken.
@SneakyThrows
String generatePlayerInvitationToken(Player requester, int newMemberId, int clanId) {
Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
if (requester.getId() != clan.getLeader().getId()) {
throw new ApiException(new Error(ErrorCode.CLAN_NOT_LEADER, clanId));
}
Player newMember = playerRepository.findById(newMemberId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_GENERATE_LINK_PLAYER_NOT_FOUND, newMemberId)));
long expire = Instant.now().plus(fafApiProperties.getClan().getInviteLinkExpireDurationMinutes(), ChronoUnit.MINUTES).toEpochMilli();
InvitationResult result = new InvitationResult(expire, ClanResult.of(clan), PlayerResult.of(newMember));
return jwtService.sign(result);
}
Aggregations