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 } describe 'games(:id).entries' do context 'when no id is given' do context '.get' do it 'should throw error' do expect { strutta.games.entries.get }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.update' do it 'should throw error' do expect { strutta.games.entries.update(title: 'title') }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.delete' do it 'should throw error' do expect { strutta.games.entries.delete }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.transitions' do it 'should throw error' do expect { strutta.games.entries.transitions }.to raise_error(Strutta::Errors::ObjectIDRequired) end end context '.all' do before do stub_request(:get, /.*entries/) .to_return(status: 200, body: ENTRY_INDEX.to_json, headers: {}) end it 'should return array of entries' do entries = strutta.games(VALID_GAME_01[:id]).entries.all results = entries['results'] expect(results.is_a? Array).to be true end context 'when state is passed' do before do stub_request(:get, /.*entries/) .with(body: hash_including(state: 111)) .to_return(status: 200, body: ENTRY_INDEX.to_json, headers: {}) end it 'should return entries with that state' do entries = strutta.games(VALID_GAME_01[:id]).entries.all(state: 111) results = entries['results'] expect(results.is_a? Array).to be true end end context 'when participant is passed' do before do stub_request(:get, /.*entries/) .to_return(status: 200, body: ENTRY_INDEX.to_json, headers: {}) end it 'should return entries with that state' do entries = strutta.games(VALID_GAME_01[:id]).entries.all(participant: SUBMISSION_ROUND[:id]) results = entries['results'] expect(results.is_a? Array).to be true end end end context '.leaderboard' do before do stub_request(:get, /.*entries\/leaderboard/) .to_return(status: 200, body: ENTRY_LEADERBOARD.to_json, headers: {}) end it 'should return a leaderboard of entries' do leaderboard = strutta.games(VALID_GAME_01[:id]).entries.leaderboard results = leaderboard['results'] expect(results.is_a? Array).to be true expect(leaderboard['paging']['top_rank'].is_a? Integer).to be true end end context '.on_demand_winners' do before do stub_request(:get, /.*entries\/on-demand-winners/) .to_return(status: 200, body: ENTRY_INDEX.to_json, headers: {}) end it 'should return a leaderboard of entries' do leaderboard = strutta.games(VALID_GAME_01[:id]).entries.on_demand_winners results = leaderboard['results'] expect(results.is_a? Array).to be true end end context '.create' do context 'with valid parameters' do before do stub_request(:post, /.*entries/) .to_return(status: 201, body: ENTRY_01.to_json, headers: {}) end it 'should return a new entry hash' do entry = strutta.games(VALID_GAME_01[:id]).entries.create(participant: PARTICIPANT_01[:id]) expect(entry['id']).to_not be nil end end end end context 'when id is given' do context '.get' do before do stub_request(:get, /.*entries\/\d+/) .to_return(status: 200, body: ENTRY_01.to_json, headers: {}) end it 'should return entry hash' do e = strutta.games(VALID_GAME_01[:id]).entries(ENTRY_01[:id]).get expect(e['id']).to_not be nil end end context '.update' do before do stub_request(:patch, /.*entries\/\d+/) .to_return(status: 200, body: ENTRY_01_UPDATED.to_json, headers: {}) end it 'should return updated entry hash' do metadata = { age: 20 } updated = strutta.games(VALID_GAME_01[:id]).entries(ENTRY_01[:id]).update(metadata: metadata) expect(updated['metadata']['age']).to eq(20) end end context '.delete' do before do stub_request(:delete, /.*entries\/\d+/) .to_return(status: 204, body: '', headers: {}) end it 'should delete entry' do expect(strutta.games(VALID_GAME_01[:id]).entries(ENTRY_01[:id]).delete).to be true end end context '.transitions' do before do stub_request(:get, %r{.*entries\/\d+\/transitions}) .to_return(status: 200, body: ENTRY_TRANSITIONS.to_json, headers: {}) end it 'should get list of entry transitions' do body = strutta.games(VALID_GAME_01[:id]).entries(ENTRY_01[:id]).transitions expect(body['transitions'].is_a? Array).to be true expect(body['transitions'].first['from'].is_a? Integer).to be true end end context '.all' do it 'should throw error' do expect { strutta.games(VALID_GAME_01[:id]).entries(ENTRY_01[:id]).all }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end context '.leaderboard' do it 'should throw error' do expect { strutta.games(VALID_GAME_01[:id]).entries(ENTRY_01[:id]).leaderboard }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end context '.create' do it 'should throw error' do expect { strutta.games(VALID_GAME_01[:id]).entries(ENTRY_01[:id]).create(email: 'my@email.com') }.to raise_error(Strutta::Errors::ObjectIDNotAllowed) end end end end end