Search in sources :

Example 21 with WithMockUser

use of org.springframework.security.test.context.support.WithMockUser in project hub-alert by blackducksoftware.

the class GlobalControllerTest method testPostConfig.

@Test
@WithMockUser(roles = "ADMIN")
public void testPostConfig() throws Exception {
    globalEntityRepository.deleteAll();
    final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post(restUrl).with(SecurityMockMvcRequestPostProcessors.user("admin").roles("ADMIN"));
    request.content(gson.toJson(restModel));
    request.contentType(contentType);
    mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isCreated());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ExternalConnectionTest(com.blackducksoftware.integration.test.annotation.ExternalConnectionTest) DatabaseConnectionTest(com.blackducksoftware.integration.test.annotation.DatabaseConnectionTest) Test(org.junit.Test)

Example 22 with WithMockUser

use of org.springframework.security.test.context.support.WithMockUser in project hub-alert by blackducksoftware.

the class GlobalControllerTest method testTestConfig.

@Test
@WithMockUser(roles = "ADMIN")
public void testTestConfig() throws Exception {
    final String testRestUrl = restUrl + "/test";
    final MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post(testRestUrl).with(SecurityMockMvcRequestPostProcessors.user("admin").roles("ADMIN"));
    mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isMethodNotAllowed());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ExternalConnectionTest(com.blackducksoftware.integration.test.annotation.ExternalConnectionTest) DatabaseConnectionTest(com.blackducksoftware.integration.test.annotation.DatabaseConnectionTest) Test(org.junit.Test)

Example 23 with WithMockUser

use of org.springframework.security.test.context.support.WithMockUser in project theskeleton by codenergic.

the class ProfileRestControllerTest method testUpdateProfilePassword.

@Test
@WithMockUser("user123")
public void testUpdateProfilePassword() throws Exception {
    final UserEntity user = new UserEntity().setId("user123").setUsername("user").setEmail("user@server");
    when(profileService.updateProfilePassword(eq("user123"), any())).thenReturn(user);
    MockHttpServletRequestBuilder request = put("/api/profile/password").content("{\"username\": \"user123\"}").contentType(MediaType.APPLICATION_JSON);
    MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-profile-password-update")).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(ProfileRestData.builder().fromUserEntity(user).build()));
    verify(profileService).updateProfilePassword(eq("user123"), any());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 24 with WithMockUser

use of org.springframework.security.test.context.support.WithMockUser in project theskeleton by codenergic.

the class ProfileRestControllerTest method testFindProfileConnectedApps.

@Test
@WithMockUser("user123")
public void testFindProfileConnectedApps() throws Exception {
    final UserEntity user = new UserEntity().setUsername("user123");
    final OAuth2ClientEntity client = new OAuth2ClientEntity().setId("client123").setName("client123");
    final UserOAuth2ClientApprovalEntity approval1 = new UserOAuth2ClientApprovalEntity().setUser(user).setClient(client).setScope("scope1").setApprovalStatus(Approval.ApprovalStatus.APPROVED);
    final UserOAuth2ClientApprovalEntity approval2 = new UserOAuth2ClientApprovalEntity().setUser(user).setClient(client).setScope("scope2").setApprovalStatus(Approval.ApprovalStatus.DENIED);
    when(profileService.findOAuth2ClientApprovalByUsername("user123")).thenReturn(Arrays.asList(approval1, approval2));
    MockHttpServletRequestBuilder request = get("/api/profile/connected-apps").contentType(MediaType.APPLICATION_JSON);
    MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-profile-connected-apps-view")).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, UserOAuth2ClientApprovalRestData.class);
    List<UserOAuth2ClientApprovalRestData> returnedData = objectMapper.readValue(response.getContentAsByteArray(), collectionType);
    assertThat(returnedData).hasSize(1);
    UserOAuth2ClientApprovalRestData restData = returnedData.get(0);
    assertThat(restData.getClientId()).isEqualTo(client.getId());
    assertThat(restData.getClientName()).isEqualTo(client.getName());
    assertThat(restData.getUsername()).isEqualTo(user.getUsername());
    ImmutableMap<String, Approval.ApprovalStatus> scopeAndStatus = restData.getScopeAndStatus();
    assertThat(scopeAndStatus).hasSize(2);
    assertThat(scopeAndStatus).containsEntry(approval1.getScope(), approval1.getApprovalStatus());
    assertThat(scopeAndStatus).containsEntry(approval2.getScope(), approval2.getApprovalStatus());
    verify(profileService).findOAuth2ClientApprovalByUsername("user123");
}
Also used : OAuth2ClientEntity(org.codenergic.theskeleton.client.OAuth2ClientEntity) UserOAuth2ClientApprovalRestData(org.codenergic.theskeleton.user.UserOAuth2ClientApprovalRestData) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) UserOAuth2ClientApprovalEntity(org.codenergic.theskeleton.user.UserOAuth2ClientApprovalEntity) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 25 with WithMockUser

use of org.springframework.security.test.context.support.WithMockUser in project theskeleton by codenergic.

the class ProfileRestControllerTest method testUpdateProfilePicture.

@Test
@WithMockUser("user123")
public void testUpdateProfilePicture() throws Exception {
    final UserEntity user = new UserEntity().setId("user123").setUsername("user").setEmail("user@server");
    when(profileService.updateProfilePicture(eq("user123"), any(), eq("image/png"))).thenReturn(user);
    InputStream image = ClassLoader.getSystemResourceAsStream("static/logo.png");
    MockHttpServletRequestBuilder request = put("/api/profile/picture").content(IOUtils.toByteArray(image)).contentType(MediaType.IMAGE_PNG);
    MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-profile-picture-update")).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(ProfileRestData.builder().fromUserEntity(user).build()));
    verify(profileService).updateProfilePicture(eq("user123"), any(), eq("image/png"));
    image.close();
}
Also used : InputStream(java.io.InputStream) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Aggregations

WithMockUser (org.springframework.security.test.context.support.WithMockUser)3191 Test (org.junit.jupiter.api.Test)1911 Test (org.junit.Test)1198 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)762 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)427 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)379 MvcResult (org.springframework.test.web.servlet.MvcResult)290 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)275 ResultActions (org.springframework.test.web.servlet.ResultActions)224 Transactional (org.springframework.transaction.annotation.Transactional)200 User (io.github.jhipster.sample.domain.User)138 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)136 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)130 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)130 Course (de.tum.in.www1.artemis.domain.Course)124 ArrayList (java.util.ArrayList)118 Exam (de.tum.in.www1.artemis.domain.exam.Exam)114 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)103 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)102 User (de.tum.in.www1.artemis.domain.User)95