use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.
the class UserRestControllerTest method testFindUserConnectedApps.
@Test
@WithMockUser("user123")
public void testFindUserConnectedApps() 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(userService.findUserOAuth2ClientApprovalByUsername("user123")).thenReturn(Arrays.asList(approval1, approval2));
MockHttpServletRequestBuilder request = get("/api/users/user123/connected-apps").contentType(MediaType.APPLICATION_JSON);
MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-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(userService).findUserOAuth2ClientApprovalByUsername("user123");
}
use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.
the class OAuth2ClientServiceImpl method deleteClient.
@Override
@Transactional
public void deleteClient(String id) {
OAuth2ClientEntity o = findClientById(id);
assertClientNotNull(o);
clientRepository.delete(o);
}
use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.
the class ProfileServiceTest method testFindOAuth2ClientApprovalByUsername.
@Test
public void testFindOAuth2ClientApprovalByUsername() throws Exception {
final UserOAuth2ClientApprovalEntity result = new UserOAuth2ClientApprovalEntity().setId("123").setUser(new UserEntity().setUsername("user")).setClient(new OAuth2ClientEntity().setId("123")).setApprovalStatus(Approval.ApprovalStatus.APPROVED).setScope("scope1");
when(approvalRepository.findByUserUsername("user")).thenReturn(Collections.singletonList(result));
List<UserOAuth2ClientApprovalEntity> approvals = profileService.findOAuth2ClientApprovalByUsername("user");
assertThat(approvals).isNotEmpty();
assertThat(approvals).hasSize(1);
assertThat(approvals.get(0)).isEqualTo(result);
verify(approvalRepository).findByUserUsername("user");
}
use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.
the class UserOauth2ClientApprovalStoreTest method testAddApprovals.
@Test
public void testAddApprovals() {
assertThatThrownBy(() -> {
approvalStore.addApprovals(null);
}).isInstanceOf(NullPointerException.class);
when(userRepository.findByUsername(anyString())).thenReturn(Optional.of(new UserEntity()));
when(approvalRepository.findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("read"))).thenReturn(new UserOAuth2ClientApprovalEntity().setUser(new UserEntity().setId("1")).setClient(new OAuth2ClientEntity().setId("2")).setScope("read").setApprovalStatus(ApprovalStatus.APPROVED));
approvalStore.addApprovals(Collections.singletonList(new Approval("1", "2", "read", new Date(), ApprovalStatus.APPROVED)));
verify(approvalRepository).findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("read"));
when(approvalRepository.findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("write"))).thenReturn(null);
approvalStore.addApprovals(Collections.singletonList(new Approval("1", "2", "write", new Date(), ApprovalStatus.APPROVED)));
verify(userRepository, atLeastOnce()).findByUsername(anyString());
verify(approvalRepository).findByUserUsernameAndClientIdAndScope(anyString(), anyString(), eq("write"));
verify(approvalRepository, times(2)).save(any(UserOAuth2ClientApprovalEntity.class));
}
use of org.codenergic.theskeleton.client.OAuth2ClientEntity in project theskeleton by codenergic.
the class UserOAuth2ClientApprovalStore method addApprovals.
@Override
public boolean addApprovals(Collection<Approval> approvals) {
Objects.requireNonNull(approvals);
approvals.forEach(a -> {
UserOAuth2ClientApprovalEntity approval = approvalRepository.findByUserUsernameAndClientIdAndScope(a.getUserId(), a.getClientId(), a.getScope());
UserOAuth2ClientApprovalEntity newApproval = new UserOAuth2ClientApprovalEntity();
if (approval != null) {
newApproval.setId(approval.getId());
newApproval.setCreatedBy(approval.getCreatedBy());
newApproval.setCreatedDate(approval.getCreatedDate());
}
UserEntity user = userRepository.findByUsername(a.getUserId()).orElseThrow(() -> new UsernameNotFoundException(a.getUserId()));
newApproval.setUser(user).setClient(new OAuth2ClientEntity().setId(a.getClientId())).setScope(a.getScope()).setApprovalStatus(a.getStatus());
approvalRepository.save(newApproval);
});
return true;
}
Aggregations