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.

72 lines
1.9 KiB

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