use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class ClientConsentControllerTest method getClientConsentLogPage.
@WithGw2AuthLogin
public void getClientConsentLogPage(MockHttpSession session) throws Exception {
final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
final ClientRegistrationEntity clientRegistration = this.testHelper.createClientRegistration(accountId, "Name");
final ClientConsentEntity clientAuthorization = this.testHelper.createClientConsent(accountId, clientRegistration.id(), Set.of(Gw2ApiPermission.ACCOUNT.oauth2()));
final Queue<ClientConsentLogEntity> insertedLogs = new PriorityQueue<>(Comparator.comparing(ClientConsentLogEntity::timestamp).reversed());
for (int i = 0; i < 143; i++) {
final int generateMessageCount = ThreadLocalRandom.current().nextInt(20);
final List<String> messages = new ArrayList<>(generateMessageCount);
for (int j = 0; j < generateMessageCount; j++) {
messages.add(UUID.randomUUID().toString());
}
insertedLogs.offer(this.testHelper.createClientLog(accountId, clientAuthorization.clientRegistrationId(), UUID.randomUUID().toString(), messages));
}
final ObjectMapper mapper = new ObjectMapper();
int page = 0;
do {
final String responseJson = this.mockMvc.perform(get("/api/client/consent/{clientId}/logs", clientRegistration.clientId()).session(session).queryParam("page", Integer.toString(page))).andExpect(status().isOk()).andExpect(jsonPath("$.page").exists()).andExpect(jsonPath("$.nextPage").exists()).andExpect(jsonPath("$.logs").exists()).andReturn().getResponse().getContentAsString();
final JsonNode responseNode = mapper.readTree(responseJson);
final int nextPage = responseNode.get("nextPage").intValue();
assertEquals(page, responseNode.get("page").intValue());
assertTrue(nextPage == page + 1 || nextPage == -1);
final JsonNode logsNode = responseNode.get("logs");
assertTrue(logsNode.isArray());
for (int i = 0; i < logsNode.size(); i++) {
final ClientConsentLogEntity expectedLog = insertedLogs.poll();
assertNotNull(expectedLog);
final JsonNode logNode = logsNode.get(i);
assertInstantEquals(expectedLog.timestamp(), logNode.get("timestamp").textValue());
assertEquals(expectedLog.type(), logNode.get("type").textValue());
final JsonNode messagesNode = logNode.get("messages");
assertTrue(messagesNode.isArray());
for (int j = 0; j < messagesNode.size(); j++) {
assertEquals(expectedLog.messages().get(j), messagesNode.get(j).textValue());
}
}
page = nextPage;
} while (page != -1);
assertTrue(insertedLogs.isEmpty());
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class VerificationControllerTest method startChallengeWithLongEnoughBetween.
@WithGw2AuthLogin
public void startChallengeWithLongEnoughBetween(MockHttpSession session) throws Exception {
final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
// prepare the testing clock
Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
this.verificationService.setClock(testingClock);
this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "1")).andExpect(status().isOk()).andExpect(jsonPath("$.challengeId").value("1")).andExpect(jsonPath("$.message.apiTokenName").isString()).andExpect(jsonPath("$.nextAllowedStartTime").isString());
final Gw2AccountVerificationChallengeEntity startedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null);
assertNotNull(startedChallenge);
// wait 29min (not enough)
testingClock = Clock.offset(testingClock, Duration.ofMinutes(31L));
this.verificationService.setClock(testingClock);
// try to start a new challenge
this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "2")).andExpect(status().isOk()).andExpect(jsonPath("$.challengeId").value("2")).andExpect(jsonPath("$.message.gw2ItemId").isNumber()).andExpect(jsonPath("$.message.buyOrderCoins").isNumber()).andExpect(jsonPath("$.nextAllowedStartTime").isString());
// started challenge should be modified
final Gw2AccountVerificationChallengeEntity updatedStartedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null);
assertNotNull(updatedStartedChallenge);
assertNotEquals(startedChallenge, updatedStartedChallenge);
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class VerificationControllerTest method startAndSubmitChallengeForGw2AccountHavingAPendingVerification.
@WithGw2AuthLogin
public void startAndSubmitChallengeForGw2AccountHavingAPendingVerification(MockHttpSession session) throws Exception {
final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
// prepare the testing clock
Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
this.verificationService.setClock(testingClock);
final UUID gw2AccountId = UUID.randomUUID();
final String gw2ApiToken = TestHelper.randomRootToken();
final String gw2ApiSubtoken = TestHelper.createSubtokenJWT(UUID.randomUUID(), Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant(), Duration.ofMinutes(90L));
// prepare the gw2 api
this.gw2RestServer.reset();
preparedGw2RestServerForCreateSubtoken(gw2ApiToken, gw2ApiSubtoken, Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant().plus(Duration.ofMinutes(90L)));
preparedGw2RestServerForAccountRequest(gw2AccountId, gw2ApiSubtoken);
prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
// start the challenge
this.verificationService.startChallenge(accountId, 1L);
// submit the challenge
this.mockMvc.perform(post("/api/verification/pending").session(session).with(csrf()).queryParam("token", gw2ApiToken)).andExpect(status().isOk()).andExpect(jsonPath("$.isSuccess").value("false")).andExpect(jsonPath("$.pending").isMap());
// started challenge should be removed
assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").isEmpty());
// pending challenge should be inserted
final Gw2AccountVerificationChallengeEntity startedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).orElse(null);
assertNotNull(startedChallenge);
// start a new challenge
this.verificationService.startChallenge(accountId, 1L);
// prepare the gw2 api again
this.gw2RestServer.reset();
preparedGw2RestServerForCreateSubtoken(gw2ApiToken, gw2ApiSubtoken, Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant().plus(Duration.ofMinutes(90L)));
preparedGw2RestServerForAccountRequest(gw2AccountId, gw2ApiSubtoken);
prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
// submit the challenge again (for the same gw2 account)
this.mockMvc.perform(post("/api/verification/pending").session(session).with(csrf()).queryParam("token", gw2ApiToken)).andExpect(status().isBadRequest());
// pending challenge should not be modified
assertEquals(startedChallenge, this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).orElse(null));
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class VerificationControllerTest method startAndSubmitApiTokenNameChallengeUnfulfilled.
@WithGw2AuthLogin
public void startAndSubmitApiTokenNameChallengeUnfulfilled(MockHttpSession session) throws Exception {
final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
// prepare the testing clock
Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
this.verificationService.setClock(testingClock);
final UUID gw2AccountId = UUID.randomUUID();
final String gw2ApiToken = TestHelper.randomRootToken();
final String gw2ApiSubtoken = TestHelper.createSubtokenJWT(UUID.randomUUID(), Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant(), Duration.ofMinutes(90L));
// prepare the gw2 api
this.gw2RestServer.reset();
preparedGw2RestServerForCreateSubtoken(gw2ApiToken, gw2ApiSubtoken, Set.of(Gw2ApiPermission.ACCOUNT), testingClock.instant().plus(Duration.ofMinutes(90L)));
preparedGw2RestServerForAccountRequest(gw2AccountId, gw2ApiSubtoken);
prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
// start the challenge
this.verificationService.startChallenge(accountId, 1L);
// submit the challenge
this.mockMvc.perform(post("/api/verification/pending").session(session).with(csrf()).queryParam("token", gw2ApiToken)).andExpect(status().isOk()).andExpect(jsonPath("$.isSuccess").value("false")).andExpect(jsonPath("$.pending").isMap());
// started challenge should be removed
assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").isEmpty());
// pending challenge should be inserted
assertTrue(this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).isPresent());
// let 91 minutes pass
testingClock = Clock.offset(testingClock, Duration.ofMinutes(91L));
this.verificationService.setClock(testingClock);
// prepare the api again
this.gw2RestServer.reset();
prepareGw2RestServerForTokenInfoRequest(gw2ApiSubtoken, "Not the name that was requested", Set.of(Gw2ApiPermission.ACCOUNT));
// simulate scheduled check
this.verificationService.tryVerifyAllPending();
// pending challenge should be updated to verification failed entity
final Gw2AccountVerificationChallengeEntity verificationFailedEntity = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, gw2AccountId.toString()).orElse(null);
assertNotNull(verificationFailedEntity);
assertEquals(-1L, verificationFailedEntity.challengeId());
assertInstantEquals(testingClock.instant().plus(Duration.ofHours(2L)), verificationFailedEntity.timeoutAt());
}
use of com.gw2auth.oauth2.server.util.QueryParam in project oauth2-server by gw2auth.
the class VerificationControllerTest method startChallengeWithSameChallengeIdAsExisting.
@WithGw2AuthLogin
public void startChallengeWithSameChallengeIdAsExisting(MockHttpSession session) throws Exception {
final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
// prepare the testing clock
Clock testingClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
this.verificationService.setClock(testingClock);
this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "1")).andExpect(status().isOk()).andExpect(jsonPath("$.challengeId").value("1")).andExpect(jsonPath("$.message.apiTokenName").isString()).andExpect(jsonPath("$.nextAllowedStartTime").isString());
final Gw2AccountVerificationChallengeEntity startedChallenge = this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null);
assertNotNull(startedChallenge);
// wait 31min (enough)
testingClock = Clock.offset(testingClock, Duration.ofMinutes(31L));
this.verificationService.setClock(testingClock);
// try to start a new challenge
this.mockMvc.perform(post("/api/verification").session(session).with(csrf()).queryParam("challengeId", "1")).andExpect(status().isBadRequest());
// started challenge should not be modified
assertEquals(startedChallenge, this.gw2AccountVerificationChallengeRepository.findByAccountIdAndGw2AccountId(accountId, "").orElse(null));
}
Aggregations