You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.8 KiB

  1. require 'rails_helper'
  2. RSpec.describe Auth::RegistrationsController, type: :controller do
  3. render_views
  4. shared_examples 'checks for enabled registrations' do |path|
  5. around do |example|
  6. registrations_mode = Setting.registrations_mode
  7. example.run
  8. Setting.registrations_mode = registrations_mode
  9. end
  10. it 'redirects if it is in single user mode while it is open for registration' do
  11. Fabricate(:account)
  12. Setting.registrations_mode = 'open'
  13. expect(Rails.configuration.x).to receive(:single_user_mode).and_return(true)
  14. get path
  15. expect(response).to redirect_to '/'
  16. end
  17. it 'redirects if it is not open for registration while it is not in single user mode' do
  18. Setting.registrations_mode = 'none'
  19. expect(Rails.configuration.x).to receive(:single_user_mode).and_return(false)
  20. get path
  21. expect(response).to redirect_to '/'
  22. end
  23. end
  24. describe 'GET #edit' do
  25. it 'returns http success' do
  26. request.env["devise.mapping"] = Devise.mappings[:user]
  27. sign_in(Fabricate(:user))
  28. get :edit
  29. expect(response).to have_http_status(200)
  30. end
  31. end
  32. describe 'GET #update' do
  33. it 'returns http success' do
  34. request.env["devise.mapping"] = Devise.mappings[:user]
  35. sign_in(Fabricate(:user), scope: :user)
  36. post :update
  37. expect(response).to have_http_status(200)
  38. end
  39. context 'when suspended' do
  40. it 'returns http forbidden' do
  41. request.env["devise.mapping"] = Devise.mappings[:user]
  42. sign_in(Fabricate(:user, account_attributes: { username: 'test', suspended_at: Time.now.utc }), scope: :user)
  43. post :update
  44. expect(response).to have_http_status(403)
  45. end
  46. end
  47. end
  48. describe 'GET #new' do
  49. before do
  50. request.env["devise.mapping"] = Devise.mappings[:user]
  51. end
  52. context do
  53. around do |example|
  54. registrations_mode = Setting.registrations_mode
  55. example.run
  56. Setting.registrations_mode = registrations_mode
  57. end
  58. it 'returns http success' do
  59. Setting.registrations_mode = 'open'
  60. get :new
  61. expect(response).to have_http_status(200)
  62. end
  63. end
  64. include_examples 'checks for enabled registrations', :new
  65. end
  66. describe 'POST #create' do
  67. let(:accept_language) { Rails.application.config.i18n.available_locales.sample.to_s }
  68. around do |example|
  69. current_locale = I18n.locale
  70. example.run
  71. I18n.locale = current_locale
  72. end
  73. before { request.env["devise.mapping"] = Devise.mappings[:user] }
  74. context do
  75. around do |example|
  76. registrations_mode = Setting.registrations_mode
  77. example.run
  78. Setting.registrations_mode = registrations_mode
  79. end
  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' } }
  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 'approval-based registrations without invite' do
  97. around do |example|
  98. registrations_mode = Setting.registrations_mode
  99. example.run
  100. Setting.registrations_mode = registrations_mode
  101. end
  102. subject do
  103. Setting.registrations_mode = 'approved'
  104. request.headers["Accept-Language"] = accept_language
  105. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678' } }
  106. end
  107. it 'redirects to setup' do
  108. subject
  109. expect(response).to redirect_to auth_setup_path
  110. end
  111. it 'creates user' do
  112. subject
  113. user = User.find_by(email: 'test@example.com')
  114. expect(user).to_not be_nil
  115. expect(user.locale).to eq(accept_language)
  116. expect(user.approved).to eq(false)
  117. end
  118. end
  119. context 'approval-based registrations with expired invite' do
  120. around do |example|
  121. registrations_mode = Setting.registrations_mode
  122. example.run
  123. Setting.registrations_mode = registrations_mode
  124. end
  125. subject do
  126. Setting.registrations_mode = 'approved'
  127. request.headers["Accept-Language"] = accept_language
  128. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.ago)
  129. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code } }
  130. end
  131. it 'redirects to setup' do
  132. subject
  133. expect(response).to redirect_to auth_setup_path
  134. end
  135. it 'creates user' do
  136. subject
  137. user = User.find_by(email: 'test@example.com')
  138. expect(user).to_not be_nil
  139. expect(user.locale).to eq(accept_language)
  140. expect(user.approved).to eq(false)
  141. end
  142. end
  143. context 'approval-based registrations with valid invite' do
  144. around do |example|
  145. registrations_mode = Setting.registrations_mode
  146. example.run
  147. Setting.registrations_mode = registrations_mode
  148. end
  149. subject do
  150. Setting.registrations_mode = 'approved'
  151. request.headers["Accept-Language"] = accept_language
  152. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.from_now)
  153. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code } }
  154. end
  155. it 'redirects to setup' do
  156. subject
  157. expect(response).to redirect_to auth_setup_path
  158. end
  159. it 'creates user' do
  160. subject
  161. user = User.find_by(email: 'test@example.com')
  162. expect(user).to_not be_nil
  163. expect(user.locale).to eq(accept_language)
  164. expect(user.approved).to eq(true)
  165. end
  166. end
  167. it 'does nothing if user already exists' do
  168. Fabricate(:user, account: Fabricate(:account, username: 'test'))
  169. subject
  170. end
  171. include_examples 'checks for enabled registrations', :create
  172. end
  173. describe 'DELETE #destroy' do
  174. let(:user) { Fabricate(:user) }
  175. before do
  176. request.env['devise.mapping'] = Devise.mappings[:user]
  177. sign_in(user, scope: :user)
  178. delete :destroy
  179. end
  180. it 'returns http not found' do
  181. expect(response).to have_http_status(:not_found)
  182. end
  183. it 'does not delete user' do
  184. expect(User.find(user.id)).to_not be_nil
  185. end
  186. end
  187. end