require 'spec_helper' describe 'Strutta API Wrapper' do before { WebMock.disable_net_connect!(allow_localhost: true, allow: /codeclimate/) } after { WebMock.allow_net_connect! } let(:token) { 'dc1479c52801fbfa0975947de092a1e7' } let(:host) { 'http://api.strutta.dev:4000' } let(:path) { '/v2/' } let(:strutta) { Strutta::API.new token, host, path } let(:game) { strutta.games(VALID_GAME_01[:id]) } let(:participant) { game.participants.create(email: 'test@test.com') } describe 'games(:id).participants' do context 'when no id is given' do context '.get' do it 'should throw error' do expect { strutta.games.participants.get }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.update' do it 'should throw error' do expect { strutta.games.participants.update(title: 'title') }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.delete' do it 'should throw error' do expect { strutta.games.participants.delete }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.token_renew' do it 'should throw error' do expect { strutta.games.participants.token_renew }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.permissions' do it 'should throw error' do expect { strutta.games.participants.permissions }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.permissions_update' do it 'should throw error' do expect { strutta.games.participants.permissions_update }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.all' do before do stub_request(:get, /.*participants/) .to_return(status: 200, body: PARTICIPANT_INDEX.to_json, headers: {}) end it 'should return array of participants' do participant_list = game.participants.all expect(participant_list.is_a? Array).to be true end end context '.create' do context 'with valid parameters' do before do stub_request(:post, /.*participants/) .to_return(status: 201, body: PARTICIPANT_01.to_json, headers: {}) end it 'should return a new participant hash' do expect(participant['id']).to_not be nil end end end context '.search' do context 'with valid parameters and existing participant' do before do stub_request(:get, /.*participants\/search/) .to_return(status: 200, body: PARTICIPANT_01.to_json, headers: {}) end it 'should return a new participant hash' do p = game.participants.search(email: PARTICIPANT_01[:email]) expect(p['id']).to_not be nil end end context 'with valid parameters and no existing participant' do before do stub_request(:get, /.*participants\/search/) .to_return(status: 404, body: { error: 'not found', message: 'not found' }.to_json, headers: {}) end it 'should throw object not found error' do expect { game.participants.search(email: PARTICIPANT_01[:email]) }.to raise_error(Strutta::Errors::ObjectNotFoundError) end end context 'with invalid parameters' do it 'should throw invalid parameters error' do expect { game.participants.search(name: PARTICIPANT_01[:id]) }.to raise_error(Strutta::Errors::InvalidSearchParameters) end end end end context 'when id is given' do context '.get' do before do stub_request(:get, /.*participants\/\d+/) .to_return(status: 200, body: PARTICIPANT_01.to_json, headers: {}) end it 'should return participant hash' do p = game.participants(PARTICIPANT_01[:id]).get expect(p['id']).to_not be nil end end context '.update' do before do stub_request(:patch, /.*participants\/\d+/) .to_return(status: 200, body: PARTICIPANT_01_UPDATED.to_json, headers: {}) end it 'should return updated game hash' do metadata = { age: 20 } updated = game.participants(PARTICIPANT_01[:id]).update(metadata: metadata) expect(updated['metadata']['age']).to eq(20) end end context '.token_renew' do before do stub_request(:patch, %r{.*participants\/\d+\/token}) .to_return(status: 200, body: PARTICIPANT_TOKEN_RENEW.to_json, headers: {}) end it 'should return updated token information' do body = game.participants(PARTICIPANT_01[:id]).token_renew expect(body['token']).not_to be nil expect(body['token_expired']).to be false end end context '.permissions' do before do stub_request(:get, %r{.*participants\/\d+\/permissions}) .to_return(status: 200, body: PARTICIPANT_PERMISSIONS.to_json, headers: {}) end it 'should return permissions array' do body = game.participants(PARTICIPANT_01[:id]).permissions expect(body['permissions'].is_a? Array).to be true expect(body['permissions'].include?('api_basic')).to be true expect(body['permissions'].include?('moderate')).to be true end end context '.permissions_update' do before do stub_request(:patch, %r{.*participants\/\d+\/permissions}) .to_return(status: 200, body: PARTICIPANT_PERMISSIONS_UPDATED.to_json, headers: {}) end it 'should return permissions array' do body = game.participants(PARTICIPANT_01[:id]).permissions_update expect(body['permissions'].is_a? Array).to be true expect(body['permissions'].include?('api_basic')).to be true expect(body['permissions'].include?('moderate')).to be false end end context '.delete' do before do stub_request(:delete, /.*participants\/\d+/) .to_return(status: 204, body: '', headers: {}) end it 'should delete participant' do expect(game.participants(PARTICIPANT_01[:id]).delete).to be true end end context '.all' do it 'should throw error' do expect { game.participants(PARTICIPANT_01[:id]).all }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end context '.create' do it 'should throw error' do expect { game.participants(PARTICIPANT_01[:id]).create(email: 'test@email.com') }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end end end end