PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/controllers/pseudonyms_controller_spec.rb

https://github.com/kidakaka/canvas-lms
Ruby | 248 lines | 221 code | 10 blank | 17 comment | 9 complexity | 7334c843ccb46272ad10aea3f2b7b24e MD5 | raw file
  1. #
  2. # Copyright (C) 2011 Instructure, Inc.
  3. #
  4. # This file is part of Canvas.
  5. #
  6. # Canvas is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU Affero General Public License as published by the Free
  8. # Software Foundation, version 3 of the License.
  9. #
  10. # Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License along
  16. # with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  19. describe PseudonymsController do
  20. describe "registration" do
  21. it "should approve an unapproved communication channel" do
  22. user_with_pseudonym
  23. user_session(@user, @pseudonym)
  24. get 'claim_pseudonym', :id => @pseudonym, :nonce => @cc.confirmation_code, :claim => '1'
  25. response.should be_redirect
  26. response.should redirect_to(dashboard_url)
  27. @cc.reload
  28. @cc.should be_active
  29. end
  30. it "should not approve an already-approved communication channel" do
  31. user_with_pseudonym
  32. user_session(@user, @pseudonym)
  33. code = @cc.confirmation_code
  34. @cc.confirm
  35. get 'claim_pseudonym', :id => @pseudonym, :nonce => code, :claim => '1'
  36. response.should be_redirect
  37. response.should redirect_to(root_url)
  38. end
  39. it "should re-send communication channel invitation for an invited channel" do
  40. user_with_pseudonym(:active_user => true)
  41. Notification.create(:name => 'Confirm Email Communication Channel')
  42. get 're_send_confirmation', :user_id => @pseudonym.user_id, :id => @cc.id
  43. response.should be_success
  44. assigns[:user].should eql(@user)
  45. assigns[:cc].should eql(@cc)
  46. assigns[:cc].messages_sent.should_not be_nil
  47. end
  48. it "should re-send enrollment invitation for an invited user" do
  49. user_with_pseudonym(:active_user => true)
  50. course(:active_all => true)
  51. @enrollment = @course.enroll_user(@user)
  52. @enrollment.context.should eql(@course)
  53. Notification.create(:name => 'Enrollment Invitation')
  54. get 're_send_confirmation', :user_id => @pseudonym.user_id, :id => @cc.id, :enrollment_id => @enrollment.id
  55. response.should be_success
  56. assigns[:user].should eql(@user)
  57. assigns[:cc].should eql(@cc)
  58. assigns[:enrollment].should eql(@enrollment)
  59. assigns[:enrollment].messages_sent.should_not be_nil
  60. end
  61. it "should send password-change email for a registered user" do
  62. user_with_pseudonym
  63. get 'forgot_password', :pseudonym_session => {:unique_id_forgot => @pseudonym.unique_id}
  64. response.should be_redirect
  65. assigns[:ccs].should include(@cc)
  66. assigns[:ccs].detect{|cc| cc == @cc}.messages_sent.should_not be_nil
  67. end
  68. it "should render confirm change password view for registered user's email" do
  69. user_with_pseudonym(:active_user => true)
  70. get 'confirm_change_password', :pseudonym_id => @pseudonym.id, :nonce => @cc.confirmation_code
  71. response.should be_success
  72. end
  73. it "should not render confirm change password view for non-email channels" do
  74. user_with_pseudonym(:active_user => true)
  75. @cc.update_attributes(:path_type => 'sms')
  76. get 'confirm_change_password', :pseudonym_id => @pseudonym.id, :nonce => @cc.confirmation_code
  77. response.should be_redirect
  78. end
  79. it "should render confirm change password view for unregistered user" do
  80. user_with_pseudonym
  81. get 'confirm_change_password', :pseudonym_id => @pseudonym.id, :nonce => @cc.confirmation_code
  82. response.should be_success
  83. end
  84. it "should change the password if authorized" do
  85. user_with_pseudonym
  86. pword = @pseudonym.crypted_password
  87. code = @cc.confirmation_code
  88. post 'change_password', :pseudonym_id => @pseudonym.id, :nonce => @cc.confirmation_code, :pseudonym => {:password => '12341234', :password_confirmation => '12341234'}
  89. response.should be_redirect
  90. assigns[:pseudonym].should eql(@pseudonym)
  91. assigns[:pseudonym].crypted_password.should_not eql(pword)
  92. assigns[:pseudonym].user.should be_registered
  93. assigns[:cc].confirmation_code.should_not eql(code)
  94. assigns[:cc].should be_active
  95. end
  96. it "should not change the password if unauthorized" do
  97. user_with_pseudonym
  98. pword = @pseudonym.crypted_password
  99. code = @cc.confirmation_code
  100. post 'change_password', :pseudonym_id => @pseudonym.id, :nonce => @cc.confirmation_code + 'a', :pseudonym => {:password => '12341234', :password_confirmation => '12341234'}
  101. response.should be_redirect
  102. assigns[:pseudonym].should eql(@pseudonym)
  103. assigns[:pseudonym].crypted_password.should eql(pword)
  104. assigns[:pseudonym].user.should_not be_registered
  105. @cc.reload
  106. @cc.confirmation_code.should eql(code)
  107. @cc.should_not be_active
  108. end
  109. it "should render 'claim pseudonym' page" do
  110. user_with_pseudonym
  111. get 'claim_pseudonym', :id => @pseudonym.id, :nonce => @cc.confirmation_code, :claim => '1'
  112. response.should be_success
  113. assigns[:pseudonym].should eql(@pseudonym)
  114. end
  115. it "should claim pseudonym for an already-logged-in user" do
  116. user
  117. @not_logged_user = @user
  118. user_with_pseudonym
  119. @logged_user = @user
  120. user_session(@logged_user, @pseudonym)
  121. @p2 = @not_logged_user.pseudonyms.create!(:unique_id => 'another@example.com', :path => 'another@example.com', :password => 'asdfqwer', :password_confirmation => 'asdfqwer')
  122. @cc2 = @p2.communication_channel
  123. get 'claim_pseudonym', :id => @p2.id, :nonce => @cc2.confirmation_code, :claim => '1'
  124. response.should be_redirect
  125. assigns[:pseudonym].reload
  126. assigns[:pseudonym].should eql(@p2)
  127. assigns[:pseudonym].user.should eql(@logged_user)
  128. assigns[:pseudonym].communication_channel.reload
  129. assigns[:pseudonym].communication_channel.should be_active
  130. @not_logged_user.reload
  131. @not_logged_user.should be_deleted
  132. end
  133. it "should finalize registration for a pre-registered user" do
  134. user_with_pseudonym
  135. get 'registration_confirmation', :id => @pseudonym.id, :nonce => @cc.confirmation_code
  136. response.should be_redirect
  137. @pseudonym.reload
  138. @pseudonym.user.should be_registered
  139. @cc.reload
  140. @cc.should be_active
  141. end
  142. it "should not finalize registration for invalid parameters" do
  143. user_with_pseudonym
  144. @cc.confirm!
  145. get 'registration_confirmation', :id => @pseudonym.id, :nonce => "asdf" #@cc.confirmation_code
  146. response.should render_template("registration_confirmation_failed")
  147. @pseudonym.reload
  148. @pseudonym.user.should_not be_registered
  149. end
  150. it "should register creation_pending user" do
  151. course
  152. @course.should_not be_available
  153. user_with_pseudonym
  154. @enrollment = @course.enroll_student(@user)
  155. @user.should_not be_registered
  156. @enrollment.should be_creation_pending
  157. get 'registration_confirmation', :id => @pseudonym.id, :nonce => @cc.confirmation_code
  158. response.should be_redirect
  159. @user.reload
  160. @user.should be_registered
  161. end
  162. end
  163. describe "destroy" do
  164. it "should not destroy if for the wrong user" do
  165. rescue_action_in_public!
  166. user_model
  167. @other_user = @user
  168. @other_pseudonym = @user.pseudonyms.create!(:unique_id => "test@test.com", :password => "password", :password_confirmation => "password")
  169. user_with_pseudonym(:active_all => true)
  170. user_session(@user, @pseudonym)
  171. delete 'destroy', :user_id => @user.id, :id => @other_pseudonym.id
  172. assert_status(404)
  173. @other_pseudonym.should be_active
  174. @pseudonym.should be_active
  175. delete 'destroy', :user_id => @other_user.id, :id => @pseudonym.id
  176. assert_unauthorized
  177. @other_pseudonym.should be_active
  178. @pseudonym.should be_active
  179. end
  180. it "should not destroy if it's the last active pseudonym" do
  181. user_with_pseudonym(:active_all => true)
  182. user_session(@user, @pseudonym)
  183. delete 'destroy', :user_id => @user.id, :id => @pseudonym.id
  184. assert_status(400)
  185. @pseudonym.should be_active
  186. end
  187. it "should destroy if for the current user with more than one pseudonym" do
  188. user_with_pseudonym(:active_all => true)
  189. user_session(@user, @pseudonym)
  190. @p2 = @user.pseudonyms.create!(:unique_id => "another_one@test.com",:password => 'password', :password_confirmation => 'password')
  191. delete 'destroy', :user_id => @user.id, :id => @p2.id
  192. assert_status(200)
  193. @pseudonym.should be_active
  194. @p2.reload.should be_deleted
  195. end
  196. it "should not destroy if for the current user and it's a system-generated pseudonym" do
  197. rescue_action_in_public!
  198. user_with_pseudonym(:active_all => true)
  199. user_session(@user, @pseudonym)
  200. @p2 = @user.pseudonyms.create!(:unique_id => "another_one@test.com",:password => 'password', :password_confirmation => 'password')
  201. @p2.sis_source_id = 'test'
  202. @p2.save!
  203. @p2.account.account_authorization_configs.create!(:auth_type => 'ldap')
  204. delete 'destroy', :user_id => @user.id, :id => @p2.id
  205. assert_status(500)
  206. @pseudonym.should be_active
  207. @p2.should be_active
  208. end
  209. it "should destroy if authorized to delete pseudonyms" do
  210. rescue_action_in_public!
  211. user_with_pseudonym(:active_all => true)
  212. Account.site_admin.add_user(@user)
  213. user_session(@user, @pseudonym)
  214. @p2 = @user.pseudonyms.build(:unique_id => "another_one@test.com",:password => 'password', :password_confirmation => 'password')
  215. @p2.sis_source_id = 'test'
  216. @p2.save!
  217. @p2.account.account_authorization_configs.create!(:auth_type => 'ldap')
  218. delete 'destroy', :user_id => @user.id, :id => @p2.id
  219. assert_status(200)
  220. @pseudonym.should be_active
  221. @p2.should be_active
  222. end
  223. end
  224. end