Skip to content

Commit

Permalink
Add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Dec 11, 2024
1 parent c054553 commit f8d8aca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ public CompletableFuture<Collection<Permission>> findPermissions(String projectN
User user) {
requireNonNull(user, "user");
if (user.isSystemAdmin()) {
return CompletableFuture.completedFuture(PerRolePermissions.ALL_PERMISSION);
return CompletableFuture.completedFuture(PerRolePermissions.READ_WRITE);
}
if (user instanceof UserWithToken) {
return findPermissions(projectName, repoName, ((UserWithToken) user).token().appId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static java.util.Objects.requireNonNull;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;

Expand All @@ -28,6 +27,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;

import com.linecorp.centraldogma.common.ProjectRole;
Expand All @@ -41,11 +41,9 @@ public final class PerRolePermissions {
/**
* {@link Permission}s for administrators.
*/
public static final Collection<Permission> ALL_PERMISSION = EnumSet.allOf(Permission.class);

public static final Collection<Permission> READ_WRITE = EnumSet.of(Permission.READ, Permission.WRITE);
public static final Collection<Permission> READ_ONLY = EnumSet.of(Permission.READ);
public static final Collection<Permission> NO_PERMISSION = EnumSet.noneOf(Permission.class);
public static final Collection<Permission> READ_WRITE = ImmutableList.of(Permission.READ, Permission.WRITE);
public static final Collection<Permission> READ_ONLY = ImmutableList.of(Permission.READ);
public static final Collection<Permission> NO_PERMISSION = ImmutableList.of();

/**
* The default permission.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ void perRolePermissions() {
assertThat(mds.findPermissions(project1, repo1, guest).join())
.containsExactlyElementsOf(NO_PERMISSION);

// Fail due to duplicated update.
assertThatThrownBy(() -> mds.updatePerRolePermissions(
author, project1, repo1, PerRolePermissions.ofPrivate()).join())
.hasCauseInstanceOf(IllegalArgumentException.class);

assertThatThrownBy(() -> mds.updatePerRolePermissions(
author, project1, REPO_DOGMA, PerRolePermissions.ofPublic()).join())
.isInstanceOf(UnsupportedOperationException.class)
Expand All @@ -224,6 +229,11 @@ void perUserPermissions() {
// Be a member of the project.
mds.addMember(author, project1, user1, ProjectRole.MEMBER).join();

// invalid repo.
assertThatThrownBy(() -> mds.addPerUserPermission(
author, project1, "invalid-repo", user1, READ_ONLY).join())
.hasCauseInstanceOf(RepositoryNotFoundException.class);

// A member of the project has no permission.
assertThat(mds.findPermissions(project1, repo1, user1).join())
.containsExactlyElementsOf(NO_PERMISSION);
Expand All @@ -243,6 +253,14 @@ void perUserPermissions() {
assertThat(mds.findPermissions(project1, repo1, user1).join())
.containsExactlyInAnyOrder(Permission.READ, Permission.WRITE);

// Update again with the same permission.
assertThatThrownBy(() -> mds.updatePerUserPermission(author, project1, repo1, user1, READ_WRITE).join())
.hasCauseInstanceOf(IllegalArgumentException.class);

// Update invalid user
assertThatThrownBy(() -> mds.updatePerUserPermission(author, project1, repo1, user2, READ_WRITE).join())
.hasCauseInstanceOf(IllegalArgumentException.class);

mds.removePerUserPermission(author, project1, repo1, user1).join();
assertThatThrownBy(() -> mds.removePerUserPermission(author, project1, repo1, user1).join())
.hasCauseInstanceOf(IllegalArgumentException.class);
Expand Down Expand Up @@ -278,11 +296,19 @@ void perTokenPermissions() {
assertThat(mds.findPermissions(project1, repo1, app1).join())
.containsExactly(Permission.READ);

// Add again.
assertThatThrownBy(() -> mds.addPerTokenPermission(author, project1, repo1, app1, READ_ONLY).join())
.hasCauseInstanceOf(IllegalArgumentException.class);

mds.updatePerTokenPermission(author, project1, repo1, app1, READ_WRITE).join();

assertThat(mds.findPermissions(project1, repo1, app1).join())
.containsExactlyInAnyOrder(Permission.READ, Permission.WRITE);

// Update again with the same permission.
assertThatThrownBy(() -> mds.updatePerTokenPermission(author, project1, repo1, app1, READ_WRITE).join())
.hasCauseInstanceOf(IllegalArgumentException.class);

mds.removePerTokenPermission(author, project1, repo1, app1).join();
assertThatThrownBy(() -> mds.removePerTokenPermission(author, project1, repo1, app1).join())
.hasCauseInstanceOf(IllegalArgumentException.class);
Expand Down

0 comments on commit f8d8aca

Please sign in to comment.