/*
* Copyright 2012 gitblit.com.
*
* Licensed 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 com.gitblit.tests;
import java.util.Date;
import org.junit.Test;
import com.gitblit.Constants.AccessPermission;
import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.Constants.AuthorizationControl;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
/**
* Comprehensive, brute-force test of all permutations of discrete permissions.
*
* @author James Moger
*
*/
public class PermissionsTest extends GitblitUnitTest {
/**
* Admin access rights/permissions
*/
@Test
public void testAdmin() throws Exception {
UserModel user = new UserModel("admin");
user.canAdmin = true;
for (AccessRestrictionType ar : AccessRestrictionType.values()) {
RepositoryModel repository = new RepositoryModel("myrepo.git", null, null, new Date());
repository.authorizationControl = AuthorizationControl.NAMED;
repository.accessRestriction = ar;
assertTrue("admin CAN NOT view!", user.canView(repository));
assertTrue("admin CAN NOT clone!", user.canClone(repository));
assertTrue("admin CAN NOT push!", user.canPush(repository));
assertTrue("admin CAN NOT create ref!", user.canCreateRef(repository));
assertTrue("admin CAN NOT delete ref!", user.canDeleteRef(repository));
assertTrue("admin CAN NOT rewind ref!", user.canRewindRef(repository));
assertEquals("admin has wrong permission!", AccessPermission.REWIND, user.getRepositoryPermission(repository).permission);
assertTrue("admin CAN NOT fork!", user.canFork(repository));
assertTrue("admin CAN NOT delete!", user.canDelete(repository));
assertTrue("admin CAN NOT edit!", user.canEdit(repository));
}
}
/**
* Anonymous access rights/permissions
*/
@Test
public void testAnonymous_NONE() throws Exception {
RepositoryModel repository = new RepositoryModel("myrepo.git", null, null, new Date());
repository.authorizationControl = AuthorizationControl.NAMED;
repository.accessRestriction = AccessRestrictionType.NONE;
UserModel user = UserModel.ANONYMOUS;
// all permissions, except fork
assertTrue("anonymous CAN NOT view!", user.canView(repository));
assertTrue("anonymous CAN NOT clone!", user.canClone(repository));
assertTrue("anonymous CAN NOT push!", user.canPush(repository));
assertTrue("anonymous CAN NOT create ref!", user.canCreateRef(repository));
assertTrue("anonymous CAN NOT delete ref!", user.canDeleteRef(repository));
assertTrue("anonymous CAN NOT rewind ref!", user.canRewindRef(repository));
assertEquals("anonymous has wrong permission!", AccessPermission.REWIND, user.getRepositoryPermission(repository).permission);
repository.allowForks = false;
assertFalse("anonymous CAN fork!", user.canFork(repository));
repository.allowForks = true;
assertFalse("anonymous CAN fork!", user.canFork(repository));
assertFalse("anonymous CAN delete!", user.canDelete(repository));
assertFalse("anonymous CAN edit!", user.canEdit(repository));
}
@Test
public void testAnonymous_PUSH() throws Exception {
RepositoryModel repository = new RepositoryModel("myrepo.git", null, null, new Date());
repository.authorizationControl = AuthorizationControl.NAMED;
repository.accessRestriction = AccessRestrictionType.PUSH;
UserModel user = UserModel.ANONYMOUS;
assertTrue("anonymous CAN NOT view!", user.canView(repository));
assertTrue("anonymous CAN NOT clone!", user.canClone(repository));
assertFalse("anonymous CAN push!", user.canPush(repository));
assertFalse("anonymous CAN create ref!", user.canCreateRef(repository));
assertFalse("anonymous CAN delete ref!", user.canDeleteRef(repository));
assertFalse("anonymous CAN rewind ref!", user.canRewindRef(repository));
assertEquals("anonymous has wrong permission!", AccessPermission.CLONE, user.getRepositoryPermission(repository).permission);
repository.allowForks = false;
assertFalse("anonymous CAN fork!", user.canFork(repository));
repository.allowForks = true;
assertFalse("anonymous CAN fork!", user.canFork(repository));
}
@Test
public void testAnonymous_CLONE() throws Exception {
RepositoryModel repository = new RepositoryModel("myrepo.git", null, null, new Date());
repository.authorizationControl = AuthorizationControl.NAMED;
repository.accessRestriction = AccessRestrictionType.CLONE;
UserModel user = UserModel.ANONYMOUS;
assertTrue("anonymous CAN NOT view!", user.canView(repository));
assertFalse("anonymous CAN clone!", user.canClone(repository));
assertFalse("anonymous CAN push!", user.canPush(repository));
assertFalse("anonymous CAN create ref!", user.canCreateRef(repository));
assertFalse("anonymous CAN delete ref!", user.canDeleteRef(repository));
assertFalse("anonymous CAN rewind ref!", user.canRewindRef(repository));
assertEquals("anonymous has wrong permission!", AccessPermission.VIEW, user.getRepositoryPermission(repository).permission);
repository.allowForks = false;
assertFalse("anonymous CAN fork!", user.canFork(repository));
repository.allowForks = true;
assertFalse("anonymous CAN fork!", user.canFork(repository));
}
@Test
public void testAnonymous_VIEW() throws Exception {
RepositoryModel repository = new RepositoryModel("myrepo.git", null, null, new Date());
repository.authorizationControl = AuthorizationControl.NAMED;
repository.accessRestriction = AccessRestrictionType.VIEW;
UserModel user = UserModel.ANONYMOUS;
assertFalse("anonymous CAN view!", user.canView(repository));
assertFalse("anonymous CAN clone!", user.canClone(repository));
assertFalse("anonymous CAN push!", user.canPush(repository));
assertFalse("anonymous CAN create ref!", user.canCreateRef(repository));
assertFalse("anonymous CAN delete ref!", user.canDeleteRef(repository));
assertFalse("anonymous CAN rewind ref!", user.canRewindRef(repository));
assertEquals("anonymous has wrong permission!", AccessPermission.NONE, user.getRepositoryPermission(repository).permission);
repository.allowForks = false;
assertFalse("anonymous CAN fork!", user.canFork(repository));
repository.allowForks = true;
assertFalse("anonymous CAN fork!", user.canFork(repository));
}
/**
* Authenticated access rights/permissions
*/
@Test
public void testAuthenticated_NONE() throws Exception {
RepositoryModel repository = new RepositoryModel("myrepo.git", null, null, new Date());
repository.authorizationControl = AuthorizationControl.AUTHENTICATED;
repository.accessRestriction = AccessRestrictionType.NONE;
UserModel user = new UserModel("test");
// all permissions, except fork
assertTrue("authenticated CAN NOT view!", user.canView(repository));
assertTrue("authenticated CAN NOT clone!", user.canClone(repository));
assertTrue("authenticated CAN NOT push!", user.canPush(repository));
assertTrue("authenticated CAN NOT create ref!", user.canCreateRef(repository));
assertTrue("authenticated CAN NOT delete ref!", user.canDeleteRef(repository));
assertTrue("authenticated CAN NOT rewind ref!", user.canRewindRef(repository));
assertEquals("authenticated has wrong permission!", AccessPermission.REWIND, user.getRepositoryPermission(repository).permission);
user.canFork = false;
repository.allowForks = false;
assertFalse("authenticated CAN fork!", user.canFork(repository));
repository.allowForks = true;
assertFalse("authenticated CAN