diff --git a/CHANGELOG.md b/CHANGELOG.md index 5976a772..b11c1d65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - **Tests**: `UserResourceTest.createUser` checks that GET after delete returns 404. `UserResourceIntegrationTest` no longer repeats that create and delete on a real server ([#313](https://github.com/sitmun/sitmun-backend-core/issues/313)). - **Tests**: `ServiceCheckStoreTest`, `AffectedApplicationsTest`, `ServiceHealthFilterTest`, `ServiceAccessSummaryTest`, and `ServiceCheckDispatcherTest` load `ServiceCheckSliceConfiguration` on the suite database, so Liquibase runs once for that slice. Connections do not autocommit, so the transactional slice removes its rows ([#317](https://github.com/sitmun/sitmun-backend-core/issues/317)). - **Tests**: `AuthenticationControllerLdapEnabledTest` starts the in-memory LDAP directory once for the class. The role-lookup case still returns HTTP 401 and no cookie only when `LdapUserAuthoritiesPopulator` rejects the user ([#319](https://github.com/sitmun/sitmun-backend-core/issues/319)). +- **Tests**: A JDBC URL `jdbc:h2:mem:testdb;MODE=NOT_A_MODE` is rejected with an SQLException that names the unknown mode. `POST /api/connections/test` returns HTTP 400 for that URL. `GET /api/connections/{id}/test` no longer repeats the same rejection ([#314](https://github.com/sitmun/sitmun-backend-core/issues/314)). - **Tests**: Database connection, role, territory, territory group type, and user repository tests use MockMvc on the shared context. They no longer boot an embedded server ([#316](https://github.com/sitmun/sitmun-backend-core/issues/316)). ### Security diff --git a/src/test/java/org/sitmun/administration/controller/DatabaseConnectionControllerTest.java b/src/test/java/org/sitmun/administration/controller/DatabaseConnectionControllerTest.java index 59cab147..3406d404 100644 --- a/src/test/java/org/sitmun/administration/controller/DatabaseConnectionControllerTest.java +++ b/src/test/java/org/sitmun/administration/controller/DatabaseConnectionControllerTest.java @@ -1,5 +1,6 @@ package org.sitmun.administration.controller; +import static org.hamcrest.Matchers.containsString; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -32,24 +33,6 @@ void failIfDatabaseConnectionDriverNotFound() throws Exception { .andExpect(jsonPath("$.message").value("java.lang.ClassNotFoundException: org.h2.DriverX")); } - @Test - @WithMockUser(roles = "ADMIN") - @DisplayName("GET returns 400 when the JDBC URL has no driver") - void failIfDatabaseConnectionException() throws Exception { - when(repository.findById(0)) - .thenReturn( - Optional.of( - DatabaseConnection.builder() - .driver("org.h2.Driver") - .url("jdb:h2:mem:testdb") - .name("sa") - .password("password") - .build())); - mvc.perform(get("/api/connections/0/test")) - .andExpect(status().isBadRequest()) - .andExpect(jsonPath("$.message").value("No suitable driver found for jdb:h2:mem:testdb")); - } - @Test @WithMockUser(roles = "ADMIN") @DisplayName("GET returns valid true") @@ -106,15 +89,15 @@ void failPostTestIfDatabaseConnectionDriverNotFound() throws Exception { @Test @WithMockUser(roles = "ADMIN") - @DisplayName("POST returns 400 when the JDBC URL has no driver") + @DisplayName("POST returns 400 when the JDBC URL is malformed") void failPostTestIfDatabaseConnectionException() throws Exception { mvc.perform( post("/api/connections/test") .contentType(MediaType.APPLICATION_JSON) .content( - "{ \"driver\" : \"org.h2.Driver\", \"url\" : \"jdb:h2:mem:testdb\", \"user\" : \"sa\", \"password\" : null }")) + "{ \"driver\" : \"org.h2.Driver\", \"url\" : \"jdbc:h2:mem:testdb;MODE=NOT_A_MODE\", \"user\" : \"sa\", \"password\" : null }")) .andExpect(status().isBadRequest()) - .andExpect(jsonPath("$.message").value("No suitable driver found for jdb:h2:mem:testdb")); + .andExpect(jsonPath("$.message", containsString("Unknown mode \"NOT_A_MODE\""))); } @Test diff --git a/src/test/java/org/sitmun/administration/service/database/tester/DatabaseConnectionTesterServiceTest.java b/src/test/java/org/sitmun/administration/service/database/tester/DatabaseConnectionTesterServiceTest.java index d4753a82..e8c46769 100644 --- a/src/test/java/org/sitmun/administration/service/database/tester/DatabaseConnectionTesterServiceTest.java +++ b/src/test/java/org/sitmun/administration/service/database/tester/DatabaseConnectionTesterServiceTest.java @@ -73,11 +73,12 @@ void malformedJdbcUrlThrowsSqlException() { DatabaseConnection connection = DatabaseConnection.builder() .driver("org.h2.Driver") - .url("jdb:h2:mem:testdb") + .url("jdbc:h2:mem:testdb;MODE=NOT_A_MODE") .user("sa") .password(null) .build(); sut.testDriver(connection); - assertThrows(SQLException.class, () -> sut.testConnection(connection)); + SQLException exception = assertThrows(SQLException.class, () -> sut.testConnection(connection)); + assertTrue(exception.getMessage().contains("Unknown mode \"NOT_A_MODE\"")); } }