registrations_controller_spec.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 'GET #update' do
  36. let(:user) { Fabricate(:user) }
  37. before do
  38. request.env['devise.mapping'] = Devise.mappings[:user]
  39. sign_in(user, scope: :user)
  40. post :update
  41. end
  42. it 'returns http success' do
  43. expect(response).to have_http_status(200)
  44. end
  45. it 'returns private cache control headers' do
  46. expect(response.headers['Cache-Control']).to include('private, no-store')
  47. end
  48. context 'when suspended' do
  49. let(:user) { Fabricate(:user, account_attributes: { username: 'test', suspended_at: Time.now.utc }) }
  50. it 'returns http forbidden' do
  51. expect(response).to have_http_status(403)
  52. end
  53. end
  54. end
  55. describe 'GET #new' do
  56. before do
  57. request.env['devise.mapping'] = Devise.mappings[:user]
  58. end
  59. context 'with open registrations' do
  60. it 'returns http success' do
  61. Setting.registrations_mode = 'open'
  62. get :new
  63. expect(response).to have_http_status(200)
  64. end
  65. end
  66. include_examples 'checks for enabled registrations', :new
  67. end
  68. describe 'POST #create' do
  69. let(:accept_language) { 'de' }
  70. before do
  71. session[:registration_form_time] = 5.seconds.ago
  72. request.env['devise.mapping'] = Devise.mappings[:user]
  73. end
  74. around do |example|
  75. I18n.with_locale(I18n.locale) do
  76. example.run
  77. end
  78. end
  79. context 'when an accept language is present in headers' do
  80. subject do
  81. Setting.registrations_mode = 'open'
  82. request.headers['Accept-Language'] = accept_language
  83. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  84. end
  85. it 'redirects to setup' do
  86. subject
  87. expect(response).to redirect_to auth_setup_path
  88. end
  89. it 'creates user' do
  90. subject
  91. user = User.find_by(email: 'test@example.com')
  92. expect(user).to_not be_nil
  93. expect(user.locale).to eq(accept_language)
  94. end
  95. end
  96. context 'when user has not agreed to terms of service' do
  97. subject do
  98. Setting.registrations_mode = 'open'
  99. request.headers['Accept-Language'] = accept_language
  100. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'false' } }
  101. end
  102. it 'does not create user' do
  103. subject
  104. user = User.find_by(email: 'test@example.com')
  105. expect(user).to be_nil
  106. end
  107. end
  108. context 'when user has an email address requiring approval' do
  109. subject do
  110. request.headers['Accept-Language'] = accept_language
  111. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  112. end
  113. before do
  114. Setting.registrations_mode = 'open'
  115. Fabricate(:email_domain_block, allow_with_approval: true, domain: 'example.com')
  116. end
  117. it 'creates unapproved user and redirects to setup' do
  118. subject
  119. expect(response).to redirect_to auth_setup_path
  120. user = User.find_by(email: 'test@example.com')
  121. expect(user).to_not be_nil
  122. expect(user.locale).to eq(accept_language)
  123. expect(user.approved).to be(false)
  124. end
  125. end
  126. context 'when user has an email address requiring approval through a MX record' do
  127. subject do
  128. request.headers['Accept-Language'] = accept_language
  129. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  130. end
  131. before do
  132. Setting.registrations_mode = 'open'
  133. Fabricate(:email_domain_block, allow_with_approval: true, domain: 'mail.example.com')
  134. allow(User).to receive(:skip_mx_check?).and_return(false)
  135. resolver = instance_double(Resolv::DNS, :timeouts= => nil)
  136. allow(resolver).to receive(:getresources)
  137. .with('example.com', Resolv::DNS::Resource::IN::MX)
  138. .and_return([instance_double(Resolv::DNS::Resource::MX, exchange: 'mail.example.com')])
  139. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
  140. allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
  141. 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')])
  142. 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')])
  143. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  144. end
  145. it 'creates unapproved user and redirects to setup' do
  146. subject
  147. expect(response).to redirect_to auth_setup_path
  148. user = User.find_by(email: 'test@example.com')
  149. expect(user).to_not be_nil
  150. expect(user.locale).to eq(accept_language)
  151. expect(user.approved).to be(false)
  152. end
  153. end
  154. context 'with Approval-based registrations without invite' do
  155. subject do
  156. Setting.registrations_mode = 'approved'
  157. request.headers['Accept-Language'] = accept_language
  158. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  159. end
  160. it 'redirects to setup' do
  161. subject
  162. expect(response).to redirect_to auth_setup_path
  163. end
  164. it 'creates user' do
  165. subject
  166. user = User.find_by(email: 'test@example.com')
  167. expect(user).to_not be_nil
  168. expect(user.locale).to eq(accept_language)
  169. expect(user.approved).to be(false)
  170. end
  171. end
  172. context 'with Approval-based registrations with expired invite' do
  173. subject do
  174. Setting.registrations_mode = 'approved'
  175. request.headers['Accept-Language'] = accept_language
  176. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.ago)
  177. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } }
  178. end
  179. it 'redirects to setup' do
  180. subject
  181. expect(response).to redirect_to auth_setup_path
  182. end
  183. it 'creates user' do
  184. subject
  185. user = User.find_by(email: 'test@example.com')
  186. expect(user).to_not be_nil
  187. expect(user.locale).to eq(accept_language)
  188. expect(user.approved).to be(false)
  189. end
  190. end
  191. context 'with Approval-based registrations with valid invite and required invite text' do
  192. subject do
  193. inviter = Fabricate(:user, confirmed_at: 2.days.ago)
  194. Setting.registrations_mode = 'approved'
  195. Setting.require_invite_text = true
  196. request.headers['Accept-Language'] = accept_language
  197. invite = Fabricate(:invite, user: inviter, max_uses: nil, expires_at: 1.hour.from_now)
  198. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } }
  199. end
  200. it 'redirects to setup' do
  201. subject
  202. expect(response).to redirect_to auth_setup_path
  203. end
  204. it 'creates user' do
  205. subject
  206. user = User.find_by(email: 'test@example.com')
  207. expect(user).to_not be_nil
  208. expect(user.locale).to eq(accept_language)
  209. expect(user.approved).to be(true)
  210. end
  211. end
  212. context 'with an already taken username' do
  213. subject do
  214. Setting.registrations_mode = 'open'
  215. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  216. end
  217. before do
  218. Fabricate(:account, username: 'test')
  219. end
  220. it 'responds with an error message about the username' do
  221. subject
  222. expect(response).to have_http_status(:success)
  223. expect(username_error_text).to eq(I18n.t('errors.messages.taken'))
  224. end
  225. def username_error_text
  226. Nokogiri::Slop(response.body).css('.user_account_username .error').text
  227. end
  228. end
  229. include_examples 'checks for enabled registrations', :create
  230. end
  231. describe 'DELETE #destroy' do
  232. let(:user) { Fabricate(:user) }
  233. before do
  234. request.env['devise.mapping'] = Devise.mappings[:user]
  235. sign_in(user, scope: :user)
  236. delete :destroy
  237. end
  238. it 'returns http not found' do
  239. expect(response).to have_http_status(404)
  240. end
  241. it 'does not delete user' do
  242. expect(User.find(user.id)).to_not be_nil
  243. end
  244. end
  245. end