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

Commit ec131cd

Browse files
committed
Added Steps, and fixed feature
1 parent b18ebff commit ec131cd

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

.run/Run Cucumber Tests.run.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Run Cucumber Tests" type="JUnit" factoryName="JUnit">
3+
<useClassPathOnly />
4+
<option name="PACKAGE_NAME" value="de.filefighter.rest.cucumber" />
5+
<option name="MAIN_CLASS_NAME" value="" />
6+
<option name="METHOD_NAME" value="" />
7+
<option name="TEST_OBJECT" value="package" />
8+
<option name="PARAMETERS" value="" />
9+
<option name="TEST_SEARCH_SCOPE">
10+
<value defaultName="wholeProject" />
11+
</option>
12+
<method v="2">
13+
<option name="Make" enabled="true" />
14+
</method>
15+
</configuration>
16+
</component>

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
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;
910
import de.filefighter.rest.domain.token.data.persistance.AccessTokenEntity;
1011
import de.filefighter.rest.domain.token.data.persistance.AccessTokenRepository;
12+
import de.filefighter.rest.domain.user.business.UserBusinessService;
1113
import de.filefighter.rest.domain.user.data.persistance.UserEntity;
1214
import de.filefighter.rest.domain.user.data.persistance.UserRepository;
1315
import io.cucumber.java.en.And;
@@ -46,21 +48,19 @@ public void databaseIsEmpty() {
4648
fileSystemRepository.deleteAll();
4749
}
4850

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

57-
@And("user {long} has access token {string}")
58-
public void userHasAccessToken(long userId, String accessTokenValue) {
59-
accessTokenRepository.save(AccessTokenEntity
59+
@And("user {long} exists")
60+
public void userExists(long userId) {
61+
userRepository.save(UserEntity
6062
.builder()
6163
.userId(userId)
62-
.value(accessTokenValue)
63-
.validUntil(Instant.now().getEpochSecond() + ACCESS_TOKEN_DURATION_IN_SECONDS)
6464
.build());
6565
}
6666

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,73 @@
11
package de.filefighter.rest.cucumber;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
36
import de.filefighter.rest.RestApplicationIntegrationTest;
47
import io.cucumber.java.en.And;
5-
import io.cucumber.java.en.Then;
68
import io.cucumber.java.en.When;
9+
import org.bson.internal.Base64;
10+
import org.springframework.http.HttpMethod;
711

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;
919

1020
public class UserAuthorizationSteps extends RestApplicationIntegrationTest {
1121

22+
private final ObjectMapper objectMapper = new ObjectMapper();
23+
1224
@When("user requests login with username {string} and password {string}")
1325
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);
1435
}
1536

1637
@When("user requests accessToken with refreshToken {string} and userId {long}")
1738
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);
1944

20-
@And("response contains valid accessToken")
21-
public void responseContainsValidAccessToken() {
45+
executeRestApiCall(HttpMethod.GET, url, authHeader);
2246
}
2347

2448
@When("user requests userInfo with accessToken {string} and userId {long}")
2549
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);
2672
}
2773
}

src/test/resources/UserAuthorization.feature

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,27 @@
1919
# And response contains key "status" and value "denied"
2020
# And response status code is 401
2121
#
22-
#Scenario: Successful retrieval of accessToken with refreshToken.
22+
#Scenario: Successful creation of new accessToken with refreshToken.
2323
# When user requests accessToken with refreshToken "token" and userId 1234
2424
# Then response contains key "userId" and value "1234"
25-
# And response contains valid accessToken
25+
# And response contains valid accessToken for user 1234
2626
# And response status code is 200
2727
#
28-
#Scenario: Failed retrieval of accessToken with wrong refreshToken.
28+
#Scenario: Successful request of existing accessToken with refreshToken.
29+
# Given accessToken with value "token" exists for user 1234
30+
# When user requests accessToken with refreshToken "token" and userId 1234
31+
# Then response contains key "userId" and value "1234"
32+
# And response contains valid accessToken for user 1234
33+
# And response status code is 200
34+
#
35+
# Scenario: Failed retrieval of accessToken with wrong refreshToken.
2936
# When user requests accessToken with refreshToken "not_the_token" and userId 1234
3037
# Then response contains key "message" and value "User not authenticated."
3138
# And response contains key "status" and value "denied"
3239
# And response status code is 401
3340
#
3441
#Scenario: Successful UserInfo request with valid accessToken.
35-
# Given user 1234 has access token "accessToken"
42+
# Given accessToken with value "accessToken" exists for user 1234
3643
# When user requests userInfo with accessToken "accessToken" and userId 1234
3744
# Then response contains the user with id 1234
3845
# And response status code is 200

0 commit comments

Comments
 (0)