Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.08 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. require 'rails_helper'
  4.  
  5. RSpec.describe 'Announcements API', type: :request do
  6.   let(:url) { '/api/announcements/' }
  7.  
  8.   let!(:author) { create(:user) }
  9.   let(:non_author) { create(:user) }
  10.  
  11.   let!(:announcements) { create_list(:announcement, 10, user: author) }
  12.   let!(:announcement_response) { create(:response, user: non_author, announcement: announcements.first) }
  13.   let(:announcement_id) { announcements.first.id }
  14.   let(:announcement_url) { url + announcement_id.to_s }
  15.  
  16.   let(:author_header) { { "USER-ID" => author.id } }
  17.   let(:non_author_header) { { "USER-ID" => non_author.id } }
  18.  
  19.   let(:expected_keys) { %w[id description status updated_at user_id created_at] }
  20.   let(:expected_keys_with_responses) { %w[id description responses status updated_at user_id created_at] }
  21.   let(:expected_response_keys) { %w[id announcement_id price status user_id] }
  22.  
  23.   describe 'GET /api/announcements' do
  24.     before do
  25.       get url, headers: author_header
  26.     end
  27.  
  28.     it 'returns announcements' do
  29.       expect(json).not_to be_empty
  30.       expect(json.first.keys).to match_array(expected_keys)
  31.       expect(json.size).to eq(10)
  32.     end
  33.  
  34.     it 'returns status code 200' do
  35.       expect(response).to have_http_status(200)
  36.     end
  37.   end
  38.  
  39.   describe 'GET /api/announcements/:id' do
  40.     context 'when user is an author' do
  41.       before do
  42.         get announcement_url, headers: author_header
  43.       end
  44.  
  45.       context 'when the record exists' do
  46.         it 'returns the announcement with responses' do
  47.           expect(json).not_to be_empty
  48.           expect(json['id']).to eq(announcement_id)
  49.           expect(json.keys).to match_array(expected_keys_with_responses)
  50.  
  51.           expect(json['responses'].first.keys).to match_array(expected_response_keys)
  52.           expect(json['responses'].first['announcement_id']).to eq(announcement_id)
  53.         end
  54.  
  55.         it 'returns status code 200' do
  56.           expect(response).to have_http_status(200)
  57.         end
  58.       end
  59.  
  60.       context 'when the record does not exist' do
  61.         let(:announcement_id) { '100' }
  62.  
  63.         it 'returns status code 404' do
  64.           expect(response).to have_http_status(404)
  65.         end
  66.  
  67.         it 'returns a not found message' do
  68.           expect(response.body).to match(/Couldn't find Announcement/)
  69.        end
  70.      end
  71.    end
  72.  
  73.    context 'when user is non author' do
  74.      before do
  75.        get announcement_url, headers: non_author_header
  76.      end
  77.  
  78.      context 'when the record exists' do
  79.        it 'returns the announcement' do
  80.          expect(json).not_to be_empty
  81.          expect(json['id']).to eq(announcement_id)
  82.          expect(json.keys).to match_array(expected_keys)
  83.        end
  84.  
  85.        it 'returns status code 200' do
  86.          expect(response).to have_http_status(200)
  87.        end
  88.      end
  89.    end
  90.  end
  91.  
  92.  # describe 'PUT /api/announcements/:id/cancel' do
  93.  #   context 'when user is an author' do
  94.  #     before do
  95.  #       put url + announcement_id + '/cancel', headers: author_header
  96.  #     end
  97.  #
  98.  #     it 'returns the announcement id' do
  99.  #       expect(json['id']).to be_present
  100.  #     end
  101.  #
  102.  #     it 'check status is cancelled' do
  103.  #       announcements.first.reload
  104.  #       expect(announcements.first.status).to eq('cancelled')
  105.  #     end
  106.  #
  107.  #     it 'check response status is declined' do
  108.  #       announcement_response.reload
  109.  #       expect(announcement_response.status).to eq('declined')
  110.  #     end
  111.  #
  112.  #     it 'returns status code 200' do
  113.  #       expect(response).to have_http_status(200)
  114.  #     end
  115.  #   end
  116.  #
  117.  #   context 'when user is non author' do
  118.  #     before do
  119.  #       put url + announcement_id + '/cancel', headers: non_author_header
  120.  #     end
  121.  #
  122.  #     it 'returns the access denied message' do
  123.  #       expect(json['error']).to eq('Access is denied')
  124.  #     end
  125.  #
  126.  #     it 'returns status code 401' do
  127.  #       expect(response).to have_http_status(401)
  128.  #     end
  129.  #   end
  130.  # end
  131.  
  132.  describe 'PUT /api/announcements/:id/cancel' do
  133.    context 'when user is an author' do
  134.      before do
  135.        put announcement_url + '/cancel', headers: author_header
  136.      end
  137.  
  138.      it 'returns the announcement id' do
  139.        expect(json['id']).to be_present
  140.      end
  141.  
  142.      it 'check status is cancelled' do
  143.        announcements.first.reload
  144.        expect(announcements.first.status).to eq('cancelled')
  145.      end
  146.  
  147.      it 'check response status is declined' do
  148.        announcement_response.reload
  149.        expect(announcement_response.status).to eq('declined')
  150.      end
  151.  
  152.      it 'returns status code 200' do
  153.        expect(response).to have_http_status(200)
  154.      end
  155.    end
  156.  
  157.    context 'when user is non author' do
  158.      before do
  159.        put announcement_url + '/cancel', headers: non_author_header
  160.      end
  161.  
  162.      it 'returns the access denied message' do
  163.        expect(json['error']).to eq('Access is denied')
  164.      end
  165.  
  166.      it 'returns status code 401' do
  167.        expect(response).to have_http_status(401)
  168.      end
  169.    end
  170.  end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement