Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions app/src/jvmE2eTest/kotlin/LemmyV1AccountE2eTest.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import it.vercruysse.lemmyapi.datatypes.ChangePassword
import it.vercruysse.lemmyapi.datatypes.DeleteAccount
import it.vercruysse.lemmyapi.datatypes.Login
import it.vercruysse.lemmyapi.datatypes.SaveUserSettings
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -82,4 +84,57 @@ class LemmyV1AccountE2eTest : LemmyV1E2eTest() {
runBlocking {
assertEquals(0, newUser().api.getUnreadCounts().getOrThrow().notification_count)
}

@Test
fun `user can change its password`() =
runBlocking<Unit> {
val changer = newUser()

// Lemmy's JWT only differs by user, issue second and expiry, so a login in the same second as the
// password change would produce the same token and fail on the unique login_token key.
// stay_logged_in gives this token another expiry, so the login after it gets a different token.
val changed =
changer.api.changePassword(
ChangePassword("e2e-new-password", "e2e-new-password", USER_PASSWORD, stay_logged_in = true),
).getOrThrow()
assertNotNull(changed.jwt)
assertTrue(anonApi.login(Login(changer.username, USER_PASSWORD)).isFailure)
assertTrue(anonApi.login(Login(changer.username, "e2e-new-password")).isSuccess)
}

@Test
fun `user can delete its account`() =
runBlocking<Unit> {
val deleter = newUser()

deleter.api.deleteAccount(DeleteAccount(USER_PASSWORD, delete_content = true)).getOrThrow()

// Lemmy 1.0 still accepts a login of a deleted account, but its existing tokens are revoked
assertTrue(deleter.api.getMyUser().isFailure)
}

@Test
fun `logins of the user are listed`() =
runBlocking<Unit> {
assertTrue(newUser().api.listLogins().getOrThrow().logins.isNotEmpty())
}

@Test
fun `exported user settings can be imported by another user`() =
runBlocking<Unit> {
val exporter = newUser()
val importer = newUser()
exporter.api.saveUserSettings(SaveUserSettings(bio = "e2e exported bio")).getOrThrow()

val exported = exporter.api.exportUserSettings().getOrThrow()
importer.api.importUserSettings(exported).getOrThrow()

eventually { assertEquals("e2e exported bio", importer.api.getMyUser().getOrThrow().local_user_view.person.bio) }
}

@Test
fun `donation dialog can be marked as shown`() =
runBlocking<Unit> {
newUser().api.markDonationDialogShown().getOrThrow()
}
}
54 changes: 54 additions & 0 deletions app/src/jvmE2eTest/kotlin/LemmyV1CommentsE2eTest.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import it.vercruysse.lemmyapi.datatypes.CreateCommentLike
import it.vercruysse.lemmyapi.datatypes.CreateCommentReport
import it.vercruysse.lemmyapi.datatypes.CreateCommentWarning
import it.vercruysse.lemmyapi.datatypes.DeleteComment
import it.vercruysse.lemmyapi.datatypes.DistinguishComment
import it.vercruysse.lemmyapi.datatypes.EditComment
import it.vercruysse.lemmyapi.datatypes.GetComment
import it.vercruysse.lemmyapi.datatypes.GetComments
import it.vercruysse.lemmyapi.datatypes.ListCommentLikes
import it.vercruysse.lemmyapi.datatypes.LockComment
import it.vercruysse.lemmyapi.datatypes.SaveComment
import it.vercruysse.lemmyapi.enums.CommentSortType
import it.vercruysse.lemmyapi.enums.VoteAction
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -133,4 +138,53 @@ class LemmyV1CommentsE2eTest : LemmyV1E2eTest() {

assertEquals("E2E report", report.comment_report_view.comment_report.reason)
}

@Test
fun `slim comment list returns the seeded comment`() =
runBlocking<Unit> {
val comments = anonApi.getCommentsSlim(GetComments(post_id = postId)).getOrThrow().items

assertTrue(comments.any { it.comment.id == commentId && it.comment.content == SEED_COMMENT_CONTENT })
}

