|
| 1 | +/** |
| 2 | + * Copyright (C) 2025 Tolgee s.r.o. and contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.tolgee.email |
| 18 | + |
| 19 | +import io.tolgee.component.publicBillingConfProvider.PublicBillingConfProvider |
| 20 | +import io.tolgee.configuration.tolgee.TolgeeProperties |
| 21 | +import io.tolgee.dtos.response.PublicBillingConfigurationDTO |
| 22 | +import io.tolgee.email.EmailGlobalVariablesProvider.Companion.SELF_HOSTED_DEFAULT_QUALIFIER |
| 23 | +import io.tolgee.testing.assert |
| 24 | +import org.junit.jupiter.api.Test |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith |
| 26 | +import org.mockito.kotlin.whenever |
| 27 | +import org.springframework.beans.factory.annotation.Autowired |
| 28 | +import org.springframework.boot.test.mock.mockito.MockBean |
| 29 | +import org.springframework.context.annotation.Import |
| 30 | +import org.springframework.stereotype.Component |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringExtension |
| 32 | + |
| 33 | +@Component |
| 34 | +@ExtendWith(SpringExtension::class) |
| 35 | +@Import(EmailGlobalVariablesProvider::class) |
| 36 | +class EmailGlobalVariablesProviderTest { |
| 37 | + @MockBean |
| 38 | + private lateinit var publicBillingConfProvider: PublicBillingConfProvider |
| 39 | + |
| 40 | + @MockBean |
| 41 | + private lateinit var tolgeeProperties: TolgeeProperties |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private lateinit var emailGlobalVariablesProvider: EmailGlobalVariablesProvider |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `it returns the correct properties based on config in cloud`() { |
| 48 | + whenever(publicBillingConfProvider.invoke()).thenReturn(PublicBillingConfigurationDTO(true)) |
| 49 | + whenever(tolgeeProperties.appName).thenReturn("Tolgee Test Edition") |
| 50 | + whenever(tolgeeProperties.frontEndUrl).thenReturn("https://tolgee.test") |
| 51 | + |
| 52 | + emailGlobalVariablesProvider().assert |
| 53 | + .containsEntry("isCloud", true) |
| 54 | + .containsEntry("instanceQualifier", "Tolgee Test Edition") |
| 55 | + .containsEntry("instanceUrl", "https://tolgee.test") |
| 56 | + .hasSize(3) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `it returns the correct properties based on config in self-hosted`() { |
| 61 | + whenever(publicBillingConfProvider.invoke()).thenReturn(PublicBillingConfigurationDTO(false)) |
| 62 | + whenever(tolgeeProperties.appName).thenReturn("Tolgee Test Edition") |
| 63 | + whenever(tolgeeProperties.frontEndUrl).thenReturn("https://tolgee.test") |
| 64 | + |
| 65 | + emailGlobalVariablesProvider().assert |
| 66 | + .containsEntry("isCloud", false) |
| 67 | + .containsEntry("instanceQualifier", "tolgee.test") |
| 68 | + .containsEntry("instanceUrl", "https://tolgee.test") |
| 69 | + .hasSize(3) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `it gracefully handles bad frontend url configuration`() { |
| 74 | + whenever(publicBillingConfProvider.invoke()).thenReturn(PublicBillingConfigurationDTO(false)) |
| 75 | + whenever(tolgeeProperties.appName).thenReturn("Tolgee Test Edition") |
| 76 | + whenever(tolgeeProperties.frontEndUrl).thenReturn("https:/tolgee.test") |
| 77 | + |
| 78 | + emailGlobalVariablesProvider().assert |
| 79 | + .containsEntry("isCloud", false) |
| 80 | + .containsEntry("instanceQualifier", SELF_HOSTED_DEFAULT_QUALIFIER) |
| 81 | + .containsEntry("instanceUrl", "https:/tolgee.test") |
| 82 | + .hasSize(3) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `it gracefully handles missing frontend url configuration`() { |
| 87 | + // That's a pathological situation to be in... but it can happen... :shrug: |
| 88 | + whenever(publicBillingConfProvider.invoke()).thenReturn(PublicBillingConfigurationDTO(false)) |
| 89 | + whenever(tolgeeProperties.appName).thenReturn("Tolgee Test Edition") |
| 90 | + whenever(tolgeeProperties.frontEndUrl).thenReturn(null) |
| 91 | + |
| 92 | + emailGlobalVariablesProvider().assert |
| 93 | + .containsEntry("isCloud", false) |
| 94 | + .containsEntry("instanceQualifier", SELF_HOSTED_DEFAULT_QUALIFIER) |
| 95 | + .containsEntry("instanceUrl", null) |
| 96 | + .hasSize(3) |
| 97 | + } |
| 98 | +} |
0 commit comments