-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor REST Admin interface security integration test
Signed-off-by: Alexander Pinchuk <alexander.v.pinchuk@gmail.com>
- Loading branch information
Showing
5 changed files
with
191 additions
and
119 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
62 changes: 62 additions & 0 deletions
62
...dmin/tests/src/test/java/org/glassfish/main/admin/test/rest/BasicAuthenticationITest.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,62 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.main.admin.test.rest; | ||
|
||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.glassfish.admin.rest.client.ClientWrapper; | ||
import org.glassfish.main.itest.tools.DomainAdminRestClient; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class BasicAuthenticationITest extends SecuredRestTestBase { | ||
|
||
private static final String INVALID_USER_NAME = "invaliduser"; | ||
|
||
private static final String INVALID_PASSWORD = "invalidpass"; | ||
|
||
@Test | ||
public void testAuthRequired() { | ||
// Invalid credentials | ||
try (BasicClient basicClient = new BasicClient(INVALID_USER_NAME, INVALID_PASSWORD)) { | ||
Response response = basicClient.get(URL_DOMAIN); | ||
assertThat(response.getStatus(), equalTo(401)); | ||
} | ||
|
||
// Anonymous access | ||
try (AnonymousClient anonymousClient = new AnonymousClient()) { | ||
Response response = anonymousClient.get(URL_DOMAIN); | ||
assertThat(response.getStatus(), equalTo(401)); | ||
} | ||
|
||
// Valid credentials | ||
try (BasicClient basicClient = new BasicClient(AUTH_USER_NAME, AUTH_PASSWORD)) { | ||
Response response = basicClient.get(URL_DOMAIN); | ||
assertThat(response.getStatus(), equalTo(200)); | ||
} | ||
} | ||
|
||
private static final class AnonymousClient extends DomainAdminRestClient { | ||
|
||
public AnonymousClient() { | ||
super(new ClientWrapper(), managementClient.getBaseUrl(), APPLICATION_JSON); | ||
} | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
...sts/admin/tests/src/test/java/org/glassfish/main/admin/test/rest/SecuredRestTestBase.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,74 @@ | ||
/* | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.main.admin.test.rest; | ||
|
||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.glassfish.admin.rest.client.ClientWrapper; | ||
import org.glassfish.main.itest.tools.DomainAdminRestClient; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
|
||
import java.util.Map; | ||
|
||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static org.glassfish.jersey.client.authentication.HttpAuthenticationFeature.basic; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
public class SecuredRestTestBase extends RestTestBase { | ||
|
||
private static final String URL_CREATE_USER = "/domain/configs/config/server-config/security-service/auth-realm/admin-realm/create-user"; | ||
|
||
private static final String URL_DELETE_USER = "/domain/configs/config/server-config/security-service/auth-realm/admin-realm/delete-user"; | ||
|
||
protected static final String AUTH_USER_NAME = "dummyuser"; | ||
|
||
protected static final String AUTH_PASSWORD = "dummypass"; | ||
|
||
@BeforeAll | ||
public static void createUser() { | ||
// Create the new user | ||
Map<String, String> newUser = Map.of( | ||
"id", AUTH_USER_NAME, | ||
"groups", "asadmin", | ||
"authrealmname", "admin-realm", | ||
"AS_ADMIN_USERPASSWORD", AUTH_PASSWORD | ||
); | ||
Response response = managementClient.post(URL_CREATE_USER, newUser); | ||
assertThat(response.getStatus(), equalTo(200)); | ||
} | ||
|
||
@AfterAll | ||
public static void deleteUser() { | ||
Response response = managementClient.delete(URL_DELETE_USER, Map.of("id", AUTH_USER_NAME)); | ||
assertThat(response.getStatus(), equalTo(200)); | ||
} | ||
|
||
protected static final class BasicClient extends DomainAdminRestClient { | ||
|
||
public BasicClient(String userName, String password) { | ||
super(createClient(userName, password), managementClient.getBaseUrl(), APPLICATION_JSON); | ||
} | ||
|
||
private static ClientWrapper createClient(String userName, String password) { | ||
ClientWrapper client = new ClientWrapper(); | ||
client.register(basic(userName, password)); | ||
return client; | ||
} | ||
} | ||
} |
Oops, something went wrong.