@Test
fun `moderator can distinguish its comment`() =
runBlocking<Unit> {
val comment = adminApi.newComment()

val distinguished = adminApi.distinguishComment(DistinguishComment(comment.comment.id, true)).getOrThrow()

assertTrue(distinguished.comment_view.comment.distinguished)
}

@Test
fun `moderator can lock and unlock a comment`() =
runBlocking<Unit> {
val id = user.api.newComment().comment.id

assertTrue(adminApi.lockComment(LockComment(id, true, "e2e lock")).getOrThrow().comment_view.comment.locked)
assertFalse(adminApi.lockComment(LockComment(id, false, "e2e unlock")).getOrThrow().comment_view.comment.locked)
}

@Test
fun `moderator can warn a comment`() =
runBlocking<Unit> {
val comment = user.api.newComment()

val warned = adminApi.warnComment(CreateCommentWarning(comment.comment.id, "e2e warning")).getOrThrow()

assertEquals(comment.comment.id, warned.comment_view.comment.id)
}

@Test
fun `admin can list the votes on a comment`() =
runBlocking<Unit> {
val voter = newUser()
val comment = adminApi.newComment()
voter.api.createCommentLike(CreateCommentLike(comment.comment.id, VoteAction.DownVote)).getOrThrow()

val likes = adminApi.listCommentLikes(ListCommentLikes(comment.comment.id)).getOrThrow().items

assertEquals(listOf(voter.personId), likes.filter { it.vote == VoteAction.DownVote }.map { it.creator.id })
}
}
157 changes: 157 additions & 0 deletions app/src/jvmE2eTest/kotlin/LemmyV1CommunityManagementE2eTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import it.vercruysse.lemmyapi.datatypes.AddModToCommunity
import it.vercruysse.lemmyapi.datatypes.ApproveCommunityPendingFollower
import it.vercruysse.lemmyapi.datatypes.CommunityIdQuery
import it.vercruysse.lemmyapi.datatypes.CreateCommunity
import it.vercruysse.lemmyapi.datatypes.CreateCommunityTag
import it.vercruysse.lemmyapi.datatypes.DeleteCommunity
import it.vercruysse.lemmyapi.datatypes.DeleteCommunityTag
import it.vercruysse.lemmyapi.datatypes.EditCommunity
import it.vercruysse.lemmyapi.datatypes.EditCommunityNotifications
import it.vercruysse.lemmyapi.datatypes.EditCommunityTag
import it.vercruysse.lemmyapi.datatypes.FollowCommunity
import it.vercruysse.lemmyapi.datatypes.GetCommunity
import it.vercruysse.lemmyapi.datatypes.GetRandomCommunity
import it.vercruysse.lemmyapi.datatypes.ListCommunityPendingFollows
import it.vercruysse.lemmyapi.datatypes.RemoveCommunity
import it.vercruysse.lemmyapi.datatypes.TransferCommunity
import it.vercruysse.lemmyapi.enums.CommunityFollowerState
import it.vercruysse.lemmyapi.enums.CommunityNotificationsMode
import it.vercruysse.lemmyapi.enums.CommunityVisibility
import it.vercruysse.lemmyapi.enums.ListingType
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

