require './test/test_helper' require 'hover/client/midas' describe Hover::Client::Midas do before do @client = Hover::Client::Midas.new('access_id', 'secret_key', 'http://localhost:3000') end it "inits" do assert @client.is_a?(Hover::Client::Midas) end describe "user" do it "gets user info" do @client.expects(:json_get).with("me.json") @client.me end end describe "orders" do describe "index" do it "gets index" do @client.expects(:json_get).with("orders.json", {}) @client.orders end it "gets second page of index" do @client.expects(:json_get).with("orders.json", {page: 2}) @client.orders(page: 2) end end describe "show" do it "shows" do @client.expects(:json_get).with("orders/1.json", {}) @client.order(1) end end describe "create" do it "creates" do @client.expects(:json_post).with("orders.json", {}) @client.create_order end end describe "complete upload" do it "completes" do @client.expects(:patch).with("orders/1/archive_uploading_complete.json") @client.complete_order_upload(1) end end describe "complete work" do it "completes" do @client.expects(:put).with("orders/1/work_complete.json") @client.order_complete_work(1) end end end describe "images" do describe "index" do it "gets index" do @client.expects(:json_get).with("images.json", {}) @client.images end it "gets second page of index" do @client.expects(:json_get).with("images.json", {page: 2}) @client.images(page: 2) end it "gets images for order" do @client.expects(:json_get).with("images.json", {order_id: 1}) @client.images(order_id: 1) end end describe "show" do it "shows" do @client.expects(:json_get).with("images/1.json") @client.image(1) end end describe "download_image" do before do @client.expects(:get_redirect_location).with("images/1.jpg", {}).once.returns("http://example.com/usr/to/s3") end it "contains the correct contents" do chunk = "testing 1 2 3" uri_mock = mock uri_mock.expects(:read).once.returns(chunk) Kernel.expects(:open).once.returns(uri_mock) @client.download_image({"id" => 1}) do |file| file.read.must_equal(chunk) file.must_be_kind_of(File) File.exist?(file.path).must_equal(true) end end end describe "upload_image" do it "uploads" do file = File.new("/tmp/test", "w") image = { "id" => 1, "image" => {"upload_url" => "http://localhost/v1/images.json"} } file = mock file.expects(:size).returns(1).once request = mock request.expects(:body_stream=).with(file).once request.expects(:content_length=).with(1).once request.expects(:[]=).with('Content-Type', '') # otherwise the signature is invalid! Net::HTTP::Put.expects(:new) .with(URI.parse(image["image"]["upload_url"]).request_uri) .returns(request) .once Net::HTTP.any_instance.expects(:use_ssl=).with(true).once Net::HTTP.any_instance.expects(:request).with(request).once @client.upload_image(image, file) end end describe "upload images for order" do it "fails if there are too many images" do collection = [{"id" => 1}] file_name_list = %w(1.jpg 2.jpg 3.jpg 4.jpg) Dir.expects(:glob).once.returns(file_name_list) @client.expects(:json_get).with("images/upload_urls.json", order_id: 1).returns(collection) @client.expects(:upload_image).never Proc.new { @client.upload_images_for_order(1, "/tmp/testing_upload_images_for_order") }.must_raise RuntimeError end it "uploads images for order" do collection = (1 .. 5).inject([]) { |a, n| a << {"id" => n} } file_name_list = %w(1.jpg 2.jpg 3.jpg 4.jpg) Dir.expects(:glob).once.returns(file_name_list) @client.expects(:json_get).with("images/upload_urls.json", order_id: 1).returns(collection) @client.expects(:upload_image).at_most(file_name_list.size).at_least(file_name_list.size) @client.upload_images_for_order(1, "/tmp/testing_upload_images_for_order") end end describe "upload prometheus export" do it "uploads export" do path = File.join(File.dirname(__FILE__), '..', '..', 'files', 'archive.zip') Zip::Entry.any_instance.expects(:extract).twice @client.expects(:upload_images_for_order).once @client.upload_images_from_prometheus_export(1, path) end end describe "upload_image_urls" do it "requests urls" do @client.expects(:json_get).with("images/upload_urls.json", order_id: 1).once @client.image_upload_urls(1) end end end end