aftership_agent_spec.rb 2.6KB

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