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.

my_test.rb 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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 File.expand_path('../../../test_helper', __FILE__)
  19. class Redmine::ApiTest::MyTest < Redmine::ApiTest::Base
  20. fixtures :users, :email_addresses, :members, :member_roles, :roles, :projects
  21. test "GET /my/account.json should return user" do
  22. assert Setting.rest_api_enabled?
  23. get '/my/account.json', :headers => credentials('dlopper', 'foo')
  24. assert_response :success
  25. assert_equal 'application/json', response.content_type
  26. json = ActiveSupport::JSON.decode(response.body)
  27. assert json.key?('user')
  28. assert_equal 'dlopper', json['user']['login']
  29. end
  30. test "PUT /my/account.xml with valid parameters should update the user" do
  31. put '/my/account.xml',
  32. :params => {
  33. :user => {
  34. :firstname => 'Dave', :lastname => 'Renamed',
  35. :mail => 'dave@somenet.foo'
  36. }
  37. },
  38. :headers => credentials('dlopper', 'foo')
  39. assert_response :no_content
  40. assert_equal '', @response.body
  41. assert user = User.find_by_lastname('Renamed')
  42. assert_equal 'Dave', user.firstname
  43. assert_equal 'Renamed', user.lastname
  44. assert_equal 'dave@somenet.foo', user.mail
  45. refute user.admin?
  46. end
  47. test "PUT /my/account.json with valid parameters should update the user" do
  48. put '/my/account.xml',
  49. :params => {
  50. :user => {
  51. :firstname => 'Dave', :lastname => 'Renamed',
  52. :mail => 'dave@somenet.foo'
  53. }
  54. },
  55. :headers => credentials('dlopper', 'foo')
  56. assert_response :no_content
  57. assert_equal '', @response.body
  58. assert user = User.find_by_lastname('Renamed')
  59. assert_equal 'Dave', user.firstname
  60. assert_equal 'Renamed', user.lastname
  61. assert_equal 'dave@somenet.foo', user.mail
  62. refute user.admin?
  63. end
  64. test "PUT /my/account.xml with invalid parameters" do
  65. put '/my/account.xml',
  66. :params => {
  67. :user => {
  68. :login => 'dlopper', :firstname => '', :lastname => 'Lastname'
  69. }
  70. },
  71. :headers => credentials('dlopper', 'foo')
  72. assert_response :unprocessable_entity
  73. assert_equal 'application/xml', @response.content_type
  74. assert_select 'errors error', :text => "First name cannot be blank"
  75. end
  76. test "PUT /my/account.json with invalid parameters" do
  77. put '/my/account.json',
  78. :params => {
  79. :user => {
  80. :login => 'dlopper', :firstname => '', :lastname => 'Lastname'
  81. }
  82. },
  83. :headers => credentials('dlopper', 'foo')
  84. assert_response :unprocessable_entity
  85. assert_equal 'application/json', @response.content_type
  86. json = ActiveSupport::JSON.decode(response.body)
  87. assert_kind_of Hash, json
  88. assert json.has_key?('errors')
  89. assert_kind_of Array, json['errors']
  90. end
  91. end