Search in sources :

Example 1 with Avatar

use of com.faforever.api.data.domain.Avatar in project faf-java-api by FAForever.

the class AvatarServiceTest method deleteAvatarWithAssignments.

@Test
public void deleteAvatarWithAssignments() throws Exception {
    when(avatarRepository.findById(AVATAR_ID)).thenReturn(Optional.of(new Avatar().setAssignments(Collections.singletonList(new AvatarAssignment()))));
    expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.AVATAR_IN_USE));
    avatarService.deleteAvatar(AVATAR_ID);
    verify(avatarRepository, never()).delete(new Avatar());
}
Also used : Avatar(com.faforever.api.data.domain.Avatar) AvatarAssignment(com.faforever.api.data.domain.AvatarAssignment) Test(org.junit.Test)

Example 2 with Avatar

use of com.faforever.api.data.domain.Avatar in project faf-java-api by FAForever.

the class AvatarServiceTest method existingAvatarReuploadWithDifferentValidName.

@Test
public void existingAvatarReuploadWithDifferentValidName() throws Exception {
    final Path avatarFilePath = avatarsPath.resolve(EXISTING_VALID_AVATAR_FILENAME);
    Files.copy(loadResource(BIG_AVATAR_FILENAME).openStream(), avatarFilePath);
    final String avatarFileName = VALID_AVATAR_FILENAME;
    final URL imageResource = loadResource(avatarFileName);
    try (final InputStream imageInputStream = imageResource.openStream()) {
        avatarService.updateAvatar(1, AVATAR_METADATA, "[./><" + avatarFileName, imageInputStream, VALID_FILE_SIZE);
        ArgumentCaptor<Avatar> avatarCaptor = ArgumentCaptor.forClass(Avatar.class);
        verify(avatarRepository, times(1)).save(avatarCaptor.capture());
        final Avatar storedAvatar = avatarCaptor.getValue();
        assertEquals(String.format(DOWNLOAD_URL_FORMAT, EXISTING_VALID_AVATAR_FILENAME), storedAvatar.getUrl());
        assertEquals(AVATAR_NAME, storedAvatar.getTooltip());
        assertThat(avatarsPath.resolve(EXISTING_VALID_AVATAR_FILENAME).toFile().length(), is(imageResource.openConnection().getContentLengthLong()));
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) URL(java.net.URL) Avatar(com.faforever.api.data.domain.Avatar) Test(org.junit.Test)

Example 3 with Avatar

use of com.faforever.api.data.domain.Avatar in project faf-java-api by FAForever.

the class AvatarServiceTest method deleteNotExistingAvatar.

@Test
public void deleteNotExistingAvatar() throws Exception {
    when(avatarRepository.findById(AVATAR_ID)).thenReturn(Optional.empty());
    expectedException.expect(NotFoundApiException.class);
    avatarService.deleteAvatar(AVATAR_ID);
    verify(avatarRepository, never()).delete(new Avatar());
}
Also used : Avatar(com.faforever.api.data.domain.Avatar) Test(org.junit.Test)

Example 4 with Avatar

use of com.faforever.api.data.domain.Avatar in project faf-java-api by FAForever.

the class AvatarControllerTest method moderatorCanReupload.

@Test
@WithUserDetails(AUTH_MODERATOR)
public void moderatorCanReupload() throws Exception {
    Files.copy(FileHandlingHelper.loadResourceAsStream("/avatars/donator.png"), Paths.get("build/cache/avatars/avatar1.png"));
    mockMvc.perform(createAvatarReuploadRequest(1).with(getOAuthToken(OAuthScope._UPLOAD_AVATAR))).andExpect(status().isOk()).andExpect(content().string(""));
    final Avatar avatar = avatarRepository.findOneByUrl("http://localhost/faf/avatars/avatar1.png").get();
    assertThat(avatar.getUrl(), is("http://localhost/faf/avatars/avatar1.png"));
    assertThat(avatar.getTooltip(), is("Best avatar"));
    verify(auditServiceSpy, times(1)).logMessage(any());
}
Also used : Avatar(com.faforever.api.data.domain.Avatar) AbstractIntegrationTest(com.faforever.api.AbstractIntegrationTest) Test(org.junit.Test) WithUserDetails(org.springframework.security.test.context.support.WithUserDetails)

Example 5 with Avatar

use of com.faforever.api.data.domain.Avatar in project faf-java-api by FAForever.

the class AvatarControllerTest method moderatorCanUpload.

@Test
@WithUserDetails(AUTH_MODERATOR)
public void moderatorCanUpload() throws Exception {
    mockMvc.perform(createAvatarUploadRequest().with(getOAuthToken(OAuthScope._UPLOAD_AVATAR))).andExpect(status().isCreated()).andExpect(content().string(""));
    final Avatar avatar = avatarRepository.findOneByUrl("http://localhost/faf/avatars/avatar3.png").get();
    assertThat(avatar.getUrl(), is("http://localhost/faf/avatars/avatar3.png"));
    assertThat(avatar.getTooltip(), is("Best avatar"));
    verify(auditServiceSpy, times(1)).logMessage(any());
}
Also used : Avatar(com.faforever.api.data.domain.Avatar) AbstractIntegrationTest(com.faforever.api.AbstractIntegrationTest) Test(org.junit.Test) WithUserDetails(org.springframework.security.test.context.support.WithUserDetails)

Aggregations

Avatar (com.faforever.api.data.domain.Avatar)12 Test (org.junit.Test)8 InputStream (java.io.InputStream)5 Path (java.nio.file.Path)5 Audit (com.faforever.api.security.Audit)3 SneakyThrows (lombok.SneakyThrows)3 Transactional (org.springframework.transaction.annotation.Transactional)3 AbstractIntegrationTest (com.faforever.api.AbstractIntegrationTest)2 ApiException (com.faforever.api.error.ApiException)2 Error (com.faforever.api.error.Error)2 NotFoundApiException (com.faforever.api.error.NotFoundApiException)2 ProgrammingError (com.faforever.api.error.ProgrammingError)2 URL (java.net.URL)2 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)2 WithUserDetails (org.springframework.security.test.context.support.WithUserDetails)2 FafApiProperties (com.faforever.api.config.FafApiProperties)1 AvatarAssignment (com.faforever.api.data.domain.AvatarAssignment)1 Before (org.junit.Before)1