Skip to content
Snippets Groups Projects
Commit 9df9b160 authored by Gabriel Mazetto's avatar Gabriel Mazetto
Browse files

refactor specs and codestyle

parent 6ba8a7c2
No related branches found
No related tags found
1 merge request!3Package Promotion
Pipeline #
Loading
Loading
@@ -2,3 +2,5 @@ Gemfile.lock
doc/
pkg/
vendor/cache/*.gem
.idea/
coverage/
Loading
Loading
@@ -42,5 +42,4 @@ describe Package do
pkg = Package.new(:file => "spec/fixtures/natty_dsc/jake_1.0-7.dsc", :source_files => nil)
expect(pkg.source_files).to be_empty
end
end
Loading
Loading
@@ -3,8 +3,8 @@ require 'webrick'
 
class PackagecloudServer < WEBrick::HTTPServlet::AbstractServlet
DISTRIBUTIONS = File.read("spec/fixtures/distros.json")
PACKAGE_CONTENTS = "{\"files\":[{\"filename\":\"jake_1.0.orig.tar.bz2\",\"size\":1108,\"md5sum\":\"a7a309b55424198ee98abcb8092d7be0\"},{\"filename\":\"jake_1.0-7.debian.tar.gz\",\"size\":1571,\"md5sum\":\"0fa5395e95ddf846b419e96575ce8044\"}]}"
GEM_VERSION = "{\"major\":\"0\",\"minor\":\"2\",\"patch\":\"1\"}"
PACKAGE_CONTENTS = '{"files":[{"filename":"jake_1.0.orig.tar.bz2","size":1108,"md5sum":"a7a309b55424198ee98abcb8092d7be0"},{"filename":"jake_1.0-7.debian.tar.gz","size":1571,"md5sum":"0fa5395e95ddf846b419e96575ce8044"}]}'
GEM_VERSION = '{"major":"0","minor":"2","patch":"1"}'
REPO = '{"name": "test_repo","created_at": "2014-08-30T03:51:37.000Z","url": "https://packagecloud.io/joedamato/test_repo","last_push_human": "about 2 months ago","package_count_human": "4 packages","private": true,"fqname": "joedamato/test_repo"}'
REPOS = "[#{REPO}]"
 
Loading
Loading
@@ -48,10 +48,12 @@ class PackagecloudServer < WEBrick::HTTPServlet::AbstractServlet
 
$request, $response = request, response
end
def route(request, response)
if request["Authorization"] && request["Authorization"] != "Basic dGVzdF90b2tlbjo="
return forbidden_response(request, response)
end
path = request.path
case path
when "/api/v1/distributions.json"
Loading
Loading
@@ -83,5 +85,4 @@ class PackagecloudServer < WEBrick::HTTPServlet::AbstractServlet
def do_POST(request, response)
route(request, response)
end
end
Loading
Loading
@@ -18,7 +18,6 @@ until @server.status == "sleep" do
end
 
describe Packagecloud do
include Packagecloud
 
before(:all) do
Loading
Loading
@@ -28,236 +27,246 @@ describe Packagecloud do
@client = Client.new(credentials, "test_client", connection)
end
 
let(:credentials) { Credentials.new("joedamato", "test_token") }
let(:connection) { connection = Connection.new("http", "localhost", 8000) }
it "should have a VERSION constant" do
expect(subject.const_get('VERSION')).to_not be_empty
end
 
it "GET api/v1/distributions.json" do
distros = @client.distributions
expect(distros.succeeded).to be_truthy
context "root level requests" do
it "GET api/v1/distributions.json" do
distros = @client.distributions
expect(distros.succeeded).to be_truthy
 
deb = distros.response["deb"]
dsc = distros.response["dsc"]
rpm = distros.response["rpm"]
deb = distros.response["deb"]
dsc = distros.response["dsc"]
rpm = distros.response["rpm"]
 
expect(deb).not_to be_empty
expect(dsc).not_to be_empty
expect(rpm).not_to be_empty
end
expect(deb).not_to be_empty
expect(dsc).not_to be_empty
expect(rpm).not_to be_empty
end
 
it "POST debian package /api/v1/repos/joedamato/test_repo/packages.json" do
path = "spec/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb"
size = File.size(path)
package = Package.new(:file => path)
it "GET /api/v1/gem_version.json" do
result = @client.gem_version
 
result = @client.put_package("test_repo", package, 22)
expect(result.succeeded).to be_truthy
expect(result.succeeded).to be_truthy
expect(result.response["major"]).to eq("0")
expect(result.response["minor"]).to eq("2")
expect(result.response["patch"]).to eq("1")
end
 
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
it "GET /api/v1/repos.json" do
result = @client.repositories
 
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
expect(result.succeeded).to be_truthy
expect(result.response[0]["private"]).to be_truthy
expect(result.response[0]["fqname"]).to eq("joedamato/test_repo")
expect(result.response[0]["name"]).to eq("test_repo")
end
 
it "POST debian package /api/v1/repos/joedamato/test_repo/packages.json twice" do
path = "spec/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb"
it "POST /api/v1/repos.json" do
body = MultiJson.dump({ "repository" => { "name" => "my_repo", "private" => "0" } })
result = @client.create_repository("my_repo")
 
package = Package.new(:file => path)
expect(result.succeeded).to be_truthy
expect($request.content_type).to include("json")
expect($request.content_length).to eq(body.size)
end
 
@client.put_package("test_repo", package, 22)
result = @client.put_package("test_repo", package, 21)
expect(result.succeeded).to be_truthy
describe "find_distribution_id" do
it "should find ubuntu/breezy" do
id = @client.find_distribution_id("ubuntu/breezy")
expect(id).to be(3)
end
it "should find just breezy" do
id = @client.find_distribution_id("breezy")
expect(id).to be(3)
end
it "should find python" do
id = @client.find_distribution_id("python")
expect(id).to be(166)
end
it "should return nil for invalid distro" do
id = @client.find_distribution_id("ubuasdalsdntu/breezy")
expect(id).to be_nil
end
it "should raise for amiguous distro queries" do
expect {
@client.find_distribution_id("ubuntu")
}.to raise_error
end
end
end
 
context "repository level requests" do
it "POST debian package /api/v1/repos/joedamato/test_repo/packages.json" do
path = "spec/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb"
size = File.size(path)
package = Package.new(:file => path)
 
it "POST gem package /api/v1/repos/joedamato/test_repo/packages.json" do
path = "spec/fixtures/chewbacca-1.0.0.gem"
size = File.size(path)
package = Package.new(:file => path)
result = @client.put_package("test_repo", package, 22)
expect(result.succeeded).to be_truthy
 
result = @client.put_package("test_repo", package)
expect(result.succeeded).to be_truthy
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
 
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
 
it "POST debian package /api/v1/repos/joedamato/test_repo/packages.json with string distro_version_id" do
allow(@client).to receive(:find_distribution_id).and_return(3)
path = "spec/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb"
size = File.size(path)
package = Package.new(:file => path)
it "POST debian package /api/v1/repos/joedamato/test_repo/packages.json twice" do
path = "spec/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb"
package = Package.new(:file => path)
 
result = @client.put_package("test_repo", package, "ubuntu/breezy")
@client.put_package("test_repo", package, 22)
result = @client.put_package("test_repo", package, 21)
 
expect(@client).to have_received(:find_distribution_id)
expect(result.succeeded).to be_truthy
expect(result.succeeded).to be_truthy
end
 
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
it "POST gem package /api/v1/repos/joedamato/test_repo/packages.json" do
path = "spec/fixtures/chewbacca-1.0.0.gem"
size = File.size(path)
package = Package.new(:file => path)
 
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
result = @client.put_package("test_repo", package)
expect(result.succeeded).to be_truthy
 
it "POST source package /api/v1/repos/joedamato/test_repo/packages.json" do
dsc = File.dirname(__FILE__) + '/fixtures/natty_dsc/jake_1.0-7.dsc'
jake_orig = File.dirname(__FILE__) + '/fixtures/natty_dsc/jake_1.0.orig.tar.bz2'
jake_debian = File.dirname(__FILE__) + '/fixtures/natty_dsc/jake_1.0-7.debian.tar.gz'
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
 
source_packages = { "jake_1.0.orig.tar.bz2" => open(jake_orig),
"jake_1.0-7.debian.tar.gz" => open(jake_debian),}
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
 
dsc_size = File.size(dsc)
jake_orig_size = File.size(jake_orig)
jake_debian_size = File.size(jake_debian)
combined_size = dsc_size + jake_orig_size + jake_debian_size
it "POST debian package /api/v1/repos/joedamato/test_repo/packages.json with string distro_version_id" do
allow(@client).to receive(:find_distribution_id).and_return(3)
path = "spec/fixtures/libampsharp2.0-cil_2.0.4-1_all.deb"
size = File.size(path)
package = Package.new(:file => path)
 
package = Package.new(:file => dsc, :source_files => source_packages)
result = @client.put_package("test_repo", package, "ubuntu/breezy")
 
result = @client.put_package("test_repo", package, 22)
expect(result.succeeded).to be_truthy
expect(@client).to have_received(:find_distribution_id)
expect(result.succeeded).to be_truthy
 
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
 
# assert body is at least bigger than fixture file
expect($request.content_length > combined_size).to be_truthy
end
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
 
it "POST /api/v1/repos/joedamato/test_repo/packages/contents.json" do
dsc = 'spec/fixtures/natty_dsc/jake_1.0-7.dsc'
size = File.size(dsc)
package = Package.new(:file => dsc)
it "POST source package /api/v1/repos/joedamato/test_repo/packages.json" do
dsc = File.dirname(__FILE__) + '/fixtures/natty_dsc/jake_1.0-7.dsc'
jake_orig = File.dirname(__FILE__) + '/fixtures/natty_dsc/jake_1.0.orig.tar.bz2'
jake_debian = File.dirname(__FILE__) + '/fixtures/natty_dsc/jake_1.0-7.debian.tar.gz'
 
result = @client.package_contents("test_repo", package, 99999)
expect(result.succeeded).to be_truthy
expect(result.response["files"]).not_to be_empty
source_packages = { "jake_1.0.orig.tar.bz2" => open(jake_orig),
"jake_1.0-7.debian.tar.gz" => open(jake_debian), }
 
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
dsc_size = File.size(dsc)
jake_orig_size = File.size(jake_orig)
jake_debian_size = File.size(jake_debian)
combined_size = dsc_size + jake_orig_size + jake_debian_size
 
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
package = Package.new(:file => dsc, :source_files => source_packages)
 
it "GET /api/v1/gem_version.json" do
result = @client.gem_version
expect(result.succeeded).to be_truthy
expect(result.response["major"]).to eq("0")
expect(result.response["minor"]).to eq("2")
expect(result.response["patch"]).to eq("1")
end
result = @client.put_package("test_repo", package, 22)
expect(result.succeeded).to be_truthy
 
it "GET /api/v1/repos.json" do
result = @client.repositories
expect(result.succeeded).to be_truthy
expect(result.response[0]["private"]).to be_truthy
expect(result.response[0]["fqname"]).to eq("joedamato/test_repo")
expect(result.response[0]["name"]).to eq("test_repo")
end
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
 
it "POST /api/v1/repos.json" do
body = MultiJson.dump({ "repository" => { "name" => "my_repo", "private" => "0" } })
result = @client.create_repository("my_repo")
expect(result.succeeded).to be_truthy
expect($request.content_type).to include("json")
expect($request.content_length).to eq(body.size)
end
# assert body is at least bigger than fixture file
expect($request.content_length > combined_size).to be_truthy
end
 
it "GET /api/v1/repos/joedamato/test_repo.json" do
result = @client.repository("test_repo")
expect(result.succeeded).to be_truthy
expect(result.response["private"]).to be_truthy
expect(result.response["fqname"]).to eq("joedamato/test_repo")
expect(result.response["name"]).to eq("test_repo")
end
it "POST /api/v1/repos/joedamato/test_repo/packages/contents.json" do
dsc = 'spec/fixtures/natty_dsc/jake_1.0-7.dsc'
size = File.size(dsc)
package = Package.new(:file => dsc)
 
it "should raise if the repo name is invalid" do
expect do
result = @client.repository("hi/test_repo")
end.to raise_error(InvalidRepoNameException)
end
result = @client.package_contents("test_repo", package, 99999)
expect(result.succeeded).to be_truthy
expect(result.response["files"]).not_to be_empty
 
it "GET /api/v1/repos/joedamato/invalid_repo.json (invalid repo)" do
result = @client.repository("invalid_repo")
expect(result.succeeded).to be_falsey
end
#assert content type is set correctly
expect($request.content_type).to include("boundary=")
expect($request.content_type).to include("multipart/form-data")
 
it "should have a distinct User-Agent that includes the version" do
@client.distributions
expect($request["User-Agent"]).to eq("packagecloud-ruby #{Packagecloud::VERSION}/test_client")
end
# assert body is at least bigger than fixture file
expect($request.content_length > size).to be_truthy
end
 
it "should raise if the username has an @ in it" do
expect do
credentials = Credentials.new("hi@test.com", "test_token")
end.to raise_error(InvalidUsernameException)
end
it "GET /api/v1/repos/joedamato/test_repo.json" do
result = @client.repository("test_repo")
 
it "invalid credentials should raise unauthenticated exception" do
credentials = Credentials.new("joedamato", "test_tafdfasoken")
connection = Connection.new("http", "localhost", 8000)
expect {
Client.new(credentials, "test_client", connection)
}.to raise_error(UnauthenticatedException)
end
expect(result.succeeded).to be_truthy
expect(result.response["private"]).to be_truthy
expect(result.response["fqname"]).to eq("joedamato/test_repo")
expect(result.response["name"]).to eq("test_repo")
end
 
context "timeouts" do
it "should have defaults" do
credentials = Credentials.new("joedamato", "test_token")
connection = Connection.new("http", "localhost", 8000)
client = Client.new(credentials, "test_client", connection)
expect(client.connection.read_timeout).to eql(60)
expect(client.connection.write_timeout).to eql(180)
expect(client.connection.connect_timeout).to eql(60)
it "should raise if the repo name is invalid" do
expect do
result = @client.repository("hi/test_repo")
end.to raise_error(InvalidRepoNameException)
end
 
it "should respect passed values" do
credentials = Credentials.new("joedamato", "test_token")
timeouts = { :connect_timeout => 10, :read_timeout => 80, :write_timeout => 280 }
connection = Connection.new("http", "localhost", 8000, timeouts)
client = Client.new(credentials, "test_client", connection)
expect(client.connection.read_timeout).to eql(80)
expect(client.connection.write_timeout).to eql(280)
expect(client.connection.connect_timeout).to eql(10)
it "GET /api/v1/repos/joedamato/invalid_repo.json (invalid repo)" do
result = @client.repository("invalid_repo")
expect(result.succeeded).to be_falsey
end
end
 
describe "find_distribution_id" do
it "should find ubuntu/breezy" do
id = @client.find_distribution_id("ubuntu/breezy")
expect(id).to be(3)
context "client error handling and validations" do
it "should have a distinct User-Agent that includes the version" do
@client.distributions
expect($request["User-Agent"]).to eq("packagecloud-ruby #{Packagecloud::VERSION}/test_client")
end
 
it "should find just breezy" do
id = @client.find_distribution_id("breezy")
expect(id).to be(3)
it "should raise if the username has an @ in it" do
expect do
credentials = Credentials.new("hi@test.com", "test_token")
end.to raise_error(InvalidUsernameException)
end
 
it "should find python" do
id = @client.find_distribution_id("python")
expect(id).to be(166)
it "invalid credentials should raise unauthenticated exception" do
wrong_credentials = Credentials.new("joedamato", "test_tafdfasoken")
expect {
Client.new(wrong_credentials, "test_client", connection)
}.to raise_error(UnauthenticatedException)
end
 
it "should return nil for invalid distro" do
id = @client.find_distribution_id("ubuasdalsdntu/breezy")
expect(id).to be_nil
end
context "timeouts" do
it "should have defaults" do
client = Client.new(credentials, "test_client", connection)
 
it "should raise for amiguous distro queries" do
expect {
@client.find_distribution_id("ubuntu")
}.to raise_error
expect(client.connection.read_timeout).to eql(60)
expect(client.connection.write_timeout).to eql(180)
expect(client.connection.connect_timeout).to eql(60)
end
it "should respect passed values" do
timeouts = { :connect_timeout => 10, :read_timeout => 80, :write_timeout => 280 }
connection_with_timeout = Connection.new("http", "localhost", 8000, timeouts)
client = Client.new(credentials, "test_client", connection_with_timeout)
expect(client.connection.read_timeout).to eql(80)
expect(client.connection.write_timeout).to eql(280)
expect(client.connection.connect_timeout).to eql(10)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment