invites_controller_spec.rb 2.1 KB

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