registrations_controller_spec.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Auth::RegistrationsController do
  4. render_views
  5. shared_examples 'checks for enabled registrations' do |path|
  6. it 'redirects if it is in single user mode while it is open for registration' do
  7. Fabricate(:account)
  8. Setting.registrations_mode = 'open'
  9. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
  10. get path
  11. expect(response).to redirect_to '/'
  12. expect(Rails.configuration.x).to have_received(:single_user_mode)
  13. end
  14. it 'redirects if it is not open for registration while it is not in single user mode' do
  15. Setting.registrations_mode = 'none'
  16. allow(Rails.configuration.x).to receive(:single_user_mode).and_return(false)
  17. get path
  18. expect(response).to redirect_to '/'
  19. expect(Rails.configuration.x).to have_received(:single_user_mode)
  20. end
  21. end
  22. describe 'GET #edit' do
  23. before do
  24. request.env['devise.mapping'] = Devise.mappings[:user]
  25. sign_in(Fabricate(:user))
  26. get :edit
  27. end
  28. it 'returns http success' do
  29. expect(response).to have_http_status(200)
  30. end
  31. it 'returns private cache control header' do
  32. expect(response.headers['Cache-Control']).to include('private, no-store')
  33. end
  34. end
  35. describe 'PUT #update' do
  36. let(:current_password) { 'current password' }
  37. let(:user) { Fabricate(:user, password: current_password) }
  38. before do
  39. request.env['devise.mapping'] = Devise.mappings[:user]
  40. sign_in(user, scope: :user)
  41. end
  42. it 'returns http success' do
  43. put :update
  44. expect(response).to have_http_status(200)
  45. end
  46. it 'returns private cache control headers' do
  47. put :update
  48. expect(response.headers['Cache-Control']).to include('private, no-store')
  49. end
  50. it 'can update the user email' do
  51. expect do
  52. put :update, params: {
  53. user: {
  54. email: 'newemail@example.com',
  55. current_password: current_password,
  56. },
  57. }
  58. expect(response).to redirect_to(edit_user_registration_path)
  59. end.to change { user.reload.unconfirmed_email }.to('newemail@example.com')
  60. end
  61. it 'requires the current password to update the email' do
  62. expect do
  63. put :update, params: {
  64. user: {
  65. email: 'newemail@example.com',
  66. current_password: 'something',
  67. },
  68. }
  69. expect(response).to have_http_status(200)
  70. end.to_not(change { user.reload.unconfirmed_email })
  71. end
  72. it 'can update the user password' do
  73. expect do
  74. put :update, params: {
  75. user: {
  76. password: 'new password',
  77. password_confirmation: 'new password',
  78. current_password: current_password,
  79. },
  80. }
  81. expect(response).to redirect_to(edit_user_registration_path)
  82. end.to(change { user.reload.encrypted_password })
  83. end
  84. it 'requires the password confirmation' do
  85. expect do
  86. put :update, params: {
  87. user: {
  88. password: 'new password',
  89. password_confirmation: 'something else',
  90. current_password: current_password,
  91. },
  92. }
  93. expect(response).to have_http_status(200)
  94. end.to_not(change { user.reload.encrypted_password })
  95. end
  96. it 'requires the current password to update the password' do
  97. expect do
  98. put :update, params: {
  99. user: {
  100. password: 'new password',
  101. password_confirmation: 'new password',
  102. current_password: 'something',
  103. },
  104. }
  105. expect(response).to have_http_status(200)
  106. end.to_not(change { user.reload.encrypted_password })
  107. end
  108. context 'when suspended' do
  109. let(:user) { Fabricate(:user, account_attributes: { username: 'test', suspended_at: Time.now.utc }) }
  110. it 'returns http forbidden' do
  111. put :update
  112. expect(response).to have_http_status(403)
  113. end
  114. end
  115. end
  116. describe 'GET #new' do
  117. before do
  118. request.env['devise.mapping'] = Devise.mappings[:user]
  119. end
  120. context 'with open registrations' do
  121. it 'returns http success' do
  122. Setting.registrations_mode = 'open'
  123. get :new
  124. expect(response).to have_http_status(200)
  125. end
  126. end
  127. include_examples 'checks for enabled registrations', :new
  128. end
  129. describe 'POST #create' do
  130. let(:accept_language) { 'de' }
  131. before do
  132. session[:registration_form_time] = 5.seconds.ago
  133. request.env['devise.mapping'] = Devise.mappings[:user]
  134. end
  135. around do |example|
  136. I18n.with_locale(I18n.locale) do
  137. example.run
  138. end
  139. end
  140. context 'when an accept language is present in headers' do
  141. subject do
  142. Setting.registrations_mode = 'open'
  143. request.headers['Accept-Language'] = accept_language
  144. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  145. end
  146. it 'redirects to setup' do
  147. subject
  148. expect(response).to redirect_to auth_setup_path
  149. end
  150. it 'creates user' do
  151. subject
  152. user = User.find_by(email: 'test@example.com')
  153. expect(user).to_not be_nil
  154. expect(user.locale).to eq(accept_language)
  155. end
  156. end
  157. context 'when user has not agreed to terms of service' do
  158. subject do
  159. Setting.registrations_mode = 'open'
  160. request.headers['Accept-Language'] = accept_language
  161. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'false' } }
  162. end
  163. it 'does not create user' do
  164. subject
  165. user = User.find_by(email: 'test@example.com')
  166. expect(user).to be_nil
  167. end
  168. end
  169. context 'when user has an email address requiring approval' do
  170. subject do
  171. request.headers['Accept-Language'] = accept_language
  172. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  173. end
  174. before do
  175. Setting.registrations_mode = 'open'
  176. Fabricate(:email_domain_block, allow_with_approval: true, domain: 'example.com')
  177. end
  178. it 'creates unapproved user and redirects to setup' do
  179. subject
  180. expect(response).to redirect_to auth_setup_path
  181. user = User.find_by(email: 'test@example.com')
  182. expect(user).to_not be_nil
  183. expect(user.locale).to eq(accept_language)
  184. expect(user.approved).to be(false)
  185. end
  186. end
  187. context 'when user has an email address requiring approval through a MX record' do
  188. subject do
  189. request.headers['Accept-Language'] = accept_language
  190. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  191. end
  192. before do
  193. Setting.registrations_mode = 'open'
  194. Fabricate(:email_domain_block, allow_with_approval: true, domain: 'mail.example.com')
  195. allow(User).to receive(:skip_mx_check?).and_return(false)
  196. resolver = instance_double(Resolv::DNS, :timeouts= => nil)
  197. allow(resolver).to receive(:getresources)
  198. .with('example.com', Resolv::DNS::Resource::IN::MX)
  199. .and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'mail.example.com')])
  200. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  201. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  202. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::A).and_return([instance_double(Resolv::DNS::Resource::IN::A, address: '2.3.4.5')])
  203. allow(resolver).to receive(:getresources).with('mail.example.com', Resolv::DNS::Resource::IN::AAAA).and_return([instance_double(Resolv::DNS::Resource::IN::AAAA, address: 'fd00::2')])
  204. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  205. end
  206. it 'creates unapproved user and redirects to setup' do
  207. subject
  208. expect(response).to redirect_to auth_setup_path
  209. user = User.find_by(email: 'test@example.com')
  210. expect(user).to_not be_nil
  211. expect(user.locale).to eq(accept_language)
  212. expect(user.approved).to be(false)
  213. end
  214. end
  215. context 'with Approval-based registrations without invite' do
  216. subject do
  217. Setting.registrations_mode = 'approved'
  218. request.headers['Accept-Language'] = accept_language
  219. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  220. end
  221. it 'redirects to setup' do
  222. subject
  223. expect(response).to redirect_to auth_setup_path
  224. end
  225. it 'creates user' do
  226. subject
  227. user = User.find_by(email: 'test@example.com')
  228. expect(user).to_not be_nil
  229. expect(user.locale).to eq(accept_language)
  230. expect(user.approved).to be(false)
  231. end
  232. end
  233. context 'with Approval-based registrations with expired invite' do
  234. subject do
  235. Setting.registrations_mode = 'approved'
  236. request.headers['Accept-Language'] = accept_language
  237. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.ago)
  238. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } }
  239. end
  240. it 'redirects to setup' do
  241. subject
  242. expect(response).to redirect_to auth_setup_path
  243. end
  244. it 'creates user' do
  245. subject
  246. user = User.find_by(email: 'test@example.com')
  247. expect(user).to_not be_nil
  248. expect(user.locale).to eq(accept_language)
  249. expect(user.approved).to be(false)
  250. end
  251. end
  252. context 'with Approval-based registrations with valid invite and required invite text' do
  253. subject do
  254. inviter = Fabricate(:user, confirmed_at: 2.days.ago)
  255. Setting.registrations_mode = 'approved'
  256. Setting.require_invite_text = true
  257. request.headers['Accept-Language'] = accept_language
  258. invite = Fabricate(:invite, user: inviter, max_uses: nil, expires_at: 1.hour.from_now)
  259. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } }
  260. end
  261. it 'redirects to setup' do
  262. subject
  263. expect(response).to redirect_to auth_setup_path
  264. end
  265. it 'creates user' do
  266. subject
  267. user = User.find_by(email: 'test@example.com')
  268. expect(user).to_not be_nil
  269. expect(user.locale).to eq(accept_language)
  270. expect(user.approved).to be(true)
  271. end
  272. end
  273. context 'with an already taken username' do
  274. subject do
  275. Setting.registrations_mode = 'open'
  276. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  277. end
  278. before do
  279. Fabricate(:account, username: 'test')
  280. end
  281. it 'responds with an error message about the username' do
  282. subject
  283. expect(response).to have_http_status(:success)
  284. expect(username_error_text).to eq(I18n.t('errors.messages.taken'))
  285. end
  286. def username_error_text
  287. Nokogiri::Slop(response.body).css('.user_account_username .error').text
  288. end
  289. end
  290. include_examples 'checks for enabled registrations', :create
  291. end
  292. describe 'DELETE #destroy' do
  293. let(:user) { Fabricate(:user) }
  294. before do
  295. request.env['devise.mapping'] = Devise.mappings[:user]
  296. sign_in(user, scope: :user)
  297. delete :destroy
  298. end
  299. it 'returns http not found' do
  300. expect(response).to have_http_status(404)
  301. end
  302. it 'does not delete user' do
  303. expect(User.find(user.id)).to_not be_nil
  304. end
  305. end
  306. end