logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

registrations_controller_spec.rb (3569B)


  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(:success)
  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(:success)
  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(:success)
  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. before { request.env["devise.mapping"] = Devise.mappings[:user] }
  61. context do
  62. around do |example|
  63. open_registrations = Setting.open_registrations
  64. example.run
  65. Setting.open_registrations = open_registrations
  66. end
  67. subject do
  68. Setting.open_registrations = true
  69. request.headers["Accept-Language"] = accept_language
  70. post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678' } }
  71. end
  72. it 'redirects to login page' do
  73. subject
  74. expect(response).to redirect_to new_user_session_path
  75. end
  76. it 'creates user' do
  77. subject
  78. user = User.find_by(email: 'test@example.com')
  79. expect(user).to_not be_nil
  80. expect(user.locale).to eq(accept_language)
  81. end
  82. end
  83. it 'does nothing if user already exists' do
  84. Fabricate(:user, account: Fabricate(:account, username: 'test'))
  85. subject
  86. end
  87. include_examples 'checks for enabled registrations', :create
  88. end
  89. describe 'DELETE #destroy' do
  90. let(:user) { Fabricate(:user) }
  91. before do
  92. request.env['devise.mapping'] = Devise.mappings[:user]
  93. sign_in(user, scope: :user)
  94. delete :destroy
  95. end
  96. it 'returns http not found' do
  97. expect(response).to have_http_status(:not_found)
  98. end
  99. it 'does not delete user' do
  100. expect(User.find(user.id)).to_not be_nil
  101. end
  102. end
  103. end