require 'helper' class BaseTest < Test::Unit::TestCase context "HDCloudApi" do setup do HDCloudApi.any_instance.stubs(:hd_cloud_stores).returns({:source_id => 3, :destination_id => 4}) end should "accept source_id and destination_id as parameters" do hd = HDCloudApi.new('foo', 'bar', 1, 2) assert_equal 1, hd.source_id assert_equal 2, hd.destination_id end should "use the constant otherwise" do hd = HDCloudApi.new('foo', 'bar') assert_equal 3, hd.source_id assert_equal 4, hd.destination_id end should "login with key and pass" do HDCloudApi.expects(:basic_auth).with('foo', 'bar') HDCloudApi.new('foo', 'bar') end context "#create_job" do setup do @hd = HDCloudApi.new('foo', 'bar') @success = <<-END [{"job": {"source_filename": "beer-drinking-pig.mpg", "encoding_profile_id": nil, "current_step": "waiting_for_file", "resolution": nil, "status_url": "http://example.com/api/v1/jobs/1.json", "id": 1, "bitrate": nil, "current_status": "waiting for file", "current_progress": nil, "remote_id": "my-own-remote-id"}}] END FakeWeb.register_uri(:post, %r|http://foo:bar@hdcloud.com/api/v1/jobs|, :body => @success, :content_type => 'application/json') end should "post to HDCloud" do HDCloudApi.expects(:post) @hd.create_job('foo.mp4') end end context "#job_status" do setup do @hd = HDCloudApi.new('foo', 'bar') @success = <<-END {"job": {"source_filename": "beer-drinking-pig.mpg", "encoding_profile_id": nil, "current_step": "encoding", "resolution": nil, "status_url": "http://example.com/api/v1/jobs/1.json", "id": 1, "bitrate": nil, "current_status": "Encoding: Pass 1", "current_progress": 42, "remote_id": "my-own-remote-id"}} END FakeWeb.register_uri(:get, %r|http://foo:bar@hdcloud.com/api/v1/jobs/1|, :body => @success, :content_type => 'application/json') end should "submit a get request to the job's status url" do @hd.job_status('http://hdcloud.com/api/v1/jobs/1') end end context "#jobs" do setup do @hd = HDCloudApi.new('foo', 'bar') @success = <<-END [{"job": {"source_filename": "beer-drinking-pig.mpg", "encoding_profile_id": nil, "current_step": "encoding", "resolution": nil, "status_url": "http://example.com/api/v1/jobs/1.json", "id": 1, "bitrate": nil, "current_status": "Encoding: Pass 1", "current_progress": 42, "remote_id": "my-own-remote-id"}}] END FakeWeb.register_uri(:get, %r|http://foo:bar@hdcloud.com/api/v1/jobs|, :body => @success, :content_type => "application/json") end should "submit a get request to jobs" do assert_equal 1, @hd.jobs.first["job"]["id"] end end context "#current_jobs" do setup do @hd = HDCloudApi.new('foo', 'bar') @success = <<-END [{"job": {"source_filename": "beer-drinking-pig.mpg", "encoding_profile_id": nil, "current_step": "encoding", "resolution": nil, "status_url": "http://example.com/api/v1/jobs/1.json", "id": 1, "bitrate": nil, "current_status": "Encoding: Pass 1", "current_progress": 42, "remote_id": "my-own-remote-id"}}] END FakeWeb.register_uri(:get, %r|http://foo:bar@hdcloud.com/api/v1/jobs/current|, :body => @success, :content_type => 'application/json') end should "submit a get request to jobs/current" do assert_equal 1, @hd.current_jobs.first["job"]["id"] assert_equal 'encoding', @hd.current_jobs.first["job"]["current_step"] end end context "#completed_jobs" do setup do @hd = HDCloudApi.new('foo', 'bar') @success = <<-END [{"job": {"source_filename": "beer-drinking-pig.mpg", "encoding_profile_id": nil, "current_step": nil, "resolution": nil, "status_url": "http://example.com/api/v1/jobs/1.json", "id": 1, "complete?": true, "encoded_filename": "beer-drinking-pig_1_800_1920x1080.mp4", "bitrate": nil, "current_status": "completed", "current_progress": nil, "remote_id": "my-own-remote-id"}}] END FakeWeb.register_uri(:get, %r|http://foo:bar@hdcloud.com/api/v1/jobs/complete|, :body => @success, :content_type => 'application/json') end should "submit a get request to jobs/completed" do assert_equal 1, @hd.completed_jobs.first["job"]["id"] end end context "#failed_jobs" do setup do @hd = HDCloudApi.new('foo', 'bar') response = <<-END [{"job": {"source_filename": "beer-drinking-pig.mpg", "encoding_profile_id": nil, "current_step": "failed", "resolution": nil, "status_url": "http://example.com/api/v1/jobs/1.json", "failed?": true, "id": 1, "bitrate": nil, "current_status": "failed", "failure_code": 500, "failure_message": "An unexpected application error occurred. Please contact support.", "current_progress": nil, "remote_id": "my-own-remote-id"}}] END FakeWeb.register_uri(:get, %r|http://foo:bar@hdcloud.com/api/v1/jobs/failed|, :body => response, :content_type => 'application/json') end should "submit a get request to jobs/failed" do assert_equal 1, @hd.failed_jobs.first["job"]["id"] assert_equal 'failed', @hd.failed_jobs.first["job"]["current_status"] end end end end