use of com.faforever.api.data.domain.ClanMembership 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));
}
use of com.faforever.api.data.domain.ClanMembership in project faf-java-api by FAForever.
the class ClanServiceTest method createClanWhereLeaderIsAlreadyInAClan.
@Test
public void createClanWhereLeaderIsAlreadyInAClan() {
String clanName = "My cool Clan";
String tag = "123";
String description = "A cool clan for testing";
Player creator = new Player();
creator.setId(1);
creator.getClanMemberships().add(new ClanMembership());
try {
instance.create(clanName, tag, description, creator);
fail();
} catch (ApiException e) {
assertThat(e, apiExceptionWithCode(ErrorCode.CLAN_CREATE_CREATOR_IS_IN_A_CLAN));
}
verify(clanRepository, Mockito.never()).save(any(Clan.class));
}
use of com.faforever.api.data.domain.ClanMembership 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.data.domain.ClanMembership in project faf-java-api by FAForever.
the class ClanService method create.
@SneakyThrows
Clan create(String name, String tag, String description, Player creator) {
if (!creator.getClanMemberships().isEmpty()) {
throw new ApiException(new Error(ErrorCode.CLAN_CREATE_CREATOR_IS_IN_A_CLAN));
}
if (clanRepository.findOneByName(name).isPresent()) {
throw new ApiException(new Error(ErrorCode.CLAN_NAME_EXISTS, name));
}
if (clanRepository.findOneByTag(tag).isPresent()) {
throw new ApiException(new Error(ErrorCode.CLAN_TAG_EXISTS, name));
}
Clan clan = new Clan();
clan.setName(name);
clan.setTag(tag);
clan.setDescription(description);
clan.setFounder(creator);
clan.setLeader(creator);
ClanMembership membership = new ClanMembership();
membership.setClan(clan);
membership.setPlayer(creator);
clan.setMemberships(Collections.singletonList(membership));
// clan membership is saved over cascading, otherwise validation will fail
clanRepository.save(clan);
return clan;
}
use of com.faforever.api.data.domain.ClanMembership in project faf-java-api by FAForever.
the class ClanServiceTest method acceptPlayerInvitationToken.
@Test
public void acceptPlayerInvitationToken() throws IOException {
String stringToken = "1234";
Clan clan = ClanFactory.builder().build();
Player newMember = new Player();
newMember.setId(2);
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);
instance.acceptPlayerInvitationToken(stringToken, null);
ArgumentCaptor<ClanMembership> captor = ArgumentCaptor.forClass(ClanMembership.class);
verify(clanMembershipRepository, Mockito.times(1)).save(captor.capture());
assertEquals(newMember.getId(), captor.getValue().getPlayer().getId());
assertEquals(clan.getId(), captor.getValue().getClan().getId());
}
Aggregations