闭社主体 forked from https://github.com/tootsuite/mastodon
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.

247 lines
7.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. 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', 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. 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 = 'open'
  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', agreement: 'false' } }
  106. end
  107. it 'does not create user' do
  108. subject
  109. user = User.find_by(email: 'test@example.com')
  110. expect(user).to be_nil
  111. end
  112. end
  113. context 'approval-based registrations without invite' do
  114. around do |example|
  115. registrations_mode = Setting.registrations_mode
  116. example.run
  117. Setting.registrations_mode = registrations_mode
  118. end
  119. subject do
  120. Setting.registrations_mode = 'approved'
  121. request.headers["Accept-Language"] = accept_language
  122. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
  123. end
  124. it 'redirects to setup' do
  125. subject
  126. expect(response).to redirect_to auth_setup_path
  127. end
  128. it 'creates user' do
  129. subject
  130. user = User.find_by(email: 'test@example.com')
  131. expect(user).to_not be_nil
  132. expect(user.locale).to eq(accept_language)
  133. expect(user.approved).to eq(false)
  134. end
  135. end
  136. context 'approval-based registrations with expired invite' do
  137. around do |example|
  138. registrations_mode = Setting.registrations_mode
  139. example.run
  140. Setting.registrations_mode = registrations_mode
  141. end
  142. subject do
  143. Setting.registrations_mode = 'approved'
  144. request.headers["Accept-Language"] = accept_language
  145. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.ago)
  146. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code, agreement: 'true' } }
  147. end
  148. it 'redirects to setup' do
  149. subject
  150. expect(response).to redirect_to auth_setup_path
  151. end
  152. it 'creates user' do
  153. subject
  154. user = User.find_by(email: 'test@example.com')
  155. expect(user).to_not be_nil
  156. expect(user.locale).to eq(accept_language)
  157. expect(user.approved).to eq(false)
  158. end
  159. end
  160. context 'approval-based registrations with valid invite' do
  161. around do |example|
  162. registrations_mode = Setting.registrations_mode
  163. example.run
  164. Setting.registrations_mode = registrations_mode
  165. end
  166. subject do
  167. Setting.registrations_mode = 'approved'
  168. request.headers["Accept-Language"] = accept_language
  169. invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.from_now)
  170. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', 'invite_code': invite.code, agreement: 'true' } }
  171. end
  172. it 'redirects to setup' do
  173. subject
  174. expect(response).to redirect_to auth_setup_path
  175. end
  176. it 'creates user' do
  177. subject
  178. user = User.find_by(email: 'test@example.com')
  179. expect(user).to_not be_nil
  180. expect(user.locale).to eq(accept_language)
  181. expect(user.approved).to eq(true)
  182. end
  183. end
  184. it 'does nothing if user already exists' do
  185. Fabricate(:user, account: Fabricate(:account, username: 'test'))
  186. subject
  187. end
  188. include_examples 'checks for enabled registrations', :create
  189. end
  190. describe 'DELETE #destroy' do
  191. let(:user) { Fabricate(:user) }
  192. before do
  193. request.env['devise.mapping'] = Devise.mappings[:user]
  194. sign_in(user, scope: :user)
  195. delete :destroy
  196. end
  197. it 'returns http not found' do
  198. expect(response).to have_http_status(:not_found)
  199. end
  200. it 'does not delete user' do
  201. expect(User.find(user.id)).to_not be_nil
  202. end
  203. end
  204. end