Search in sources :

Example 1 with InvitationResult

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());
}
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 2 with InvitationResult

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);
}
Also used : Player(com.faforever.api.data.domain.Player) Jwt(org.springframework.security.jwt.Jwt) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 3 with InvitationResult

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);
}
Also used : Player(com.faforever.api.data.domain.Player) Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) InvitationResult(com.faforever.api.clan.result.InvitationResult) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Aggregations

InvitationResult (com.faforever.api.clan.result.InvitationResult)3 Clan (com.faforever.api.data.domain.Clan)3 Player (com.faforever.api.data.domain.Player)3 ApiException (com.faforever.api.error.ApiException)2 Error (com.faforever.api.error.Error)2 ProgrammingError (com.faforever.api.error.ProgrammingError)2 SneakyThrows (lombok.SneakyThrows)2 FafApiProperties (com.faforever.api.config.FafApiProperties)1 ClanMembership (com.faforever.api.data.domain.ClanMembership)1 Test (org.junit.Test)1 Jwt (org.springframework.security.jwt.Jwt)1