class LemmyV1CommunityManagementE2eTest : LemmyV1E2eTest() {
@Test
fun `moderator can edit a community`() =
runBlocking<Unit> {
val owner = newUser()
val community = owner.api.newCommunity()

val edited =
owner.api.editCommunity(EditCommunity(community, title = "Edited title", summary = "Edited summary"))
.getOrThrow().community_view.community

assertEquals("Edited title", edited.title)
assertEquals("Edited summary", anonApi.getCommunity(GetCommunity(id = community)).getOrThrow().community_view.community.summary)
}

@Test
fun `owner can delete and restore a community`() =
runBlocking<Unit> {
val owner = newUser()
val community = owner.api.newCommunity()

assertTrue(owner.api.deleteCommunity(DeleteCommunity(community, true)).getOrThrow().community_view.community.deleted)
assertFalse(owner.api.deleteCommunity(DeleteCommunity(community, false)).getOrThrow().community_view.community.deleted)
}

@Test
fun `admin can remove and restore a community`() =
runBlocking<Unit> {
val community = newUser().api.newCommunity()

assertTrue(
adminApi.removeCommunity(RemoveCommunity(community, true, "e2e remove")).getOrThrow().community_view.community.removed,
)
assertFalse(
adminApi.removeCommunity(RemoveCommunity(community, false, "e2e restore")).getOrThrow().community_view.community.removed,
)
}

@Test
fun `moderator can be added and the community transferred`() =
runBlocking<Unit> {
val owner = newUser()
val mod = newUser()
val community = owner.api.newCommunity()

val mods = owner.api.addModToCommunity(AddModToCommunity(community, mod.personId, added = true)).getOrThrow().moderators
assertEquals(listOf(owner.personId, mod.personId), mods.map { it.moderator.id })

val transferred = owner.api.transferCommunity(TransferCommunity(community, mod.personId)).getOrThrow()
assertEquals(mod.personId, transferred.moderators.first().moderator.id)
}

@Test
fun `random community returns a local community`() =
runBlocking<Unit> {
val community = anonApi.getRandomCommunity(GetRandomCommunity(type_ = ListingType.Local)).getOrThrow().community_view

assertTrue(community.community.local)
}

@Test
fun `user can change the notifications of a community`() =
runBlocking<Unit> {
val follower = newUser()
val community = adminApi.newCommunity()
follower.api.followCommunity(FollowCommunity(community, follow = true)).getOrThrow()

follower.api.editCommunityNotifications(EditCommunityNotifications(community, CommunityNotificationsMode.Mute)).getOrThrow()

val actions = follower.api.getCommunity(GetCommunity(id = community)).getOrThrow().community_view.community_actions
assertEquals(CommunityNotificationsMode.Mute, actions?.notifications)
}

@Test
fun `community tags can be created, edited and deleted`() =
runBlocking<Unit> {
val community = adminApi.newCommunity()

val tag = adminApi.createCommunityTag(CreateCommunityTag(community, name = unique("e2etag"), display_name = "E2E tag"))
.getOrThrow()
assertEquals(listOf(tag.id), adminApi.getCommunity(GetCommunity(id = community)).getOrThrow().community_view.tags.map { it.id })

val edited = adminApi.editCommunityTag(EditCommunityTag(tag.id, display_name = "Edited tag")).getOrThrow()
assertEquals("Edited tag", edited.display_name)

assertTrue(adminApi.deleteCommunityTag(DeleteCommunityTag(tag.id, delete = true)).getOrThrow().deleted)
}

@Test
fun `pending follow of a private community can be approved`() =
runBlocking<Unit> {
val owner = newUser()
val follower = newUser()
val community =
owner.api.createCommunity(
CreateCommunity(name = unique("e2epriv"), title = "E2E private", visibility = CommunityVisibility.Private),
).getOrThrow().community_view.community.id

follower.api.followCommunity(FollowCommunity(community, follow = true)).getOrThrow()

val pending = owner.api.listCommunityPendingFollows(ListCommunityPendingFollows()).getOrThrow().items
assertEquals(listOf(follower.personId), pending.filter { it.community.id == community }.map { it.person.id })

owner.api.approveCommunityPendingFollow(ApproveCommunityPendingFollower(community, follower.personId, approve = true))
.getOrThrow()

val state = follower.api.getCommunity(GetCommunity(id = community)).getOrThrow().community_view.community_actions?.follow_state
assertEquals(CommunityFollowerState.Accepted, state)
}

@Test
fun `moderator can upload and delete the community icon and banner`() =
runBlocking<Unit> {
val owner = newUser()
val community = owner.api.newCommunity()
val query = CommunityIdQuery(community)

val icon = owner.api.uploadCommunityIcon(PNG, query).getOrThrow()
val banner = owner.api.uploadCommunityBanner(PNG, query).getOrThrow()
val uploaded = anonApi.getCommunity(GetCommunity(id = community)).getOrThrow().community_view.community
assertEquals(icon.image_url, uploaded.icon)
assertEquals(banner.image_url, uploaded.banner)

owner.api.deleteCommunityIcon(query).getOrThrow()
owner.api.deleteCommunityBanner(query).getOrThrow()
val deleted = anonApi.getCommunity(GetCommunity(id = community)).getOrThrow().community_view.community
assertNull(deleted.icon)
assertNull(deleted.banner)
}
}
7 changes: 7 additions & 0 deletions app/src/jvmE2eTest/kotlin/LemmyV1E2eTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import it.vercruysse.lemmyapi.datatypes.CreatePost
import it.vercruysse.lemmyapi.datatypes.PagedResponse
import it.vercruysse.lemmyapi.datatypes.PaginationCursor
import it.vercruysse.lemmyapi.datatypes.PostView
import java.util.Base64
import java.util.concurrent.atomic.AtomicInteger

