mirror of
https://github.com/bitwarden/android.git
synced 2025-03-15 18:58:59 +03:00
Consolidate Environment(UrlDataJson)Extensions files in data layer (#509)
This commit is contained in:
parent
d72a3065a2
commit
0a3377d98a
5 changed files with 96 additions and 108 deletions
|
@ -1,21 +0,0 @@
|
|||
package com.x8bit.bitwarden.data.platform.repository.util
|
||||
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
|
||||
/**
|
||||
* Converts a raw [EnvironmentUrlDataJson] to an externally-consumable [Environment].
|
||||
*/
|
||||
fun EnvironmentUrlDataJson.toEnvironmentUrls(): Environment =
|
||||
when (this) {
|
||||
Environment.Us.environmentUrlData -> Environment.Us
|
||||
Environment.Eu.environmentUrlData -> Environment.Eu
|
||||
else -> Environment.SelfHosted(environmentUrlData = this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a nullable [EnvironmentUrlDataJson] to an [Environment], where `null` values default to
|
||||
* the US environment.
|
||||
*/
|
||||
fun EnvironmentUrlDataJson?.toEnvironmentUrlsOrDefault(): Environment =
|
||||
this?.toEnvironmentUrls() ?: Environment.Us
|
|
@ -22,3 +22,20 @@ val EnvironmentUrlDataJson.labelOrBaseUrlHost: String
|
|||
.orEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a raw [EnvironmentUrlDataJson] to an externally-consumable [Environment].
|
||||
*/
|
||||
fun EnvironmentUrlDataJson.toEnvironmentUrls(): Environment =
|
||||
when (this) {
|
||||
Environment.Us.environmentUrlData -> Environment.Us
|
||||
Environment.Eu.environmentUrlData -> Environment.Eu
|
||||
else -> Environment.SelfHosted(environmentUrlData = this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a nullable [EnvironmentUrlDataJson] to an [Environment], where `null` values default to
|
||||
* the US environment.
|
||||
*/
|
||||
fun EnvironmentUrlDataJson?.toEnvironmentUrlsOrDefault(): Environment =
|
||||
this?.toEnvironmentUrls() ?: Environment.Us
|
||||
|
|
|
@ -165,7 +165,7 @@ class EnvironmentRepositoryTest {
|
|||
}
|
||||
|
||||
private const val ENVIRONMENT_EXTENSIONS_PATH =
|
||||
"com.x8bit.bitwarden.data.platform.repository.util.EnvironmentExtensionsKt"
|
||||
"com.x8bit.bitwarden.data.platform.repository.util.EnvironmentUrlDataJsonExtensionsKt"
|
||||
|
||||
private class FakeEnvironmentDiskSource : EnvironmentDiskSource {
|
||||
override var preAuthEnvironmentUrlData: EnvironmentUrlDataJson? = null
|
||||
|
|
|
@ -1,86 +0,0 @@
|
|||
package com.x8bit.bitwarden.data.platform.repository.util
|
||||
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class EnvironmentExtensionsTest {
|
||||
@Test
|
||||
fun `toEnvironmentUrls should correctly convert US urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Us,
|
||||
EnvironmentUrlDataJson.DEFAULT_US.toEnvironmentUrls(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrls should correctly convert EU urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Eu,
|
||||
EnvironmentUrlDataJson.DEFAULT_EU.toEnvironmentUrls(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrls should correctly convert custom urls to the expected type`() {
|
||||
val environmentUrlData = EnvironmentUrlDataJson(
|
||||
base = "base",
|
||||
api = "api",
|
||||
identity = "identity",
|
||||
icon = "icon",
|
||||
notifications = "notifications",
|
||||
webVault = "webVault",
|
||||
events = "events",
|
||||
)
|
||||
assertEquals(
|
||||
Environment.SelfHosted(
|
||||
environmentUrlData = environmentUrlData,
|
||||
),
|
||||
environmentUrlData.toEnvironmentUrls(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should correctly convert US urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Us,
|
||||
EnvironmentUrlDataJson.DEFAULT_US.toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should correctly convert EU urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Eu,
|
||||
EnvironmentUrlDataJson.DEFAULT_EU.toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should correctly convert custom urls to the expected type`() {
|
||||
val environmentUrlData = EnvironmentUrlDataJson(
|
||||
base = "base",
|
||||
api = "api",
|
||||
identity = "identity",
|
||||
icon = "icon",
|
||||
notifications = "notifications",
|
||||
webVault = "webVault",
|
||||
events = "events",
|
||||
)
|
||||
assertEquals(
|
||||
Environment.SelfHosted(
|
||||
environmentUrlData = environmentUrlData,
|
||||
),
|
||||
environmentUrlData.toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should convert null types to US values`() {
|
||||
assertEquals(
|
||||
Environment.Us,
|
||||
(null as EnvironmentUrlDataJson?).toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
}
|
|
@ -33,4 +33,82 @@ class EnvironmentUrlsDataJsonExtensionsTest {
|
|||
environment.labelOrBaseUrlHost,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrls should correctly convert US urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Us,
|
||||
EnvironmentUrlDataJson.DEFAULT_US.toEnvironmentUrls(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrls should correctly convert EU urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Eu,
|
||||
EnvironmentUrlDataJson.DEFAULT_EU.toEnvironmentUrls(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrls should correctly convert custom urls to the expected type`() {
|
||||
val environmentUrlData = EnvironmentUrlDataJson(
|
||||
base = "base",
|
||||
api = "api",
|
||||
identity = "identity",
|
||||
icon = "icon",
|
||||
notifications = "notifications",
|
||||
webVault = "webVault",
|
||||
events = "events",
|
||||
)
|
||||
assertEquals(
|
||||
Environment.SelfHosted(
|
||||
environmentUrlData = environmentUrlData,
|
||||
),
|
||||
environmentUrlData.toEnvironmentUrls(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should correctly convert US urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Us,
|
||||
EnvironmentUrlDataJson.DEFAULT_US.toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should correctly convert EU urls to the expected type`() {
|
||||
assertEquals(
|
||||
Environment.Eu,
|
||||
EnvironmentUrlDataJson.DEFAULT_EU.toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should correctly convert custom urls to the expected type`() {
|
||||
val environmentUrlData = EnvironmentUrlDataJson(
|
||||
base = "base",
|
||||
api = "api",
|
||||
identity = "identity",
|
||||
icon = "icon",
|
||||
notifications = "notifications",
|
||||
webVault = "webVault",
|
||||
events = "events",
|
||||
)
|
||||
assertEquals(
|
||||
Environment.SelfHosted(
|
||||
environmentUrlData = environmentUrlData,
|
||||
),
|
||||
environmentUrlData.toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEnvironmentUrlsOrDefault should convert null types to US values`() {
|
||||
assertEquals(
|
||||
Environment.Us,
|
||||
(null as EnvironmentUrlDataJson?).toEnvironmentUrlsOrDefault(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue