Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # frozen_string_literal: true
- require 'rails_helper'
- RSpec.describe 'Announcements API', type: :request do
- let(:url) { '/api/announcements/' }
- let!(:author) { create(:user) }
- let(:non_author) { create(:user) }
- let!(:announcements) { create_list(:announcement, 10, user: author) }
- let!(:announcement_response) { create(:response, user: non_author, announcement: announcements.first) }
- let(:announcement_id) { announcements.first.id }
- let(:announcement_url) { url + announcement_id.to_s }
- let(:author_header) { { "USER-ID" => author.id } }
- let(:non_author_header) { { "USER-ID" => non_author.id } }
- let(:expected_keys) { %w[id description status updated_at user_id created_at] }
- let(:expected_keys_with_responses) { %w[id description responses status updated_at user_id created_at] }
- let(:expected_response_keys) { %w[id announcement_id price status user_id] }
- describe 'GET /api/announcements' do
- before do
- get url, headers: author_header
- end
- it 'returns announcements' do
- expect(json).not_to be_empty
- expect(json.first.keys).to match_array(expected_keys)
- expect(json.size).to eq(10)
- end
- it 'returns status code 200' do
- expect(response).to have_http_status(200)
- end
- end
- describe 'GET /api/announcements/:id' do
- context 'when user is an author' do
- before do
- get announcement_url, headers: author_header
- end
- context 'when the record exists' do
- it 'returns the announcement with responses' do
- expect(json).not_to be_empty
- expect(json['id']).to eq(announcement_id)
- expect(json.keys).to match_array(expected_keys_with_responses)
- expect(json['responses'].first.keys).to match_array(expected_response_keys)
- expect(json['responses'].first['announcement_id']).to eq(announcement_id)
- end
- it 'returns status code 200' do
- expect(response).to have_http_status(200)
- end
- end
- context 'when the record does not exist' do
- let(:announcement_id) { '100' }
- it 'returns status code 404' do
- expect(response).to have_http_status(404)
- end
- it 'returns a not found message' do
- expect(response.body).to match(/Couldn't find Announcement/)
- end
- end
- end
- context 'when user is non author' do
- before do
- get announcement_url, headers: non_author_header
- end
- context 'when the record exists' do
- it 'returns the announcement' do
- expect(json).not_to be_empty
- expect(json['id']).to eq(announcement_id)
- expect(json.keys).to match_array(expected_keys)
- end
- it 'returns status code 200' do
- expect(response).to have_http_status(200)
- end
- end
- end
- end
- # describe 'PUT /api/announcements/:id/cancel' do
- # context 'when user is an author' do
- # before do
- # put url + announcement_id + '/cancel', headers: author_header
- # end
- #
- # it 'returns the announcement id' do
- # expect(json['id']).to be_present
- # end
- #
- # it 'check status is cancelled' do
- # announcements.first.reload
- # expect(announcements.first.status).to eq('cancelled')
- # end
- #
- # it 'check response status is declined' do
- # announcement_response.reload
- # expect(announcement_response.status).to eq('declined')
- # end
- #
- # it 'returns status code 200' do
- # expect(response).to have_http_status(200)
- # end
- # end
- #
- # context 'when user is non author' do
- # before do
- # put url + announcement_id + '/cancel', headers: non_author_header
- # end
- #
- # it 'returns the access denied message' do
- # expect(json['error']).to eq('Access is denied')
- # end
- #
- # it 'returns status code 401' do
- # expect(response).to have_http_status(401)
- # end
- # end
- # end
- describe 'PUT /api/announcements/:id/cancel' do
- context 'when user is an author' do
- before do
- put announcement_url + '/cancel', headers: author_header
- end
- it 'returns the announcement id' do
- expect(json['id']).to be_present
- end
- it 'check status is cancelled' do
- announcements.first.reload
- expect(announcements.first.status).to eq('cancelled')
- end
- it 'check response status is declined' do
- announcement_response.reload
- expect(announcement_response.status).to eq('declined')
- end
- it 'returns status code 200' do
- expect(response).to have_http_status(200)
- end
- end
- context 'when user is non author' do
- before do
- put announcement_url + '/cancel', headers: non_author_header
- end
- it 'returns the access denied message' do
- expect(json['error']).to eq('Access is denied')
- end
- it 'returns status code 401' do
- expect(response).to have_http_status(401)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement