|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +package io.element.android.libraries.wellknown.impl |
| 9 | + |
| 10 | +import com.google.common.truth.Truth.assertThat |
| 11 | +import io.element.android.libraries.matrix.test.AN_EXCEPTION |
| 12 | +import io.element.android.libraries.matrix.test.FakeMatrixClient |
| 13 | +import io.element.android.libraries.wellknown.api.ElementWellKnown |
| 14 | +import io.element.android.libraries.wellknown.api.WellKnown |
| 15 | +import io.element.android.libraries.wellknown.api.WellKnownBaseConfig |
| 16 | +import io.element.android.tests.testutils.lambda.lambdaRecorder |
| 17 | +import io.element.android.tests.testutils.lambda.value |
| 18 | +import kotlinx.coroutines.test.runTest |
| 19 | +import kotlinx.serialization.json.Json |
| 20 | +import org.junit.Test |
| 21 | + |
| 22 | +class DefaultSessionWellknownRetrieverTest { |
| 23 | + @Test |
| 24 | + fun `get empty wellknown`() = runTest { |
| 25 | + val getUrlLambda = lambdaRecorder<String, Result<ByteArray>>() { |
| 26 | + Result.success("{}".toByteArray()) |
| 27 | + } |
| 28 | + val sut = createDefaultSessionWellknownRetriever( |
| 29 | + getUrlLambda = getUrlLambda, |
| 30 | + ) |
| 31 | + assertThat(sut.getWellKnown()).isEqualTo( |
| 32 | + WellKnown( |
| 33 | + homeServer = null, |
| 34 | + identityServer = null, |
| 35 | + ) |
| 36 | + ) |
| 37 | + getUrlLambda.assertions().isCalledOnce() |
| 38 | + .with(value("https://user.domain.org/.well-known/matrix/client")) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `get wellknown with full content`() = runTest { |
| 43 | + val sut = createDefaultSessionWellknownRetriever( |
| 44 | + getUrlLambda = { |
| 45 | + Result.success( |
| 46 | + """{ |
| 47 | + "m.homeserver": { |
| 48 | + "base_url": "https://example.org" |
| 49 | + }, |
| 50 | + "m.identity_server": { |
| 51 | + "base_url": "https://identity.example.org" |
| 52 | + } |
| 53 | + }""".trimIndent().toByteArray() |
| 54 | + ) |
| 55 | + } |
| 56 | + ) |
| 57 | + assertThat(sut.getWellKnown()).isEqualTo( |
| 58 | + WellKnown( |
| 59 | + homeServer = WellKnownBaseConfig( |
| 60 | + baseURL = "https://example.org", |
| 61 | + ), |
| 62 | + identityServer = WellKnownBaseConfig( |
| 63 | + baseURL = "https://identity.example.org", |
| 64 | + ), |
| 65 | + ) |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `get wellknown with full content empty base_url`() = runTest { |
| 71 | + val sut = createDefaultSessionWellknownRetriever( |
| 72 | + getUrlLambda = { |
| 73 | + Result.success( |
| 74 | + """{ |
| 75 | + "m.homeserver": { |
| 76 | + "base_url": "https://example.org" |
| 77 | + }, |
| 78 | + "m.identity_server": {} |
| 79 | + }""".trimIndent().toByteArray() |
| 80 | + ) |
| 81 | + } |
| 82 | + ) |
| 83 | + assertThat(sut.getWellKnown()).isEqualTo( |
| 84 | + WellKnown( |
| 85 | + homeServer = WellKnownBaseConfig( |
| 86 | + baseURL = "https://example.org", |
| 87 | + ), |
| 88 | + identityServer = WellKnownBaseConfig( |
| 89 | + baseURL = null, |
| 90 | + ), |
| 91 | + ) |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `get wellknown with unknown key`() = runTest { |
| 97 | + val sut = createDefaultSessionWellknownRetriever( |
| 98 | + getUrlLambda = { |
| 99 | + Result.success( |
| 100 | + """{ |
| 101 | + "m.homeserver": { |
| 102 | + "base_url": "https://example.org" |
| 103 | + }, |
| 104 | + "m.identity_server": { |
| 105 | + "base_url": "https://identity.example.org" |
| 106 | + }, |
| 107 | + "other": true |
| 108 | + }""".trimIndent().toByteArray() |
| 109 | + ) |
| 110 | + } |
| 111 | + ) |
| 112 | + assertThat(sut.getWellKnown()).isEqualTo( |
| 113 | + WellKnown( |
| 114 | + homeServer = WellKnownBaseConfig( |
| 115 | + baseURL = "https://example.org", |
| 116 | + ), |
| 117 | + identityServer = WellKnownBaseConfig( |
| 118 | + baseURL = "https://identity.example.org", |
| 119 | + ), |
| 120 | + ) |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `get wellknown json error`() = runTest { |
| 126 | + val sut = createDefaultSessionWellknownRetriever( |
| 127 | + getUrlLambda = { |
| 128 | + Result.success( |
| 129 | + """{ |
| 130 | + "m.homeserver": { |
| 131 | + "base_url": "https://example.org" |
| 132 | + }, |
| 133 | + error |
| 134 | + }""".trimIndent().toByteArray() |
| 135 | + ) |
| 136 | + } |
| 137 | + ) |
| 138 | + assertThat(sut.getWellKnown()).isNull() |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + fun `get wellknown network error`() = runTest { |
| 143 | + val sut = createDefaultSessionWellknownRetriever( |
| 144 | + getUrlLambda = { |
| 145 | + Result.failure(AN_EXCEPTION) |
| 146 | + } |
| 147 | + ) |
| 148 | + assertThat(sut.getWellKnown()).isNull() |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `get empty element wellknown`() = runTest { |
| 153 | + val getUrlLambda = lambdaRecorder<String, Result<ByteArray>>() { |
| 154 | + Result.success("{}".toByteArray()) |
| 155 | + } |
| 156 | + val sut = createDefaultSessionWellknownRetriever( |
| 157 | + getUrlLambda = getUrlLambda, |
| 158 | + ) |
| 159 | + assertThat(sut.getElementWellKnown()).isEqualTo( |
| 160 | + ElementWellKnown( |
| 161 | + registrationHelperUrl = null, |
| 162 | + enforceElementPro = null, |
| 163 | + rageshakeUrl = null, |
| 164 | + ) |
| 165 | + ) |
| 166 | + getUrlLambda.assertions().isCalledOnce() |
| 167 | + .with(value("https://user.domain.org/.well-known/element/element.json")) |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + fun `get element wellknown with full content`() = runTest { |
| 172 | + val sut = createDefaultSessionWellknownRetriever( |
| 173 | + getUrlLambda = { |
| 174 | + Result.success( |
| 175 | + """{ |
| 176 | + "registration_helper_url": "a_registration_url", |
| 177 | + "enforce_element_pro": true, |
| 178 | + "rageshake_url": "a_rageshake_url" |
| 179 | + }""".trimIndent().toByteArray() |
| 180 | + ) |
| 181 | + } |
| 182 | + ) |
| 183 | + assertThat(sut.getElementWellKnown()).isEqualTo( |
| 184 | + ElementWellKnown( |
| 185 | + registrationHelperUrl = "a_registration_url", |
| 186 | + enforceElementPro = true, |
| 187 | + rageshakeUrl = "a_rageshake_url", |
| 188 | + ) |
| 189 | + ) |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + fun `get element wellknown with unknown key`() = runTest { |
| 194 | + val sut = createDefaultSessionWellknownRetriever( |
| 195 | + getUrlLambda = { |
| 196 | + Result.success( |
| 197 | + """{ |
| 198 | + "registration_helper_url": "a_registration_url", |
| 199 | + "enforce_element_pro": true, |
| 200 | + "rageshake_url": "a_rageshake_url", |
| 201 | + "other": true |
| 202 | + }""".trimIndent().toByteArray() |
| 203 | + ) |
| 204 | + } |
| 205 | + ) |
| 206 | + assertThat(sut.getElementWellKnown()).isEqualTo( |
| 207 | + ElementWellKnown( |
| 208 | + registrationHelperUrl = "a_registration_url", |
| 209 | + enforceElementPro = true, |
| 210 | + rageshakeUrl = "a_rageshake_url", |
| 211 | + ) |
| 212 | + ) |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + fun `get element wellknown json error`() = runTest { |
| 217 | + val sut = createDefaultSessionWellknownRetriever( |
| 218 | + getUrlLambda = { |
| 219 | + Result.success( |
| 220 | + """{ |
| 221 | + "registration_helper_url" = "a_registration_url", |
| 222 | + error |
| 223 | + }""".trimIndent().toByteArray() |
| 224 | + ) |
| 225 | + } |
| 226 | + ) |
| 227 | + assertThat(sut.getElementWellKnown()).isNull() |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + fun `get element wellknown network error`() = runTest { |
| 232 | + val sut = createDefaultSessionWellknownRetriever( |
| 233 | + getUrlLambda = { |
| 234 | + Result.failure(AN_EXCEPTION) |
| 235 | + } |
| 236 | + ) |
| 237 | + assertThat(sut.getElementWellKnown()).isNull() |
| 238 | + } |
| 239 | + |
| 240 | + private fun createDefaultSessionWellknownRetriever( |
| 241 | + getUrlLambda: (String) -> Result<ByteArray>, |
| 242 | + ) = DefaultSessionWellknownRetriever( |
| 243 | + matrixClient = FakeMatrixClient( |
| 244 | + userIdServerNameLambda = { "user.domain.org" }, |
| 245 | + getUrlLambda = getUrlLambda, |
| 246 | + ), |
| 247 | + parser = Json { ignoreUnknownKeys = true } |
| 248 | + ) |
| 249 | +} |
0 commit comments