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.

mail_handler_controller_test.rb 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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 MailHandlerControllerTest < Redmine::ControllerTest
  20. fixtures :users, :email_addresses, :projects, :enabled_modules,
  21. :roles, :members, :member_roles, :issues, :issue_statuses,
  22. :trackers, :projects_trackers, :enumerations, :versions
  23. FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
  24. def setup
  25. User.current = nil
  26. end
  27. def test_should_create_issue
  28. # Enable API and set a key
  29. with_settings(
  30. :mail_handler_api_enabled => 1,
  31. :mail_handler_api_key => 'secret'
  32. ) do
  33. assert_difference 'Issue.count' do
  34. post(
  35. :index,
  36. :params => {
  37. :key => 'secret',
  38. :email =>
  39. IO.read(
  40. File.join(FIXTURES_PATH, 'ticket_on_given_project.eml')
  41. )
  42. }
  43. )
  44. end
  45. end
  46. assert_response 201
  47. end
  48. def test_should_create_issue_with_options
  49. # Enable API and set a key
  50. with_settings(
  51. :mail_handler_api_enabled => 1,
  52. :mail_handler_api_key => 'secret'
  53. ) do
  54. assert_difference 'Issue.count' do
  55. post(
  56. :index,
  57. :params => {
  58. :key => 'secret',
  59. :email =>
  60. IO.read(
  61. File.join(FIXTURES_PATH, 'ticket_on_given_project.eml')
  62. ),
  63. :issue => {
  64. :is_private => '1'
  65. }
  66. }
  67. )
  68. end
  69. end
  70. assert_response 201
  71. issue = Issue.order(:id => :desc).first
  72. assert_equal true, issue.is_private
  73. end
  74. def test_should_update_issue
  75. # Enable API and set a key
  76. with_settings(
  77. :mail_handler_api_enabled => 1,
  78. :mail_handler_api_key => 'secret'
  79. ) do
  80. assert_no_difference 'Issue.count' do
  81. assert_difference 'Journal.count' do
  82. post(
  83. :index,
  84. :params => {
  85. :key => 'secret',
  86. :email => IO.read(File.join(FIXTURES_PATH, 'ticket_reply.eml'))
  87. }
  88. )
  89. end
  90. end
  91. end
  92. assert_response 201
  93. end
  94. def test_should_respond_with_422_if_not_created
  95. Project.find('onlinestore').destroy
  96. with_settings(
  97. :mail_handler_api_enabled => 1,
  98. :mail_handler_api_key => 'secret'
  99. ) do
  100. assert_no_difference 'Issue.count' do
  101. post(
  102. :index,
  103. :params => {
  104. :key => 'secret',
  105. :email =>
  106. IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  107. }
  108. )
  109. end
  110. end
  111. assert_response 422
  112. end
  113. def test_should_not_allow_with_api_disabled
  114. # Disable API
  115. with_settings(
  116. :mail_handler_api_enabled => 0,
  117. :mail_handler_api_key => 'secret'
  118. ) do
  119. assert_no_difference 'Issue.count' do
  120. post(
  121. :index,
  122. :params => {
  123. :key => 'secret',
  124. :email =>
  125. IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  126. }
  127. )
  128. end
  129. end
  130. assert_response 403
  131. assert_include 'Access denied', response.body
  132. end
  133. def test_should_not_allow_with_wrong_key
  134. with_settings(
  135. :mail_handler_api_enabled => 1,
  136. :mail_handler_api_key => 'secret'
  137. ) do
  138. assert_no_difference 'Issue.count' do
  139. post(
  140. :index,
  141. :params => {
  142. :key => 'wrong',
  143. :email =>
  144. IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  145. }
  146. )
  147. end
  148. end
  149. assert_response 403
  150. assert_include 'Access denied', response.body
  151. end
  152. def test_new
  153. with_settings(
  154. :mail_handler_api_enabled => 1,
  155. :mail_handler_api_key => 'secret'
  156. ) do
  157. get(:new, :params => {:key => 'secret'})
  158. end
  159. assert_response :success
  160. end
  161. def test_should_skip_verify_authenticity_token
  162. ActionController::Base.allow_forgery_protection = true
  163. assert_nothing_raised {test_should_create_issue}
  164. ensure
  165. ActionController::Base.allow_forgery_protection = false
  166. end
  167. end