Search in sources :

Example 1 with UserOAuth2ClientApprovalRestData

use of org.codenergic.theskeleton.user.UserOAuth2ClientApprovalRestData 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)

Aggregations

CollectionType (com.fasterxml.jackson.databind.type.CollectionType)1 OAuth2ClientEntity (org.codenergic.theskeleton.client.OAuth2ClientEntity)1 UserEntity (org.codenergic.theskeleton.user.UserEntity)1 UserOAuth2ClientApprovalEntity (org.codenergic.theskeleton.user.UserOAuth2ClientApprovalEntity)1 UserOAuth2ClientApprovalRestData (org.codenergic.theskeleton.user.UserOAuth2ClientApprovalRestData)1 Test (org.junit.Test)1 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 WithMockUser (org.springframework.security.test.context.support.WithMockUser)1 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)1