diff --git a/Gemfile b/Gemfile index eb9ec209..6f25d30f 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ group :test do gem 'rspec', '~> 2.11' gem 'factory_girl', '~> 2.4.0' gem 'mocha', '~> 0.12' + gem 'database_cleaner', '~> 0.8.0' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index d06dfcac..b534ad43 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,6 +121,7 @@ GEM data_migrations (0.0.1) activerecord rake + database_cleaner (0.8.0) diff-lcs (1.1.3) erubis (2.7.0) eventmachine (1.0.0) @@ -234,6 +235,7 @@ PLATFORMS DEPENDENCIES bunny + database_cleaner (~> 0.8.0) factory_girl (~> 2.4.0) foreman gh! diff --git a/config.ru b/config.ru index a861a79e..1238730d 100644 --- a/config.ru +++ b/config.ru @@ -1,2 +1,6 @@ +# Make sure we set that before everything +ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] || ENV['ENV'] +ENV['RAILS_ENV'] = ENV['RACK_ENV'] + require 'travis/api/app' run Travis::Api::App.new diff --git a/lib/travis/api/app.rb b/lib/travis/api/app.rb index 87ce1d97..9a871ea9 100644 --- a/lib/travis/api/app.rb +++ b/lib/travis/api/app.rb @@ -1,7 +1,3 @@ -# Make sure we set that before everything -ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] || ENV['ENV'] -ENV['RAILS_ENV'] = ENV['RACK_ENV'] - require 'travis' require 'backports' require 'rack' diff --git a/spec/integration/v1/branches_spec.rb b/spec/integration/v1/branches_spec.rb new file mode 100644 index 00000000..8827a1f8 --- /dev/null +++ b/spec/integration/v1/branches_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe 'Branches' do + before { Scenario.default } + + let(:repo) { Repository.first } + + it 'GET /branches?repository_id=:repository_id' do + response = get "/branches?repository_id=#{repo.id}" + response.should deliver_json_for('branches/all', 'v1', { repository_id: repo.id }, type: 'branches') + end +end diff --git a/spec/integration/v1/builds_spec.rb b/spec/integration/v1/builds_spec.rb new file mode 100644 index 00000000..90b8af6e --- /dev/null +++ b/spec/integration/v1/builds_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe 'Builds' do + before { Scenario.default } + + let(:repo) { Repository.first } + let(:build) { repo.builds.first } + + describe 'collection' do + it 'GET /builds?repository_id=:repository_id' do + response = get "/builds?repository_id=#{repo.id}" + response.should deliver_json_for('builds/all', 'v1', repository_id: repo.id) + end + + xit 'GET /:owner_name/:name/builds' do + response = get '/svenfuchs/minimal/builds' + response.should deliver_json_for('builds/all', 'v1', repository_id: repo.id) + end + + xit 'GET /builds?repository_id=:repository_id (404)' do + response = get "/builds?repository_id=0" + response.should be_not_found + end + + it 'GET /:owner_name/:name/builds (404)' do + response = get '/rails/rails/builds' + response.should be_not_found + end + end + + describe 'item' do + it 'GET /builds/:id' do + response = get("/builds/#{build.id}") + response.should deliver_json_for('builds/one', 'v1', id: build.id) + end + + xit 'GET /:owner_name/:name/builds/:id' do + response = get "/svenfuchs/minimal/builds/#{build.id}" + response.should deliver_json_for('builds/one', 'v1', id: build.id) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bc8a121e..f5bc980f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,45 @@ ENV['RACK_ENV'] = ENV['RAILS_ENV'] = ENV['ENV'] = 'test' require 'rspec' -require 'travis/api/app' +require 'database_cleaner' require 'sinatra/test_helpers' require 'logger' require 'gh' require 'multi_json' +require 'travis/api/app' +require 'travis/testing/scenario' +require 'travis/testing/factories' + +RSpec::Matchers.define :deliver_json_for do |type, version, params, options = {}| + match do |response| + actual = parse(response.body) + expected = data(type, version, params, options) + + failure_message_for_should do + "expected\n\n#{actual}\n\nto equal\n\n#{expected}" + end + + actual == expected + end + + def data(type, version, params, options) + resource = service(type, params).run + Travis::Api.data(resource, options.merge(version: version)) + end + + def service(service, params) + "Travis::Services::#{service.camelize}".constantize.new(nil, params) + end + + def parse(body) + MultiJson.decode(body) + end +end + Travis.logger = Logger.new(StringIO.new) Travis::Api::App.setup -Backports.require_relative_dir 'support' - module TestHelpers include Sinatra::TestHelpers @@ -31,17 +59,24 @@ module TestHelpers end end -RSpec.configure do |config| - config.mock_framework = :mocha - config.expect_with :rspec, :stdlib - config.include TestHelpers +RSpec.configure do |c| + c.mock_framework = :mocha + c.expect_with :rspec, :stdlib + c.include TestHelpers - config.before :each do + c.before :suite do + DatabaseCleaner.strategy = :truncation + DatabaseCleaner.clean_with :truncation + end + + c.before :each do + DatabaseCleaner.start ::Redis.connect(url: Travis.config.redis.url).flushdb set_app Travis::Api::App.new end - config.after :each do + c.after :each do + DatabaseCleaner.clean custom_endpoints.each do |endpoint| endpoint.superclass.direct_subclasses.delete(endpoint) end diff --git a/spec/support/factories.rb b/spec/support/factories.rb deleted file mode 100644 index 1bec8835..00000000 --- a/spec/support/factories.rb +++ /dev/null @@ -1,99 +0,0 @@ -require 'factory_girl' - -FactoryGirl.define do - factory :build do - repository { Repository.first || Factory(:repository) } - association :request - association :commit - end - - factory :commit do - repository { Repository.first || Factory(:repository) } - commit '62aae5f70ceee39123ef' - branch 'master' - message 'the commit message' - committed_at '2011-11-11T11:11:11Z' - committer_name 'Sven Fuchs' - committer_email 'svenfuchs@artweb-design.de' - author_name 'Sven Fuchs' - author_email 'svenfuchs@artweb-design.de' - compare_url 'https://github.com/svenfuchs/minimal/compare/master...develop' - end - - factory :test, :class => 'Job::Test' do - repository { Repository.first || Factory(:repository) } - commit { Factory(:commit) } - source { Factory(:build) } - log { Factory(:log) } - queue "ruby" - end - - factory :log, :class => 'Artifact::Log' do - content '$ bundle install --pa' - end - - factory :request do - repository { Repository.first || Factory(:repository) } - association :commit - token 'the-token' - end - - factory :repository do - name 'minimal' - owner_name 'svenfuchs' - owner_email 'svenfuchs@artweb-design.de' - url { |r| "http://github.com/#{r.owner_name}/#{r.name}" } - last_duration 60 - created_at { |r| Time.utc(2011, 01, 30, 5, 25) } - updated_at { |r| r.created_at + 5.minutes } - end - - factory :minimal, :parent => :repository do - end - - factory :enginex, :class => Repository do - name 'enginex' - owner_name 'josevalim' - last_duration 30 - end - - factory :running_build, :parent => :build do - repository { Factory(:repository, :name => 'running_build') } - state 'started' - end - - factory :successful_build, :parent => :build do - repository { Factory(:repository, :name => 'successful_build', :last_build_result => 0) } - result 0 - state 'finished' - started_at { Time.now.utc } - finished_at { Time.now.utc } - end - - factory :broken_build, :parent => :build do - repository { Factory(:repository, :name => 'broken_build', :last_build_result => 1) } - result 1 - state 'finished' - started_at { Time.now.utc } - finished_at { Time.now.utc } - end - - factory :user do - name 'Sven Fuchs' - login 'svenfuchs' - email 'sven@fuchs.com' - tokens { [Token.new] } - end - - factory :worker do - name 'worker-1' - host 'ruby-1.workers.travis-ci.org' - state :working - last_seen_at { Time.now.utc } - end - - factory :ssl_key do - private_key "-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQDGed1uxl9szL0PVE/B6v9PDso+xRHs9e9YDB8Dm+QYFDyddud1\nn1134ZY39Dxg6zNhXDGKYilHP4E9boIuvgfSADN12eD1clogX46M4oBGgUAhtr5Q\nvGLn9TEW4IbeI+nDshMJLTLethCmB6Hwm5Ld9QnRVT6U/AztOTv9eJ/xKQIDAQAB\nAoGABQ3zcq/AnF+2bN6DzXdzmwrQYbrZEwTMXJyqaYgdzfMt/ACcMmWllrj6/1/L\n7dfvjgowBMstK/BVFUBsNk6GmmoCDHFAU+BgeyyqUxyeb63+0dIDwVYx9LHTL4dr\n9a8cVyeefqc3mqB13B9NUlS40Ij4kuK6EOGP3DZwC1FQVwECQQDtBQFqgRuNdfbV\naGIcXnuMnD4BGrnFHm0IBdLYsK4ULL85gFbhEew6DTYGYlGqX1dXbXYue8F18D8i\nzqL6HOBhAkEA1l6zvLdC2t3J9UnwpkwU0jSPX4BpHH7IkrCoGRggjwtbSxJFcCKB\nRrbPFDNAwchsa2/ldXSBrFg6Y7GlwF3lyQJAaJk+6LuVZzZZ+hAYzCA+Me15x479\n0Kn+v/2h8RL3n9ungD7NGIKKV4wg/WxCUgfFScX608S1udCObFP4xJwdwQJBALtl\nwEQqBGSmXCV0xM3rVoxH7En1TG3fm2E400pUoCnMKLugtlkHoPF7X91tzJ9aoQTu\npa2e8rkBy9FY++gFbZkCQAJ46lGEXZJqcACvLX0t/+RrvmqWMxCydLFG50kOnD8b\nVNILVyUn1lYasTs4aMYr6BRtVZoCxqV5/+rkMhb1eOM=\n-----END RSA PRIVATE KEY-----\n" - public_key "-----BEGIN RSA PUBLIC KEY-----\nMIGJAoGBAMZ53W7GX2zMvQ9UT8Hq/08Oyj7FEez171gMHwOb5BgUPJ1253WfXXfh\nljf0PGDrM2FcMYpiKUc/gT1ugi6+B9IAM3XZ4PVyWiBfjozigEaBQCG2vlC8Yuf1\nMRbght4j6cOyEwktMt62EKYHofCbkt31CdFVPpT8DO05O/14n/EpAgMBAAE=\n-----END RSA PUBLIC KEY-----\n" - end -end diff --git a/spec/app_spec.rb b/spec/unit/app_spec.rb similarity index 100% rename from spec/app_spec.rb rename to spec/unit/app_spec.rb diff --git a/spec/default_spec.rb b/spec/unit/default_spec.rb similarity index 100% rename from spec/default_spec.rb rename to spec/unit/default_spec.rb diff --git a/spec/endpoint/accounts_spec.rb b/spec/unit/endpoint/accounts_spec.rb similarity index 100% rename from spec/endpoint/accounts_spec.rb rename to spec/unit/endpoint/accounts_spec.rb diff --git a/spec/endpoint/artifacts_spec.rb b/spec/unit/endpoint/artifacts_spec.rb similarity index 100% rename from spec/endpoint/artifacts_spec.rb rename to spec/unit/endpoint/artifacts_spec.rb diff --git a/spec/endpoint/authorization_spec.rb b/spec/unit/endpoint/authorization_spec.rb similarity index 100% rename from spec/endpoint/authorization_spec.rb rename to spec/unit/endpoint/authorization_spec.rb diff --git a/spec/endpoint/branches_spec.rb b/spec/unit/endpoint/branches_spec.rb similarity index 100% rename from spec/endpoint/branches_spec.rb rename to spec/unit/endpoint/branches_spec.rb diff --git a/spec/endpoint/builds_spec.rb b/spec/unit/endpoint/builds_spec.rb similarity index 100% rename from spec/endpoint/builds_spec.rb rename to spec/unit/endpoint/builds_spec.rb diff --git a/spec/endpoint/documentation_spec.rb b/spec/unit/endpoint/documentation_spec.rb similarity index 100% rename from spec/endpoint/documentation_spec.rb rename to spec/unit/endpoint/documentation_spec.rb diff --git a/spec/endpoint/endpoints_spec.rb b/spec/unit/endpoint/endpoints_spec.rb similarity index 100% rename from spec/endpoint/endpoints_spec.rb rename to spec/unit/endpoint/endpoints_spec.rb diff --git a/spec/endpoint/hooks_spec.rb b/spec/unit/endpoint/hooks_spec.rb similarity index 100% rename from spec/endpoint/hooks_spec.rb rename to spec/unit/endpoint/hooks_spec.rb diff --git a/spec/endpoint/jobs_spec.rb b/spec/unit/endpoint/jobs_spec.rb similarity index 100% rename from spec/endpoint/jobs_spec.rb rename to spec/unit/endpoint/jobs_spec.rb diff --git a/spec/endpoint/repositories_spec.rb b/spec/unit/endpoint/repositories_spec.rb similarity index 100% rename from spec/endpoint/repositories_spec.rb rename to spec/unit/endpoint/repositories_spec.rb diff --git a/spec/endpoint/users_spec.rb b/spec/unit/endpoint/users_spec.rb similarity index 100% rename from spec/endpoint/users_spec.rb rename to spec/unit/endpoint/users_spec.rb diff --git a/spec/endpoint/workers_spec.rb b/spec/unit/endpoint/workers_spec.rb similarity index 100% rename from spec/endpoint/workers_spec.rb rename to spec/unit/endpoint/workers_spec.rb diff --git a/spec/endpoint_spec.rb b/spec/unit/endpoint_spec.rb similarity index 100% rename from spec/endpoint_spec.rb rename to spec/unit/endpoint_spec.rb diff --git a/spec/extensions/scoping_spec.rb b/spec/unit/extensions/scoping_spec.rb similarity index 100% rename from spec/extensions/scoping_spec.rb rename to spec/unit/extensions/scoping_spec.rb diff --git a/spec/extensions/smart_constants_spec.rb b/spec/unit/extensions/smart_constants_spec.rb similarity index 100% rename from spec/extensions/smart_constants_spec.rb rename to spec/unit/extensions/smart_constants_spec.rb diff --git a/spec/extensions/subclass_tracker_spec.rb b/spec/unit/extensions/subclass_tracker_spec.rb similarity index 100% rename from spec/extensions/subclass_tracker_spec.rb rename to spec/unit/extensions/subclass_tracker_spec.rb diff --git a/spec/helpers/json_renderer_spec.rb b/spec/unit/helpers/json_renderer_spec.rb similarity index 100% rename from spec/helpers/json_renderer_spec.rb rename to spec/unit/helpers/json_renderer_spec.rb diff --git a/spec/middleware/cors_spec.rb b/spec/unit/middleware/cors_spec.rb similarity index 100% rename from spec/middleware/cors_spec.rb rename to spec/unit/middleware/cors_spec.rb diff --git a/spec/middleware/logging_spec.rb b/spec/unit/middleware/logging_spec.rb similarity index 100% rename from spec/middleware/logging_spec.rb rename to spec/unit/middleware/logging_spec.rb diff --git a/spec/middleware/scope_check_spec.rb b/spec/unit/middleware/scope_check_spec.rb similarity index 100% rename from spec/middleware/scope_check_spec.rb rename to spec/unit/middleware/scope_check_spec.rb diff --git a/spec/middleware_spec.rb b/spec/unit/middleware_spec.rb similarity index 100% rename from spec/middleware_spec.rb rename to spec/unit/middleware_spec.rb