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.

sys_controller_test.rb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # frozen_string_literal: true
  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_helper'
  19. class SysControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :repositories, :enabled_modules
  21. def setup
  22. Setting.sys_api_enabled = '1'
  23. Setting.enabled_scm = %w(Subversion Git)
  24. end
  25. def teardown
  26. Setting.clear_cache
  27. end
  28. def test_projects_with_repository_enabled
  29. get :projects
  30. assert_response :success
  31. assert_equal 'application/json', @response.media_type
  32. data = ActiveSupport::JSON.decode(response.body)
  33. assert_equal Project.active.has_module(:repository).count, data.size
  34. project = data.first
  35. assert project['identifier']
  36. assert project['is_public']
  37. assert_not_include 'extra-info', response.body
  38. assert_not_include 'extra_info', response.body
  39. end
  40. def test_create_project_repository
  41. assert_nil Project.find(4).repository
  42. post(
  43. :create_project_repository,
  44. :params => {
  45. :id => 4,
  46. :vendor => 'Subversion',
  47. :repository => {:url => 'file:///create/project/repository/subproject2'}
  48. }
  49. )
  50. assert_response :created
  51. assert_equal 'application/json', @response.media_type
  52. r = Project.find(4).repository
  53. assert r.is_a?(Repository::Subversion)
  54. assert_equal 'file:///create/project/repository/subproject2', r.url
  55. data = ActiveSupport::JSON.decode(response.body)
  56. assert data['repository-subversion']
  57. assert_equal r.id, data['repository-subversion']['id']
  58. assert_equal r.url, data['repository-subversion']['url']
  59. assert_not_include 'extra-info', response.body
  60. assert_not_include 'extra_info', response.body
  61. end
  62. def test_create_already_existing
  63. post(
  64. :create_project_repository,
  65. :params => {
  66. :id => 1,
  67. :vendor => 'Subversion',
  68. :repository => {:url => 'file:///create/project/repository/subproject2'}
  69. }
  70. )
  71. assert_response :conflict
  72. end
  73. def test_create_with_failure
  74. post(
  75. :create_project_repository,
  76. :params => {
  77. :id => 4,
  78. :vendor => 'Subversion',
  79. :repository => {:url => 'invalid url'}
  80. }
  81. )
  82. assert_response :unprocessable_entity
  83. end
  84. def test_fetch_changesets
  85. Repository::Subversion.any_instance.expects(:fetch_changesets).twice.returns(true)
  86. get :fetch_changesets
  87. assert_response :success
  88. end
  89. def test_fetch_changesets_one_project_by_identifier
  90. Repository::Subversion.any_instance.expects(:fetch_changesets).once.returns(true)
  91. get :fetch_changesets, :params => {:id => 'ecookbook'}
  92. assert_response :success
  93. end
  94. def test_fetch_changesets_one_project_by_id
  95. Repository::Subversion.any_instance.expects(:fetch_changesets).once.returns(true)
  96. get :fetch_changesets, :params => {:id => '1'}
  97. assert_response :success
  98. end
  99. def test_fetch_changesets_unknown_project
  100. get :fetch_changesets, :params => {:id => 'unknown'}
  101. assert_response 404
  102. end
  103. def test_disabled_ws_should_respond_with_403_error
  104. with_settings :sys_api_enabled => '0' do
  105. get :projects
  106. assert_response 403
  107. assert_include 'Access denied', response.body
  108. end
  109. end
  110. def test_api_key
  111. with_settings :sys_api_key => 'my_secret_key' do
  112. get :projects, :params => {:key => 'my_secret_key'}
  113. assert_response :success
  114. end
  115. end
  116. def test_wrong_key_should_respond_with_403_error
  117. with_settings :sys_api_enabled => 'my_secret_key' do
  118. get :projects, :params => {:key => 'wrong_key'}
  119. assert_response 403
  120. assert_include 'Access denied', response.body
  121. end
  122. end
  123. def test_should_skip_verify_authenticity_token
  124. ActionController::Base.allow_forgery_protection = true
  125. assert_nothing_raised {test_create_project_repository}
  126. ensure
  127. ActionController::Base.allow_forgery_protection = false
  128. end
  129. end