Skip to content
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

Bugfix: Removed moderator rights to edit posts; added test #119

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.IAnswerPost
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.IBasePost

data class PostActions(
Expand Down Expand Up @@ -46,24 +45,24 @@ fun rememberPostActions(
onRequestRetrySend,
clipboardManager
) {
if (post != null) {
val doesPostExistOnServer = post.serverPostId != null
val hasEditPostRights = hasModerationRights || post.authorId == clientId
val hasResolvePostRights = isAtLeastTutorInCourse || post.authorId == clientId

PostActions(
requestEditPost = if (doesPostExistOnServer && hasEditPostRights) onRequestEdit else null,
requestDeletePost = if (hasEditPostRights) onRequestDelete else null,
onClickReaction = if (doesPostExistOnServer) onClickReaction else null,
onCopyText = {
clipboardManager.setText(AnnotatedString(post.content.orEmpty()))
},
onReplyInThread = if (doesPostExistOnServer) onReplyInThread else null,
onResolvePost = if (hasResolvePostRights) onResolvePost else null,
onRequestRetrySend = onRequestRetrySend
)
} else {
PostActions()
if (post == null) {
return@remember PostActions()
}

val doesPostExistOnServer = post.serverPostId != null
val hasEditPostRights = post.authorId == clientId
val hasResolvePostRights = isAtLeastTutorInCourse || post.authorId == clientId

PostActions(
requestEditPost = if (doesPostExistOnServer && hasEditPostRights) onRequestEdit else null,
requestDeletePost = if (hasEditPostRights) onRequestDelete else null,
FelberMartin marked this conversation as resolved.
Show resolved Hide resolved
onClickReaction = if (doesPostExistOnServer) onClickReaction else null,
onCopyText = {
clipboardManager.setText(AnnotatedString(post.content.orEmpty()))
},
onReplyInThread = if (doesPostExistOnServer) onReplyInThread else null,
onResolvePost = if (hasResolvePostRights) onResolvePost else null,
onRequestRetrySend = onRequestRetrySend
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package de.tum.informatics.www1.artemis.native_app.feature.metis.conversation.ui.post

import androidx.compose.runtime.Composable
import androidx.test.ext.junit.runners.AndroidJUnit4
import de.tum.informatics.www1.artemis.native_app.core.common.test.UnitTest
import de.tum.informatics.www1.artemis.native_app.core.model.account.User
import de.tum.informatics.www1.artemis.native_app.core.test.BaseComposeTest
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.StandalonePost
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.experimental.categories.Category
import org.junit.runner.RunWith

@Category(UnitTest::class)
@RunWith(AndroidJUnit4::class)
class PostActionsTest : BaseComposeTest() {

private val EDIT_POST = {}

private val author = User(id = 1)
private val instructor = User(id = 2)

private val post = StandalonePost(
id = 1,
author = author
)

@Test
fun `test GIVEN a post WHEN calling rememberPostActions as the post author THEN onRequestEditPost is not null`() {
composeTestRule.setContent {
val postActions = createPostActions(
editorId = author.id,
hasModerationRights = false
)

assertNotNull(postActions.requestEditPost)
assertEquals(EDIT_POST, postActions.requestEditPost)
}
}


@Test
fun `test GIVEN a user with moderation-rights WHEN calling rememberPostActions THEN onRequestEditPost is null`() {
composeTestRule.setContent {
val postActions = createPostActions(
editorId = instructor.id,
hasModerationRights = true
)

assertNull(postActions.requestEditPost)
}
}


@Composable
private fun createPostActions(
editorId: Long,
hasModerationRights: Boolean = false,
): PostActions {
val postActions = rememberPostActions(
post = post,
hasModerationRights = hasModerationRights,
isAtLeastTutorInCourse = false,
clientId = editorId,
onRequestEdit = EDIT_POST,
onRequestDelete = {},
onClickReaction = { _, _ -> },
onReplyInThread = {},
onResolvePost = {},
onRequestRetrySend = {}
)
return postActions
}
}
Loading