public Optional<String> getUserPlatformUsername(final Principal user, final Platform platform) { if (platform != Platform.GITHUB) { throw new RuntimeException("only github is supported for now"); } return keycloakRepository.getUserIdentities(user.getName()) .filter(i -> i.getProvider().name().equalsIgnoreCase(platform.toString())) .findFirst() .map(UserIdentity::getUsername); } }
public UserDto map(User user) { UserDto result = delegate.map(user); if (result != null) { result.setUserIdentities(keycloakRepository.getUserIdentities(result.getUserId()).collect(Collectors.toList())); } return result; }
@Test public void getUserProfileReturnsCreationDate() { UserRepresentation userRepresentation = new UserRepresentation(); Date creationDate = DateUtil.parse("2018-01-31"); userRepresentation.setCreatedTimestamp(creationDate.getTime()); userRepresentation.setEmail("davy.van.roy@gmail.com"); when(keycloakRepository.getUser("davy")).thenReturn(userRepresentation); when(keycloakRepository.getUserIdentities("davy")).thenReturn(Stream.empty()); UserProfile userProfile = profileService.getUserProfile(() -> "davy"); assertThat(userProfile.getCreatedAt()).isEqualTo(creationDate.getTime()); }
@Test public void getUserProfileReturnsEmailVerifiedSignature() throws Exception { UserRepresentation userRepresentation = new UserRepresentation(); userRepresentation.setEmail("davy.van.roy@gmail.com"); when(keycloakRepository.getUser("davy")).thenReturn(userRepresentation); when(keycloakRepository.getUserIdentities("davy")).thenReturn(Stream.empty()); UserProfile userProfile = profileService.getUserProfile(() -> "davy"); assertThat(userProfile.getEmailSignedVerification()).isEqualTo("424a4f5feb0d4adeec05c717f6260c734a0ca036a133c7d29e911ac3c4fbb775"); }
@Override @Cacheable(value = "user_profile", key = "#principal.name") public UserProfile getUserProfile(Principal principal) { final Map<Provider, UserProfileProvider> providers = keycloakRepository.getUserIdentities(principal.getName()) .collect(Collectors.toMap(UserIdentity::getProvider, x -> UserProfileProvider.builder()
@Test public void canClaim_differentUser() { final Principal principal = PrincipalMother.davyvanroy(); final RequestDto requestDto = RequestDtoMother.fundRequestArea51(); final IssueInformationDto issueInformation = requestDto.getIssueInformation(); when(githubScraper.fetchGithubIssue(issueInformation.getOwner(), issueInformation.getRepo(), issueInformation.getNumber())).thenReturn(GithubIssue.builder() .solver("dfgj") .status("Closed") .build()); when(keycloakRepository.getUserIdentities(principal.getName())).thenReturn(Stream.of(UserIdentity.builder().provider(Provider.GITHUB).username("davyvanroy").build())); assertThat(claimResolver.canClaim(principal, requestDto)).isFalse(); }
@Test void getProfileWithWallets() { KeycloakAuthenticationToken principal = new KeycloakAuthenticationToken(new KeycloakAccount() { @Override public Principal getPrincipal() { return () -> "davy"; } @Override public Set<String> getRoles() { return null; } }, true); UserRepresentation userRepresentation = new UserRepresentation(); when(keycloakRepository.getUser("davy")).thenReturn(userRepresentation); when(keycloakRepository.getAccessToken(principal, Provider.ARKANE)).thenReturn("token"); Wallet expectedWallet = WalletMother.aWallet(); when(keycloakRepository.getUserIdentities("davy")).thenReturn(Stream.of(UserIdentity.builder().provider(Provider.ARKANE).build())); WalletsResult walletsResult = new WalletsResult(); walletsResult.setResult(Collections.singletonList(expectedWallet)); when(arkaneRepository.getWallets("Bearer token")).thenReturn(walletsResult); UserProfile profile = profileService.getUserProfile(principal); assertThat(profile.getWallets()).containsExactly(expectedWallet); } }
@Test public void canClaim() { final Principal principal = PrincipalMother.davyvanroy(); final RequestDto requestDto = RequestDtoMother.fundRequestArea51(); final IssueInformationDto issueInformation = requestDto.getIssueInformation(); when(githubScraper.fetchGithubIssue(issueInformation.getOwner(), issueInformation.getRepo(), issueInformation.getNumber())).thenReturn(GithubIssue.builder() .solver("davyvanroy") .status("Closed") .build()); when(keycloakRepository.getUserIdentities(principal.getName())).thenReturn(Stream.of(UserIdentity.builder().provider(Provider.GITHUB).username("davyvanroy").build())); assertThat(claimResolver.canClaim(principal, requestDto)).isTrue(); }
@Test public void claim() { final RequestDto request = RequestDtoMother.freeCodeCampNoUserStories(); final UserClaimRequest userClaimRequest = createClaimRequest(request); final IssueInformationDto issueInformation = request.getIssueInformation(); final SignClaimCommand signClaimCommand = createSignClaimCommand(userClaimRequest, "davyvanroy"); final ClaimSignature claimSignature = createClaimSignature(signClaimCommand); when(keycloakRepository.getUserIdentities("davyvanroy")).thenReturn(Stream.of(UserIdentity.builder() .provider(Provider.GITHUB) .username("davyvanroy") .build())); when(githubScraper.fetchGithubIssue(issueInformation.getOwner(), issueInformation.getRepo(), issueInformation.getNumber())).thenReturn(GithubIssue.builder() .solver("davyvanroy") .status("Closed") .build()); when(azraelClient.getSignature(signClaimCommand)).thenReturn(claimSignature); SignedClaim result = claimResolver.getSignedClaim(() -> "davyvanroy", userClaimRequest, request); assertThat(result.getPlatform().toString()).isEqualTo(claimSignature.getPlatform()); assertThat(result.getPlatformId()).isEqualTo(claimSignature.getPlatformId()); assertThat(result.getSolver()).isEqualTo(claimSignature.getSolver()); assertThat(result.getSolverAddress()).isEqualTo(claimSignature.getAddress()); assertThat(result.getR()).isEqualTo(claimSignature.getR()); assertThat(result.getS()).isEqualTo(claimSignature.getS()); assertThat(result.getV()).isEqualTo(claimSignature.getV()); }