# frozen_string_literal: true require 'spec_helper' require 'shared_examples_for_endpoints' module Fizzy module Api module Endpoints describe RenderGraph do let(:dossier_id) { '123' } let(:protocol_subscription_id) { 'abc' } let(:path) { 'welbevinden.svg' } let(:options) { {} } let(:response) { httparty_response('') } let(:session) { FactoryBot.build :basic_auth_session } it_behaves_like 'an endpoint' before do allow(Sessions::BasicAuthSession).to receive(:new).and_return session end it 'should call the correct url' do expect(session).to receive(:get) .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options) .and_return(response) outcome = described_class.run! dossier_id: dossier_id, protocol_subscription_id: protocol_subscription_id, path: path, options: options expect(outcome).to eq response end describe 'error handling' do it 'should notice a 202' do allow(response).to receive(:code).and_return 202 expect(session).to receive(:get) .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options) .and_return(response) outcome = lambda do described_class.run! dossier_id: dossier_id, protocol_subscription_id: protocol_subscription_id, path: path, options: options end expect { outcome.call }.to raise_error(Errors::OutcomeNotAvailableError) end it 'should notice a 204' do allow(response).to receive(:code).and_return 204 expect(session).to receive(:get) .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options) .and_return(response) outcome = lambda do described_class.run! dossier_id: dossier_id, protocol_subscription_id: protocol_subscription_id, path: path, options: options end expect { outcome.call }.to raise_error(Errors::TooFewMeasurementsError) end it 'should notice a 404' do allow(response).to receive(:code).and_return 404 expect(session).to receive(:get) .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options) .and_return(response) outcome = lambda do described_class.run! dossier_id: dossier_id, protocol_subscription_id: protocol_subscription_id, path: path, options: options end expect { outcome.call }.to raise_error(Errors::GraphNotFoundError) end it 'should fail a 500 with an UnexpectedStatusError' do allow(response).to receive(:code).and_return 500 expect(session).to receive(:get) .with("/dossier/#{dossier_id}/protocol_subscriptions/#{protocol_subscription_id}/render/#{path}", options) .and_return(response) outcome = lambda do described_class.run! dossier_id: dossier_id, protocol_subscription_id: protocol_subscription_id, path: path, options: options end expect { outcome.call }.to raise_error(Errors::UnexpectedStatusError) end end end end end end