specs for services
This commit is contained in:
76
spec/services/exercise_service/check_external_spec.rb
Normal file
76
spec/services/exercise_service/check_external_spec.rb
Normal file
@ -0,0 +1,76 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe ExerciseService::CheckExternal do
|
||||
describe '.new' do
|
||||
subject(:export_service) { described_class.new(uuid: uuid, codeharbor_link: codeharbor_link) }
|
||||
|
||||
let(:uuid) { SecureRandom.uuid }
|
||||
let(:codeharbor_link) { FactoryBot.build(:codeharbor_link) }
|
||||
|
||||
it 'assigns uuid' do
|
||||
expect(export_service.instance_variable_get(:@uuid)).to be uuid
|
||||
end
|
||||
|
||||
it 'assigns codeharbor_link' do
|
||||
expect(export_service.instance_variable_get(:@codeharbor_link)).to be codeharbor_link
|
||||
end
|
||||
end
|
||||
|
||||
describe '#execute' do
|
||||
subject(:check_external_service) { described_class.call(uuid: uuid, codeharbor_link: codeharbor_link) }
|
||||
|
||||
let(:uuid) { SecureRandom.uuid }
|
||||
let(:codeharbor_link) { FactoryBot.build(:codeharbor_link) }
|
||||
let(:response) { {}.to_json }
|
||||
|
||||
before { stub_request(:post, codeharbor_link.check_uuid_url).to_return(body: response) }
|
||||
|
||||
it 'calls the correct url' do
|
||||
expect(check_external_service).to have_requested(:post, codeharbor_link.check_uuid_url)
|
||||
end
|
||||
|
||||
it 'submits the correct headers' do
|
||||
expect(check_external_service).to have_requested(:post, codeharbor_link.check_uuid_url)
|
||||
.with(headers: {content_type: 'application/json', authorization: "Bearer #{codeharbor_link.api_key}"})
|
||||
end
|
||||
|
||||
it 'submits the correct body' do
|
||||
expect(check_external_service).to have_requested(:post, codeharbor_link.check_uuid_url)
|
||||
.with(body: {uuid: uuid}.to_json)
|
||||
end
|
||||
|
||||
context 'when response contains a JSON with expected keys' do
|
||||
let(:response) { {message: 'message', exercise_found: true, update_right: true}.to_json }
|
||||
|
||||
it 'returns the correct hash' do
|
||||
expect(check_external_service).to eql(error: false, message: 'message', exercise_found: true, update_right: true)
|
||||
end
|
||||
|
||||
context 'with different values' do
|
||||
let(:response) { {message: 'message', exercise_found: false, update_right: false}.to_json }
|
||||
|
||||
it 'returns the correct hash' do
|
||||
expect(check_external_service).to eql(error: false, message: 'message', exercise_found: false, update_right: false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when response does not contain JSON' do
|
||||
let(:response) { 'foo' }
|
||||
|
||||
it 'returns the correct hash' do
|
||||
expect(check_external_service).to eql(error: true, message: I18n.t('exercises.export_codeharbor.error'))
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the request fails' do
|
||||
before { allow(Faraday).to receive(:new).and_raise(Faraday::Error, 'error') }
|
||||
|
||||
it 'returns the correct hash' do
|
||||
expect(check_external_service).to eql(error: true, message: I18n.t('exercises.export_codeharbor.error'))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
64
spec/services/exercise_service/push_external_spec.rb
Normal file
64
spec/services/exercise_service/push_external_spec.rb
Normal file
@ -0,0 +1,64 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ExerciseService::PushExternal do
|
||||
describe '.new' do
|
||||
subject(:push_external) { described_class.new(zip: zip, codeharbor_link: codeharbor_link) }
|
||||
|
||||
let(:zip) { ProformaService::ExportTask.call(exercise: FactoryBot.build(:dummy)) }
|
||||
let(:codeharbor_link) { FactoryBot.build(:codeharbor_link) }
|
||||
|
||||
it 'assigns zip' do
|
||||
expect(push_external.instance_variable_get(:@zip)).to be zip
|
||||
end
|
||||
|
||||
it 'assigns codeharbor_link' do
|
||||
expect(push_external.instance_variable_get(:@codeharbor_link)).to be codeharbor_link
|
||||
end
|
||||
end
|
||||
|
||||
describe '#execute' do
|
||||
subject(:push_external) { described_class.call(zip: zip, codeharbor_link: codeharbor_link) }
|
||||
|
||||
let(:zip) { ProformaService::ExportTask.call(exercise: FactoryBot.build(:dummy)) }
|
||||
let(:codeharbor_link) { FactoryBot.build(:codeharbor_link) }
|
||||
let(:status) { 200 }
|
||||
let(:response) { '' }
|
||||
|
||||
before { stub_request(:post, codeharbor_link.push_url).to_return(status: status, body: response) }
|
||||
|
||||
it 'calls the correct url' do
|
||||
expect(push_external).to have_requested(:post, codeharbor_link.push_url)
|
||||
end
|
||||
|
||||
it 'submits the correct headers' do
|
||||
expect(push_external).to have_requested(:post, codeharbor_link.push_url)
|
||||
.with(headers: {content_type: 'application/zip',
|
||||
authorization: "Bearer #{codeharbor_link.api_key}",
|
||||
content_length: zip.string.length})
|
||||
end
|
||||
|
||||
it 'submits the correct body' do
|
||||
expect(push_external).to have_requested(:post, codeharbor_link.push_url)
|
||||
.with(body: zip.string)
|
||||
end
|
||||
|
||||
context 'when response status is success' do
|
||||
it { is_expected.to be nil }
|
||||
|
||||
context 'when response status is 500' do
|
||||
let(:status) { 500 }
|
||||
let(:response) { 'an error occured' }
|
||||
|
||||
it { is_expected.to be response }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when an error occurs' do
|
||||
before { allow(Faraday).to receive(:new).and_raise(StandardError) }
|
||||
|
||||
it { is_expected.not_to be nil }
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user