aftership_agent_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rails_helper'
  2. describe Agents::AftershipAgent do
  3. before do
  4. stub_request(:get, /trackings/).to_return(
  5. :body => File.read(Rails.root.join("spec/data_fixtures/aftership.json")),
  6. :status => 200,
  7. :headers => {"Content-Type" => "text/json"}
  8. )
  9. @opts = {
  10. "api_key" => '800deeaf-e285-9d62-bc90-j999c1973cc9',
  11. "path" => 'trackings'
  12. }
  13. @checker = Agents::AftershipAgent.new(:name => "tectonic", :options => @opts)
  14. @checker.user = users(:bob)
  15. @checker.save!
  16. end
  17. describe '#helpers' do
  18. it "should return the correct request header" do
  19. expect(@checker.send(:request_options)).to eq({:headers => {"aftership-api-key" => '800deeaf-e285-9d62-bc90-j999c1973cc9', "Content-Type"=>"application/json"}})
  20. end
  21. it "should generate the correct events url" do
  22. expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/trackings")
  23. end
  24. it "should generate the correct specific tracking url" do
  25. @checker.options['path'] = "trackings/usps/9361289878905919630610"
  26. expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/trackings/usps/9361289878905919630610")
  27. end
  28. it "should generate the correct last checkpoint url" do
  29. @checker.options['path'] = "last_checkpoint/usps/9361289878905919630610"
  30. expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/last_checkpoint/usps/9361289878905919630610")
  31. end
  32. end
  33. describe "#that checker should be valid" do
  34. it "should check that the aftership object is valid" do
  35. expect(@checker).to be_valid
  36. end
  37. it "should require credentials" do
  38. @checker.options['api_key'] = nil
  39. expect(@checker).not_to be_valid
  40. end
  41. end
  42. describe "path request must exist" do
  43. it "should check that validation added if path does not exist" do
  44. opts = @opts.tap { |o| o.delete('path') }
  45. @checker = Agents::AftershipAgent.new(:name => "tectonic", :options => opts)
  46. @checker.user = users(:bob)
  47. expect(@checker.save).to eq false
  48. expect(@checker.errors.full_messages.first).to eq("You need to specify a path request")
  49. end
  50. end
  51. describe '#check' do
  52. it "should check that initial run creates an event" do
  53. @checker.memory[:last_updated_at] = '2016-03-15T14:01:05+00:00'
  54. expect { @checker.check }.to change { Event.count }.by(1)
  55. end
  56. end
  57. end