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(:new_game) { strutta.games.create } let(:round_params) do { title: 'my round', type: 'submission', start_date: Time.now.to_i, end_date: Time.now.to_i + 3600, rules: { num_entries: 100, interval: 'day' } } end before do stub_request(:post, /.*games/) .to_return(status: 201, body: VALID_GAME_02.to_json, headers: {}) end describe 'games(:id).rounds' do context 'when no id is given' do context '.get' do it 'should throw error' do expect { strutta.games.rounds.get }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.update' do it 'should throw error' do expect { strutta.games.rounds.update(title: 'title') }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.delete' do it 'should throw error' do expect { strutta.games.rounds.delete }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.all' do before do stub_request(:get, /.*rounds/) .to_return(status: 200, body: ROUND_INDEX.to_json, headers: {}) end it 'should return array of rounds' do round_list = strutta.games(VALID_GAME_02[:id]).rounds.all expect(round_list.is_a? Array).to be true end end context '.create' do before do stub_request(:post, /.*rounds/) .to_return(status: 201, body: SUBMISSION_ROUND.to_json, headers: {}) end context 'with valid parameters' do it 'should return a new round hash' do round = strutta.games(VALID_GAME_02[:id]).rounds.create(round_params) expect(round['id']).to_not be nil end end end end context 'when id is given' do before do stub_request(:get, /.*rounds\/\d+/) .to_return(status: 200, body: RANDOM_DRAW_ROUND.to_json, headers: {}) end context '.get' do it 'should return round hash' do r = strutta.games(VALID_GAME_01[:id]).rounds(RANDOM_DRAW_ROUND[:id]).get expect(r['id']).to_not be nil end end context '.update' do before do stub_request(:patch, /.*rounds\/\d+/) .to_return(status: 200, body: SUBMISSION_ROUND_UPDATED.to_json, headers: {}) end it 'should return updated round hash' do updated = strutta.games(VALID_GAME_01[:id]).rounds(SUBMISSION_ROUND[:id]).update(title: 'updated') expect(updated['title']).to eq('updated') end end context '.delete' do before do stub_request(:delete, /.*rounds\/\d+/) .to_return(status: 204, body: '', headers: {}) end it 'should delete round' do expect(strutta.games(VALID_GAME_01[:id]).rounds(SUBMISSION_ROUND[:id]).delete).to be true end end context '.all' do it 'should throw error' do expect { strutta.games(VALID_GAME_01[:id]).rounds(PRIZE_FULFILLMENT_ROUND[:id]).all }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end context '.create' do it 'should throw error' do expect { strutta.games(VALID_GAME_01[:id]).rounds(PRIZE_FULFILLMENT_ROUND[:id]).create(round_params) }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end end end end