Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quarkus unit tests for REST and API for /api/v1/admin/usage
- Loading branch information
Showing
3 changed files
with
188 additions
and
1 deletion.
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
70 changes: 70 additions & 0 deletions
70
vcell-rest/src/test/java/org/vcell/restq/AdminResourceTest.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,70 @@ | ||
package org.vcell.restq; | ||
|
||
import cbit.vcell.resource.PropertyLoader; | ||
import cbit.vcell.xml.XmlParseException; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.keycloak.client.KeycloakTestClient; | ||
import io.restassured.response.Response; | ||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.junit.jupiter.api.*; | ||
import org.vcell.restclient.ApiClient; | ||
import org.vcell.restclient.ApiException; | ||
import org.vcell.restclient.api.UsersResourceApi; | ||
import org.vcell.restq.config.CDIVCellConfigProvider; | ||
import org.vcell.restq.db.AgroalConnectionFactory; | ||
import org.vcell.util.DataAccessException; | ||
|
||
import java.beans.PropertyVetoException; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.sql.SQLException; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
@QuarkusTest | ||
public class AdminResourceTest { | ||
@ConfigProperty(name = "quarkus.http.test-port") | ||
Integer testPort; | ||
|
||
KeycloakTestClient keycloakClient = new KeycloakTestClient(); | ||
@Inject | ||
AgroalConnectionFactory agroalConnectionFactory; | ||
|
||
private ApiClient aliceAPIClient; | ||
|
||
@BeforeAll | ||
public static void setupConfig(){ | ||
PropertyLoader.setConfigProvider(new CDIVCellConfigProvider()); | ||
} | ||
|
||
@BeforeEach | ||
public void createClients(){ | ||
aliceAPIClient = TestEndpointUtils.createAuthenticatedAPIClient(keycloakClient, testPort, TestEndpointUtils.TestOIDCUsers.alice); | ||
} | ||
|
||
@AfterEach | ||
public void removeOIDCMappings() throws SQLException, DataAccessException { | ||
TestEndpointUtils.removeAllMappings(agroalConnectionFactory); | ||
} | ||
|
||
// TODO: Right now the biomodel endpoint doesn't implement authentication, but when it does it'll need to | ||
@Test | ||
public void testGetUsage() throws IOException, ApiException, XmlParseException, PropertyVetoException { | ||
|
||
boolean mapped = new UsersResourceApi(aliceAPIClient).mapUser(TestEndpointUtils.administratorUserLoginInfo); | ||
Assertions.assertTrue(mapped); | ||
|
||
Response response = given() | ||
.auth().oauth2(keycloakClient.getAccessToken(TestEndpointUtils.TestOIDCUsers.alice.name())) | ||
.when().get("/api/v1/admin/usage") | ||
.then().extract().response(); | ||
|
||
Assertions.assertEquals(200, response.getStatusCode()); | ||
Assertions.assertEquals("application/pdf", response.getContentType()); | ||
|
||
byte[] pdfBytes = response.asByteArray(); | ||
Assertions.assertTrue(pdfBytes.length > 0); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
vcell-rest/src/test/java/org/vcell/restq/apiclient/AdminApiTest.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,115 @@ | ||
package org.vcell.restq.apiclient; | ||
|
||
import cbit.vcell.resource.PropertyLoader; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.keycloak.client.KeycloakTestClient; | ||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.junit.jupiter.api.*; | ||
import org.vcell.restclient.ApiClient; | ||
import org.vcell.restclient.ApiException; | ||
import org.vcell.restclient.api.AdminResourceApi; | ||
import org.vcell.restclient.api.UsersResourceApi; | ||
import org.vcell.restq.TestEndpointUtils; | ||
import org.vcell.restq.config.CDIVCellConfigProvider; | ||
import org.vcell.restq.db.AgroalConnectionFactory; | ||
import org.vcell.util.DataAccessException; | ||
|
||
import java.io.File; | ||
import java.sql.SQLException; | ||
|
||
@QuarkusTest | ||
public class AdminApiTest { | ||
|
||
@ConfigProperty(name = "quarkus.oidc.auth-server-url") | ||
String authServerUrl; | ||
|
||
@ConfigProperty(name = "quarkus.http.test-port") | ||
Integer testPort; | ||
|
||
@Inject | ||
AgroalConnectionFactory agroalConnectionFactory; | ||
|
||
KeycloakTestClient keycloakClient = new KeycloakTestClient(); | ||
|
||
private ApiClient aliceAPIClient; | ||
private ApiClient bobAPIClient; | ||
|
||
@BeforeAll | ||
public static void setupConfig(){ | ||
PropertyLoader.setConfigProvider(new CDIVCellConfigProvider()); | ||
} | ||
|
||
@BeforeEach | ||
public void createClients(){ | ||
aliceAPIClient = TestEndpointUtils.createAuthenticatedAPIClient(keycloakClient, testPort, TestEndpointUtils.TestOIDCUsers.alice); | ||
bobAPIClient = TestEndpointUtils.createAuthenticatedAPIClient(keycloakClient, testPort, TestEndpointUtils.TestOIDCUsers.bob); | ||
} | ||
|
||
@AfterEach | ||
public void removeOIDCMappings() throws SQLException, DataAccessException { | ||
TestEndpointUtils.removeAllMappings(agroalConnectionFactory); | ||
} | ||
|
||
@Test | ||
public void testUsageReport_AdminRole_AdminUser() throws ApiException { | ||
// alice has admin role, mapped to an admin user - should succeed | ||
boolean mapped = new UsersResourceApi(aliceAPIClient).mapUser(TestEndpointUtils.administratorUserLoginInfo); | ||
Assertions.assertTrue(mapped); | ||
File usageSummaryFile = new AdminResourceApi(aliceAPIClient).getUsage(); | ||
Assertions.assertTrue(usageSummaryFile.exists()); | ||
Assertions.assertTrue(usageSummaryFile.isFile()); | ||
Assertions.assertTrue(usageSummaryFile.length() > 0); | ||
} | ||
|
||
@Test | ||
public void testUsageReport_AdminRole_NonadminUser() throws ApiException { | ||
// alice has an admin role, but is mapped to a non-admin user - should fail | ||
boolean mapped = new UsersResourceApi(aliceAPIClient).mapUser(TestEndpointUtils.vcellNagiosUserLoginInfo); | ||
Assertions.assertTrue(mapped); | ||
ApiException apiException = Assertions.assertThrows(ApiException.class, () -> { | ||
new AdminResourceApi(aliceAPIClient).getUsage(); | ||
}); | ||
Assertions.assertEquals(403, apiException.getCode()); | ||
} | ||
|
||
@Test | ||
public void testUsageReport_AdminRole_NotMapped() throws ApiException { | ||
// alice has admin role, but is not mapped any user - should fail | ||
ApiException apiException = Assertions.assertThrows(ApiException.class, () -> { | ||
new AdminResourceApi(aliceAPIClient).getUsage(); | ||
}); | ||
Assertions.assertEquals(401, apiException.getCode()); | ||
} | ||
|
||
@Test | ||
public void testUsageReport_NonadminRole_AdminUser() throws ApiException { | ||
// bob does not have admin role, mapped to admin user - should fail | ||
boolean mapped = new UsersResourceApi(bobAPIClient).mapUser(TestEndpointUtils.administratorUserLoginInfo); | ||
Assertions.assertTrue(mapped); | ||
ApiException apiException = Assertions.assertThrows(ApiException.class, () -> { | ||
new AdminResourceApi(aliceAPIClient).getUsage(); | ||
}); | ||
Assertions.assertEquals(401, apiException.getCode()); | ||
} | ||
|
||
@Test | ||
public void testUsageReport_NonadminRole_NonadminUser() throws ApiException { | ||
// bob is not an admin, mapped to a non-admin user - should fail | ||
boolean mapped = new UsersResourceApi(bobAPIClient).mapUser(TestEndpointUtils.vcellNagiosUserLoginInfo); | ||
Assertions.assertTrue(mapped); | ||
ApiException apiException = Assertions.assertThrows(ApiException.class, () -> { | ||
new AdminResourceApi(aliceAPIClient).getUsage(); | ||
}); | ||
Assertions.assertEquals(401, apiException.getCode()); | ||
} | ||
|
||
@Test | ||
public void testUsageReport_NonadminRole_NotMapped() throws ApiException { | ||
// bob is not an admin, and is not mapped to any user - should fail | ||
ApiException apiException = Assertions.assertThrows(ApiException.class, () -> { | ||
new AdminResourceApi(aliceAPIClient).getUsage(); | ||
}); | ||
Assertions.assertEquals(401, apiException.getCode()); | ||
} | ||
} |