|
4 | 4 | import com.fasterxml.jackson.databind.JsonNode;
|
5 | 5 | import com.fasterxml.jackson.databind.ObjectMapper;
|
6 | 6 | import de.filefighter.rest.RestApplicationIntegrationTest;
|
| 7 | +import de.filefighter.rest.domain.token.data.persistance.AccessTokenEntity; |
| 8 | +import de.filefighter.rest.domain.token.data.persistance.AccessTokenRepository; |
7 | 9 | import io.cucumber.java.en.And;
|
| 10 | +import io.cucumber.java.en.Given; |
8 | 11 | import io.cucumber.java.en.When;
|
9 | 12 | import org.bson.internal.Base64;
|
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
10 | 14 | import org.springframework.http.HttpMethod;
|
11 | 15 |
|
12 | 16 | import java.time.Instant;
|
|
15 | 19 |
|
16 | 20 | import static com.mongodb.internal.connection.tlschannel.util.Util.assertTrue;
|
17 | 21 | import static de.filefighter.rest.configuration.RestConfiguration.*;
|
| 22 | +import static de.filefighter.rest.domain.token.business.AccessTokenBusinessService.ACCESS_TOKEN_DURATION_IN_SECONDS; |
18 | 23 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 24 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
19 | 25 |
|
20 | 26 | public class UserAuthorizationSteps extends RestApplicationIntegrationTest {
|
21 | 27 |
|
22 |
| - private final ObjectMapper objectMapper = new ObjectMapper(); |
| 28 | + private final ObjectMapper objectMapper; |
| 29 | + private final AccessTokenRepository accessTokenRepository; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + public UserAuthorizationSteps(AccessTokenRepository accessTokenRepository) { |
| 33 | + this.objectMapper = new ObjectMapper(); |
| 34 | + this.accessTokenRepository = accessTokenRepository; |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + @Given("accessToken with value {string} exists for user {long} and is valid until {long}") |
| 39 | + public void accessTokenWithValueExistsForUserAndIsValidUntil(String tokenValue, long userId, long validUntil) { |
| 40 | + accessTokenRepository.save(AccessTokenEntity.builder() |
| 41 | + .userId(userId) |
| 42 | + .value(tokenValue) |
| 43 | + .validUntil(validUntil) |
| 44 | + .validUntil(Instant.now().getEpochSecond()+ ACCESS_TOKEN_DURATION_IN_SECONDS).build()); |
| 45 | + } |
| 46 | + |
| 47 | + @Given("accessToken with value {string} exists for user {long}") |
| 48 | + public void accessTokenWithValueExistsForUser(String tokenValue, long userId) { |
| 49 | + accessTokenRepository.save(AccessTokenEntity.builder() |
| 50 | + .userId(userId) |
| 51 | + .value(tokenValue) |
| 52 | + .validUntil(Instant.now().getEpochSecond()+ ACCESS_TOKEN_DURATION_IN_SECONDS).build()); |
| 53 | + } |
23 | 54 |
|
24 | 55 | @When("user requests login with username {string} and password {string}")
|
25 | 56 | public void userRequestsLoginWithUsernameAndPassword(String username, String password) {
|
26 | 57 | String authString = username + ":" + password;
|
27 | 58 | String base64encoded = Base64.encode(authString.getBytes());
|
28 |
| - base64encoded = AUTHORIZATION_BASIC_PREFIX+ base64encoded; |
| 59 | + base64encoded = AUTHORIZATION_BASIC_PREFIX + base64encoded; |
29 | 60 |
|
30 | 61 | HashMap<String, String> authHeader = new HashMap<>();
|
31 | 62 | authHeader.put("Authorization", base64encoded);
|
@@ -83,4 +114,14 @@ public void responseContainsRefreshTokenAndTheUserWithId(String refreshToken, lo
|
83 | 114 | assertEquals(userId, actualUserId);
|
84 | 115 | assertEquals(refreshToken, actualRefreshToken);
|
85 | 116 | }
|
| 117 | + |
| 118 | + @And("response contains valid accessToken for user {long} with a different value than {string}") |
| 119 | + public void responseContainsValidAccessTokenForUserWithADifferentValueThan(int userId, String differentTokenValue) throws JsonProcessingException { |
| 120 | + JsonNode rootNode = objectMapper.readTree(latestResponse.getBody()); |
| 121 | + String actualTokenValue = rootNode.get("token").asText(); |
| 122 | + long actualUserId = rootNode.get("userId").asLong(); |
| 123 | + |
| 124 | + assertEquals(userId, actualUserId); |
| 125 | + assertNotEquals(differentTokenValue, actualTokenValue); |
| 126 | + } |
86 | 127 | }
|
0 commit comments