diff --git a/spec/jobs/nbp_sync_job_spec.rb b/spec/jobs/nbp_sync_job_spec.rb new file mode 100644 index 000000000..4b89f7129 --- /dev/null +++ b/spec/jobs/nbp_sync_job_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe NbpSyncJob do + let(:task) { create(:task, access_level:) } + let(:uuid) { task.uuid } + + before do + allow(Nbp::PushConnector).to receive(:instance) + allow(Nbp::PushConnector.instance).to receive(:push_lom!) + allow(Nbp::PushConnector.instance).to receive(:delete_task!) + end + + describe 'perform' do + subject(:perform_job) { described_class.perform_now(uuid) } + + context 'when the task is public' do + let(:access_level) { :public } + + it 'pushes the task' do + perform_job + expect(Nbp::PushConnector.instance).to have_received(:push_lom!) + end + end + + context 'when the task does not exist' do + let(:uuid) { :not_existing_uuid } + + it 'deletes the task' do + perform_job + expect(Nbp::PushConnector.instance).to have_received(:delete_task!) + end + end + + context 'when the task is private' do + let(:access_level) { :private } + + it 'deletes the task' do + perform_job + expect(Nbp::PushConnector.instance).to have_received(:delete_task!) + end + end + end +end diff --git a/spec/lib/nbp/push_connector_spec.rb b/spec/lib/nbp/push_connector_spec.rb index beca53fec..decbb2bfe 100644 --- a/spec/lib/nbp/push_connector_spec.rb +++ b/spec/lib/nbp/push_connector_spec.rb @@ -15,15 +15,14 @@ stub_request(:post, Settings.nbp.push_connector.token_path).to_return_json(body: {token: 'sometoken', expires_in: 600}) stub_request(:post, "#{api_host}/datenraum/api/core/sources") stub_request(:put, "#{api_host}/push-connector/api/lom-v2/#{source_slug}") + stub_request(:delete, %r{#{api_host}/push-connector/api/course/#{source_slug}/}) stub_request(:get, "#{api_host}/datenraum/api/core/sources/slug/#{source_slug}").to_return(status: 404) end - describe 'push_lom!' do - subject(:push_lom!) { connector.push_lom!(task_xml) } - + describe 'initialize' do context 'when no source exists' do it 'creates a source' do - push_lom! + connector expect(WebMock).to have_requested(:post, "#{api_host}/datenraum/api/core/sources") end end @@ -32,10 +31,14 @@ before { stub_request(:get, "#{api_host}/datenraum/api/core/sources/slug/#{source_slug}").to_return(status: 200) } it 'does not create a source' do - push_lom! + connector expect(WebMock).not_to have_requested(:post, "#{api_host}/datenraum/api/core/sources") end end + end + + describe 'push_lom!' do + subject(:push_lom!) { connector.push_lom!(task_xml) } context 'without any errors' do it 'pushes the metadata' do @@ -44,4 +47,15 @@ end end end + + describe 'delete_task!' do + subject(:delete_task!) { connector.delete_task!(task.uuid) } + + context 'without any errors' do + it 'pushes the metadata' do + delete_task! + expect(WebMock).to have_requested(:delete, "#{api_host}/push-connector/api/course/#{source_slug}/#{task.uuid}") + end + end + end end