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.

135 lines
3.6 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. open_registrations = Setting.open_registrations
  7. example.run
  8. Setting.open_registrations = open_registrations
  9. end
  10. it 'redirects if it is in single user mode while it is open for registration' do
  11. Fabricate(:account)
  12. Setting.open_registrations = true
  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.open_registrations = false
  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. open_registrations = Setting.open_registrations
  47. example.run
  48. Setting.open_registrations = open_registrations
  49. end
  50. it 'returns http success' do
  51. Setting.open_registrations = true
  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. open_registrations = Setting.open_registrations
  69. example.run
  70. Setting.open_registrations = open_registrations
  71. end
  72. subject do
  73. Setting.open_registrations = true
  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. it 'does nothing if user already exists' do
  89. Fabricate(:user, account: Fabricate(:account, username: 'test'))
  90. subject
  91. end
  92. include_examples 'checks for enabled registrations', :create
  93. end
  94. describe 'DELETE #destroy' do
  95. let(:user) { Fabricate(:user) }
  96. before do
  97. request.env['devise.mapping'] = Devise.mappings[:user]
  98. sign_in(user, scope: :user)
  99. delete :destroy
  100. end
  101. it 'returns http not found' do
  102. expect(response).to have_http_status(:not_found)
  103. end
  104. it 'does not delete user' do
  105. expect(User.find(user.id)).to_not be_nil
  106. end
  107. end
  108. end