carloea2 commented on code in PR #5156:
URL: https://github.com/apache/texera/pull/5156#discussion_r3292314777


##########
amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/project/ProjectAccessResourceSpec.scala:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.project
+
+import org.apache.texera.auth.SessionUser
+import org.apache.texera.dao.MockTexeraDB
+import org.apache.texera.dao.jooq.generated.Tables.{PROJECT, 
PROJECT_USER_ACCESS, USER}
+import org.apache.texera.dao.jooq.generated.enums.{PrivilegeEnum, UserRoleEnum}
+import org.apache.texera.dao.jooq.generated.tables.daos.{ProjectUserAccessDao, 
UserDao}
+import org.apache.texera.dao.jooq.generated.tables.pojos.{ProjectUserAccess, 
User}
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
+
+class ProjectAccessResourceSpec
+    extends AnyFlatSpec
+    with BeforeAndAfterAll
+    with BeforeAndAfterEach
+    with MockTexeraDB {
+
+  private val ownerUid = 7101
+  private val readerUid = 7102
+  private val noAccessUid = 7103
+
+  private var owner: User = _
+  private var reader: User = _
+  private var noAccessUser: User = _
+  private var userDao: UserDao = _
+  private var projectUserAccessDao: ProjectUserAccessDao = _
+  private var projectResource: ProjectResource = _
+
+  override protected def beforeAll(): Unit = {
+    initializeDBAndReplaceDSLContext()
+  }
+
+  override protected def beforeEach(): Unit = {
+    userDao = new UserDao(getDSLContext.configuration())
+    projectUserAccessDao = new 
ProjectUserAccessDao(getDSLContext.configuration())
+    projectResource = new ProjectResource()
+
+    owner = createUser(ownerUid, "project_owner", "[email protected]")
+    reader = createUser(readerUid, "project_reader", "[email protected]")
+    noAccessUser = createUser(noAccessUid, "project_no_access", 
"[email protected]")
+
+    cleanupTestData()
+
+    userDao.insert(owner)
+    userDao.insert(reader)
+    userDao.insert(noAccessUser)
+  }
+
+  override protected def afterEach(): Unit = {
+    cleanupTestData()
+  }
+
+  override protected def afterAll(): Unit = {
+    shutdownDB()
+  }
+
+  private def createUser(uid: Int, name: String, email: String): User = {
+    val user = new User
+    user.setUid(uid)
+    user.setName(name)
+    user.setEmail(email)
+    user.setPassword("password")
+    user.setRole(UserRoleEnum.REGULAR)
+    user
+  }
+
+  private def cleanupTestData(): Unit = {
+    getDSLContext
+      .deleteFrom(PROJECT_USER_ACCESS)
+      .where(PROJECT_USER_ACCESS.UID.in(ownerUid, readerUid, noAccessUid))
+      .execute()
+
+    getDSLContext
+      .deleteFrom(PROJECT)
+      .where(PROJECT.OWNER_ID.eq(ownerUid))
+      .execute()
+
+    getDSLContext
+      .deleteFrom(USER)
+      .where(USER.UID.in(ownerUid, readerUid, noAccessUid))
+      .execute()
+  }
+
+  "ProjectAccessResource.getProjectAccessPrivilege" should "return WRITE if 
granted" in {
+    val project = projectResource.createProject(new SessionUser(owner), 
"write-project")
+    val privilege = 
ProjectAccessResource.getProjectAccessPrivilege(project.getPid, ownerUid)
+
+    assert(privilege == PrivilegeEnum.WRITE)
+    assert(ProjectAccessResource.userHasWriteAccess(project.getPid, ownerUid))
+  }
+
+  it should "return READ if a project access row grants READ" in {
+    val project = projectResource.createProject(new SessionUser(owner), 
"read-project")
+    projectUserAccessDao.merge(
+      new ProjectUserAccess(readerUid, project.getPid, PrivilegeEnum.READ)
+    )
+
+    val privilege = 
ProjectAccessResource.getProjectAccessPrivilege(project.getPid, readerUid)
+
+    assert(privilege == PrivilegeEnum.READ)
+    assert(!ProjectAccessResource.userHasWriteAccess(project.getPid, 
readerUid))
+  }
+
+  it should "return NONE if the user has no project access row" in {

Review Comment:
   Could we make this negative test cover the `(pid, uid)` bug more directly?
   
   Right now `noAccessUid` has no rows in `PROJECT_USER_ACCESS`, so the test 
only proves that a missing user row returns `NONE`. Since this PR specifically 
fixes lookup by both `pid` and `uid`, please add or adjust this case so the 
user has access to a different project, then assert that lookup on this project 
still returns `NONE`.
   
   For example: create two projects, grant `readerUid` READ on project A only, 
then assert `getProjectAccessPrivilege(projectB.getPid, readerUid) == 
PrivilegeEnum.NONE`. That would catch a regression where the query accidentally 
filters only by `uid` and ignores `pid`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to