deletes_controller_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require 'rails_helper'
  2. describe Settings::DeletesController do
  3. render_views
  4. describe 'GET #show' do
  5. context 'when signed in' do
  6. let(:user) { Fabricate(:user) }
  7. before do
  8. sign_in user, scope: :user
  9. end
  10. it 'renders confirmation page' do
  11. get :show
  12. expect(response).to have_http_status(200)
  13. end
  14. context 'when suspended' do
  15. let(:user) { Fabricate(:user, account_attributes: { username: 'alice', suspended_at: Time.now.utc }) }
  16. it 'returns http forbidden' do
  17. get :show
  18. expect(response).to have_http_status(403)
  19. end
  20. end
  21. end
  22. context 'when not signed in' do
  23. it 'redirects' do
  24. get :show
  25. expect(response).to redirect_to '/auth/sign_in'
  26. end
  27. end
  28. end
  29. describe 'DELETE #destroy' do
  30. context 'when signed in' do
  31. let(:user) { Fabricate(:user, password: 'petsmoldoggos') }
  32. before do
  33. sign_in user, scope: :user
  34. end
  35. context 'with correct password' do
  36. before do
  37. delete :destroy, params: { form_delete_confirmation: { password: 'petsmoldoggos' } }
  38. end
  39. it 'redirects to sign in page' do
  40. expect(response).to redirect_to '/auth/sign_in'
  41. end
  42. it 'removes user record' do
  43. expect(User.find_by(id: user.id)).to be_nil
  44. end
  45. it 'marks account as suspended' do
  46. expect(user.account.reload).to be_suspended
  47. end
  48. context 'when suspended' do
  49. let(:user) { Fabricate(:user, account_attributes: { username: 'alice', suspended_at: Time.now.utc }) }
  50. it 'returns http forbidden' do
  51. expect(response).to have_http_status(403)
  52. end
  53. end
  54. end
  55. context 'with incorrect password' do
  56. before do
  57. delete :destroy, params: { form_delete_confirmation: { password: 'blaze420' } }
  58. end
  59. it 'redirects back to confirmation page' do
  60. expect(response).to redirect_to settings_delete_path
  61. end
  62. end
  63. end
  64. context 'when not signed in' do
  65. it 'redirects' do
  66. delete :destroy
  67. expect(response).to redirect_to '/auth/sign_in'
  68. end
  69. end
  70. context do
  71. around do |example|
  72. open_deletion = Setting.open_deletion
  73. example.run
  74. Setting.open_deletion = open_deletion
  75. end
  76. it 'redirects' do
  77. Setting.open_deletion = false
  78. delete :destroy
  79. expect(response).to redirect_to root_path
  80. end
  81. end
  82. end
  83. end