|
| 1 | +require 'spec_helper' |
| 2 | +require 'travis/services/assembla_notify_service' |
| 3 | + |
| 4 | +RSpec.describe Travis::Services::AssemblaNotifyService do |
| 5 | + let(:payload) { { 'action' => 'restrict', 'object' => 'tool', 'id' => '12345' } } |
| 6 | + let(:service) { described_class.new(payload) } |
| 7 | + let(:vcs_repository) { instance_double(Travis::RemoteVCS::Repository) } |
| 8 | + let(:vcs_organization) { instance_double(Travis::RemoteVCS::Organization) } |
| 9 | + |
| 10 | + before do |
| 11 | + allow(Travis::RemoteVCS::Repository).to receive(:new).and_return(vcs_repository) |
| 12 | + allow(vcs_repository).to receive(:destroy).with(any_args) |
| 13 | + allow(vcs_repository).to receive(:restore) |
| 14 | + allow(Travis::RemoteVCS::Organization).to receive(:new).and_return(vcs_organization) |
| 15 | + allow(vcs_organization).to receive(:destroy) |
| 16 | + allow(vcs_organization).to receive(:restore) |
| 17 | + allow(Travis.logger).to receive(:error) |
| 18 | + end |
| 19 | + |
| 20 | + describe '#run' do |
| 21 | + context 'with a valid payload for tool restriction' do |
| 22 | + it 'calls destroy on the vcs_repository' do |
| 23 | + expect(vcs_repository).to receive(:destroy).with(repository_id: '12345', vcs_type: 'AssemblaRepository') |
| 24 | + service.run |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context 'with a valid payload for tool restoration' do |
| 29 | + let(:payload) { { 'action' => 'restore', 'object' => 'tool', 'id' => '12345' } } |
| 30 | + it 'calls restore on the vcs_repository' do |
| 31 | + expect(vcs_repository).to receive(:restore).with(repository_id: '12345') |
| 32 | + service.run |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context 'with a valid payload for space restriction' do |
| 37 | + let(:payload) { { 'action' => 'restrict', 'object' => 'space', 'id' => '67890' } } |
| 38 | + |
| 39 | + it 'calls destroy on the vcs_organization' do |
| 40 | + expect(vcs_organization).to receive(:destroy).with(org_id: '67890') |
| 41 | + service.run |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'with a valid payload for space restoration' do |
| 46 | + let(:payload) { { 'action' => 'restore', 'object' => 'space', 'id' => '67890' } } |
| 47 | + |
| 48 | + it 'calls restore on the vcs_organization' do |
| 49 | + expect(vcs_organization).to receive(:restore).with(org_id: '67890') |
| 50 | + service.run |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context 'with an invalid object type' do |
| 55 | + let(:payload) { { 'action' => 'restrict', 'object' => 'repository', 'id' => '12345' } } |
| 56 | + |
| 57 | + it 'returns false and logs an error' do |
| 58 | + expect(service.run).to be_falsey |
| 59 | + expect(Travis.logger).to have_received(:error).with("Invalid object type: repository. Allowed objects: space, tool") |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context 'with an invalid action type' do |
| 64 | + let(:payload) { { 'action' => 'modify', 'object' => 'tool', 'id' => '12345' } } |
| 65 | + |
| 66 | + it 'returns false and logs an error' do |
| 67 | + expect(service.run).to be_falsey |
| 68 | + expect(Travis.logger).to have_received(:error).with("Invalid action: modify. Allowed actions: restrict, restore") |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context 'with an unsupported object type for an action' do |
| 73 | + before do |
| 74 | + stub_const("Travis::Services::AssemblaNotifyService::VALID_OBJECTS", %w[space tool unsupported]) |
| 75 | + end |
| 76 | + let(:payload) { { 'action' => 'restrict', 'object' => 'unsupported', 'id' => '12345' } } |
| 77 | + |
| 78 | + it 'returns false without logging an error for the action' do |
| 79 | + expect(service.run).to be_falsey |
| 80 | + expect(vcs_repository).not_to receive(:destroy) |
| 81 | + expect(vcs_repository).not_to receive(:restore) |
| 82 | + expect(vcs_organization).not_to receive(:destroy) |
| 83 | + expect(vcs_organization).not_to receive(:restore) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments