Skip to content

Commit

Permalink
fix(api): 🐛 fix /user for API token-based request
Browse files Browse the repository at this point in the history
  • Loading branch information
SCadilhac committed Sep 28, 2024
1 parent 7ed31ee commit 84fc64c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/onl/netfishers/netshot/rest/RestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7098,8 +7098,8 @@ public UiUser login(RsLogin rsLogin) throws WebApplicationException {
description = "Returns the current logged in user."
)
@Tag(name = "Login", description = "Login and password management for standard user")
public UiUser getUser(@Context HttpServletRequest request) throws WebApplicationException {
UiUser user = (UiUser) request.getAttribute("user");
public User getUser(@Context HttpServletRequest request) throws WebApplicationException {
User user = (User) request.getAttribute("user");
return user;
}

Expand Down
51 changes: 50 additions & 1 deletion src/test/java/onl/netfishers/netshot/RestServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ void notAdminToken() throws IOException, InterruptedException {
Assertions.assertInstanceOf(MissingNode.class,
response.body(), "Response body not empty");
}

@Test
@DisplayName("Current user retrieval for API token")
void currentUser() throws IOException, InterruptedException {
String secret = "jmE5C9JHDpLtbGswYfWBdUayKFn7Th6R";
ApiToken token1 = new ApiToken("Token get test", secret, UiUser.LEVEL_READONLY);
try (Session session = Database.getSession()) {
session.beginTransaction();
session.persist(token1);
session.getTransaction().commit();
}
apiClient.setApiToken(secret);
HttpResponse<JsonNode> response = apiClient.get("/user");
Assertions.assertEquals(
Response.Status.OK.getStatusCode(), response.statusCode(),
"Not getting 200 response for /user");

Assertions.assertEquals(
JsonNodeFactory.instance.objectNode()
.put("id", token1.getId())
.put("description", token1.getDescription())
.put("level", Long.valueOf(token1.getLevel())),
response.body(),
"Retrieved user/token doesn't match expected object");
}


}

@Nested
Expand All @@ -176,6 +203,7 @@ class LocalAuthenticationTest {
};
private int testUserLevel = UiUser.LEVEL_ADMIN;
private int passwordAge = 0;
private UiUser testUser = null;

private void createTestUser() {
try (Session session = Database.getSession()) {
Expand All @@ -199,6 +227,7 @@ private void createTestUser() {
}
session.persist(user);
session.getTransaction().commit();
this.testUser = user;
}
}

Expand All @@ -221,6 +250,26 @@ void localUserAuth() throws IOException, InterruptedException {
"Not getting 403 response for post-logout request");
}

@Test
@DisplayName("Current user retrieval for local user")
void currentUser() throws IOException, InterruptedException {
this.createTestUser();
apiClient.setLogin(testUsername, testPassword);
HttpResponse<JsonNode> response = apiClient.get("/user");
Assertions.assertEquals(
Response.Status.OK.getStatusCode(), response.statusCode(),
"Not getting 200 response for /user");

Assertions.assertEquals(
JsonNodeFactory.instance.objectNode()
.put("id", this.testUser.getId())
.put("username", this.testUser.getUsername())
.put("local", true)
.put("level", Long.valueOf(this.testUser.getLevel())),
response.body(),
"Retrieved user doesn't match expected object");
}

@Test
@DisplayName("Wrong cookie")
void wrongCookie() throws IOException, InterruptedException {
Expand Down Expand Up @@ -391,7 +440,7 @@ void passwordChangeWithPolicy() throws IOException, InterruptedException {
.put("username", testUsername)
.put("password", testPassword)
.put("newPassword", newPassword);
HttpResponse<JsonNode> response = apiClient.put("/user/0", data);
HttpResponse<JsonNode> response = apiClient.put("/user/0", data);
Assertions.assertEquals(
Response.Status.BAD_REQUEST.getStatusCode(), response.statusCode(),
"Not getting 400 response");
Expand Down

0 comments on commit 84fc64c

Please sign in to comment.