abstract class LemmyV1E2eTest {
Expand Down Expand Up @@ -71,6 +72,12 @@ abstract class LemmyV1E2eTest {
const val SEED_COMMENT_CONTENT = LemmyV1E2eEnvironment.SEED_COMMENT_CONTENT
const val SEED_PAGING_COUNT = LemmyV1E2eEnvironment.SEED_PAGING_COUNT

/** 1x1 transparent PNG */
val PNG: ByteArray =
Base64.getDecoder().decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==",
)

private val uniqueCounter = AtomicInteger()

fun unique(prefix: String): String = "$prefix${uniqueCounter.incrementAndGet()}"
Expand Down
43 changes: 35 additions & 8 deletions app/src/jvmE2eTest/kotlin/LemmyV1MediaE2eTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import it.vercruysse.lemmyapi.datatypes.DeleteImageParams
import it.vercruysse.lemmyapi.datatypes.ListMedia
import kotlinx.coroutines.runBlocking
import java.util.Base64
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

class LemmyV1MediaE2eTest : LemmyV1E2eTest() {
Expand Down Expand Up @@ -71,11 +71,38 @@ class LemmyV1MediaE2eTest : LemmyV1E2eTest() {
assertTrue(result.isFailure)
}

private companion object {
// 1x1 transparent PNG
val PNG: ByteArray =
Base64.getDecoder().decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==",
)
}
@Test
fun `user can delete its avatar and banner`() =
runBlocking<Unit> {
val uploader = newUser()
uploader.api.uploadUserAvatar(PNG).getOrThrow()
uploader.api.uploadUserBanner(PNG).getOrThrow()

uploader.api.deleteUserAvatar().getOrThrow()
uploader.api.deleteUserBanner().getOrThrow()

val person = uploader.api.getMyUser().getOrThrow().local_user_view.person
assertNull(person.avatar)
assertNull(person.banner)
}

@Test
fun `admin can list and delete the images of other users`() =
runBlocking<Unit> {
val uploader = newUser()
val upload = uploader.api.uploadImage(PNG).getOrThrow()

val all = fetchPages { cursor -> adminApi.listMediaAdmin(ListMedia(page_cursor = cursor)) }.flatten()
assertTrue(all.any { it.local_image.pictrs_alias == upload.filename })

adminApi.deleteMediaAdmin(DeleteImageParams(upload.filename)).getOrThrow()

assertTrue(uploader.api.listMedia(ListMedia()).getOrThrow().items.isEmpty())
}

@Test
fun `image server is healthy`() =
runBlocking<Unit> {
anonApi.imageHealth().getOrThrow()
}
}
Loading
Loading