require 'rails_helper'

RSpec.describe "/api/me/contact_requests/:id/reject", type: :request do
  describe 'POST' do
    it_should_be_an_authenticated_api { |context| context.post '/api/me/contact_requests/1/reject' }

    context "when authenticated" do
      context "when rejecting an owned request" do
        before(:each) do
          @requester = FactoryGirl.create :user, :confirmed
          @owner = FactoryGirl.create :user, :confirmed
          @contact_request = FactoryGirl.create :contact_request, requester: @requester, owner: @owner

          @requester.feeds.destroy_all

          post "/api/me/contact_requests/#{@contact_request.id}/reject", {}, authorization_headers_for(@owner)
        end

        it_should_respond_with_ok

        it "should not make both users contacts of each other" do
          expect(@requester.contact_ids).not_to include(@owner.id)
          expect(@owner.contact_ids).not_to include(@requester.id)
        end

        it "should generate a notification for the requester" do
          expect(@requester.feeds(true).count).to eq(1)
        end

        it "should destroy the request" do
          expect(ContactRequest.count).to eq(0)
        end
      end

      context "when rejecting request owner by another user" do
        before(:each) do
          @requester = FactoryGirl.create :user, :confirmed
          @owner = FactoryGirl.create :user, :confirmed
          @contact_request = FactoryGirl.create :contact_request, requester: @requester, owner: @owner
          @accepter = FactoryGirl.create :user, :confirmed

          @requester.feeds.destroy_all
          @owner.feeds.destroy_all

          post "/api/me/contact_requests/#{@contact_request.id}/reject", {}, authorization_headers_for(@accepter)
        end

        it_should_respond_with_not_found

        it "should not generate a notification for the requester" do
          expect(@requester.feeds(true).count).to eq(0)
        end
      end
    end
  end
end

