require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "Cloudhdr::Job" do before(:each) do @api_key = 'abc123' end describe ".create" do before(:each) do @url = "#{Cloudhdr.base_url}/api/v1/jobs" @params = {:api_key => @api_key,:input => "s3://bucket-name/file-name.jpg" } @params_as_json = Cloudhdr::Base.encode(@params, :json) @params_as_xml = Cloudhdr::Base.encode(@params, :xml) end it "should POST to the correct url and return a response" do Cloudhdr::HTTP.should_receive(:post).with(@url + ".json", @params_as_json, {}).and_return(Cloudhdr::Response.new) r = Cloudhdr::Job.create(@params) r.class.should == Cloudhdr::Response end it "should apply the global API key when JSON and no api_key is passed" do pending "Need to fix" Cloudhdr.api_key = 'asdfasdf' Cloudhdr::HTTP.should_receive(:post).with(@url + ".json",@params_as_json,{}) do |url, params, options| Cloudhdr::Base.decode(params)['api_key'].should == Cloudhdr.api_key end.and_return(Cloudhdr::Response.new) Cloudhdr::Job.create(:input => @params[:input]) Cloudhdr.api_key = nil end it "should apply the global API key when XML and no api_key is passed" do pending "Need to fix" Cloudhdr.api_key = 'asdfasdf' Cloudhdr::HTTP.should_receive(:post).with(@url + ".xml",@params_as_xml,{}) do |url, params, options| Cloudhdr::Base.decode(params, :xml)['api_request']['api_key'].should == Cloudhdr.api_key end.and_return(Cloudhdr::Response.new) Cloudhdr::Job.create({:api_request => {:input => @params[:input]}}, {:format => :xml}) Cloudhdr.api_key = nil end it "should apply the global API key when an XML string is passed and no api_key is passed" do pending "Need to fix" Cloudhdr.api_key = 'asdfasdf' Cloudhdr::HTTP.should_receive(:post).with(@url + ".json",@params_as_xml,{}) do |url, params, options| Cloudhdr::Base.decode(params, :xml)['api_request']['api_key'] == Cloudhdr.api_key end.and_return(Cloudhdr::Response.new) Cloudhdr::Job.create({:input => @params[:input]}.to_xml(:root => :api_request), {:format => :xml}) Cloudhdr.api_key = nil end end describe ".list" do before(:each) do @url = "#{Cloudhdr.base_url}/api/v1/jobs" end it "should GET the correct url and return a response" do Cloudhdr::HTTP.stub(:get).with(@url + ".json", {:params => {:api_key => @api_key, :page => 1, :per_page => 50, :state => nil}}).and_return(Cloudhdr::Response.new) Cloudhdr::Response.should == Cloudhdr::Job.list(:api_key => @api_key).class end it "should merge params well" do Cloudhdr::HTTP.stub(:get).with(@url + ".json", {:params => {:api_key => @api_key, :page => 1, :per_page => 50, :some => 'param', :state => nil}}).and_return(Cloudhdr::Response.new) Cloudhdr::Response.should == Cloudhdr::Job.list(:api_key => @api_key, :params => {:some => 'param'}).class end end describe ".details" do before(:each) do @job_id = 1 @url = "#{Cloudhdr.base_url}/api/v1/jobs/#{@job_id}" end it "should GET the correct url and return a response" do Cloudhdr::HTTP.stub(:get).with(@url + ".json", {:params => {:api_key => @api_key}}).and_return(Cloudhdr::Response.new) Cloudhdr::Response.should == Cloudhdr::Job.details(1, :api_key => @api_key).class end end describe ".resubmit" do before(:each) do @job_id = 1 @url = "#{Cloudhdr.base_url}/api/v1/jobs/#{@job_id}/resubmit" end it "should GET the correct url and return a response" do Cloudhdr::HTTP.stub(:get).with(@url + ".json", {:params => {:api_key => @api_key}}).and_return(Cloudhdr::Response.new) Cloudhdr::Response.should == Cloudhdr::Job.resubmit(1, :api_key => @api_key).class end end describe ".cancel" do before(:each) do @job_id = 1 @url = "#{Cloudhdr.base_url}/api/v1/jobs/#{@job_id}/cancel" end it "should GET the correct url and return a response" do Cloudhdr::HTTP.stub(:get).with(@url + ".json", {:params => {:api_key => @api_key}}).and_return(Cloudhdr::Response.new) Cloudhdr::Response.should == Cloudhdr::Job.cancel(1, :api_key => @api_key).class end end describe ".delete" do before(:each) do @job_id = 1 @url = "#{Cloudhdr.base_url}/api/v1/jobs/#{@job_id}" end it "should DELETE the correct url and return a response" do Cloudhdr::HTTP.stub(:delete).with(@url + ".json", {:params => {:api_key => @api_key}}).and_return(Cloudhdr::Response.new) Cloudhdr::Response.should == Cloudhdr::Job.delete(1, :api_key => @api_key).class end end end