You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitBlitTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tests;
  17. import java.util.List;
  18. import junit.framework.TestCase;
  19. import com.gitblit.Constants.AccessRestrictionType;
  20. import com.gitblit.FileSettings;
  21. import com.gitblit.GitBlit;
  22. import com.gitblit.models.RepositoryModel;
  23. import com.gitblit.models.UserModel;
  24. public class GitBlitTest extends TestCase {
  25. public void testRepositoryModel() throws Exception {
  26. List<String> repositories = GitBlit.self().getRepositoryList();
  27. assertTrue("Repository list is empty!", repositories.size() > 0);
  28. assertTrue(
  29. "Missing Helloworld repository!",
  30. repositories.contains(GitBlitSuite.getHelloworldRepository().getDirectory()
  31. .getName()));
  32. RepositoryModel model = GitBlit.self().getRepositoryModel(
  33. GitBlitSuite.getHelloworldRepository().getDirectory().getName());
  34. assertTrue("Helloworld model is null!", model != null);
  35. assertTrue(model.toString().equals(
  36. GitBlitSuite.getHelloworldRepository().getDirectory().getName()));
  37. }
  38. public void testUserModel() throws Exception {
  39. List<String> users = GitBlit.self().getAllUsernames();
  40. assertTrue("No users found!", users.size() > 0);
  41. assertTrue("Admin not found", users.contains("admin"));
  42. UserModel model = GitBlit.self().getUserModel("admin");
  43. assertTrue(model.toString().equals("admin"));
  44. assertTrue("Admin missing #admin role!", model.canAdmin);
  45. model.canAdmin = false;
  46. assertFalse("Admin should not have #admin!", model.canAdmin);
  47. String repository = GitBlitSuite.getHelloworldRepository().getDirectory().getName();
  48. assertFalse("Admin can still access repository!", model.canAccessRepository(repository));
  49. model.addRepository(repository);
  50. assertTrue("Admin can't access repository!", model.canAccessRepository(repository));
  51. }
  52. public void testAccessRestrictionTypes() throws Exception {
  53. assertTrue(AccessRestrictionType.PUSH.exceeds(AccessRestrictionType.NONE));
  54. assertTrue(AccessRestrictionType.CLONE.exceeds(AccessRestrictionType.PUSH));
  55. assertTrue(AccessRestrictionType.VIEW.exceeds(AccessRestrictionType.CLONE));
  56. assertFalse(AccessRestrictionType.NONE.exceeds(AccessRestrictionType.PUSH));
  57. assertFalse(AccessRestrictionType.PUSH.exceeds(AccessRestrictionType.CLONE));
  58. assertFalse(AccessRestrictionType.CLONE.exceeds(AccessRestrictionType.VIEW));
  59. assertTrue(AccessRestrictionType.PUSH.atLeast(AccessRestrictionType.NONE));
  60. assertTrue(AccessRestrictionType.CLONE.atLeast(AccessRestrictionType.PUSH));
  61. assertTrue(AccessRestrictionType.VIEW.atLeast(AccessRestrictionType.CLONE));
  62. assertFalse(AccessRestrictionType.NONE.atLeast(AccessRestrictionType.PUSH));
  63. assertFalse(AccessRestrictionType.PUSH.atLeast(AccessRestrictionType.CLONE));
  64. assertFalse(AccessRestrictionType.CLONE.atLeast(AccessRestrictionType.VIEW));
  65. assertTrue(AccessRestrictionType.PUSH.toString().equals("PUSH"));
  66. assertTrue(AccessRestrictionType.CLONE.toString().equals("CLONE"));
  67. assertTrue(AccessRestrictionType.VIEW.toString().equals("VIEW"));
  68. assertTrue(AccessRestrictionType.fromName("none").equals(AccessRestrictionType.NONE));
  69. assertTrue(AccessRestrictionType.fromName("push").equals(AccessRestrictionType.PUSH));
  70. assertTrue(AccessRestrictionType.fromName("clone").equals(AccessRestrictionType.CLONE));
  71. assertTrue(AccessRestrictionType.fromName("view").equals(AccessRestrictionType.VIEW));
  72. }
  73. public void testFileSettings() throws Exception {
  74. FileSettings settings = new FileSettings("distrib/gitblit.properties");
  75. assertTrue(settings.getBoolean("missing", true) == true);
  76. assertTrue(settings.getString("missing", "default").equals("default"));
  77. assertTrue(settings.getInteger("missing", 10) == 10);
  78. assertTrue(settings.getInteger("realm.realmFile", 5) == 5);
  79. assertTrue(settings.getBoolean("git.enableGitServlet", false) == true);
  80. assertTrue(settings.getString("realm.userService", null).equals("users.properties"));
  81. assertTrue(settings.getInteger("realm.minPasswordLength", 0) == 5);
  82. List<String> mdExtensions = settings.getStrings("web.markdownExtensions");
  83. assertTrue(mdExtensions.size() > 0);
  84. assertTrue(mdExtensions.contains("md"));
  85. List<String> keys = settings.getAllKeys("server");
  86. assertTrue(keys.size() > 0);
  87. assertTrue(keys.contains("server.httpsPort"));
  88. }
  89. public void testGitblitSettings() throws Exception {
  90. // These are already tested by above test method.
  91. assertTrue(GitBlit.getBoolean("missing", true) == true);
  92. assertTrue(GitBlit.getString("missing", "default").equals("default"));
  93. assertTrue(GitBlit.getInteger("missing", 10) == 10);
  94. assertTrue(GitBlit.getInteger("realm.userService", 5) == 5);
  95. assertTrue(GitBlit.getBoolean("git.enableGitServlet", false) == true);
  96. assertTrue(GitBlit.getString("realm.userService", null).equals("users.properties"));
  97. assertTrue(GitBlit.getInteger("realm.minPasswordLength", 0) == 5);
  98. List<String> mdExtensions = GitBlit.getStrings("web.markdownExtensions");
  99. assertTrue(mdExtensions.size() > 0);
  100. assertTrue(mdExtensions.contains("md"));
  101. List<String> keys = GitBlit.getAllKeys("server");
  102. assertTrue(keys.size() > 0);
  103. assertTrue(keys.contains("server.httpsPort"));
  104. }
  105. public void testAuthentication() throws Exception {
  106. assertTrue(GitBlit.self().authenticate("admin", "admin".toCharArray()) != null);
  107. }
  108. public void testRepositories() throws Exception {
  109. assertTrue(GitBlit.self().getRepository("missing") == null);
  110. assertTrue(GitBlit.self().getRepositoryModel("missing") == null);
  111. }
  112. }