Nessuna descrizione http://j1x-huginn.herokuapp.com

aftership_agent_spec.rb 2.5KB

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