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.

users_test.rb 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 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 Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
  19. fixtures :users, :email_addresses, :members, :member_roles, :roles, :projects
  20. test "GET /users.xml should return users" do
  21. get '/users.xml', {}, credentials('admin')
  22. assert_response :success
  23. assert_equal 'application/xml', response.content_type
  24. assert_select 'users' do
  25. assert_select 'user', assigns(:users).size
  26. end
  27. end
  28. test "GET /users.json should return users" do
  29. get '/users.json', {}, credentials('admin')
  30. assert_response :success
  31. assert_equal 'application/json', response.content_type
  32. json = ActiveSupport::JSON.decode(response.body)
  33. assert json.key?('users')
  34. assert_equal assigns(:users).size, json['users'].size
  35. end
  36. test "GET /users/:id.xml should return the user" do
  37. get '/users/2.xml'
  38. assert_response :success
  39. assert_select 'user id', :text => '2'
  40. end
  41. test "GET /users/:id.json should return the user" do
  42. get '/users/2.json'
  43. assert_response :success
  44. json = ActiveSupport::JSON.decode(response.body)
  45. assert_kind_of Hash, json
  46. assert_kind_of Hash, json['user']
  47. assert_equal 2, json['user']['id']
  48. end
  49. test "GET /users/:id.xml with include=memberships should include memberships" do
  50. get '/users/2.xml?include=memberships'
  51. assert_response :success
  52. assert_select 'user memberships', 1
  53. end
  54. test "GET /users/:id.json with include=memberships should include memberships" do
  55. get '/users/2.json?include=memberships'
  56. assert_response :success
  57. json = ActiveSupport::JSON.decode(response.body)
  58. assert_kind_of Array, json['user']['memberships']
  59. assert_equal [{
  60. "id"=>1,
  61. "project"=>{"name"=>"eCookbook", "id"=>1},
  62. "roles"=>[{"name"=>"Manager", "id"=>1}]
  63. }], json['user']['memberships']
  64. end
  65. test "GET /users/current.xml should require authentication" do
  66. get '/users/current.xml'
  67. assert_response 401
  68. end
  69. test "GET /users/current.xml should return current user" do
  70. get '/users/current.xml', {}, credentials('jsmith')
  71. assert_select 'user id', :text => '2'
  72. end
  73. test "GET /users/:id should not return login for other user" do
  74. get '/users/3.xml', {}, credentials('jsmith')
  75. assert_response :success
  76. assert_select 'user login', 0
  77. end
  78. test "GET /users/:id should return login for current user" do
  79. get '/users/2.xml', {}, credentials('jsmith')
  80. assert_response :success
  81. assert_select 'user login', :text => 'jsmith'
  82. end
  83. test "GET /users/:id should not return api_key for other user" do
  84. get '/users/3.xml', {}, credentials('jsmith')
  85. assert_response :success
  86. assert_select 'user api_key', 0
  87. end
  88. test "GET /users/:id should return api_key for current user" do
  89. get '/users/2.xml', {}, credentials('jsmith')
  90. assert_response :success
  91. assert_select 'user api_key', :text => User.find(2).api_key
  92. end
  93. test "GET /users/:id should not return status for standard user" do
  94. get '/users/3.xml', {}, credentials('jsmith')
  95. assert_response :success
  96. assert_select 'user status', 0
  97. end
  98. test "GET /users/:id should return status for administrators" do
  99. get '/users/2.xml', {}, credentials('admin')
  100. assert_response :success
  101. assert_select 'user status', :text => User.find(1).status.to_s
  102. end
  103. test "POST /users.xml with valid parameters should create the user" do
  104. assert_difference('User.count') do
  105. post '/users.xml', {
  106. :user => {
  107. :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
  108. :mail => 'foo@example.net', :password => 'secret123',
  109. :mail_notification => 'only_assigned'}
  110. },
  111. credentials('admin')
  112. end
  113. user = User.order('id DESC').first
  114. assert_equal 'foo', user.login
  115. assert_equal 'Firstname', user.firstname
  116. assert_equal 'Lastname', user.lastname
  117. assert_equal 'foo@example.net', user.mail
  118. assert_equal 'only_assigned', user.mail_notification
  119. assert !user.admin?
  120. assert user.check_password?('secret123')
  121. assert_response :created
  122. assert_equal 'application/xml', @response.content_type
  123. assert_select 'user id', :text => user.id.to_s
  124. end
  125. test "POST /users.json with valid parameters should create the user" do
  126. assert_difference('User.count') do
  127. post '/users.json', {
  128. :user => {
  129. :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
  130. :mail => 'foo@example.net', :password => 'secret123',
  131. :mail_notification => 'only_assigned'}
  132. },
  133. credentials('admin')
  134. end
  135. user = User.order('id DESC').first
  136. assert_equal 'foo', user.login
  137. assert_equal 'Firstname', user.firstname
  138. assert_equal 'Lastname', user.lastname
  139. assert_equal 'foo@example.net', user.mail
  140. assert !user.admin?
  141. assert_response :created
  142. assert_equal 'application/json', @response.content_type
  143. json = ActiveSupport::JSON.decode(response.body)
  144. assert_kind_of Hash, json
  145. assert_kind_of Hash, json['user']
  146. assert_equal user.id, json['user']['id']
  147. end
  148. test "POST /users.xml with with invalid parameters should return errors" do
  149. assert_no_difference('User.count') do
  150. post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
  151. end
  152. assert_response :unprocessable_entity
  153. assert_equal 'application/xml', @response.content_type
  154. assert_select 'errors error', :text => "First name cannot be blank"
  155. end
  156. test "POST /users.json with with invalid parameters should return errors" do
  157. assert_no_difference('User.count') do
  158. post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
  159. end
  160. assert_response :unprocessable_entity
  161. assert_equal 'application/json', @response.content_type
  162. json = ActiveSupport::JSON.decode(response.body)
  163. assert_kind_of Hash, json
  164. assert json.has_key?('errors')
  165. assert_kind_of Array, json['errors']
  166. end
  167. test "PUT /users/:id.xml with valid parameters should update the user" do
  168. assert_no_difference('User.count') do
  169. put '/users/2.xml', {
  170. :user => {
  171. :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
  172. :mail => 'jsmith@somenet.foo'}
  173. },
  174. credentials('admin')
  175. end
  176. user = User.find(2)
  177. assert_equal 'jsmith', user.login
  178. assert_equal 'John', user.firstname
  179. assert_equal 'Renamed', user.lastname
  180. assert_equal 'jsmith@somenet.foo', user.mail
  181. assert !user.admin?
  182. assert_response :ok
  183. assert_equal '', @response.body
  184. end
  185. test "PUT /users/:id.json with valid parameters should update the user" do
  186. assert_no_difference('User.count') do
  187. put '/users/2.json', {
  188. :user => {
  189. :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
  190. :mail => 'jsmith@somenet.foo'}
  191. },
  192. credentials('admin')
  193. end
  194. user = User.find(2)
  195. assert_equal 'jsmith', user.login
  196. assert_equal 'John', user.firstname
  197. assert_equal 'Renamed', user.lastname
  198. assert_equal 'jsmith@somenet.foo', user.mail
  199. assert !user.admin?
  200. assert_response :ok
  201. assert_equal '', @response.body
  202. end
  203. test "PUT /users/:id.xml with invalid parameters" do
  204. assert_no_difference('User.count') do
  205. put '/users/2.xml', {
  206. :user => {
  207. :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
  208. :mail => 'foo'}
  209. },
  210. credentials('admin')
  211. end
  212. assert_response :unprocessable_entity
  213. assert_equal 'application/xml', @response.content_type
  214. assert_select 'errors error', :text => "First name cannot be blank"
  215. end
  216. test "PUT /users/:id.json with invalid parameters" do
  217. assert_no_difference('User.count') do
  218. put '/users/2.json', {
  219. :user => {
  220. :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
  221. :mail => 'foo'}
  222. },
  223. credentials('admin')
  224. end
  225. assert_response :unprocessable_entity
  226. assert_equal 'application/json', @response.content_type
  227. json = ActiveSupport::JSON.decode(response.body)
  228. assert_kind_of Hash, json
  229. assert json.has_key?('errors')
  230. assert_kind_of Array, json['errors']
  231. end
  232. test "DELETE /users/:id.xml should delete the user" do
  233. assert_difference('User.count', -1) do
  234. delete '/users/2.xml', {}, credentials('admin')
  235. end
  236. assert_response :ok
  237. assert_equal '', @response.body
  238. end
  239. test "DELETE /users/:id.json should delete the user" do
  240. assert_difference('User.count', -1) do
  241. delete '/users/2.json', {}, credentials('admin')
  242. end
  243. assert_response :ok
  244. assert_equal '', @response.body
  245. end
  246. end