PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/functional/users_controller_test.rb

http://github.com/zena/zena
Ruby | 328 lines | 281 code | 45 blank | 2 comment | 0 complexity | 740a927cd224323fed78dab755c0a980 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. require 'test_helper'
  2. class UsersControllerTest < Zena::Controller::TestCase
  3. context "Accessing index" do
  4. context "with an admin user" do
  5. setup do
  6. login(:lion)
  7. get :index
  8. end
  9. should_assign_to :users
  10. should_render_with_layout :_main
  11. should_respond_with :success
  12. end
  13. context "with a regular user" do
  14. setup do
  15. login(:ant)
  16. get :index
  17. end
  18. should_not_assign_to :users
  19. should_render_without_layout
  20. should_respond_with 404
  21. end
  22. context "with an invalid layout" do
  23. setup do
  24. login(:lion)
  25. # Make a bad admin layout
  26. Version.connection.execute "UPDATE #{Version.table_name} SET properties = '{\"data\":{\"title\":\"foo\",\"text\":\"empty\",\"tkpath\":\"N\"},\"json_class\":\"Property::Properties\"}' WHERE id = #{versions_id(:Node_admin_layout_zafu_en)}"
  27. without_files('test.host/zafu') do
  28. get 'index'
  29. end
  30. end
  31. should_respond_with :success
  32. # Renders with default layout "default/Node-+adminLayout.zafu" => compilation as "_main"
  33. should_render_with_layout :_main
  34. end
  35. end
  36. context "Accessing a user" do
  37. setup do
  38. login(:lion)
  39. get(:show, {'id'=>visitor.id})
  40. end
  41. should 'succeed' do
  42. assert_response :success
  43. end
  44. should_render_without_layout
  45. end
  46. context 'With an admin user' do
  47. setup do
  48. login(:lion)
  49. end
  50. context 'setting dev_skin' do
  51. subject do
  52. get(:dev_skin, {'skin_id' => nodes_zip(:wikiSkin)})
  53. end
  54. should 'store value in visitor properties' do
  55. subject
  56. assert_equal nodes_zip(:wikiSkin), visitor.dev_skin_id
  57. end
  58. end # setting dev_skin
  59. context 'calling rescue' do
  60. subject do
  61. get(:rescue)
  62. end
  63. should 'set dev_skin in visitor properties' do
  64. subject
  65. assert_equal -1, visitor.dev_skin_id
  66. end
  67. end # setting dev_skin
  68. context 'creating a new user' do
  69. subject do
  70. {
  71. :user => {
  72. 'lang' => 'fr',
  73. 'time_zone' => 'Europe/Zurich',
  74. 'status' => '50',
  75. 'password' => 'secret',
  76. 'login' => 'bolomey',
  77. 'group_ids' => [groups_id(:admin), ''],
  78. 'infamous' => '' # This is to test bad blank values
  79. },
  80. '_' => '', # This is in the original post
  81. :action => 'create',
  82. :node => {
  83. 'name' => 'Dupont',
  84. 'first_name' => 'Paul',
  85. 'email' => 'paul.bolomey@brainfuck.com',
  86. },
  87. }
  88. end
  89. should 'succeed' do
  90. assert_difference('User.count', 1) do
  91. post_subject
  92. assert_response :success
  93. user = assigns(:user)
  94. end
  95. end
  96. should 'create a new node' do
  97. assert_difference('Node.count', 1) do
  98. post_subject
  99. end
  100. end
  101. should 'set node attributes' do
  102. post_subject
  103. node = secure(Node) { Node.find(assigns(:user).node_id) }
  104. assert_equal 'Dupont', node.name
  105. assert_equal 'Paul', node.first_name
  106. assert_equal 'paul.bolomey@brainfuck.com', node.email
  107. end
  108. context 'with an existing node' do
  109. subject do
  110. {
  111. :user => {
  112. 'lang' => 'fr',
  113. 'time_zone' => 'Europe/Zurich',
  114. 'status' => '50',
  115. 'password' => 'secret',
  116. 'login' => 'bolomey',
  117. 'group_ids' => [groups_id(:admin), ''],
  118. 'infamous' => '' # This is to test bad blank values
  119. },
  120. '_' => '', # This is in the original post
  121. :action => 'create',
  122. :node => {
  123. 'id' => 'Solen',
  124. 'name' => 'Dupont',
  125. 'first_name' => 'Paul',
  126. 'email' => 'paul.bolomey@brainfuck.com',
  127. },
  128. }
  129. end
  130. should 'succeed' do
  131. assert_difference('User.count', 1) do
  132. post_subject
  133. assert_response :success
  134. user = assigns(:user)
  135. end
  136. end
  137. should 'not create a new node' do
  138. assert_difference('Node.count', 0) do
  139. post_subject
  140. end
  141. end
  142. should 'set node attributes' do
  143. post_subject
  144. node = secure(Node) { Node.find(assigns(:user).node_id) }
  145. assert_equal nodes_id(:ant), node.id
  146. assert_equal 'Dupont', node.name
  147. assert_equal 'Paul', node.first_name
  148. assert_equal 'paul.bolomey@brainfuck.com', node.email
  149. end
  150. end # with an existing node
  151. end # creating a new user
  152. end # With an admin user
  153. context 'Accessing preferences' do
  154. setup do
  155. login(:ant)
  156. get(:preferences, {'id'=>visitor.id})
  157. end
  158. should_respond_with :success
  159. should_render_with_layout :_main
  160. end
  161. context "Updating a user" do
  162. setup do
  163. login(:lion)
  164. put 'update',
  165. 'id' => users_id(:lion),
  166. 'user'=> {
  167. 'lang'=>'en',
  168. 'time_zone'=>'Africa/Algiers',
  169. 'login'=>'lion',
  170. }
  171. end
  172. should_assign_to :user
  173. should_respond_with :success
  174. should "set timezone" do
  175. err assigns(:user)
  176. assert_equal 'Africa/Algiers', users(:lion)[:time_zone]
  177. end
  178. end # Updating a user
  179. context "Changing password" do
  180. setup do
  181. login(:ant)
  182. end
  183. subject do
  184. put 'update',
  185. 'id' => users_id(:ant),
  186. 'update' => 'pass',
  187. 'user' => {
  188. 'password' => 'superman',
  189. 'retype_password' => 'superman',
  190. 'old_password' => 'ant'
  191. }
  192. end
  193. should 'set password' do
  194. subject
  195. assert_response :success
  196. assert users(:ant).valid_password?('superman')
  197. end
  198. context 'with an invalid previous password' do
  199. subject do
  200. put 'update',
  201. 'id' => users_id(:ant),
  202. 'update' => 'pass',
  203. 'user' => {
  204. 'password' => 'superman',
  205. 'retype_password' => 'superman',
  206. 'old_password' => 'antaddf'
  207. }
  208. end
  209. should 'not change password' do
  210. subject
  211. assert_response :success
  212. assert !users(:ant).valid_password?('superman')
  213. assert users(:ant).valid_password?('ant')
  214. end
  215. should 'set an error' do
  216. subject
  217. assert_equal 'not correct', assigns(:user).errors[:old_password]
  218. end
  219. end # with an invalid previous password
  220. context 'by an admin' do
  221. setup do
  222. login(:lion)
  223. end
  224. subject do
  225. put 'update',
  226. 'id' => users_id(:lion),
  227. 'update' => 'pass',
  228. 'user' => {
  229. 'password' => 'superman',
  230. 'retype_password' => 'superman',
  231. 'old_password' => 'lion'
  232. }
  233. end
  234. should 'set password' do
  235. subject
  236. assert_response :success
  237. assert users(:lion).valid_password?('superman')
  238. end
  239. should 'set other user password' do
  240. put 'update',
  241. 'id' => users_id(:ant),
  242. 'update' => 'pass',
  243. 'user' => {
  244. 'password' => 'cymbal'
  245. }
  246. assert_response :success
  247. assert users(:ant).valid_password?('cymbal')
  248. end
  249. should 'not set own password without previous password' do
  250. put 'update',
  251. 'id' => users_id(:lion),
  252. 'update' => 'pass',
  253. 'user' => {
  254. 'password' => 'cymbal'
  255. }
  256. assert_response :success
  257. assert_equal 'not correct', assigns(:user).errors[:old_password]
  258. end
  259. context 'with an invalid previous password' do
  260. subject do
  261. put 'update',
  262. 'id' => users_id(:lion),
  263. 'update' => 'pass',
  264. 'user' => {
  265. 'password' => 'superman',
  266. 'retype_password' => 'superman',
  267. 'old_password' => 'antaddf'
  268. }
  269. end
  270. should 'not change password' do
  271. subject
  272. assert_response :success
  273. assert !assigns(:user).valid_password?('superman')
  274. assert assigns(:user).valid_password?('lion')
  275. end
  276. should 'set an error' do
  277. subject
  278. assert_equal 'not correct', assigns(:user).errors[:old_password]
  279. end
  280. end # with an invalid previous password
  281. end # by an admin
  282. end # Changing password
  283. end