This is an automated email from the ASF dual-hosted git repository.

aicam pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/texera.git


The following commit(s) were added to refs/heads/main by this push:
     new 39f8508742 feat: add test cases for revoking workflow access. (#4187)
39f8508742 is described below

commit 39f8508742c733ba4ba19e9cdaba0ffb7ee7b62b
Author: Seongjin Yoon <[email protected]>
AuthorDate: Mon Feb 9 15:55:27 2026 -0800

    feat: add test cases for revoking workflow access. (#4187)
    
    <!--
    Thanks for sending a pull request (PR)! Here are some tips for you:
    1. If this is your first time, please read our contributor guidelines:
    [Contributing to
    Texera](https://github.com/apache/texera/blob/main/CONTRIBUTING.md)
      2. Ensure you have added or run the appropriate tests for your PR
      3. If the PR is work in progress, mark it a draft on GitHub.
      4. Please write your PR title to summarize what this PR proposes, we
        are following Conventional Commits style for PR titles as well.
      5. Be sure to keep the PR description updated to reflect all changes.
    -->
    
    ### What changes were proposed in this PR?
    <!--
    Please clarify what changes you are proposing. The purpose of this
    section
    is to outline the changes. Here are some tips for you:
      1. If you propose a new API, clarify the use case for a new API.
      2. If you fix a bug, you can clarify why it is a bug.
      3. If it is a refactoring, clarify what has been changed.
      3. It would be helpful to include a before-and-after comparison using
         screenshots or GIFs.
      4. Please consider writing useful notes for better and faster reviews.
    -->
    
    This PR adds test cases for the `revokeAccess()` method in
    `WorkflowAccessResourceSpec.scala`.
    
    **The test cases cover the following scenarios:**
    - Users with WRITE access can revoke other users' access.
    - Users with READ access cannot revoke other users' access.
    - Users can revoke their own access regardless of access level.
    - Owner's access cannot be revoked by others.
    - Owner cannot revoke their own access.
    - Error handling for non-existing users.
    - Revoking access does not affect other users' access level.
    - Revoke access of a user who does not have access.
    
    ### Any related issues, documentation, discussions?
    <!--
    Please use this section to link other resources if not mentioned
    already.
    1. If this PR fixes an issue, please include `Fixes #1234`, `Resolves
    #1234`
    or `Closes #1234`. If it is only related, simply mention the issue
    number.
      2. If there is design documentation, please add the link.
      3. If there is a discussion in the mailing list, please add the link.
    -->
    
    #4143
    
    ### How was this PR tested?
    <!--
    If tests were added, say they were added here. Or simply mention that if
    the PR
    is tested with existing test cases. Make sure to include/update test
    cases that
    check the changes thoroughly including negative and positive cases if
    possible.
    If it was tested in a way different from regular unit tests, please
    clarify how
    you tested step by step, ideally copy and paste-able, so that other
    reviewers can
    test and check, and descendants can verify in the future. If tests were
    not added,
    please describe why they were not added and/or why it was difficult to
    add.
    -->
    
    `sbt test`
    
    ### Was this PR authored or co-authored using generative AI tooling?
    <!--
    If generative AI tooling has been used in the process of authoring this
    PR,
    please include the phrase: 'Generated-by: ' followed by the name of the
    tool
    and its version. If no, write 'No'.
    Please refer to the [ASF Generative Tooling
    Guidance](https://www.apache.org/legal/generative-tooling.html) for
    details.
    -->
    
    revised with Claude Code.
    
    Co-authored-by: ali risheh <[email protected]>
---
 .../user/workflow/WorkflowAccessResourceSpec.scala | 405 +++++++++++++++++++++
 1 file changed, 405 insertions(+)

diff --git 
a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResourceSpec.scala
 
b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResourceSpec.scala
new file mode 100644
index 0000000000..163f7b2683
--- /dev/null
+++ 
b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResourceSpec.scala
@@ -0,0 +1,405 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.texera.web.resource.dashboard.user.workflow
+
+import org.apache.texera.auth.SessionUser
+import org.apache.texera.dao.MockTexeraDB
+import org.apache.texera.dao.jooq.generated.Tables._
+import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum
+import org.apache.texera.dao.jooq.generated.tables.daos.{
+  UserDao,
+  WorkflowDao,
+  WorkflowOfUserDao,
+  WorkflowUserAccessDao
+}
+import org.apache.texera.dao.jooq.generated.tables.pojos.{
+  User,
+  Workflow,
+  WorkflowOfUser,
+  WorkflowUserAccess
+}
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
+
+import java.sql.Timestamp
+import javax.ws.rs.{BadRequestException, ForbiddenException}
+
+class WorkflowAccessResourceSpec
+    extends AnyFlatSpec
+    with BeforeAndAfterAll
+    with BeforeAndAfterEach
+    with MockTexeraDB {
+
+  private val ownerUid = 1000 + scala.util.Random.nextInt(1000)
+  private val userWithWriteUid = 2000 + scala.util.Random.nextInt(1000)
+  private val userWithReadUid = 3000 + scala.util.Random.nextInt(1000)
+  private val targetUserUid = 4000 + scala.util.Random.nextInt(1000)
+  private val testWorkflowWid = 5000 + scala.util.Random.nextInt(1000)
+
+  private var owner: User = _
+  private var userWithWrite: User = _
+  private var userWithRead: User = _
+  private var targetUser: User = _
+  private var testWorkflow: Workflow = _
+
+  private var userDao: UserDao = _
+  private var workflowDao: WorkflowDao = _
+  private var workflowOfUserDao: WorkflowOfUserDao = _
+  private var workflowUserAccessDao: WorkflowUserAccessDao = _
+  private var workflowAccessResource: WorkflowAccessResource = _
+
+  override protected def beforeAll(): Unit = {
+    initializeDBAndReplaceDSLContext()
+  }
+
+  override protected def beforeEach(): Unit = {
+    // Initialize DAOs
+    userDao = new UserDao(getDSLContext.configuration())
+    workflowDao = new WorkflowDao(getDSLContext.configuration())
+    workflowOfUserDao = new WorkflowOfUserDao(getDSLContext.configuration())
+    workflowUserAccessDao = new 
WorkflowUserAccessDao(getDSLContext.configuration())
+    workflowAccessResource = new WorkflowAccessResource()
+
+    // Create test users
+    owner = new User
+    owner.setUid(ownerUid)
+    owner.setName("owner")
+    owner.setEmail("[email protected]")
+    owner.setPassword("password")
+
+    userWithWrite = new User
+    userWithWrite.setUid(userWithWriteUid)
+    userWithWrite.setName("user_with_write")
+    userWithWrite.setEmail("[email protected]")
+    userWithWrite.setPassword("password")
+
+    userWithRead = new User
+    userWithRead.setUid(userWithReadUid)
+    userWithRead.setName("user_with_read")
+    userWithRead.setEmail("[email protected]")
+    userWithRead.setPassword("password")
+
+    targetUser = new User
+    targetUser.setUid(targetUserUid)
+    targetUser.setName("target_user")
+    targetUser.setEmail("[email protected]")
+    targetUser.setPassword("password")
+
+    // Create test workflow
+    testWorkflow = new Workflow
+    testWorkflow.setWid(testWorkflowWid)
+    testWorkflow.setName("test_workflow")
+    testWorkflow.setContent("{}")
+    testWorkflow.setDescription("test description")
+    testWorkflow.setCreationTime(new Timestamp(System.currentTimeMillis()))
+    testWorkflow.setLastModifiedTime(new Timestamp(System.currentTimeMillis()))
+
+    // Clean up before each test
+    cleanupTestData()
+
+    // Insert test data
+    userDao.insert(owner)
+    userDao.insert(userWithWrite)
+    userDao.insert(userWithRead)
+    userDao.insert(targetUser)
+    workflowDao.insert(testWorkflow)
+
+    // Set up workflow ownership
+    val workflowOfUser = new WorkflowOfUser
+    workflowOfUser.setUid(ownerUid)
+    workflowOfUser.setWid(testWorkflowWid)
+    workflowOfUserDao.insert(workflowOfUser)
+
+    // Grant write access to userWithWrite
+    val writeAccess = new WorkflowUserAccess
+    writeAccess.setUid(userWithWriteUid)
+    writeAccess.setWid(testWorkflowWid)
+    writeAccess.setPrivilege(PrivilegeEnum.WRITE)
+    workflowUserAccessDao.insert(writeAccess)
+
+    // Grant read access to userWithRead
+    val readAccess = new WorkflowUserAccess
+    readAccess.setUid(userWithReadUid)
+    readAccess.setWid(testWorkflowWid)
+    readAccess.setPrivilege(PrivilegeEnum.READ)
+    workflowUserAccessDao.insert(readAccess)
+
+    // Grant write access to targetUser
+    val targetAccess = new WorkflowUserAccess
+    targetAccess.setUid(targetUserUid)
+    targetAccess.setWid(testWorkflowWid)
+    targetAccess.setPrivilege(PrivilegeEnum.WRITE)
+    workflowUserAccessDao.insert(targetAccess)
+  }
+
+  override protected def afterEach(): Unit = {
+    cleanupTestData()
+  }
+
+  private def cleanupTestData(): Unit = {
+    getDSLContext
+      .deleteFrom(WORKFLOW_USER_ACCESS)
+      .where(WORKFLOW_USER_ACCESS.WID.eq(testWorkflowWid))
+      .execute()
+
+    getDSLContext
+      .deleteFrom(WORKFLOW_OF_USER)
+      .where(WORKFLOW_OF_USER.WID.eq(testWorkflowWid))
+      .execute()
+
+    getDSLContext
+      .deleteFrom(WORKFLOW)
+      .where(WORKFLOW.WID.eq(testWorkflowWid))
+      .execute()
+
+    getDSLContext
+      .deleteFrom(USER)
+      .where(
+        USER.UID.in(ownerUid, userWithWriteUid, userWithReadUid, targetUserUid)
+      )
+      .execute()
+  }
+
+  override protected def afterAll(): Unit = {
+    shutdownDB()
+  }
+
+  "WorkflowAccessResource.revokeAccess" should "successfully revoke access 
when user has WRITE permission" in {
+    val sessionUser = new SessionUser(userWithWrite)
+
+    // Verify target user has access before revocation
+    val accessBefore = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(targetUserUid)
+          )
+      )
+      .fetchOne()
+    assert(accessBefore != null, "Target user should have access before 
revocation")
+
+    // Revoke access
+    workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+
+    // Verify access has been revoked
+    val accessAfter = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(targetUserUid)
+          )
+      )
+      .fetchOne()
+
+    assert(accessAfter == null, "Target user's access should be revoked")
+  }
+
+  it should "successfully allow user to revoke their own access" in {
+    val sessionUser = new SessionUser(userWithRead)
+
+    // Verify user has access before revocation
+    val accessBefore = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(userWithReadUid)
+          )
+      )
+      .fetchOne()
+    assert(accessBefore != null, "User should have access before revocation")
+
+    // User revokes their own access
+    workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+
+    // Verify access has been revoked
+    val accessAfter = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(userWithReadUid)
+          )
+      )
+      .fetchOne()
+
+    assert(accessAfter == null, "User's own access should be revoked")
+  }
+
+  it should "throw ForbiddenException when user without WRITE permission tries 
to revoke others' access" in {
+    val sessionUser = new SessionUser(userWithRead)
+
+    assertThrows[ForbiddenException] {
+      workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+    }
+
+    // Verify target user's access is still intact
+    val access = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(targetUserUid)
+          )
+      )
+      .fetchOne()
+
+    assert(access != null, "Target user's access should remain intact")
+  }
+
+  it should "throw ForbiddenException when trying to revoke owner's access" in 
{
+    val sessionUser = new SessionUser(userWithWrite)
+
+    val exception = intercept[ForbiddenException] {
+      workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+    }
+
+    assert(
+      exception.getMessage.contains("owner cannot revoke their own access"),
+      "Exception message should indicate owner cannot revoke their own access"
+    )
+  }
+
+  it should "throw ForbiddenException when owner tries to revoke their own 
access" in {
+    val sessionUser = new SessionUser(owner)
+
+    val exception = intercept[ForbiddenException] {
+      workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+    }
+
+    assert(
+      exception.getMessage.contains("owner cannot revoke their own access"),
+      "Exception message should indicate owner cannot revoke their own access"
+    )
+  }
+
+  it should "throw BadRequestException when email does not exist" in {
+    val sessionUser = new SessionUser(userWithWrite)
+
+    assertThrows[BadRequestException] {
+      workflowAccessResource.revokeAccess(
+        testWorkflowWid,
+        "[email protected]",
+        sessionUser
+      )
+    }
+  }
+
+  it should "not affect other users' access when revoking one user's access" 
in {
+    val sessionUser = new SessionUser(userWithWrite)
+
+    // Verify both users have access before revocation
+    val readAccessBefore = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(userWithReadUid)
+          )
+      )
+      .fetchOne()
+    assert(readAccessBefore != null, "Read user should have access before 
revocation")
+
+    val targetAccessBefore = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(targetUserUid)
+          )
+      )
+      .fetchOne()
+    assert(targetAccessBefore != null, "Target user should have access before 
revocation")
+
+    // Revoke only target user's access
+    workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+
+    // Verify read user's access is still intact
+    val readAccessAfter = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(userWithReadUid)
+          )
+      )
+      .fetchOne()
+    assert(readAccessAfter != null, "Read user's access should remain intact")
+
+    // Verify target user's access has been revoked
+    val targetAccessAfter = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(targetUserUid)
+          )
+      )
+      .fetchOne()
+    assert(targetAccessAfter == null, "Target user's access should be revoked")
+  }
+
+  it should "handle revoking access for a user who already has no access 
gracefully" in {
+    val sessionUser = new SessionUser(userWithWrite)
+
+    // First revocation
+    workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+
+    // Verify access has been revoked
+    val accessAfterFirst = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(targetUserUid)
+          )
+      )
+      .fetchOne()
+    assert(accessAfterFirst == null, "Target user's access should be revoked")
+
+    // Second revocation attempt (should not throw an error, just do nothing)
+    workflowAccessResource.revokeAccess(testWorkflowWid, "[email protected]", 
sessionUser)
+
+    // Verify access is still revoked
+    val accessAfterSecond = getDSLContext
+      .selectFrom(WORKFLOW_USER_ACCESS)
+      .where(
+        WORKFLOW_USER_ACCESS.WID
+          .eq(testWorkflowWid)
+          .and(
+            WORKFLOW_USER_ACCESS.UID.eq(targetUserUid)
+          )
+      )
+      .fetchOne()
+    assert(accessAfterSecond == null, "Target user's access should still be 
revoked")
+  }
+}

Reply via email to