invites_controller_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe InvitesController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. before do
  7. sign_in user
  8. end
  9. describe 'GET #index' do
  10. before do
  11. Fabricate(:invite, user: user)
  12. end
  13. context 'when everyone can invite' do
  14. before do
  15. UserRole.everyone.update(permissions: UserRole.everyone.permissions | UserRole::FLAGS[:invite_users])
  16. get :index
  17. end
  18. it 'returns http success' do
  19. expect(response).to have_http_status(:success)
  20. end
  21. it 'returns private cache control headers' do
  22. expect(response.headers['Cache-Control']).to include('private, no-store')
  23. end
  24. end
  25. context 'when not everyone can invite' do
  26. before do
  27. UserRole.everyone.update(permissions: UserRole.everyone.permissions & ~UserRole::FLAGS[:invite_users])
  28. get :index
  29. end
  30. it 'returns http forbidden' do
  31. expect(response).to have_http_status(403)
  32. end
  33. end
  34. end
  35. describe 'POST #create' do
  36. subject { post :create, params: { invite: { max_uses: '10', expires_in: 1800 } } }
  37. context 'when everyone can invite' do
  38. before do
  39. UserRole.everyone.update(permissions: UserRole.everyone.permissions | UserRole::FLAGS[:invite_users])
  40. end
  41. it 'succeeds to create a invite' do
  42. expect { subject }.to change(Invite, :count).by(1)
  43. expect(subject).to redirect_to invites_path
  44. expect(Invite.last).to have_attributes(user_id: user.id, max_uses: 10)
  45. end
  46. end
  47. context 'when not everyone can invite' do
  48. before do
  49. UserRole.everyone.update(permissions: UserRole.everyone.permissions & ~UserRole::FLAGS[:invite_users])
  50. end
  51. it 'returns http forbidden' do
  52. expect(subject).to have_http_status(403)
  53. end
  54. end
  55. end
  56. describe 'DELETE #create' do
  57. let(:invite) { Fabricate(:invite, user: user, expires_at: nil) }
  58. before do
  59. delete :destroy, params: { id: invite.id }
  60. end
  61. it 'redirects' do
  62. expect(response).to redirect_to invites_path
  63. end
  64. it 'expires invite' do
  65. expect(invite.reload).to be_expired
  66. end
  67. end
  68. end