diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt index a701661..b913523 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/model/Configuration.kt @@ -81,9 +81,14 @@ fun configuration( configuration(type = key.toConfigurationType(), content = content) } +fun secureConfiguration( + type: ConfigurationType, + lastUpdated: LocalDateTime? = null +) = SecureConfiguration(type, lastUpdated ?: LocalDateTime.now()) + fun secureConfiguration( configuration: Configuration -) = SecureConfiguration(configuration.type, configuration.lastUpdated) +) = secureConfiguration(configuration.type, configuration.lastUpdated) enum class ConfigurationType( val key: String, diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationServiceTest.kt index 8c4df85..e49fc60 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/ConfigurationServiceTest.kt @@ -10,6 +10,7 @@ import io.mockk.* import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows +import kotlin.UnsupportedOperationException import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -165,7 +166,47 @@ class ConfigurationServiceTest { } @Test - fun `get(type) decrypts configuration content when the given ConfigurationType is secure`() { + fun `get(type) returns a SecureConfiguration when the given ConfigurationType is secure`() { + val type = ConfigurationType.DATABASE_PASSWORD + val configuration = configuration( + type = type, + content = "securepassword".encrypt(type.key, securityProperties.configSalt!!) + ) + + every { configurationSource.get(type) } returns configuration + + val found = service.get(type) + + assertTrue { found is SecureConfiguration } + } + + @Test + fun `getContent(type) returns configuration content`() { + val type = ConfigurationType.INSTANCE_NAME + val configuration = configuration( + type = type, + content = "content" + ) + + every { service.get(type) } returns configuration + + val found = service.getContent(type) + + assertEquals(configuration.content, found) + } + + @Test + fun `getContent(type) throws UnsupportedOperationException when configuration is secure`() { + val type = ConfigurationType.DATABASE_PASSWORD + val configuration = secureConfiguration(type) + + every { service.get(type) } returns configuration + + assertThrows { service.getContent(type) } + } + + @Test + fun `getSecure(type) returns decrypted configuration content`() { val type = ConfigurationType.DATABASE_PASSWORD val content = "securepassword" val configuration = configuration( @@ -175,9 +216,16 @@ class ConfigurationServiceTest { every { configurationSource.get(type) } returns configuration - val found = service.get(type) + val found = service.getSecure(type) - assertEquals(content, found.content) + assertEquals(content, found) + } + + @Test + fun `getSecure(type) throws UnsupportedOperationException when configuration is not secure`() { + val type = ConfigurationType.INSTANCE_NAME + + assertThrows { service.getSecure(type) } } @Test @@ -197,7 +245,7 @@ class ConfigurationServiceTest { fun `set(configuration) encrypts secure configurations`() { val type = ConfigurationType.DATABASE_PASSWORD val content = "securepassword" - val encryptedContent =content.encrypt(type.key, securityProperties.configSalt!!) + val encryptedContent = content.encrypt(type.key, securityProperties.configSalt!!) val configuration = configuration(type = type, content = content) mockkStatic(String::encrypt) diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt index 58a1193..0e055ad 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/RecipeServiceTest.kt @@ -80,9 +80,9 @@ class RecipeServiceTest : @Test fun `isApprobationExpired() returns false when the approbation date of the given recipe is within the configured period`() { val period = Period.ofMonths(4) - val config = configuration(type = ConfigurationType.RECIPE_APPROBATION_EXPIRATION, content = period.toString()) val recipe = recipe(approbationDate = LocalDate.now()) - whenever(configService.get(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(config) + + whenever(configService.getContent(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(period.toString()) val approbationExpired = service.isApprobationExpired(recipe) @@ -93,9 +93,9 @@ class RecipeServiceTest : @Test fun `isApprobationExpired() returns true when the approbation date of the given recipe is outside the configured period`() { val period = Period.ofMonths(4) - val config = configuration(type = ConfigurationType.RECIPE_APPROBATION_EXPIRATION, content = period.toString()) val recipe = recipe(approbationDate = LocalDate.now().minus(period).minusMonths(1)) - whenever(configService.get(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(config) + + whenever(configService.getContent(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(period.toString()) val approbationExpired = service.isApprobationExpired(recipe) @@ -106,9 +106,9 @@ class RecipeServiceTest : @Test fun `isApprobationExpired() returns null when the given recipe as no approbation date`() { val period = Period.ofMonths(4) - val config = configuration(type = ConfigurationType.RECIPE_APPROBATION_EXPIRATION, content = period.toString()) val recipe = recipe(approbationDate = null) - whenever(configService.get(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(config) + + whenever(configService.getContent(ConfigurationType.RECIPE_APPROBATION_EXPIRATION)).doReturn(period.toString()) val approbationExpired = service.isApprobationExpired(recipe) diff --git a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/TouchUpKitServiceTest.kt b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/TouchUpKitServiceTest.kt index c913d45..f6b5ba2 100644 --- a/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/TouchUpKitServiceTest.kt +++ b/src/test/kotlin/dev/fyloz/colorrecipesexplorer/service/TouchUpKitServiceTest.kt @@ -131,10 +131,7 @@ class TouchUpKitServiceTest { this.setCachePdf(false) private fun TouchUpKitServiceTestContext.setCachePdf(enabled: Boolean) { - every { configService.get(ConfigurationType.TOUCH_UP_KIT_CACHE_PDF) } returns configuration( - type = ConfigurationType.TOUCH_UP_KIT_CACHE_PDF, - enabled.toString() - ) + every { configService.getContent(ConfigurationType.TOUCH_UP_KIT_CACHE_PDF) } returns enabled.toString() } private fun test(test: TouchUpKitServiceTestContext.() -> Unit) {