From 397bb21146dbdb38c8f97103cab3daeca25e547e Mon Sep 17 00:00:00 2001 From: Francisco J Lopez-Pellicer Date: Wed, 21 Oct 2026 19:18:13 +0200 Subject: [PATCH] test: reject a malformed JDBC URL --- CHANGELOG.md | 1 + .../DatabaseConnectionControllerTest.java | 25 +++---------------- .../DatabaseConnectionTesterServiceTest.java | 5 ++-- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c725eb30..da9bad63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - **Tests**: `SequenceMigrationTest` checks the `ATR_ID` generator on `STM_APP_TREE` ([#318](https://github.com/sitmun/sitmun-backend-core/issues/318)). - **Tests**: `UserResourceTest` stores a fixed bcrypt hash in setup. The password update still asserts the stored hash changes ([#321](https://github.com/sitmun/sitmun-backend-core/issues/321)). - **Tests**: `DashboardMetricsQueryTest` uses the suite database. Its connections do not autocommit, so the transactional slice removes its rows. The day bucket still checks year, month, and day, and its count rises by one. Per-application counts stay `1` or `0` on an `app-` plus UUID name, including the built-in public user ([#322](https://github.com/sitmun/sitmun-backend-core/issues/322)). +- **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)). ### 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\"")); } }