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.

repository_subversion_test_pm.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # frozen_string_literal: false
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative 'test_case'
  19. require 'tmpdir'
  20. class RedminePmTest::RepositorySubversionTest < RedminePmTest::TestCase
  21. fixtures :projects, :users, :members, :roles, :member_roles, :auth_sources, :enabled_modules
  22. SVN_BIN = Redmine::Configuration['scm_subversion_command'] || "svn"
  23. def test_anonymous_read_on_public_repo_with_permission_should_succeed
  24. assert_success "ls", svn_url
  25. end
  26. def test_anonymous_read_on_public_repo_with_anonymous_group_permission_should_succeed
  27. Role.anonymous.remove_permission! :browse_repository
  28. Member.create!(:project_id => 1, :principal => Group.anonymous, :role_ids => [2])
  29. assert_success "ls", svn_url
  30. end
  31. def test_anonymous_read_on_public_repo_without_permission_should_fail
  32. Role.anonymous.remove_permission! :browse_repository
  33. assert_failure "ls", svn_url
  34. end
  35. def test_anonymous_read_on_public_project_with_module_disabled_should_fail
  36. Project.find(1).disable_module! :repository
  37. assert_failure "ls", svn_url
  38. end
  39. def test_anonymous_read_on_private_repo_should_fail
  40. Project.find(1).update_attribute :is_public, false
  41. assert_failure "ls", svn_url
  42. end
  43. def test_anonymous_commit_on_public_repo_should_fail
  44. Role.anonymous.add_permission! :commit_access
  45. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  46. end
  47. def test_anonymous_commit_on_private_repo_should_fail
  48. Role.anonymous.add_permission! :commit_access
  49. Project.find(1).update_attribute :is_public, false
  50. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  51. end
  52. def test_non_member_read_on_public_repo_with_permission_should_succeed
  53. Role.anonymous.remove_permission! :browse_repository
  54. with_credentials "miscuser8", "foo" do
  55. assert_success "ls", svn_url
  56. end
  57. end
  58. def test_non_member_read_on_public_repo_with_non_member_group_permission_should_succeed
  59. Role.anonymous.remove_permission! :browse_repository
  60. Role.non_member.remove_permission! :browse_repository
  61. Member.create!(:project_id => 1, :principal => Group.non_member, :role_ids => [2])
  62. with_credentials "miscuser8", "foo" do
  63. assert_success "ls", svn_url
  64. end
  65. end
  66. def test_non_member_read_on_public_repo_without_permission_should_fail
  67. Role.anonymous.remove_permission! :browse_repository
  68. Role.non_member.remove_permission! :browse_repository
  69. with_credentials "miscuser8", "foo" do
  70. assert_failure "ls", svn_url
  71. end
  72. end
  73. def test_non_member_read_on_private_repo_should_fail
  74. Project.find(1).update_attribute :is_public, false
  75. with_credentials "miscuser8", "foo" do
  76. assert_failure "ls", svn_url
  77. end
  78. end
  79. def test_non_member_commit_on_public_repo_should_fail
  80. Role.non_member.add_permission! :commit_access
  81. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  82. end
  83. def test_non_member_commit_on_private_repo_should_fail
  84. Role.non_member.add_permission! :commit_access
  85. Project.find(1).update_attribute :is_public, false
  86. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  87. end
  88. def test_member_read_on_public_repo_with_permission_should_succeed
  89. Role.anonymous.remove_permission! :browse_repository
  90. Role.non_member.remove_permission! :browse_repository
  91. with_credentials "dlopper", "foo" do
  92. assert_success "ls", svn_url
  93. end
  94. end
  95. def test_member_read_on_public_repo_without_permission_should_fail
  96. Role.anonymous.remove_permission! :browse_repository
  97. Role.non_member.remove_permission! :browse_repository
  98. Role.find(2).remove_permission! :browse_repository
  99. with_credentials "dlopper", "foo" do
  100. assert_failure "ls", svn_url
  101. end
  102. end
  103. def test_member_read_on_private_repo_with_permission_should_succeed
  104. Project.find(1).update_attribute :is_public, false
  105. with_credentials "dlopper", "foo" do
  106. assert_success "ls", svn_url
  107. end
  108. end
  109. def test_member_read_on_private_repo_without_permission_should_fail
  110. Role.find(2).remove_permission! :browse_repository
  111. Project.find(1).update_attribute :is_public, false
  112. with_credentials "dlopper", "foo" do
  113. assert_failure "ls", svn_url
  114. end
  115. end
  116. def test_member_read_on_private_repo_with_module_disabled_should_fail
  117. Role.find(2).add_permission! :browse_repository
  118. Project.find(1).update_attribute :is_public, false
  119. Project.find(1).disable_module! :repository
  120. with_credentials "dlopper", "foo" do
  121. assert_failure "ls", svn_url
  122. end
  123. end
  124. def test_member_commit_on_public_repo_with_permission_should_succeed
  125. Role.find(2).add_permission! :commit_access
  126. with_credentials "dlopper", "foo" do
  127. assert_success "mkdir --message Creating_a_directory", svn_url(random_filename)
  128. end
  129. end
  130. def test_member_commit_on_public_repo_without_permission_should_fail
  131. Role.find(2).remove_permission! :commit_access
  132. with_credentials "dlopper", "foo" do
  133. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  134. end
  135. end
  136. def test_member_commit_on_private_repo_with_permission_should_succeed
  137. Role.find(2).add_permission! :commit_access
  138. Project.find(1).update_attribute :is_public, false
  139. with_credentials "dlopper", "foo" do
  140. assert_success "mkdir --message Creating_a_directory", svn_url(random_filename)
  141. end
  142. end
  143. def test_member_commit_on_private_repo_without_permission_should_fail
  144. Role.find(2).remove_permission! :commit_access
  145. Project.find(1).update_attribute :is_public, false
  146. with_credentials "dlopper", "foo" do
  147. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  148. end
  149. end
  150. def test_member_commit_on_private_repo_with_module_disabled_should_fail
  151. Role.find(2).add_permission! :commit_access
  152. Project.find(1).update_attribute :is_public, false
  153. Project.find(1).disable_module! :repository
  154. with_credentials "dlopper", "foo" do
  155. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  156. end
  157. end
  158. def test_invalid_credentials_should_fail
  159. Project.find(1).update_attribute :is_public, false
  160. with_credentials "dlopper", "foo" do
  161. assert_success "ls", svn_url
  162. end
  163. with_credentials "dlopper", "wrong" do
  164. assert_failure "ls", svn_url
  165. end
  166. end
  167. def test_anonymous_read_should_fail_with_login_required
  168. assert_success "ls", svn_url
  169. with_settings :login_required => '1' do
  170. assert_failure "ls", svn_url
  171. end
  172. end
  173. def test_authenticated_read_should_succeed_with_login_required
  174. with_settings :login_required => '1' do
  175. with_credentials "miscuser8", "foo" do
  176. assert_success "ls", svn_url
  177. end
  178. end
  179. end
  180. def test_read_on_archived_projects_should_fail
  181. Project.find(1).update_attribute :status, Project::STATUS_ARCHIVED
  182. assert_failure "ls", svn_url
  183. end
  184. def test_read_on_archived_private_projects_should_fail
  185. Project.find(1).update_attribute :status, Project::STATUS_ARCHIVED
  186. Project.find(1).update_attribute :is_public, false
  187. with_credentials "dlopper", "foo" do
  188. assert_failure "ls", svn_url
  189. end
  190. end
  191. def test_read_on_closed_projects_should_succeed
  192. Project.find(1).update_attribute :status, Project::STATUS_CLOSED
  193. assert_success "ls", svn_url
  194. end
  195. def test_read_on_closed_private_projects_should_succeed
  196. Project.find(1).update_attribute :status, Project::STATUS_CLOSED
  197. Project.find(1).update_attribute :is_public, false
  198. with_credentials "dlopper", "foo" do
  199. assert_success "ls", svn_url
  200. end
  201. end
  202. def test_commit_on_closed_projects_should_fail
  203. Project.find(1).update_attribute :status, Project::STATUS_CLOSED
  204. Role.find(2).add_permission! :commit_access
  205. with_credentials "dlopper", "foo" do
  206. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  207. end
  208. end
  209. def test_commit_on_closed_private_projects_should_fail
  210. Project.find(1).update_attribute :status, Project::STATUS_CLOSED
  211. Project.find(1).update_attribute :is_public, false
  212. Role.find(2).add_permission! :commit_access
  213. with_credentials "dlopper", "foo" do
  214. assert_failure "mkdir --message Creating_a_directory", svn_url(random_filename)
  215. end
  216. end
  217. if ldap_configured?
  218. def test_user_with_ldap_auth_source_should_authenticate_with_ldap_credentials
  219. ldap_user = User.new(:mail => 'example1@redmine.org', :firstname => 'LDAP', :lastname => 'user', :auth_source_id => 1)
  220. ldap_user.login = 'example1'
  221. ldap_user.save!
  222. with_settings :login_required => '1' do
  223. with_credentials "example1", "123456" do
  224. assert_success "ls", svn_url
  225. end
  226. end
  227. with_settings :login_required => '1' do
  228. with_credentials "example1", "wrong" do
  229. assert_failure "ls", svn_url
  230. end
  231. end
  232. end
  233. end
  234. def test_checkout
  235. Dir.mktmpdir do |dir|
  236. assert_success "checkout", svn_url, dir
  237. end
  238. end
  239. def test_read_commands
  240. assert_success "info", svn_url
  241. assert_success "ls", svn_url
  242. assert_success "log", svn_url
  243. end
  244. def test_write_commands
  245. Role.find(2).add_permission! :commit_access
  246. filename = random_filename
  247. Dir.mktmpdir do |dir|
  248. assert_success "checkout", svn_url, dir
  249. Dir.chdir(dir) do
  250. # creates a file in the working copy
  251. f = File.new(File.join(dir, filename), "w")
  252. f.write "test file content"
  253. f.close
  254. assert_success "add", filename
  255. with_credentials "dlopper", "foo" do
  256. assert_success "commit --message Committing_a_file"
  257. assert_success "copy --message Copying_a_file", svn_url(filename), svn_url("#{filename}_copy")
  258. assert_success "delete --message Deleting_a_file", svn_url(filename)
  259. assert_success "mkdir --message Creating_a_directory", svn_url("#{filename}_dir")
  260. end
  261. assert_success "update"
  262. # checks that the working copy was updated
  263. assert File.exist?(File.join(dir, "#{filename}_copy"))
  264. assert File.directory?(File.join(dir, "#{filename}_dir"))
  265. end
  266. end
  267. end
  268. def test_read_invalid_repo_should_fail
  269. assert_failure "ls", svn_url("invalid")
  270. end
  271. protected
  272. def execute(*args)
  273. a = [SVN_BIN, "--no-auth-cache --non-interactive"]
  274. a << "--username #{username}" if username
  275. a << "--password #{password}" if password
  276. super a, *args
  277. end
  278. def svn_url(path=nil)
  279. host = ENV['REDMINE_TEST_DAV_SERVER'] || '127.0.0.1'
  280. url = "http://#{host}/svn/ecookbook"
  281. url << "/#{path}" if path
  282. url
  283. end
  284. end