Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 3247b39

Browse files
committed
Implemented requested changes by @qvalentin (#9)
1 parent 683a14d commit 3247b39

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

src/test/java/de/filefighter/rest/cucumber/CommonCucumberSteps.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import de.filefighter.rest.RestApplicationIntegrationTest;
77
import de.filefighter.rest.domain.filesystem.data.persistance.FileSystemEntity;
88
import de.filefighter.rest.domain.filesystem.data.persistance.FileSystemRepository;
9-
import de.filefighter.rest.domain.token.business.AccessTokenBusinessService;
10-
import de.filefighter.rest.domain.token.data.persistance.AccessTokenEntity;
119
import de.filefighter.rest.domain.token.data.persistance.AccessTokenRepository;
12-
import de.filefighter.rest.domain.user.business.UserBusinessService;
1310
import de.filefighter.rest.domain.user.data.persistance.UserEntity;
1411
import de.filefighter.rest.domain.user.data.persistance.UserRepository;
1512
import io.cucumber.java.en.And;
@@ -18,11 +15,8 @@
1815
import org.springframework.beans.factory.annotation.Autowired;
1916

2017
import java.io.IOException;
21-
import java.time.Instant;
2218
import java.util.Arrays;
23-
import java.util.UUID;
2419

25-
import static de.filefighter.rest.domain.token.business.AccessTokenBusinessService.ACCESS_TOKEN_DURATION_IN_SECONDS;
2620
import static org.junit.jupiter.api.Assertions.assertEquals;
2721
import static org.junit.jupiter.api.Assertions.assertTrue;
2822

@@ -48,14 +42,6 @@ public void databaseIsEmpty() {
4842
fileSystemRepository.deleteAll();
4943
}
5044

51-
@Given("accessToken with value {string} exists for user {long}")
52-
public void accessTokenWithValueExistsForUser(String tokenValue, long userId) {
53-
accessTokenRepository.save(AccessTokenEntity.builder()
54-
.userId(userId)
55-
.value(tokenValue)
56-
.validUntil(Instant.now().getEpochSecond()+ ACCESS_TOKEN_DURATION_IN_SECONDS).build());
57-
}
58-
5945
@And("user {long} exists")
6046
public void userExists(long userId) {
6147
userRepository.save(UserEntity

src/test/java/de/filefighter/rest/cucumber/UserAuthorizationSteps.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import com.fasterxml.jackson.databind.JsonNode;
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
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;
79
import io.cucumber.java.en.And;
10+
import io.cucumber.java.en.Given;
811
import io.cucumber.java.en.When;
912
import org.bson.internal.Base64;
13+
import org.springframework.beans.factory.annotation.Autowired;
1014
import org.springframework.http.HttpMethod;
1115

1216
import java.time.Instant;
@@ -15,17 +19,44 @@
1519

1620
import static com.mongodb.internal.connection.tlschannel.util.Util.assertTrue;
1721
import static de.filefighter.rest.configuration.RestConfiguration.*;
22+
import static de.filefighter.rest.domain.token.business.AccessTokenBusinessService.ACCESS_TOKEN_DURATION_IN_SECONDS;
1823
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
1925

2026
public class UserAuthorizationSteps extends RestApplicationIntegrationTest {
2127

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+
}
2354

2455
@When("user requests login with username {string} and password {string}")
2556
public void userRequestsLoginWithUsernameAndPassword(String username, String password) {
2657
String authString = username + ":" + password;
2758
String base64encoded = Base64.encode(authString.getBytes());
28-
base64encoded = AUTHORIZATION_BASIC_PREFIX+ base64encoded;
59+
base64encoded = AUTHORIZATION_BASIC_PREFIX + base64encoded;
2960

3061
HashMap<String, String> authHeader = new HashMap<>();
3162
authHeader.put("Authorization", base64encoded);
@@ -83,4 +114,14 @@ public void responseContainsRefreshTokenAndTheUserWithId(String refreshToken, lo
83114
assertEquals(userId, actualUserId);
84115
assertEquals(refreshToken, actualRefreshToken);
85116
}
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+
}
86127
}

src/test/resources/UserAuthorization.feature

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Background:
99

1010
Scenario: Successful login with username and password.
1111
When user requests login with username "user" and password "secure_password"
12-
And response status code is 200
12+
Then response status code is 200
1313
And response contains refreshToken "token" and the user with id 1234
1414

15-
Scenario: Failed login with username and password.
15+
Scenario: Failed login with wrong username or password.
1616
When user requests login with username "user" and password "wrong_password"
1717
Then response contains key "message" and value "User could not be authenticated. No User found for username or password."
1818
And response contains key "status" and value "denied"
@@ -24,13 +24,21 @@ Scenario: Successful creation of new accessToken with refreshToken.
2424
And response contains valid accessToken for user 1234
2525
And response status code is 200
2626

27-
Scenario: Successful request of existing accessToken with refreshToken.
27+
Scenario: Successful retrieval of existing accessToken with refreshToken.
2828
Given accessToken with value "6bb9cb4f-7b51-4c0a-8013-ed7a34e56282" exists for user 1234
2929
When user requests accessToken with refreshToken "token" and userId 1234
3030
Then response contains key "userId" and value "1234"
3131
And response contains valid accessToken for user 1234
3232
And response status code is 200
3333

34+
Scenario: Successful retrieval of freshly created accessToken with refreshToken
35+
Given accessToken with value "6bb9cb4f-7b51-4c0a-8013-ed7a34e56282" exists for user 1234 and is valid until 0
36+
When user requests accessToken with refreshToken "token" and userId 1234
37+
Then response contains key "userId" and value "1234"
38+
And response contains valid accessToken for user 1234 with a different value than "6bb9cb4f-7b51-4c0a-8013-ed7a34e56282"
39+
And response status code is 200
40+
41+
3442
Scenario: Failed retrieval of accessToken with wrong refreshToken.
3543
When user requests accessToken with refreshToken "not_the_token" and userId 1234
3644
Then response contains key "message" and value "User with the id 1234 could not be authenticated."

0 commit comments

Comments
 (0)