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.create } describe '.games' do context 'when no id is given' do context '.get' do it 'should throw error' do expect { strutta.games.get }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.update' do it 'should throw error' do expect { strutta.games.update(title: 'title') }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.delete' do it 'should throw error' do expect { strutta.games.delete }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.rounds' do it 'should throw error' do expect { strutta.games.rounds }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.participants' do it 'should throw error' do expect { strutta.games.participants }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.flow' do it 'should throw error' do expect { strutta.games.flow }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.entries' do it 'should throw error' do expect { strutta.games.entries }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.all' do before do stub_request(:get, /.*games/) .to_return(status: 200, body: GAME_INDEX.to_json, headers: {}) end it 'should return array of games' do games = strutta.games.all results = games['results'] expect(results.is_a? Array).to be true expect(results.first['id']).to_not be nil expect(results.first['entries_count']).to_not be nil expect(results.last['id']).to_not be nil expect(results.last['entries_count']).to_not be nil expect(games['paging']['min_id']).to equal VALID_GAME_01[:id] expect(games['paging']['max_id']).to equal VALID_GAME_02[:id] end end context '.create' do before do stub_request(:post, /.*games/) .to_return(status: 201, body: VALID_GAME_01.to_json, headers: {}) end it 'should return a new game hash' do expect(game['id']).to eq VALID_GAME_01[:id] end end end context 'when id is given' do before do stub_request(:post, /.*games/) .to_return(status: 201, body: VALID_GAME_02.to_json, headers: {}) stub_request(:get, /.*games\/\d+/) .to_return(status: 200, body: VALID_GAME_02.to_json, headers: {}) end context '.get' do it 'should return game hash' do g = strutta.games(game['id']).get expect(g['id']).to eq VALID_GAME_02[:id] end end context '.update' do before do stub_request(:post, /.*games/) .to_return(status: 200, body: VALID_GAME_01.to_json, headers: {}) stub_request(:patch, /.*games\/\d+/) .to_return(status: 200, body: VALID_GAME_01_UPDATED.to_json, headers: {}) end it 'should return updated game hash' do updated = strutta.games(game['id']).update(title: 'updated') expect(updated['title']).to eq('updated') end end context '.delete' do before do stub_request(:post, /.*games/) .to_return(status: 201, body: VALID_GAME_02.to_json, headers: {}) stub_request(:delete, /.*games\/\d+/) .to_return(status: 204, body: nil, headers: {}) end it 'should delete game' do expect(strutta.games(game['id']).delete).to be true end end context 'check proper object returns' do before do stub_request(:post, /.*games/) .to_return(status: 201, body: VALID_GAME_02.to_json, headers: {}) end context '.rounds' do it 'should return a Rounds object' do rounds = strutta.games(game['id']).rounds expect(rounds.is_a? Strutta::Rounds).to be true end end context '.participants' do it 'should return a Participants object' do participants = strutta.games(game['id']).participants expect(participants.is_a? Strutta::Participants).to be true end end context '.flow' do it 'should return a Flow object' do flow = strutta.games(game['id']).flow expect(flow.is_a? Strutta::Flow).to be true end end context '.entries' do it 'should return a Entries object' do entries = strutta.games(game['id']).entries expect(entries.is_a? Strutta::Entries).to be true end end context '.all' do it 'should throw error' do expect { strutta.games(game['id']).all }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end context '.create' do it 'should throw error' do expect { strutta.games(game['id']).create }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end end end end end