Skip to content

Commit 6a7713a

Browse files
authored
Improve coverage in permalinks package (#1502)
* Improve coverage in permalinks package
1 parent ffef9d9 commit 6a7713a

File tree

5 files changed

+330
-0
lines changed

5 files changed

+330
-0
lines changed

libraries/matrix/api/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ dependencies {
4444

4545
testImplementation(libs.test.junit)
4646
testImplementation(libs.test.truth)
47+
testImplementation(libs.test.robolectric)
48+
testImplementation(projects.tests.testutils)
4749
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 android.net.Uri
20+
import com.google.common.truth.Truth.assertThat
21+
import org.junit.Test
22+
import org.junit.runner.RunWith
23+
import org.robolectric.RobolectricTestRunner
24+
25+
@RunWith(RobolectricTestRunner::class)
26+
class MatrixToConverterTests {
27+
28+
@Test
29+
fun `converting a matrix-to url does nothing`() {
30+
val url = Uri.parse("https://matrix.to/#/#element-android:matrix.org")
31+
assertThat(MatrixToConverter.convert(url)).isEqualTo(url)
32+
}
33+
34+
@Test
35+
fun `converting a url with a supported room path returns a matrix-to url`() {
36+
val url = Uri.parse("https://riot.im/develop/#/room/#element-android:matrix.org")
37+
assertThat(MatrixToConverter.convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/#element-android:matrix.org"))
38+
}
39+
40+
@Test
41+
fun `converting a url with a supported user path returns a matrix-to url`() {
42+
val url = Uri.parse("https://riot.im/develop/#/user/@test:matrix.org")
43+
assertThat(MatrixToConverter.convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/@test:matrix.org"))
44+
}
45+
46+
@Test
47+
fun `converting a url with a supported group path returns a matrix-to url`() {
48+
val url = Uri.parse("https://riot.im/develop/#/group/+group:matrix.org")
49+
assertThat(MatrixToConverter.convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/+group:matrix.org"))
50+
}
51+
52+
@Test
53+
fun `converting an unsupported url returns null`() {
54+
val url = Uri.parse("https://element.io/")
55+
assertThat(MatrixToConverter.convert(url)).isNull()
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 io.element.android.libraries.matrix.api.core.RoomId
21+
import io.element.android.libraries.matrix.api.core.UserId
22+
import io.element.android.tests.testutils.assertThrowsInDebug
23+
import io.element.android.tests.testutils.isInDebug
24+
import org.junit.Test
25+
26+
class PermalinkBuilderTests {
27+
28+
fun `building a permalink for an invalid user id throws when verifying the id`() {
29+
assertThrowsInDebug {
30+
val userId = UserId("some invalid user id")
31+
PermalinkBuilder.permalinkForUser(userId)
32+
}
33+
}
34+
35+
fun `building a permalink for an invalid room id throws when verifying the id`() {
36+
assertThrowsInDebug {
37+
val roomId = RoomId("some invalid room id")
38+
PermalinkBuilder.permalinkForRoomId(roomId)
39+
}
40+
}
41+
42+
@Test
43+
fun `building a permalink for an invalid user id returns failure when not verifying the id`() {
44+
if (!isInDebug()) {
45+
val userId = UserId("some invalid user id")
46+
assertThat(PermalinkBuilder.permalinkForUser(userId).isFailure).isTrue()
47+
}
48+
}
49+
50+
@Test
51+
fun `building a permalink for an invalid room id returns failure when not verifying the id`() {
52+
if (!isInDebug()) {
53+
val roomId = RoomId("some invalid room id")
54+
assertThat(PermalinkBuilder.permalinkForRoomId(roomId).isFailure).isTrue()
55+
}
56+
}
57+
58+
@Test
59+
fun `building a permalink for an invalid room alias returns failure`() {
60+
val roomAlias = "an invalid room alias"
61+
assertThat(PermalinkBuilder.permalinkForRoomAlias(roomAlias).isFailure).isTrue()
62+
}
63+
64+
@Test
65+
fun `building a permalink for a valid user id returns a matrix-to url`() {
66+
val userId = UserId("@user:matrix.org")
67+
assertThat(PermalinkBuilder.permalinkForUser(userId).getOrNull()).isEqualTo("https://matrix.to/#/@user:matrix.org")
68+
}
69+
70+
@Test
71+
fun `building a permalink for a valid room id returns a matrix-to url`() {
72+
val roomId = RoomId("!aBCdEFG1234:matrix.org")
73+
assertThat(PermalinkBuilder.permalinkForRoomId(roomId).getOrNull()).isEqualTo("https://matrix.to/#/!aBCdEFG1234:matrix.org")
74+
}
75+
76+
@Test
77+
fun `building a permalink for a valid room alias returns a matrix-to url`() {
78+
val roomAlias = "#room:matrix.org"
79+
assertThat(PermalinkBuilder.permalinkForRoomAlias(roomAlias).getOrNull()).isEqualTo("https://matrix.to/#/#room:matrix.org")
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
email = "[email protected]",
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.tests.testutils
18+
19+
/**
20+
* Returns true if the app is in debug mode.
21+
*/
22+
fun isInDebug() = BuildConfig.DEBUG

0 commit comments

Comments
 (0)