-
Notifications
You must be signed in to change notification settings - Fork 233
Improve coverage in permalinks package #1502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...c/test/kotlin/io/element/android/libraries/matrix/api/permalink/MatrixToConverterTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2023 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.element.android.libraries.matrix.api.permalink | ||
|
||
import android.net.Uri | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class MatrixToConverterTests { | ||
|
||
@Test | ||
fun `converting a matrix-to url does nothing`() { | ||
val url = Uri.parse("https://matrix.to/#/#element-android:matrix.org") | ||
assertThat(MatrixToConverter.convert(url)).isEqualTo(url) | ||
} | ||
|
||
@Test | ||
fun `converting a url with a supported room path returns a matrix-to url`() { | ||
val url = Uri.parse("https://riot.im/develop/#/room/#element-android:matrix.org") | ||
assertThat(MatrixToConverter.convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/#element-android:matrix.org")) | ||
} | ||
|
||
@Test | ||
fun `converting a url with a supported user path returns a matrix-to url`() { | ||
val url = Uri.parse("https://riot.im/develop/#/user/@test:matrix.org") | ||
assertThat(MatrixToConverter.convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/@test:matrix.org")) | ||
} | ||
|
||
@Test | ||
fun `converting a url with a supported group path returns a matrix-to url`() { | ||
val url = Uri.parse("https://riot.im/develop/#/group/+group:matrix.org") | ||
assertThat(MatrixToConverter.convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/+group:matrix.org")) | ||
} | ||
|
||
@Test | ||
fun `converting an unsupported url returns null`() { | ||
val url = Uri.parse("https://element.io/") | ||
assertThat(MatrixToConverter.convert(url)).isNull() | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...rc/test/kotlin/io/element/android/libraries/matrix/api/permalink/PermalinkBuilderTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2023 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.element.android.libraries.matrix.api.permalink | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import io.element.android.libraries.matrix.api.core.RoomId | ||
import io.element.android.libraries.matrix.api.core.UserId | ||
import io.element.android.tests.testutils.assertThrowsInDebug | ||
import io.element.android.tests.testutils.isInDebug | ||
import org.junit.Test | ||
|
||
class PermalinkBuilderTests { | ||
|
||
fun `building a permalink for an invalid user id throws when verifying the id`() { | ||
assertThrowsInDebug { | ||
val userId = UserId("some invalid user id") | ||
PermalinkBuilder.permalinkForUser(userId) | ||
} | ||
} | ||
|
||
fun `building a permalink for an invalid room id throws when verifying the id`() { | ||
assertThrowsInDebug { | ||
val roomId = RoomId("some invalid room id") | ||
PermalinkBuilder.permalinkForRoomId(roomId) | ||
} | ||
} | ||
|
||
@Test | ||
fun `building a permalink for an invalid user id returns failure when not verifying the id`() { | ||
if (!isInDebug()) { | ||
val userId = UserId("some invalid user id") | ||
assertThat(PermalinkBuilder.permalinkForUser(userId).isFailure).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun `building a permalink for an invalid room id returns failure when not verifying the id`() { | ||
if (!isInDebug()) { | ||
val roomId = RoomId("some invalid room id") | ||
assertThat(PermalinkBuilder.permalinkForRoomId(roomId).isFailure).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun `building a permalink for an invalid room alias returns failure`() { | ||
val roomAlias = "an invalid room alias" | ||
assertThat(PermalinkBuilder.permalinkForRoomAlias(roomAlias).isFailure).isTrue() | ||
} | ||
|
||
@Test | ||
fun `building a permalink for a valid user id returns a matrix-to url`() { | ||
val userId = UserId("@user:matrix.org") | ||
assertThat(PermalinkBuilder.permalinkForUser(userId).getOrNull()).isEqualTo("https://matrix.to/#/@user:matrix.org") | ||
} | ||
|
||
@Test | ||
fun `building a permalink for a valid room id returns a matrix-to url`() { | ||
val roomId = RoomId("!aBCdEFG1234:matrix.org") | ||
assertThat(PermalinkBuilder.permalinkForRoomId(roomId).getOrNull()).isEqualTo("https://matrix.to/#/!aBCdEFG1234:matrix.org") | ||
} | ||
|
||
@Test | ||
fun `building a permalink for a valid room alias returns a matrix-to url`() { | ||
val roomAlias = "#room:matrix.org" | ||
assertThat(PermalinkBuilder.permalinkForRoomAlias(roomAlias).getOrNull()).isEqualTo("https://matrix.to/#/#room:matrix.org") | ||
} | ||
} |
167 changes: 167 additions & 0 deletions
167
...src/test/kotlin/io/element/android/libraries/matrix/api/permalink/PermalinkParserTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Copyright (c) 2023 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.element.android.libraries.matrix.api.permalink | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class PermalinkParserTests { | ||
|
||
@Test | ||
fun `parsing an invalid url returns a fallback link`() { | ||
val url = "https://element.io" | ||
assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) | ||
} | ||
|
||
@Test | ||
fun `parsing an invalid url with the right path but no content returns a fallback link`() { | ||
val url = "https://app.element.io/#/user" | ||
assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) | ||
} | ||
|
||
@Test | ||
fun `parsing an invalid url with the right path but empty content returns a fallback link`() { | ||
val url = "https://app.element.io/#/user/" | ||
assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) | ||
} | ||
|
||
@Test | ||
fun `parsing an invalid url with the right path but invalid content returns a fallback link`() { | ||
val url = "https://app.element.io/#/user/some%20user!" | ||
assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) | ||
} | ||
|
||
@Test | ||
fun `parsing a valid user url returns a user link`() { | ||
val url = "https://app.element.io/#/user/@test:matrix.org" | ||
assertThat(PermalinkParser.parse(url)).isEqualTo( | ||
PermalinkData.UserLink( | ||
userId = "@test:matrix.org" | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `parsing a valid room id url returns a room link`() { | ||
val url = "https://app.element.io/#/room/!aBCD1234:matrix.org" | ||
assertThat(PermalinkParser.parse(url)).isEqualTo( | ||
PermalinkData.RoomLink( | ||
roomIdOrAlias = "!aBCD1234:matrix.org", | ||
isRoomAlias = false, | ||
eventId = null, | ||
viaParameters = emptyList(), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `parsing a valid room id with event id url returns a room link`() { | ||
val url = "https://app.element.io/#/room/!aBCD1234:matrix.org/$1234567890abcdef:matrix.org" | ||
assertThat(PermalinkParser.parse(url)).isEqualTo( | ||
PermalinkData.RoomLink( | ||
roomIdOrAlias = "!aBCD1234:matrix.org", | ||
isRoomAlias = false, | ||
eventId = "\$1234567890abcdef:matrix.org", | ||
viaParameters = emptyList(), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `parsing a valid room id with and invalid event id url returns a room link with no event id`() { | ||
val url = "https://app.element.io/#/room/!aBCD1234:matrix.org/1234567890abcdef:matrix.org" | ||
assertThat(PermalinkParser.parse(url)).isEqualTo( | ||
PermalinkData.RoomLink( | ||
roomIdOrAlias = "!aBCD1234:matrix.org", | ||
isRoomAlias = false, | ||
eventId = null, | ||
viaParameters = emptyList(), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `parsing a valid room id with event id and via parameters url returns a room link`() { | ||
val url = "https://app.element.io/#/room/!aBCD1234:matrix.org/$1234567890abcdef:matrix.org?via=matrix.org&via=matrix.com" | ||
assertThat(PermalinkParser.parse(url)).isEqualTo( | ||
PermalinkData.RoomLink( | ||
roomIdOrAlias = "!aBCD1234:matrix.org", | ||
isRoomAlias = false, | ||
eventId = "\$1234567890abcdef:matrix.org", | ||
viaParameters = listOf("matrix.org", "matrix.com"), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `parsing a valid room alias url returns a room link`() { | ||
val url = "https://app.element.io/#/room/#element-android:matrix.org" | ||
assertThat(PermalinkParser.parse(url)).isEqualTo( | ||
PermalinkData.RoomLink( | ||
roomIdOrAlias = "#element-android:matrix.org", | ||
isRoomAlias = true, | ||
eventId = null, | ||
viaParameters = emptyList(), | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `parsing a url with an invalid signurl returns a fallback link`() { | ||
// This url has no private key | ||
val url = "https://app.element.io/#/room/%21aBCDEF12345%3Amatrix.org" + | ||
"?email=testuser%40element.io" + | ||
"&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3Da_token" + | ||
"&room_name=TestRoom" + | ||
"&room_avatar_url=" + | ||
"&inviter_name=User" + | ||
"&guest_access_token=" + | ||
"&guest_user_id=" + | ||
"&room_type=" | ||
assertThat(PermalinkParser.parse(url)).isInstanceOf(PermalinkData.FallbackLink::class.java) | ||
} | ||
|
||
@Test | ||
fun `parsing a url with signurl returns a room email invite link`() { | ||
val url = "https://app.element.io/#/room/%21aBCDEF12345%3Amatrix.org" + | ||
"?email=testuser%40element.io" + | ||
"&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3Da_token%26private_key%3Da_private_key" + | ||
"&room_name=TestRoom" + | ||
"&room_avatar_url=" + | ||
"&inviter_name=User" + | ||
"&guest_access_token=" + | ||
"&guest_user_id=" + | ||
"&room_type=" | ||
assertThat(PermalinkParser.parse(url)).isEqualTo( | ||
PermalinkData.RoomEmailInviteLink( | ||
roomId = "!aBCDEF12345:matrix.org", | ||
email = "[email protected]", | ||
signUrl = "https://vector.im/_matrix/identity/api/v1/sign-ed25519?token=a_token&private_key=a_private_key", | ||
roomName = "TestRoom", | ||
roomAvatarUrl = "", | ||
inviterName = "User", | ||
identityServer = "vector.im", | ||
token = "a_token", | ||
privateKey = "a_private_key", | ||
roomType = "", | ||
) | ||
) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/testutils/src/main/kotlin/io/element/android/tests/testutils/IsInDebug.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2023 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.element.android.tests.testutils | ||
|
||
/** | ||
* Returns true if the app is in debug mode. | ||
*/ | ||
fun isInDebug() = BuildConfig.DEBUG | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I call "API renaming", I used to do quite a good amount of this in another codebase and then started wondering if I really should:
BuildConfig.DEBUG
is but nobody knows whatisInDebug()
so it could potentially do anything).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just did this because
BuildConfig.DEBUG
didn't seem to be accessible in unit tests. Maybe by generating resources it would work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BuildConfig
generation is optional since AGP8, maybe that's the issue?But even better question, why do you need BuildConfig in a unit test, what are you trying to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the
BuildConfig
generation is already enabled for that module. As for why I need this, it's becauseUserId("foo")
will crash in debug mode so we can track invalid ids but will work fine in release mode. Since we have some code that needs to check for invalid ids in release mode in these classes we need to take into account both cases.Another option would be to remove this check.