PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/integration/api_test/users_test.rb

https://github.com/ginkel/redmine
Ruby | 275 lines | 229 code | 30 blank | 16 comment | 1 complexity | 6905251b1347f6f64707d32fa6c69fdb MD5 | raw file
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2010 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. require 'pp'
  19. class ApiTest::UsersTest < ActionController::IntegrationTest
  20. fixtures :users
  21. def setup
  22. Setting.rest_api_enabled = '1'
  23. end
  24. context "GET /users" do
  25. should_allow_api_authentication(:get, "/users.xml")
  26. should_allow_api_authentication(:get, "/users.json")
  27. end
  28. context "GET /users/2" do
  29. context ".xml" do
  30. should "return requested user" do
  31. get '/users/2.xml'
  32. assert_tag :tag => 'user',
  33. :child => {:tag => 'id', :content => '2'}
  34. end
  35. end
  36. context ".json" do
  37. should "return requested user" do
  38. get '/users/2.json'
  39. json = ActiveSupport::JSON.decode(response.body)
  40. assert_kind_of Hash, json
  41. assert_kind_of Hash, json['user']
  42. assert_equal 2, json['user']['id']
  43. end
  44. end
  45. end
  46. context "GET /users/current" do
  47. context ".xml" do
  48. should "require authentication" do
  49. get '/users/current.xml'
  50. assert_response 401
  51. end
  52. should "return current user" do
  53. get '/users/current.xml', {}, :authorization => credentials('jsmith')
  54. assert_tag :tag => 'user',
  55. :child => {:tag => 'id', :content => '2'}
  56. end
  57. end
  58. end
  59. context "POST /users" do
  60. context "with valid parameters" do
  61. setup do
  62. @parameters = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}}
  63. end
  64. context ".xml" do
  65. should_allow_api_authentication(:post,
  66. '/users.xml',
  67. {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}},
  68. {:success_code => :created})
  69. should "create a user with the attributes" do
  70. assert_difference('User.count') do
  71. post '/users.xml', @parameters, :authorization => credentials('admin')
  72. end
  73. user = User.first(:order => 'id DESC')
  74. assert_equal 'foo', user.login
  75. assert_equal 'Firstname', user.firstname
  76. assert_equal 'Lastname', user.lastname
  77. assert_equal 'foo@example.net', user.mail
  78. assert_equal 'only_assigned', user.mail_notification
  79. assert !user.admin?
  80. assert user.check_password?('secret')
  81. assert_response :created
  82. assert_equal 'application/xml', @response.content_type
  83. assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
  84. end
  85. end
  86. context ".json" do
  87. should_allow_api_authentication(:post,
  88. '/users.json',
  89. {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}},
  90. {:success_code => :created})
  91. should "create a user with the attributes" do
  92. assert_difference('User.count') do
  93. post '/users.json', @parameters, :authorization => credentials('admin')
  94. end
  95. user = User.first(:order => 'id DESC')
  96. assert_equal 'foo', user.login
  97. assert_equal 'Firstname', user.firstname
  98. assert_equal 'Lastname', user.lastname
  99. assert_equal 'foo@example.net', user.mail
  100. assert !user.admin?
  101. assert_response :created
  102. assert_equal 'application/json', @response.content_type
  103. json = ActiveSupport::JSON.decode(response.body)
  104. assert_kind_of Hash, json
  105. assert_kind_of Hash, json['user']
  106. assert_equal user.id, json['user']['id']
  107. end
  108. end
  109. end
  110. context "with invalid parameters" do
  111. setup do
  112. @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
  113. end
  114. context ".xml" do
  115. should "return errors" do
  116. assert_no_difference('User.count') do
  117. post '/users.xml', @parameters, :authorization => credentials('admin')
  118. end
  119. assert_response :unprocessable_entity
  120. assert_equal 'application/xml', @response.content_type
  121. assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"}
  122. end
  123. end
  124. context ".json" do
  125. should "return errors" do
  126. assert_no_difference('User.count') do
  127. post '/users.json', @parameters, :authorization => credentials('admin')
  128. end
  129. assert_response :unprocessable_entity
  130. assert_equal 'application/json', @response.content_type
  131. json = ActiveSupport::JSON.decode(response.body)
  132. assert_kind_of Hash, json
  133. assert json.has_key?('errors')
  134. assert_kind_of Array, json['errors']
  135. end
  136. end
  137. end
  138. end
  139. context "PUT /users/2" do
  140. context "with valid parameters" do
  141. setup do
  142. @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}
  143. end
  144. context ".xml" do
  145. should_allow_api_authentication(:put,
  146. '/users/2.xml',
  147. {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
  148. {:success_code => :ok})
  149. should "update user with the attributes" do
  150. assert_no_difference('User.count') do
  151. put '/users/2.xml', @parameters, :authorization => credentials('admin')
  152. end
  153. user = User.find(2)
  154. assert_equal 'jsmith', user.login
  155. assert_equal 'John', user.firstname
  156. assert_equal 'Renamed', user.lastname
  157. assert_equal 'jsmith@somenet.foo', user.mail
  158. assert !user.admin?
  159. assert_response :ok
  160. end
  161. end
  162. context ".json" do
  163. should_allow_api_authentication(:put,
  164. '/users/2.json',
  165. {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
  166. {:success_code => :ok})
  167. should "update user with the attributes" do
  168. assert_no_difference('User.count') do
  169. put '/users/2.json', @parameters, :authorization => credentials('admin')
  170. end
  171. user = User.find(2)
  172. assert_equal 'jsmith', user.login
  173. assert_equal 'John', user.firstname
  174. assert_equal 'Renamed', user.lastname
  175. assert_equal 'jsmith@somenet.foo', user.mail
  176. assert !user.admin?
  177. assert_response :ok
  178. end
  179. end
  180. end
  181. context "with invalid parameters" do
  182. setup do
  183. @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}}
  184. end
  185. context ".xml" do
  186. should "return errors" do
  187. assert_no_difference('User.count') do
  188. put '/users/2.xml', @parameters, :authorization => credentials('admin')
  189. end
  190. assert_response :unprocessable_entity
  191. assert_equal 'application/xml', @response.content_type
  192. assert_tag 'errors', :child => {:tag => 'error', :content => "Firstname can't be blank"}
  193. end
  194. end
  195. context ".json" do
  196. should "return errors" do
  197. assert_no_difference('User.count') do
  198. put '/users/2.json', @parameters, :authorization => credentials('admin')
  199. end
  200. assert_response :unprocessable_entity
  201. assert_equal 'application/json', @response.content_type
  202. json = ActiveSupport::JSON.decode(response.body)
  203. assert_kind_of Hash, json
  204. assert json.has_key?('errors')
  205. assert_kind_of Array, json['errors']
  206. end
  207. end
  208. end
  209. context "DELETE /users/2" do
  210. context ".xml" do
  211. should "not be allowed" do
  212. assert_no_difference('User.count') do
  213. delete '/users/2.xml'
  214. end
  215. assert_response :method_not_allowed
  216. end
  217. end
  218. context ".json" do
  219. should "not be allowed" do
  220. assert_no_difference('User.count') do
  221. delete '/users/2.json'
  222. end
  223. assert_response :method_not_allowed
  224. end
  225. end
  226. end
  227. end
  228. def credentials(user, password=nil)
  229. ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
  230. end
  231. end