|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 New Vector Ltd |
| 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.element.android.libraries.matrix.impl.auth |
| 18 | + |
| 19 | +import com.google.common.truth.ThrowableSubject |
| 20 | +import com.google.common.truth.Truth.assertThat |
| 21 | +import io.element.android.libraries.matrix.api.auth.AuthenticationException |
| 22 | +import org.junit.Test |
| 23 | +import org.matrix.rustcomponents.sdk.AuthenticationException as RustAuthenticationException |
| 24 | + |
| 25 | +class AuthenticationExceptionMappingTests { |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `mapping an exception with no message returns 'Unknown error' message`() { |
| 29 | + val exception = Exception() |
| 30 | + val mappedException = exception.mapAuthenticationException() |
| 31 | + assertThat(mappedException.message).isEqualTo("Unknown error") |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `mapping a generic exception returns a Generic AuthenticationException`() { |
| 36 | + val exception = Exception("Generic exception") |
| 37 | + val mappedException = exception.mapAuthenticationException() |
| 38 | + assertThat(mappedException).isException<AuthenticationException.Generic>("Generic exception") |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `mapping specific exceptions map to their kotlin counterparts`() { |
| 43 | + assertThat(RustAuthenticationException.ClientMissing("Client missing").mapAuthenticationException()) |
| 44 | + .isException<AuthenticationException.ClientMissing>("Client missing") |
| 45 | + |
| 46 | + assertThat(RustAuthenticationException.Generic("Generic").mapAuthenticationException()).isException<AuthenticationException.Generic>("Generic") |
| 47 | + |
| 48 | + assertThat(RustAuthenticationException.InvalidServerName("Invalid server name").mapAuthenticationException()) |
| 49 | + .isException<AuthenticationException.InvalidServerName>("Invalid server name") |
| 50 | + |
| 51 | + assertThat(RustAuthenticationException.SessionMissing("Session missing").mapAuthenticationException()) |
| 52 | + .isException<AuthenticationException.SessionMissing>("Session missing") |
| 53 | + |
| 54 | + assertThat(RustAuthenticationException.SlidingSyncNotAvailable("Sliding sync not available").mapAuthenticationException()) |
| 55 | + .isException<AuthenticationException.SlidingSyncNotAvailable>("Sliding sync not available") |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `mapping Oidc related exceptions creates an 'OidcError' with different types`() { |
| 60 | + assertIsOidcError( |
| 61 | + throwable = RustAuthenticationException.OidcException("Oidc exception"), |
| 62 | + type = "OidcException", |
| 63 | + message = "Oidc exception" |
| 64 | + ) |
| 65 | + assertIsOidcError( |
| 66 | + throwable = RustAuthenticationException.OidcMetadataInvalid("Oidc metadata invalid"), |
| 67 | + type = "OidcMetadataInvalid", |
| 68 | + message = "Oidc metadata invalid" |
| 69 | + ) |
| 70 | + assertIsOidcError( |
| 71 | + throwable = RustAuthenticationException.OidcMetadataMissing("Oidc metadata missing"), |
| 72 | + type = "OidcMetadataMissing", |
| 73 | + message = "Oidc metadata missing" |
| 74 | + ) |
| 75 | + assertIsOidcError( |
| 76 | + throwable = RustAuthenticationException.OidcNotSupported("Oidc not supported"), |
| 77 | + type = "OidcNotSupported", |
| 78 | + message = "Oidc not supported" |
| 79 | + ) |
| 80 | + assertIsOidcError( |
| 81 | + throwable = RustAuthenticationException.OidcCancelled("Oidc cancelled"), |
| 82 | + type = "OidcCancelled", |
| 83 | + message = "Oidc cancelled" |
| 84 | + ) |
| 85 | + assertIsOidcError( |
| 86 | + throwable = RustAuthenticationException.OidcCallbackUrlInvalid("Oidc callback url invalid"), |
| 87 | + type = "OidcCallbackUrlInvalid", |
| 88 | + message = "Oidc callback url invalid" |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + private inline fun <reified T> ThrowableSubject.isException(message: String) { |
| 93 | + isInstanceOf(T::class.java) |
| 94 | + hasMessageThat().isEqualTo(message) |
| 95 | + } |
| 96 | + |
| 97 | + private inline fun assertIsOidcError(throwable: Throwable, type: String, message: String) { |
| 98 | + val authenticationException = throwable.mapAuthenticationException() |
| 99 | + assertThat(authenticationException).isInstanceOf(AuthenticationException.OidcError::class.java) |
| 100 | + assertThat((authenticationException as? AuthenticationException.OidcError)?.type).isEqualTo(type) |
| 101 | + assertThat(authenticationException.message).isEqualTo(message) |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments