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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class MailHandlerControllerTest < Redmine::ControllerTest
  19. fixtures :users, :email_addresses, :projects, :enabled_modules, :roles, :members, :member_roles, :issues, :issue_statuses,
  20. :trackers, :projects_trackers, :enumerations
  21. FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
  22. def setup
  23. User.current = nil
  24. end
  25. def test_should_create_issue
  26. # Enable API and set a key
  27. Setting.mail_handler_api_enabled = 1
  28. Setting.mail_handler_api_key = 'secret'
  29. assert_difference 'Issue.count' do
  30. post :index, :params => {
  31. :key => 'secret',
  32. :email => IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  33. }
  34. end
  35. assert_response 201
  36. end
  37. def test_should_create_issue_with_options
  38. # Enable API and set a key
  39. Setting.mail_handler_api_enabled = 1
  40. Setting.mail_handler_api_key = 'secret'
  41. assert_difference 'Issue.count' do
  42. post :index, :params => {
  43. :key => 'secret',
  44. :email => IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml')),
  45. :issue => {
  46. :is_private => '1'
  47. }
  48. }
  49. end
  50. assert_response 201
  51. issue = Issue.order(:id => :desc).first
  52. assert_equal true, issue.is_private
  53. end
  54. def test_should_update_issue
  55. # Enable API and set a key
  56. Setting.mail_handler_api_enabled = 1
  57. Setting.mail_handler_api_key = 'secret'
  58. assert_no_difference 'Issue.count' do
  59. assert_difference 'Journal.count' do
  60. post :index, :params => {
  61. :key => 'secret',
  62. :email => IO.read(File.join(FIXTURES_PATH, 'ticket_reply.eml'))
  63. }
  64. end
  65. end
  66. assert_response 201
  67. end
  68. def test_should_respond_with_422_if_not_created
  69. Project.find('onlinestore').destroy
  70. Setting.mail_handler_api_enabled = 1
  71. Setting.mail_handler_api_key = 'secret'
  72. assert_no_difference 'Issue.count' do
  73. post :index, :params => {
  74. :key => 'secret',
  75. :email => IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  76. }
  77. end
  78. assert_response 422
  79. end
  80. def test_should_not_allow_with_api_disabled
  81. # Disable API
  82. Setting.mail_handler_api_enabled = 0
  83. Setting.mail_handler_api_key = 'secret'
  84. assert_no_difference 'Issue.count' do
  85. post :index, :params => {
  86. :key => 'secret',
  87. :email => IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  88. }
  89. end
  90. assert_response 403
  91. assert_include 'Access denied', response.body
  92. end
  93. def test_should_not_allow_with_wrong_key
  94. Setting.mail_handler_api_enabled = 1
  95. Setting.mail_handler_api_key = 'secret'
  96. assert_no_difference 'Issue.count' do
  97. post :index, :params => {
  98. :key => 'wrong',
  99. :email => IO.read(File.join(FIXTURES_PATH, 'ticket_on_given_project.eml'))
  100. }
  101. end
  102. assert_response 403
  103. assert_include 'Access denied', response.body
  104. end
  105. def test_new
  106. Setting.mail_handler_api_enabled = 1
  107. Setting.mail_handler_api_key = 'secret'
  108. get :new, :params => {
  109. :key => 'secret'
  110. }
  111. assert_response :success
  112. end
  113. end