Skip to content

Commit

Permalink
Adding tests for NbpSyncJob and Nbp::PushConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathis-Z committed Jul 9, 2024
1 parent 09ced8b commit 1fe164c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
45 changes: 45 additions & 0 deletions spec/jobs/nbp_sync_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 19 additions & 5 deletions spec/lib/nbp/push_connector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 1fe164c

Please sign in to comment.