-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
commafeed-server/src/test/java/com/commafeed/integration/SecurityIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.commafeed.integration; | ||
|
||
import java.util.Base64; | ||
|
||
import javax.ws.rs.client.Entity; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.eclipse.jetty.http.HttpStatus; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.commafeed.CommaFeedDropwizardAppExtension; | ||
import com.commafeed.frontend.model.Entries; | ||
import com.commafeed.frontend.model.UserModel; | ||
import com.commafeed.frontend.model.request.ProfileModificationRequest; | ||
import com.commafeed.frontend.model.request.SubscribeRequest; | ||
|
||
class SecurityIT extends BaseIT { | ||
|
||
@Override | ||
protected CommaFeedDropwizardAppExtension buildExtension() { | ||
// override so we don't add http basic auth | ||
return new CommaFeedDropwizardAppExtension(); | ||
} | ||
|
||
@Test | ||
void notLoggedIn() { | ||
try (Response response = getClient().target(getApiBaseUrl() + "user/profile").request().get()) { | ||
Assertions.assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus()); | ||
} | ||
} | ||
|
||
@Test | ||
void wrongPassword() { | ||
String auth = "Basic " + Base64.getEncoder().encodeToString("admin:wrong-password".getBytes()); | ||
try (Response response = getClient().target(getApiBaseUrl() + "user/profile") | ||
.request() | ||
.header(HttpHeaders.AUTHORIZATION, auth) | ||
.get()) { | ||
Assertions.assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus()); | ||
} | ||
} | ||
|
||
@Test | ||
void missingRole() { | ||
String auth = "Basic " + Base64.getEncoder().encodeToString("demo:demo".getBytes()); | ||
try (Response response = getClient().target(getApiBaseUrl() + "admin/settings") | ||
.request() | ||
.header(HttpHeaders.AUTHORIZATION, auth) | ||
.get()) { | ||
Assertions.assertEquals(HttpStatus.FORBIDDEN_403, response.getStatus()); | ||
} | ||
} | ||
|
||
@Test | ||
void apiKey() { | ||
String auth = "Basic " + Base64.getEncoder().encodeToString("admin:admin".getBytes()); | ||
|
||
// create api key | ||
ProfileModificationRequest req = new ProfileModificationRequest(); | ||
req.setCurrentPassword("admin"); | ||
req.setNewApiKey(true); | ||
getClient().target(getApiBaseUrl() + "user/profile") | ||
.request() | ||
.header(HttpHeaders.AUTHORIZATION, auth) | ||
.post(Entity.json(req)) | ||
.close(); | ||
|
||
// fetch api key | ||
String apiKey = getClient().target(getApiBaseUrl() + "user/profile") | ||
.request() | ||
.header(HttpHeaders.AUTHORIZATION, auth) | ||
.get(UserModel.class) | ||
.getApiKey(); | ||
|
||
// subscribe to a feed | ||
SubscribeRequest subscribeRequest = new SubscribeRequest(); | ||
subscribeRequest.setUrl(getFeedUrl()); | ||
subscribeRequest.setTitle("my title for this feed"); | ||
long subscriptionId = getClient().target(getApiBaseUrl() + "feed/subscribe") | ||
.request() | ||
.header(HttpHeaders.AUTHORIZATION, auth) | ||
.post(Entity.json(subscribeRequest), Long.class); | ||
|
||
// get entries with api key | ||
Entries entries = getClient().target(getApiBaseUrl() + "feed/entries") | ||
.queryParam("id", subscriptionId) | ||
.queryParam("readType", "unread") | ||
.queryParam("apiKey", apiKey) | ||
.request() | ||
.get(Entries.class); | ||
Assertions.assertEquals("my title for this feed", entries.getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,7 @@ | |
import java.util.List; | ||
|
||
import javax.ws.rs.client.Entity; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.eclipse.jetty.http.HttpStatus; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -21,10 +19,8 @@ class AdminIT extends BaseIT { | |
|
||
@Test | ||
void getApplicationSettings() { | ||
try (Response response = getClient().target(getApiBaseUrl() + "admin/settings").request().get()) { | ||
ApplicationSettings settings = response.readEntity(ApplicationSettings.class); | ||
Assertions.assertTrue(settings.getAllowRegistrations()); | ||
} | ||
ApplicationSettings settings = getClient().target(getApiBaseUrl() + "admin/settings").request().get(ApplicationSettings.class); | ||
Assertions.assertTrue(settings.getAllowRegistrations()); | ||
} | ||
|
||
@Nested | ||
|
@@ -37,45 +33,38 @@ void saveThenDeleteNewUser() { | |
user.setName("test"); | ||
user.setPassword("test".getBytes()); | ||
user.setEmail("[email protected]"); | ||
getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user), Void.TYPE); | ||
|
||
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user))) { | ||
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus()); | ||
|
||
List<UserModel> newUsers = getAllUsers(); | ||
Assertions.assertEquals(existingUsers.size() + 1, newUsers.size()); | ||
List<UserModel> newUsers = getAllUsers(); | ||
Assertions.assertEquals(existingUsers.size() + 1, newUsers.size()); | ||
|
||
UserModel newUser = newUsers.stream().filter(u -> u.getName().equals("test")).findFirst().get(); | ||
user.setId(newUser.getId()); | ||
} | ||
UserModel newUser = newUsers.stream() | ||
.filter(u -> u.getName().equals("test")) | ||
.findFirst() | ||
.orElseThrow(() -> new NullPointerException("User not found")); | ||
user.setId(newUser.getId()); | ||
|
||
IDRequest req = new IDRequest(); | ||
req.setId(user.getId()); | ||
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/delete").request().post(Entity.json(req))) { | ||
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus()); | ||
|
||
List<UserModel> newUsers = getAllUsers(); | ||
Assertions.assertEquals(existingUsers.size(), newUsers.size()); | ||
} | ||
getClient().target(getApiBaseUrl() + "admin/user/delete").request().post(Entity.json(req), Void.TYPE); | ||
Assertions.assertEquals(existingUsers.size(), getAllUsers().size()); | ||
} | ||
|
||
@Test | ||
void editExistingUser() { | ||
List<UserModel> existingUsers = getAllUsers(); | ||
UserModel user = existingUsers.stream().filter(u -> u.getName().equals("admin")).findFirst().get(); | ||
UserModel user = existingUsers.stream() | ||
.filter(u -> u.getName().equals("admin")) | ||
.findFirst() | ||
.orElseThrow(() -> new NullPointerException("User not found")); | ||
user.setEmail("[email protected]"); | ||
|
||
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user))) { | ||
Assertions.assertEquals(HttpStatus.OK_200, response.getStatus()); | ||
|
||
List<UserModel> newUsers = getAllUsers(); | ||
Assertions.assertEquals(existingUsers.size(), newUsers.size()); | ||
} | ||
getClient().target(getApiBaseUrl() + "admin/user/save").request().post(Entity.json(user), Void.TYPE); | ||
Assertions.assertEquals(existingUsers.size(), getAllUsers().size()); | ||
} | ||
|
||
private List<UserModel> getAllUsers() { | ||
try (Response response = getClient().target(getApiBaseUrl() + "admin/user/getAll").request().get()) { | ||
return Arrays.asList(response.readEntity(UserModel[].class)); | ||
} | ||
return Arrays.asList(getClient().target(getApiBaseUrl() + "admin/user/getAll").request().get(UserModel[].class)); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.