|
1 | 1 | package de.filefighter.rest.cucumber;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.JsonNode; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
3 | 6 | import de.filefighter.rest.RestApplicationIntegrationTest;
|
4 | 7 | import io.cucumber.java.en.And;
|
5 |
| -import io.cucumber.java.en.Then; |
6 | 8 | import io.cucumber.java.en.When;
|
| 9 | +import org.bson.internal.Base64; |
| 10 | +import org.springframework.http.HttpMethod; |
7 | 11 |
|
8 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | +import java.time.Instant; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +import static com.mongodb.internal.connection.tlschannel.util.Util.assertTrue; |
| 17 | +import static de.filefighter.rest.configuration.RestConfiguration.*; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
9 | 19 |
|
10 | 20 | public class UserAuthorizationSteps extends RestApplicationIntegrationTest {
|
11 | 21 |
|
| 22 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 23 | + |
12 | 24 | @When("user requests login with username {string} and password {string}")
|
13 | 25 | public void userRequestsLoginWithUsernameAndPassword(String username, String password) {
|
| 26 | + String authString = AUTHORIZATION_BASIC_PREFIX + username + ":" + password; |
| 27 | + String base64encoded = Base64.encode(authString.getBytes()); |
| 28 | + |
| 29 | + HashMap<String, String> authHeader = new HashMap<>(); |
| 30 | + authHeader.put("Authorization", base64encoded); |
| 31 | + |
| 32 | + String url = BASE_API_URI + USER_BASE_URI + "login"; |
| 33 | + |
| 34 | + executeRestApiCall(HttpMethod.GET, url, authHeader); |
14 | 35 | }
|
15 | 36 |
|
16 | 37 | @When("user requests accessToken with refreshToken {string} and userId {long}")
|
17 | 38 | public void userRequestsAccessTokenWithRefreshTokenAndUserId(String refreshTokenValue, long userId) {
|
18 |
| - } |
| 39 | + String authHeaderString = AUTHORIZATION_BEARER_PREFIX + refreshTokenValue; |
| 40 | + String url = BASE_API_URI + USER_BASE_URI + userId + "/login"; |
| 41 | + |
| 42 | + HashMap<String, String> authHeader = new HashMap<>(); |
| 43 | + authHeader.put("Authorization", authHeaderString); |
19 | 44 |
|
20 |
| - @And("response contains valid accessToken") |
21 |
| - public void responseContainsValidAccessToken() { |
| 45 | + executeRestApiCall(HttpMethod.GET, url, authHeader); |
22 | 46 | }
|
23 | 47 |
|
24 | 48 | @When("user requests userInfo with accessToken {string} and userId {long}")
|
25 | 49 | public void userRequestsUserInfoWithAccessTokenAndUserId(String accessTokenValue, long userId) {
|
| 50 | + String authHeaderString = AUTHORIZATION_BEARER_PREFIX + accessTokenValue; |
| 51 | + String url = BASE_API_URI + USER_BASE_URI + userId + "/info"; |
| 52 | + |
| 53 | + HashMap<String, String> authHeader = new HashMap<>(); |
| 54 | + authHeader.put("Authorization", authHeaderString); |
| 55 | + |
| 56 | + executeRestApiCall(HttpMethod.GET, url, authHeader); |
| 57 | + } |
| 58 | + |
| 59 | + @And("response contains valid accessToken for user {long}") |
| 60 | + public void responseContainsValidAccessTokenForUser(long userId) throws JsonProcessingException { |
| 61 | + JsonNode rootNode = objectMapper.readTree(latestResponse.getBody()); |
| 62 | + String tokenValue = rootNode.get("token").asText(); |
| 63 | + long actualUserId = rootNode.get("userId").asLong(); |
| 64 | + long validUntil = rootNode.get("validUntil").asLong(); |
| 65 | + |
| 66 | + int expectedTokenLength = UUID.randomUUID().toString().length(); |
| 67 | + long expectedValidUntil = Instant.now().getEpochSecond(); |
| 68 | + |
| 69 | + assertEquals(expectedTokenLength, tokenValue.length()); |
| 70 | + assertTrue(validUntil > expectedValidUntil); |
| 71 | + assertEquals(userId, actualUserId); |
26 | 72 | }
|
27 | 73 | }
|
0 commit comments