stubhub_agent_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. require 'rails_helper'
  2. describe Agents::StubhubAgent do
  3. let(:name) { 'Agent Name' }
  4. let(:url) { 'https://www.stubhub.com/event/name-1-1-2014-12345' }
  5. let(:parsed_body) { JSON.parse(body)['response']['docs'][0] }
  6. let(:valid_params) { { 'url' => parsed_body['url'] } }
  7. let(:body) { File.read(Rails.root.join('spec/data_fixtures/stubhub_data.json')) }
  8. let(:stubhub_event_id) { 12345 }
  9. let(:response_payload) { {
  10. 'url' => url,
  11. 'name' => parsed_body['seo_description_en_US'],
  12. 'date' => parsed_body['event_date_local'],
  13. 'max_price' => parsed_body['maxPrice'],
  14. 'min_price' => parsed_body['minPrice'],
  15. 'total_postings' => parsed_body['totalPostings'],
  16. 'total_tickets' => parsed_body['totalTickets'],
  17. 'venue_name' => parsed_body['venue_name']
  18. } }
  19. before do
  20. stub_request(:get, "https://www.stubhub.com/listingCatalog/select/?q=%2B%20stubhubDocumentType:event%0D%0A%2B%20event_id:#{stubhub_event_id}%0D%0A&rows=10&start=0&wt=json").
  21. to_return(:status => 200, :body => body, :headers => {})
  22. @stubhub_agent = described_class.new(name: name, options: valid_params)
  23. @stubhub_agent.user = users(:jane)
  24. @stubhub_agent.save!
  25. end
  26. describe "#check" do
  27. it 'should create an event' do
  28. expect { @stubhub_agent.check }.to change { Event.count }.by(1)
  29. end
  30. it 'should properly parse the response' do
  31. event = @stubhub_agent.check
  32. expect(event.payload).to eq(response_payload)
  33. end
  34. end
  35. describe "validations" do
  36. before do
  37. expect(@stubhub_agent).to be_valid
  38. end
  39. it "should require a url" do
  40. @stubhub_agent.options['url'] = nil
  41. expect(@stubhub_agent).not_to be_valid
  42. end
  43. end
  44. describe "#working?" do
  45. it "checks if events have been received within the expected receive period" do
  46. expect(@stubhub_agent).not_to be_working
  47. Agents::StubhubAgent.async_check @stubhub_agent.id
  48. expect(@stubhub_agent.reload).to be_working
  49. two_days_from_now = 2.days.from_now
  50. allow(Time).to receive(:now) { two_days_from_now }
  51. expect(@stubhub_agent.reload).not_to be_working
  52. end
  53. end
  54. end