spec/shelly/client_spec.rb in shelly-0.0.40 vs spec/shelly/client_spec.rb in shelly-0.0.41.pre
- old
+ new
@@ -1,15 +1,16 @@
require "spec_helper"
describe Shelly::Client::APIError do
before do
- body = {"message" => "something went wrong", "errors" => [["first", "foo"]], "url" => "https://foo.bar"}
- @error = Shelly::Client::APIError.new(body.to_json, 404)
+ body = {"message" => "Couldn't find Cloud with code_name = fooo",
+ "errors" => [["first", "foo"]], "url" => "https://foo.bar"}
+ @error = Shelly::Client::APIError.new(404, body)
end
it "should return error message" do
- @error.message.should == "something went wrong"
+ @error.message.should == "Couldn't find Cloud with code_name = fooo"
end
it "should return array of errors" do
@error.errors.should == [["first", "foo"]]
end
@@ -17,29 +18,44 @@
it "should return url" do
@error.url.should == "https://foo.bar"
end
it "should return user friendly string" do
- @error.each_error{|error| error.should == "First foo"}
+ @error.each_error { |error| error.should == "First foo" }
end
-
+
+ describe "#resource_not_found?" do
+ context "on 404 response" do
+ it "should return which resource was not found" do
+ @error.resource_not_found.should == :cloud
+ end
+ end
+
+ context "on non 404 response" do
+ it "should return nil" do
+ error = Shelly::Client::APIError.new(401)
+ error.resource_not_found.should be_nil
+ end
+ end
+ end
+
describe "#not_found?" do
it "should return true if response status code is 404" do
@error.should be_not_found
end
-
+
it "should return false if response status code is not 404" do
- error = Shelly::Client::APIError.new({}.to_json, 500)
+ error = Shelly::Client::APIError.new(500)
error.should_not be_not_found
end
end
describe "#validation?" do
context "when error is caused by validation errors" do
it "should return true" do
body = {"message" => "Validation Failed"}
- error = Shelly::Client::APIError.new(body.to_json, 422)
+ error = Shelly::Client::APIError.new(422, body)
error.should be_validation
end
end
context "when error is not caused by validation errors" do
@@ -50,12 +66,11 @@
end
describe "#unauthorized?" do
context "when error is caused by unauthorized error" do
it "should return true" do
- body = {"message" => "Unauthorized"}
- error = Shelly::Client::APIError.new(body.to_json, 401)
+ error = Shelly::Client::APIError.new(401)
error.should be_unauthorized
end
end
context "when error is not caused by unauthorized" do
@@ -69,11 +84,11 @@
describe Shelly::Client do
before do
ENV['SHELLY_URL'] = nil
@client = Shelly::Client.new("bob@example.com", "secret")
end
-
+
def api_url(resource = "")
auth = "#{CGI.escape(@client.email)}:#{@client.password}@"
"https://#{auth}admin.winniecloud.com/apiv2/#{resource}"
end
@@ -140,10 +155,42 @@
response = @client.app_configs("staging-foo")
response.should == [{"created_by_user" => true, "path" => "config/app.yml"}]
end
end
+ describe "#app_config" do
+ it "should send get request" do
+ FakeWeb.register_uri(:get, api_url("apps/staging-foo/configs/path"), :body => [{:content => "content", :path => "path"}].to_json)
+ response = @client.app_config("staging-foo", "path")
+ response.should == [{"content" => "content", "path" => "path"}]
+ end
+ end
+
+ describe "#app_create_config" do
+ it "should send post request" do
+ FakeWeb.register_uri(:post, api_url("apps/staging-foo/configs"), :body => {}.to_json, :status => 201)
+ response = @client.app_create_config("staging-foo", "path", "content")
+ response.should == {}
+ end
+ end
+
+ describe "#app_update_config" do
+ it "should send put request" do
+ FakeWeb.register_uri(:put, api_url("apps/staging-foo/configs/path"), :body => {}.to_json)
+ response = @client.app_update_config("staging-foo", "path", "content")
+ response.should == {}
+ end
+ end
+
+ describe "#app_delete_config" do
+ it "should send delete request" do
+ FakeWeb.register_uri(:delete, api_url("apps/staging-foo/configs/path"), :body => {}.to_json)
+ response = @client.app_delete_config("staging-foo", "path")
+ response.should == {}
+ end
+ end
+
describe "#application_logs" do
it "should send get request" do
time = Time.now
FakeWeb.register_uri(:get, api_url("apps/staging-foo/logs"),
:body => {:logs => ["application_log_1", "application_log_2"]}.to_json)
@@ -165,16 +212,17 @@
{:email => "test2@example.com"}].to_json)
response = @client.app_users("staging-foo")
response.should == [{"email" => "test@example.com"}, {"email" => "test2@example.com"}]
end
end
-
- describe "#app_ips" do
- it "should send get request with app code_name" do
- FakeWeb.register_uri(:get, api_url("apps/staging-foo/ips"), :body => {:mail_server_ip => "10.0.1.1", :web_server_ip => "88.198.21.187"}.to_json)
- response = @client.app_ips("staging-foo")
- response.should == {"mail_server_ip" => "10.0.1.1", "web_server_ip" => "88.198.21.187"}
+
+ describe "#app" do
+ it "should fetch app from API" do
+ FakeWeb.register_uri(:get, api_url("apps/staging-foo"),
+ :body => {:web_server_ip => "192.0.2.1", :mail_server_ip => "192.0.2.3"}.to_json)
+ response = @client.app("staging-foo")
+ response.should == {"web_server_ip" => "192.0.2.1", "mail_server_ip" => "192.0.2.3"}
end
end
describe "#send_invitation" do
it "should send post with developer's email" do
@@ -219,47 +267,47 @@
it "should send get request with ssh key" do
@client.should_receive(:get).with("/users/new", {:ssh_key => "ssh-key Abb"})
@client.ssh_key_available?("ssh-key Abb")
end
end
-
+
describe "#database_backup" do
it "should fetch backup description from API" do
expected = {
"filename" => @filename,
"size" => 1234,
"human_size" => "2KB"
}
filename = "2011.11.26.04.00.10.foo.postgres.tar.gz"
url = api_url("apps/foo/database_backups/#{filename}")
FakeWeb.register_uri(:get, url, :body => expected.to_json)
-
+
@client.database_backup("foo", filename).should == expected
end
end
-
+
describe "#download_backup" do
before do
@filename = "2011.11.26.04.00.10.foo.postgres.tar.gz"
url = api_url("apps/foo/database_backups/#{@filename}")
response = Net::HTTPResponse.new('', '', '')
# Streaming
response.stub(:read_body).and_yield("aaa").and_yield("bbbbb").and_yield("dddf")
FakeWeb.register_uri(:get, url, :response => response)
end
-
+
it "should write streamed database backup to file" do
@client.download_backup("foo", @filename)
File.read(@filename).should == %w(aaa bbbbb dddf).join
end
-
+
it "should execute progress_callback with size of every chunk" do
progress = mock(:update => true)
progress.should_receive(:update).with(3)
progress.should_receive(:update).with(5)
progress.should_receive(:update).with(4)
-
+
callback = lambda { |size| progress.update(size) }
@client.download_backup("foo", @filename, callback)
end
end
@@ -328,9 +376,16 @@
lambda {
@client.post("/api/apps/flower/command", :body => "puts User.count")
}.should raise_error(Shelly::Client::APIError, "random error happened")
end
end
+ end
+
+ it "should return empty hash if response is not a valid JSON" do
+ JSON.should_receive(:parse).with("").and_raise(JSON::ParserError)
+ @response.stub(:code).and_return("204")
+ @response.stub(:body).and_return("")
+ @client.post("/api/apps/flower").should == {}
end
end
describe "#headers" do
it "should return hash of headers" do