]> source.dussan.org Git - gitblit.git/commitdiff
Unit tests which confirm working/expected behavior (issue-259)
authorJames Moger <james.moger@gitblit.com>
Wed, 3 Jul 2013 01:18:46 +0000 (21:18 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 3 Jul 2013 01:18:46 +0000 (21:18 -0400)
src/test/java/com/gitblit/tests/GitBlitSuite.java
src/test/java/com/gitblit/tests/Issue0259Test.java [new file with mode: 0644]
src/test/resources/issue0259.conf [new file with mode: 0644]

index dca158c25b1a000d6d783fa0afaf30947240e3ef..a226329837c8cc0d43e578a23d4b7099674961fa 100644 (file)
@@ -60,7 +60,7 @@ import com.gitblit.utils.JGitUtils;
                DiffUtilsTest.class, MetricUtilsTest.class, TicgitUtilsTest.class, X509UtilsTest.class,\r
                GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GitDaemonTest.class,\r
                GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class, RepositoryModelTest.class,\r
-               FanoutServiceTest.class })\r
+               FanoutServiceTest.class, Issue0259Test.class })\r
 public class GitBlitSuite {\r
 \r
        public static final File REPOSITORIES = new File("data/git");\r
diff --git a/src/test/java/com/gitblit/tests/Issue0259Test.java b/src/test/java/com/gitblit/tests/Issue0259Test.java
new file mode 100644 (file)
index 0000000..174f7cc
--- /dev/null
@@ -0,0 +1,204 @@
+/*\r
+ * Copyright 2013 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.tests;\r
+\r
+import java.io.File;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+\r
+import com.gitblit.ConfigUserService;\r
+import com.gitblit.Constants.AccessPermission;\r
+import com.gitblit.Constants.AccessRestrictionType;\r
+import com.gitblit.models.RepositoryModel;\r
+import com.gitblit.models.TeamModel;\r
+import com.gitblit.models.UserModel;\r
+\r
+/**\r
+ * https://code.google.com/p/gitblit/issues/detail?id=259\r
+ * \r
+ * Reported Problem:\r
+ * We have an user with RWD access rights, but he can’t push.\r
+ * \r
+ * @see src/test/resources/issue0259.conf\r
+ * \r
+ * At the next day he try again and he can push to the project.\r
+ * \r
+ * @author James Moger\r
+ *\r
+ */\r
+public class Issue0259Test extends Assert {\r
+\r
+       RepositoryModel repo(String name, AccessRestrictionType restriction) {\r
+               RepositoryModel repo = new RepositoryModel();\r
+               repo.name = name;\r
+               repo.accessRestriction = restriction;\r
+               return repo;\r
+       }\r
+       \r
+       /**\r
+        * Test the provided users.conf file for expected access permissions.\r
+        *  \r
+        * @throws Exception\r
+        */\r
+       @Test\r
+       public void testFile() throws Exception {\r
+               File realmFile = new File("src/test/resources/issue0259.conf");\r
+               ConfigUserService service = new ConfigUserService(realmFile);\r
+               \r
+               RepositoryModel test = repo("test.git", AccessRestrictionType.VIEW);\r
+               RepositoryModel projects_test = repo("projects/test.git", AccessRestrictionType.VIEW);\r
+               \r
+               UserModel a = service.getUserModel("a");\r
+               UserModel b = service.getUserModel("b");\r
+               UserModel c = service.getUserModel("c");\r
+               \r
+               // assert RWD or RW+ for projects/test.git\r
+               assertEquals(AccessPermission.DELETE, a.getRepositoryPermission(projects_test).permission);\r
+               assertEquals(AccessPermission.DELETE, b.getRepositoryPermission(projects_test).permission);\r
+               assertEquals(AccessPermission.REWIND, c.getRepositoryPermission(projects_test).permission);\r
+               \r
+               assertTrue(a.canPush(projects_test));\r
+               assertTrue(b.canPush(projects_test));\r
+               assertTrue(c.canPush(projects_test));\r
+\r
+               assertTrue(a.canDeleteRef(projects_test));\r
+               assertTrue(b.canDeleteRef(projects_test));\r
+               assertTrue(c.canDeleteRef(projects_test));\r
+\r
+               assertFalse(a.canRewindRef(projects_test));\r
+               assertFalse(b.canRewindRef(projects_test));\r
+               assertTrue(c.canRewindRef(projects_test));\r
+               \r
+               // assert R for test.git\r
+               assertEquals(AccessPermission.CLONE, a.getRepositoryPermission(test).permission);\r
+               assertEquals(AccessPermission.CLONE, b.getRepositoryPermission(test).permission);\r
+               assertEquals(AccessPermission.REWIND, c.getRepositoryPermission(test).permission);\r
+\r
+               assertTrue(a.canClone(test));\r
+               assertTrue(b.canClone(test));\r
+\r
+               assertFalse(a.canPush(test));\r
+               assertFalse(b.canPush(test));\r
+               assertTrue(c.canPush(test));\r
+       }\r
+       \r
+       @Test\r
+       public void testTeamsOrder() throws Exception {\r
+               testTeams(false);\r
+       }\r
+\r
+       @Test\r
+       public void testTeamsReverseOrder() throws Exception {\r
+               testTeams(true);\r
+       }\r
+\r
+       /**\r
+        * Tests multiple teams each with a regex permisson that will match.  The\r
+        * highest matching permission should be used.  Order should be irrelevant.\r
+        *  \r
+        * @param reverseOrder\r
+        * @throws Exception\r
+        */\r
+       private void testTeams(boolean reverseOrder) throws Exception {\r
+               RepositoryModel test = repo("test.git", AccessRestrictionType.VIEW);\r
+               RepositoryModel projects_test = repo("projects/test.git", AccessRestrictionType.VIEW);\r
+               \r
+               TeamModel t1 = new TeamModel("t1");\r
+               t1.setRepositoryPermission(".*", AccessPermission.CLONE);\r
+               \r
+               TeamModel t2 = new TeamModel("t2");\r
+               t2.setRepositoryPermission("projects/.*", AccessPermission.DELETE);             \r
+               \r
+               UserModel a = new UserModel("a");\r
+               if (reverseOrder) {\r
+                       a.teams.add(t2);\r
+                       a.teams.add(t1);\r
+               } else {\r
+                       a.teams.add(t1);\r
+                       a.teams.add(t2);\r
+               }\r
+               \r
+               assertEquals(AccessPermission.CLONE, a.getRepositoryPermission(test).permission);\r
+               assertEquals(AccessPermission.DELETE, a.getRepositoryPermission(projects_test).permission);\r
+               \r
+               assertTrue(a.canClone(test));\r
+               assertTrue(a.canClone(projects_test));\r
+               \r
+               assertFalse(a.canDeleteRef(test));\r
+               assertTrue(a.canDeleteRef(projects_test));\r
+       }\r
+       \r
+       @Test\r
+       public void testTeam() throws Exception {\r
+               testTeam(false);                \r
+       }\r
+       \r
+       @Test\r
+       public void testTeamReverseOrder() throws Exception {\r
+               testTeam(true);\r
+       }\r
+       \r
+       /**\r
+        * Test a single team that has multiple repository permissions that all match.\r
+        * Here defined order IS important.  The first permission match wins so it is\r
+        * important to define permissions from most-specific match to least-specific\r
+        * match.\r
+        * \r
+        * If the defined permissions are:\r
+        *   R:.*\r
+        *   RWD:projects/.*\r
+        * then the expected result is R for all repositories because it is first.\r
+        * \r
+        * But if the defined permissions are:\r
+        *   RWD:projects/.*\r
+        *   R:.*\r
+        * then the expected result is RWD for projects/test.git and R for test.git\r
+        *  \r
+        * @param reverseOrder\r
+        * @throws Exception\r
+        */\r
+       private void testTeam(boolean reverseOrder) throws Exception {\r
+               RepositoryModel test = repo("test.git", AccessRestrictionType.VIEW);\r
+               RepositoryModel projects_test = repo("projects/test.git", AccessRestrictionType.VIEW);\r
+               \r
+               TeamModel t1 = new TeamModel("t1");\r
+               if (reverseOrder) {\r
+                       t1.setRepositoryPermission("projects/.*", AccessPermission.DELETE);\r
+                       t1.setRepositoryPermission(".*", AccessPermission.CLONE);\r
+               } else {\r
+                       t1.setRepositoryPermission(".*", AccessPermission.CLONE);\r
+                       t1.setRepositoryPermission("projects/.*", AccessPermission.DELETE);\r
+               }\r
+               UserModel a = new UserModel("a");\r
+               a.teams.add(t1);\r
+               \r
+               assertEquals(AccessPermission.CLONE, a.getRepositoryPermission(test).permission);\r
+               assertTrue(a.canClone(test));\r
+               assertFalse(a.canDeleteRef(test));\r
+               assertTrue(a.canClone(projects_test));\r
+\r
+               if (reverseOrder) {\r
+                       // RWD permission is found first\r
+                       assertEquals(AccessPermission.DELETE, a.getRepositoryPermission(projects_test).permission);\r
+                       assertTrue(a.canDeleteRef(projects_test));\r
+               } else {\r
+                       // R permission is found first\r
+                       assertEquals(AccessPermission.CLONE, a.getRepositoryPermission(projects_test).permission);\r
+                       assertFalse(a.canDeleteRef(projects_test));\r
+               }\r
+       }\r
+}\r
diff --git a/src/test/resources/issue0259.conf b/src/test/resources/issue0259.conf
new file mode 100644 (file)
index 0000000..ad6f0c3
--- /dev/null
@@ -0,0 +1,32 @@
+[user "A"]
+       password = "#externalAccount"
+       role = "#none"
+[user "B"]
+       password = "#externalAccount"
+       role = "#none"
+[user "C"]
+       password = "#externalAccount"
+       role = "#admin"
+[team "git_1560"]
+       role = "#none"
+       repository = RWC:trade/.*
+       repository = RWC:dev.support/admin.behelfsskripte.git
+       repository = RWD:projects/.*
+       repository = RWD:birt/.*
+       repository = RWD:fact/.*
+       repository = RWD:of/.*
+       repository = RWD:portal.solutions/.*
+       repository = RWD:rem/.*
+       repository = RWD:transparency/.*
+       repository = RWD:process/.*
+       user = B
+       user = A
+       user = C
+[team "git_EcgUser"]
+       role = "#create"
+       repository = R:.*
+       repository = RWD:test.projects/.*
+       repository = R:~krulls/regressionstests.base.git
+       user = A
+       user = C
+       user = B