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.

218 lines
6.5 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. end
  40. describe 'GET #new' do
  41. before do
  42. request.env["devise.mapping"] = Devise.mappings[:user]
  43. end
  44. context do
  45. around do |example|
  46. registrations_mode = Setting.registrations_mode
  47. example.run
  48. Setting.registrations_mode = registrations_mode
  49. end
  50. it 'returns http success' do
  51. Setting.registrations_mode = 'open'
  52. get :new
  53. expect(response).to have_http_status(200)
  54. end
  55. end
  56. include_examples 'checks for enabled registrations', :new
  57. end
  58. describe 'POST #create' do
  59. let(:accept_language) { Rails.application.config.i18n.available_locales.sample.to_s }
  60. around do |example|
  61. current_locale = I18n.locale
  62. example.run
  63. I18n.locale = current_locale
  64. end
  65. before { request.env["devise.mapping"] = Devise.mappings[:user] }
  66. context do
  67. around do |example|
  68. registrations_mode = Setting.registrations_mode
  69. example.run
  70. Setting.registrations_mode = registrations_mode
  71. end
  72. subject do
  73. Setting.registrations_mode = 'open'
  74. request.headers["Accept-Language"] = accept_language
  75. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678' } }
  76. end
  77. it 'redirects to login page' do
  78. subject
  79. expect(response).to redirect_to new_user_session_path
  80. end
  81. it 'creates user' do
  82. subject
  83. user = User.find_by(email: 'test@example.com')
  84. expect(user).to_not be_nil
  85. expect(user.locale).to eq(accept_language)
  86. end
  87. end
  88. context 'approval-based registrations without invite' do
  89. around do |example|
  90. registrations_mode = Setting.registrations_mode
  91. example.run
  92. Setting.registrations_mode = registrations_mode
  93. end
  94. subject do
  95. Setting.registrations_mode = 'approved'
  96. request.headers["Accept-Language"] = accept_language
  97. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678' } }
  98. end
  99. it 'redirects to login page' do
  100. subject
  101. expect(response).to redirect_to new_user_session_path
  102. end
  103. it 'creates user' do
  104. subject
  105. user = User.find_by(email: 'test@example.com')
  106. expect(user).to_not be_nil
  107. expect(user.locale).to eq(accept_language)
  108. expect(user.approved).to eq(false)
  109. end
  110. end
  111. context 'approval-based registrations with expired invite' do
  112. around do |example|
  113. registrations_mode = Setting.registrations_mode
  114. example.run
  115. Setting.registrations_mode = registrations_mode
  116. end
  117. subject do
  118. Setting.registrations_mode = 'approved'
  119. request.headers["Accept-Language"] = accept_language
  120. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.ago)
  121. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code } }
  122. end
  123. it 'redirects to login page' do
  124. subject
  125. expect(response).to redirect_to new_user_session_path
  126. end
  127. it 'creates user' do
  128. subject
  129. user = User.find_by(email: 'test@example.com')
  130. expect(user).to_not be_nil
  131. expect(user.locale).to eq(accept_language)
  132. expect(user.approved).to eq(false)
  133. end
  134. end
  135. context 'approval-based registrations with valid invite' do
  136. around do |example|
  137. registrations_mode = Setting.registrations_mode
  138. example.run
  139. Setting.registrations_mode = registrations_mode
  140. end
  141. subject do
  142. Setting.registrations_mode = 'approved'
  143. request.headers["Accept-Language"] = accept_language
  144. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.from_now)
  145. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code } }
  146. end
  147. it 'redirects to login page' do
  148. subject
  149. expect(response).to redirect_to new_user_session_path
  150. end
  151. it 'creates user' do
  152. subject
  153. user = User.find_by(email: 'test@example.com')
  154. expect(user).to_not be_nil
  155. expect(user.locale).to eq(accept_language)
  156. expect(user.approved).to eq(true)
  157. end
  158. end
  159. it 'does nothing if user already exists' do
  160. Fabricate(:user, account: Fabricate(:account, username: 'test'))
  161. subject
  162. end
  163. include_examples 'checks for enabled registrations', :create
  164. end
  165. describe 'DELETE #destroy' do
  166. let(:user) { Fabricate(:user) }
  167. before do
  168. request.env['devise.mapping'] = Devise.mappings[:user]
  169. sign_in(user, scope: :user)
  170. delete :destroy
  171. end
  172. it 'returns http not found' do
  173. expect(response).to have_http_status(:not_found)
  174. end
  175. it 'does not delete user' do
  176. expect(User.find(user.id)).to_not be_nil
  177. end
  178. end
  179. end