require_relative 'spec_helper' require_relative '../lib/action/custom' describe Action::Custom, vcr: true do let(:key_id) { 'key-1' } let(:secret) { "0a3938d9d95d807e94d937af3a4fbbea" } let(:base_api_endpoint) { 'http://localhost:3000/api/v4' } let(:json_str_valid) do { 'payload' => { 'api_endpoints' => %w{fake/info_refs fake/push}, 'data' => { 'username' => 'user1', 'primary_repo' => 'http://localhost:3001/user1/repo1.git' } } }.to_json end let(:json_str_missing_api_endpoints) do { 'payload' => { 'data' => { 'username' => 'user1', 'primary_repo' => 'http://localhost:3001/user1/repo1.git' } } }.to_json end let(:json_str_empty_api_endpoints) do { 'payload' => { 'api_endpoints' => [], 'data' => { 'username' => 'user1', 'primary_repo' => 'http://localhost:3001/user1/repo1.git' } } }.to_json end let(:json_str_missing_data) do { 'payload' => { 'api_endpoints' => %w{fake/info_refs fake/push} } }.to_json end let(:json_str_invalid_api_endpoints) do { 'payload' => { 'api_endpoints' => %w{fake/info_refs_bad fake/push_bad}, 'data' => { 'username' => 'user1', 'primary_repo' => 'http://localhost:3001/user1/repo1.git' } } }.to_json end let(:json_str_empty) { { '' => {} }.to_json } let(:json_str_empty_payload) { { 'payload' => {} }.to_json } describe '.create_from_json' do it 'creates an instance from a JSON string' do described_class.create_from_json(json_str_empty) end end context 'instantiated' do describe '#execute' do context 'with an empy JSON string' do subject { described_class.create_from_json(json_str_empty) } it 'returns nil' do expect { subject.execute(key_id) }.to raise_error(CustomActionStatus::MissingPayloadError) end end context 'with an empty payload' do subject { described_class.create_from_json(json_str_empty_payload) } it 'returns nil' do expect { subject.execute(key_id) }.to raise_error(CustomActionStatus::MissingPayloadError) end end context 'with api_endpoints defined' do before do subject.stub(:base_api_endpoint).and_return(base_api_endpoint) subject.stub(:secret_token).and_return(secret) $stdin.stub(:read).and_return('') end context 'that are valid' do subject { described_class.create_from_json(json_str_valid) } it 'HTTP posts data to defined api_endpoints' do VCR.use_cassette("custom-action-ok") do subject.execute(key_id).should be_instance_of(Net::HTTPCreated) end end end context 'that are invalid' do context 'where api_endpoints key is missing' do subject { described_class.create_from_json(json_str_missing_api_endpoints) } it 'raises a MissingAPIEndpointsError exception' do expect { subject.execute(key_id) }.to raise_error(CustomActionStatus::MissingAPIEndpointsError) end end context 'where api_endpoints is empty' do subject { described_class.create_from_json(json_str_empty_api_endpoints) } it 'raises a MissingAPIEndpointsError exception' do expect { subject.execute(key_id) }.to raise_error(CustomActionStatus::MissingAPIEndpointsError) end end context 'where data key is missing' do subject { described_class.create_from_json(json_str_missing_data) } it 'raises a MissingDataError exception' do expect { subject.execute(key_id) }.to raise_error(CustomActionStatus::MissingDataError) end end context 'where API endpoints are bad' do subject { described_class.create_from_json(json_str_invalid_api_endpoints) } it 'HTTP posts data to defined api_endpoints' do VCR.use_cassette("custom-action-not-ok") do subject.execute(key_id).should be_instance_of(Net::HTTPForbidden) end end end end end end end end