require 'spec_helper' describe Capgun::Client do before do @keys = Capgun::Config::VALID_OPTIONS_KEYS end context "invalid client" do specify "not allowed to get estimates without authorization" do stub_request(:post, "https://api.capgun.io/v1/orders/estimate.json"). with(:body => "{\"url\":\"http://example.com/test\"}", :headers => {'Accept'=>'application/json', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 401, :body => fixture("unauthorized.json"), :headers => {}) lambda { Capgun.estimate("http://example.com/test") }.should raise_error(Capgun::Error::Unauthorized, "Unauthorized") end specify "no urls are submitted for capture" do stub_request(:post, "https://api.capgun.io/v1/orders.json"). with(:body => "{\"url\":\"http://example.com/test\"}", :headers => {'Accept'=>'application/json', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 401, :body => fixture("unauthorized.json"), :headers => {}) lambda { Capgun.capture("http://example.com/test") }.should raise_error(Capgun::Error::Unauthorized, "Unauthorized") end specify "not allowed to view on order without authorization" do stub_request(:get, "https://api.capgun.io/v1/orders/4fd20a1288f560177600000a.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 401, :body => fixture("unauthorized.json"), :headers => {}) lambda { Capgun.order("4fd20a1288f560177600000a") }.should raise_error(Capgun::Error::Unauthorized, "Unauthorized") end specify "not allowed to view job status without authorization" do stub_request(:get, "https://api.capgun.io/v1/jobs/4fd20a1288f5601776000012.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 401, :body => fixture('unauthorized.json'), :headers => {}) lambda { Capgun.status("4fd20a1288f5601776000012") }.should raise_error(Capgun::Error::Unauthorized, "Unauthorized") end specify "not allowed to view account" do stub_request(:get, "https://api.capgun.io/v1/account.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 401, :body => fixture('unauthorized.json'), :headers => {}) lambda { Capgun.account }.should raise_error(Capgun::Error::Unauthorized, "Unauthorized") end end context "client with capture credits" do before do Capgun.configure do |config| config.auth_token = 'test' end end after do Capgun.reset end specify "get an estimate to capture a url" do stub_request(:post, "https://api.capgun.io/v1/orders/estimate.json"). with(:body => "{\"url\":\"http://example.com/test\"}", :headers => {'Accept'=>'application/json', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture("estimate.json"), :headers => {}) estimate = Capgun.estimate("http://example.com/test") estimate.cost.should == 1 end specify "estimate with options in a block" do stub_request(:post, "https://api.capgun.io/v1/orders/estimate.json"). with(:body => '{"viewport":"1024x2048","packages":["images","viewport"],"url":"http://example.com/test"}', :headers => {'Accept'=>'application/json', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture("estimate-with-options.json"), :headers => {}) estimate = Capgun.estimate("http://example.com/test", :viewport => "200x100") do |options| options.packages = ['images', 'viewport'] options.viewport = '1024x2048' end estimate.cost.should == 3 end specify "urls are submitted for capture" do stub_request(:post, "https://api.capgun.io/v1/orders.json"). with(:body => "{\"url\":\"http://example.com/test\"}", :headers => {'Accept'=>'application/json', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture("order.json"), :headers => {}) order = Capgun.capture("http://example.com/test") order.id.should == '4fd20a1288f560177600000a' order.cost.should == 1 end specify "capture with options can be in a block" do stub_request(:post, "https://api.capgun.io/v1/orders.json"). with(:body => '{"packages":["images","viewport"],"viewport":"1024x2048","url":"http://example.com/test"}', :headers => {'Accept'=>'application/json', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture("order-with-options.json"), :headers => {}) order = Capgun.capture("http://example.com/test", :packages => [ :images, :viewport ], :viewport => "1024x2048") order.id.should == '4fd20a1288f560177600000a' order.cost.should == 1 order.viewport.should == '1024x2048+0+0' order.packages.should == ['base', 'images', 'viewport'] end specify "capture with options can be in a block" do stub_request(:post, "https://api.capgun.io/v1/orders.json"). with(:body => '{"viewport":"1024x2048","packages":["images","viewport"],"url":"http://example.com/test"}', :headers => {'Accept'=>'application/json', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture("order-with-options.json"), :headers => {}) order = Capgun.capture("http://example.com/test", :viewport => "200x100") do |options| options.packages = ['images', 'viewport'] options.viewport = '1024x2048' end order.id.should == '4fd20a1288f560177600000a' order.cost.should == 1 order.viewport.should == '1024x2048+0+0' order.packages.should == ['base', 'images', 'viewport'] end specify "showing a capture order" do stub_request(:get, "https://api.capgun.io/v1/orders/4fd20a1288f560177600000a.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture("order.json"), :headers => {}) order = Capgun.order("4fd20a1288f560177600000a") order.id.should == '4fd20a1288f560177600000a' order.cost.should == 1 end specify "capture jobs have a status" do stub_request(:get, "https://api.capgun.io/v1/jobs/4fd20a1288f5601776000012.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture('job.json'), :headers => {}) job = Capgun.status("4fd20a1288f5601776000012") job.id.should == '4fd20a1288f5601776000012' job.state.should == 'initialized' end specify "account can be retrieved" do stub_request(:get, "https://api.capgun.io/v1/account.json"). with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'test', 'User-Agent'=>'Capgun.io Ruby Gem 0.1.1'}). to_return(:status => 200, :body => fixture('account.json'), :headers => {}) account = Capgun.account account.id.should == '4fd193db88f5600472000001' account.name.should == 'Acme Co.' account.balance.should == 200 end end context "with module configuration" do before do Capgun.configure do |config| @keys.each do |key| config.send("#{key}=", key) end end end after do Capgun.reset end specify "should inherit module configuration" do api = Capgun::Client.new @keys.each do |key| api.send(key).should == key end end context "with class configuration" do before do @configuration = { :adapter => :net_http, :connection_options => nil, :endpoint => 'http://example.com/', :gateway => nil, :auth_token => 'AT', :user_agent => 'Custom User Agent', :proxy => nil, } end context "during initialization" do it "should override module configuration" do api = Capgun::Client.new(@configuration) @keys.each do |key| api.send(key).should == @configuration[key] end end end context "after initilization" do it "should override module configuration after initialization" do api = Capgun::Client.new @configuration.each do |key, value| api.send("#{key}=", value) end @keys.each do |key| api.send(key).should == @configuration[key] end end end end end end