|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Stickies' do |
| 6 | + let(:user) { Fabricate(:moderator_user) } |
| 7 | + let(:scopes) { 'write:statuses' } |
| 8 | + let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) } |
| 9 | + let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } |
| 10 | + |
| 11 | + describe 'POST /api/v1/statuses/:status_id/sticky' do |
| 12 | + subject do |
| 13 | + post "/api/v1/statuses/#{status.id}/sticky", headers: headers |
| 14 | + end |
| 15 | + |
| 16 | + let(:status) { Fabricate(:status) } |
| 17 | + |
| 18 | + it_behaves_like 'forbidden for wrong scope', 'read' |
| 19 | + |
| 20 | + context 'when a moderator' do |
| 21 | + it 'creates a sticky and returns updated json with sticky: true', :aggregate_failures do |
| 22 | + subject |
| 23 | + |
| 24 | + expect(response).to have_http_status(200) |
| 25 | + expect(Sticky.exists?(status_id: status.id)).to be true |
| 26 | + expect(response.parsed_body).to match( |
| 27 | + a_hash_including(id: status.id.to_s, sticky: true) |
| 28 | + ) |
| 29 | + expect(response.parsed_body.keys.map(&:to_s)).to include('sticky') |
| 30 | + end |
| 31 | + |
| 32 | + it 'is idempotent when called twice' do |
| 33 | + subject |
| 34 | + expect { post "/api/v1/statuses/#{status.id}/sticky", headers: headers } |
| 35 | + .to_not change(Sticky, :count) |
| 36 | + expect(response).to have_http_status(200) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + context 'when a regular user' do |
| 41 | + let(:user) { Fabricate(:user) } |
| 42 | + |
| 43 | + it 'returns http forbidden' do |
| 44 | + subject |
| 45 | + expect(response).to have_http_status(403) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context 'without an authorization header' do |
| 50 | + let(:headers) { {} } |
| 51 | + |
| 52 | + it 'returns http unauthorized' do |
| 53 | + subject |
| 54 | + expect(response).to have_http_status(401) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'when the status does not exist' do |
| 59 | + it 'returns http not found' do |
| 60 | + post '/api/v1/statuses/-1/sticky', headers: headers |
| 61 | + expect(response).to have_http_status(404) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe 'POST /api/v1/statuses/:status_id/unsticky' do |
| 67 | + subject do |
| 68 | + post "/api/v1/statuses/#{status.id}/unsticky", headers: headers |
| 69 | + end |
| 70 | + |
| 71 | + let(:status) { Fabricate(:status) } |
| 72 | + |
| 73 | + it_behaves_like 'forbidden for wrong scope', 'read' |
| 74 | + |
| 75 | + context 'when a moderator' do |
| 76 | + context 'when the status is currently sticky' do |
| 77 | + before { Sticky.create!(status: status) } |
| 78 | + |
| 79 | + it 'removes the sticky and returns updated json with sticky: false', :aggregate_failures do |
| 80 | + subject |
| 81 | + |
| 82 | + expect(response).to have_http_status(200) |
| 83 | + expect(Sticky.exists?(status_id: status.id)).to be false |
| 84 | + expect(response.parsed_body).to match( |
| 85 | + a_hash_including(id: status.id.to_s, sticky: false) |
| 86 | + ) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context 'when the status is not sticky' do |
| 91 | + it 'returns http success without error' do |
| 92 | + subject |
| 93 | + expect(response).to have_http_status(200) |
| 94 | + expect(response.parsed_body).to match( |
| 95 | + a_hash_including(id: status.id.to_s, sticky: false) |
| 96 | + ) |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context 'when a regular user' do |
| 102 | + let(:user) { Fabricate(:user) } |
| 103 | + |
| 104 | + before { Sticky.create!(status: status) } |
| 105 | + |
| 106 | + it 'returns http forbidden and does not remove the sticky', :aggregate_failures do |
| 107 | + subject |
| 108 | + expect(response).to have_http_status(403) |
| 109 | + expect(Sticky.exists?(status_id: status.id)).to be true |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | +end |
0 commit comments