|
| 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.api.permalink |
| 18 | + |
| 19 | +import com.google.common.truth.Truth.assertThat |
| 20 | +import org.junit.Test |
| 21 | +import org.junit.runner.RunWith |
| 22 | +import org.robolectric.RobolectricTestRunner |
| 23 | + |
| 24 | +@RunWith(RobolectricTestRunner::class) |
| 25 | +class PermalinkParserTests { |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `parsing an invalid url returns a fallback link`() { |
| 29 | + val url = "https://element.io" |
| 30 | + assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `parsing an invalid url with the right path but no content returns a fallback link`() { |
| 35 | + val url = "https://app.element.io/#/user" |
| 36 | + assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `parsing an invalid url with the right path but empty content returns a fallback link`() { |
| 41 | + val url = "https://app.element.io/#/user/" |
| 42 | + assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `parsing an invalid url with the right path but invalid content returns a fallback link`() { |
| 47 | + val url = "https://app.element.io/#/user/some%20user!" |
| 48 | + assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `parsing a valid user url returns a user link`() { |
| 53 | + val url = "https://app.element.io/#/user/@test:matrix.org" |
| 54 | + assertThat(PermalinkParser.parse(url)).isEqualTo( |
| 55 | + PermalinkData.UserLink( |
| 56 | + userId = "@test:matrix.org" |
| 57 | + ) |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `parsing a valid room id url returns a room link`() { |
| 63 | + val url = "https://app.element.io/#/room/!aBCD1234:matrix.org" |
| 64 | + assertThat(PermalinkParser.parse(url)).isEqualTo( |
| 65 | + PermalinkData.RoomLink( |
| 66 | + roomIdOrAlias = "!aBCD1234:matrix.org", |
| 67 | + isRoomAlias = false, |
| 68 | + eventId = null, |
| 69 | + viaParameters = emptyList(), |
| 70 | + ) |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `parsing a valid room id with event id url returns a room link`() { |
| 76 | + val url = "https://app.element.io/#/room/!aBCD1234:matrix.org/$1234567890abcdef:matrix.org" |
| 77 | + assertThat(PermalinkParser.parse(url)).isEqualTo( |
| 78 | + PermalinkData.RoomLink( |
| 79 | + roomIdOrAlias = "!aBCD1234:matrix.org", |
| 80 | + isRoomAlias = false, |
| 81 | + eventId = "\$1234567890abcdef:matrix.org", |
| 82 | + viaParameters = emptyList(), |
| 83 | + ) |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `parsing a valid room id with and invalid event id url returns a room link with no event id`() { |
| 89 | + val url = "https://app.element.io/#/room/!aBCD1234:matrix.org/1234567890abcdef:matrix.org" |
| 90 | + assertThat(PermalinkParser.parse(url)).isEqualTo( |
| 91 | + PermalinkData.RoomLink( |
| 92 | + roomIdOrAlias = "!aBCD1234:matrix.org", |
| 93 | + isRoomAlias = false, |
| 94 | + eventId = null, |
| 95 | + viaParameters = emptyList(), |
| 96 | + ) |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `parsing a valid room id with event id and via parameters url returns a room link`() { |
| 102 | + val url = "https://app.element.io/#/room/!aBCD1234:matrix.org/$1234567890abcdef:matrix.org?via=matrix.org&via=matrix.com" |
| 103 | + assertThat(PermalinkParser.parse(url)).isEqualTo( |
| 104 | + PermalinkData.RoomLink( |
| 105 | + roomIdOrAlias = "!aBCD1234:matrix.org", |
| 106 | + isRoomAlias = false, |
| 107 | + eventId = "\$1234567890abcdef:matrix.org", |
| 108 | + viaParameters = listOf("matrix.org", "matrix.com"), |
| 109 | + ) |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun `parsing a valid room alias url returns a room link`() { |
| 115 | + val url = "https://app.element.io/#/room/#element-android:matrix.org" |
| 116 | + assertThat(PermalinkParser.parse(url)).isEqualTo( |
| 117 | + PermalinkData.RoomLink( |
| 118 | + roomIdOrAlias = "#element-android:matrix.org", |
| 119 | + isRoomAlias = true, |
| 120 | + eventId = null, |
| 121 | + viaParameters = emptyList(), |
| 122 | + ) |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun `parsing a url with an invalid signurl returns a fallback link`() { |
| 128 | + // This url has no private key |
| 129 | + val url = "https://app.element.io/#/room/%21aBCDEF12345%3Amatrix.org" + |
| 130 | + "?email=testuser%40element.io" + |
| 131 | + "&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3Da_token" + |
| 132 | + "&room_name=TestRoom" + |
| 133 | + "&room_avatar_url=" + |
| 134 | + "&inviter_name=User" + |
| 135 | + "&guest_access_token=" + |
| 136 | + "&guest_user_id=" + |
| 137 | + "&room_type=" |
| 138 | + assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + fun `parsing a url with signurl returns a room email invite link`() { |
| 143 | + val url = "https://app.element.io/#/room/%21aBCDEF12345%3Amatrix.org" + |
| 144 | + "?email=testuser%40element.io" + |
| 145 | + "&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3Da_token%26private_key%3Da_private_key" + |
| 146 | + "&room_name=TestRoom" + |
| 147 | + "&room_avatar_url=" + |
| 148 | + "&inviter_name=User" + |
| 149 | + "&guest_access_token=" + |
| 150 | + "&guest_user_id=" + |
| 151 | + "&room_type=" |
| 152 | + assertThat(PermalinkParser.parse(url)).isEqualTo( |
| 153 | + PermalinkData.RoomEmailInviteLink( |
| 154 | + roomId = "!aBCDEF12345:matrix.org", |
| 155 | + |
| 156 | + signUrl = "https://vector.im/_matrix/identity/api/v1/sign-ed25519?token=a_token&private_key=a_private_key", |
| 157 | + roomName = "TestRoom", |
| 158 | + roomAvatarUrl = "", |
| 159 | + inviterName = "User", |
| 160 | + identityServer = "vector.im", |
| 161 | + token = "a_token", |
| 162 | + privateKey = "a_private_key", |
| 163 | + roomType = "", |
| 164 | + ) |
| 165 | + ) |
| 166 | + } |
| 167 | +} |
0 commit comments