# frozen_string_literal: true require 'spec_helper' RSpec.describe Spearly::Auth::Team do let!(:connection_double) { instance_double(Faraday::Connection) } before do stub_const('ENV', { 'SPEARLY_API_URL' => 'https://api.spearly.test' }) allow(Faraday).to receive(:new).and_return(connection_double) end describe '.create_team()' do let!(:response_double) { instance_double(Faraday::Response, status: 201, body: { 'data' => { 'id' => 'team_1' } }.to_json) } let!(:params) { { hey: 'jude' } } let!(:client) { Spearly::Auth::Client.new('token') } before do allow(connection_double).to receive(:run_request).and_return(response_double) end it 'makes POST request to /teams' do uri_parsed = Addressable::URI.parse('/teams').normalize.to_s expect(connection_double).to receive(:run_request).with(:post, uri_parsed, params, 'Accept' => 'application/vnd.spearly.v2+json', 'Authorization' => 'token') client.create_team(params) end it 'returns object if 201' do res = client.create_team(params) expect(res).to eq('data' => { 'id' => 'team_1' }) end it 'returns errors object if 422' do response_double = instance_double(Faraday::Response, status: 422, body: { 'errors' => { 'owner' => ["can't own more than 2 free teams"] } }.to_json) allow(connection_double).to receive(:run_request).and_return(response_double) res = client.create_team(params) expect(res['errors']).to eq('owner' => ["can't own more than 2 free teams"]) end it 'throws AuthorizationError if 401' do response_double = instance_double(Faraday::Response, status: 401) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.create_team(params) end.to raise_error(Spearly::Auth::AuthorizationError) end it 'throws NotFoundError if 404' do response_double = instance_double(Faraday::Response, status: 404) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.create_team(params) end.to raise_error(Spearly::Auth::NotFoundError) end it 'throws ServerError if 500' do response_double = instance_double(Faraday::Response, status: 500) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.create_team(params) end.to raise_error(Spearly::Auth::ServerError) end it 'throws ServiceUnavailableError if 503' do response_double = instance_double(Faraday::Response, status: 503) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.create_team(params) end.to raise_error(Spearly::Auth::ServiceUnavailableError) end end describe '.get_team()' do let!(:response_double) { instance_double(Faraday::Response, status: 200, body: { 'data' => { 'id' => 'team_1' } }.to_json) } let!(:team_id) { 'team_1' } let!(:client) { Spearly::Auth::Client.new('token') } before do allow(connection_double).to receive(:run_request).and_return(response_double) end it 'makes GET request to /teams/:team_id' do uri_parsed = Addressable::URI.parse("/teams/#{team_id}").normalize.to_s expect(connection_double).to receive(:run_request).with(:get, uri_parsed, nil, 'Accept' => 'application/vnd.spearly.v2+json', 'Authorization' => 'token') client.get_team(team_id) end it 'returns data array if 200' do res = client.get_team(team_id) expect(res).to eq('data' => { 'id' => 'team_1' }) end it 'throws AuthorizationError if 401' do response_double = instance_double(Faraday::Response, status: 401) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_team(team_id) end.to raise_error(Spearly::Auth::AuthorizationError) end it 'throws NotFoundError if 404' do response_double = instance_double(Faraday::Response, status: 404) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_team(team_id) end.to raise_error(Spearly::Auth::NotFoundError) end it 'throws ServerError if 500' do response_double = instance_double(Faraday::Response, status: 500) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_team(team_id) end.to raise_error(Spearly::Auth::ServerError) end it 'throws ServiceUnavailableError if 503' do response_double = instance_double(Faraday::Response, status: 503) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_team(team_id) end.to raise_error(Spearly::Auth::ServiceUnavailableError) end end describe '.get_teams()' do let!(:response_double) { instance_double(Faraday::Response, status: 200, body: { 'data' => [{ 'id' => 'team_1' }] }.to_json) } let!(:params) { { service_name: 'cms' } } let!(:client) { Spearly::Auth::Client.new('token') } before do allow(connection_double).to receive(:run_request).and_return(response_double) end it 'makes GET request to /teams' do uri_parsed = Addressable::URI.parse('/teams').normalize.to_s expect(connection_double).to receive(:run_request).with(:get, uri_parsed, params, 'Accept' => 'application/vnd.spearly.v2+json', 'Authorization' => 'token') client.get_teams(params) end it 'returns data array if 200' do res = client.get_teams(params) expect(res).to eq('data' => [{ 'id' => 'team_1' }]) end it 'throws AuthorizationError if 401' do response_double = instance_double(Faraday::Response, status: 401) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_teams(params) end.to raise_error(Spearly::Auth::AuthorizationError) end it 'throws NotFoundError if 404' do response_double = instance_double(Faraday::Response, status: 404) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_teams(params) end.to raise_error(Spearly::Auth::NotFoundError) end it 'throws ServerError if 500' do response_double = instance_double(Faraday::Response, status: 500) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_teams(params) end.to raise_error(Spearly::Auth::ServerError) end it 'throws ServiceUnavailableError if 503' do response_double = instance_double(Faraday::Response, status: 503) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.get_teams(params) end.to raise_error(Spearly::Auth::ServiceUnavailableError) end end describe '.update_team()' do let!(:response_double) { instance_double(Faraday::Response, status: 200, body: { 'data' => { 'id' => 'team_1' } }.to_json) } let!(:team_id) { 'team_1' } let!(:client) { Spearly::Auth::Client.new('token') } let!(:params) do { team: { name: 'new name' } } end before do allow(connection_double).to receive(:run_request).and_return(response_double) end it 'makes PUT request to /teams/:team_id' do uri_parsed = Addressable::URI.parse("/teams/#{team_id}").normalize.to_s expect(connection_double).to receive(:run_request).with(:put, uri_parsed, params, 'Accept' => 'application/vnd.spearly.v2+json', 'Authorization' => 'token') client.update_team(team_id, params) end it 'returns data object if 200' do res = client.update_team(team_id, params) expect(res).to eq('data' => { 'id' => 'team_1' }) end it 'throws AuthorizationError if 401' do response_double = instance_double(Faraday::Response, status: 401) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.update_team(team_id, params) end.to raise_error(Spearly::Auth::AuthorizationError) end it 'throws NotFoundError if 404' do response_double = instance_double(Faraday::Response, status: 404) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.update_team(team_id, params) end.to raise_error(Spearly::Auth::NotFoundError) end it 'throws ServerError if 500' do response_double = instance_double(Faraday::Response, status: 500) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.update_team(team_id, params) end.to raise_error(Spearly::Auth::ServerError) end it 'throws ServiceUnavailableError if 503' do response_double = instance_double(Faraday::Response, status: 503) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.update_team(team_id, params) end.to raise_error(Spearly::Auth::ServiceUnavailableError) end end describe '.regenerate_team_api_access_token()' do let!(:response_double) { instance_double(Faraday::Response, status: 200, body: { 'data' => { 'id' => 'team_1' } }.to_json) } let!(:team_id) { 'team_1' } let!(:client) { Spearly::Auth::Client.new('token') } before do allow(connection_double).to receive(:run_request).and_return(response_double) end it 'makes GET request to /teams/:team_id/regenerate_api_access_token' do uri_parsed = Addressable::URI.parse("/teams/#{team_id}/regenerate_api_access_token").normalize.to_s expect(connection_double).to receive(:run_request).with(:get, uri_parsed, nil, 'Accept' => 'application/vnd.spearly.v2+json', 'Authorization' => 'token') client.regenerate_team_api_access_token(team_id) end it 'returns data object if 200' do res = client.regenerate_team_api_access_token(team_id) expect(res).to eq('data' => { 'id' => 'team_1' }) end it 'throws AuthorizationError if 401' do response_double = instance_double(Faraday::Response, status: 401) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.regenerate_team_api_access_token(team_id) end.to raise_error(Spearly::Auth::AuthorizationError) end it 'throws NotFoundError if 404' do response_double = instance_double(Faraday::Response, status: 404) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.regenerate_team_api_access_token(team_id) end.to raise_error(Spearly::Auth::NotFoundError) end it 'throws ServerError if 500' do response_double = instance_double(Faraday::Response, status: 500) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.regenerate_team_api_access_token(team_id) end.to raise_error(Spearly::Auth::ServerError) end it 'throws ServiceUnavailableError if 503' do response_double = instance_double(Faraday::Response, status: 503) allow(connection_double).to receive(:run_request).and_return(response_double) expect do client.regenerate_team_api_access_token(team_id) end.to raise_error(Spearly::Auth::ServiceUnavailableError) end end end