-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from lhm/test_unit
Test setup integration tests
- Loading branch information
Showing
13 changed files
with
213 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ config.yml | |
.bundle/ | ||
db/*.sqlite3 | ||
|
||
.rvmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
#example for mysql | ||
#database: api | ||
#adapter: mysql | ||
#host: localhost | ||
#username: db_user | ||
#password: db_pass | ||
#encoding: utf8 | ||
development: | ||
adapter: sqlite3 | ||
database: db/development.sqlite3 | ||
pool: 5 | ||
timeout: 5000 | ||
|
||
test: | ||
adapter: sqlite3 | ||
database: db/test.sqlite3 | ||
|
||
#example for sqlite3 | ||
#adapter: sqlite3 | ||
#database: db/development.sqlite3 | ||
#pool: 5 | ||
#timeout: 5000 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Api | ||
module Assertions | ||
|
||
def assert_status(expected, response=last_response) | ||
assert_equal expected, response.status, "Expected a #{expected} status, got #{response.status} instead" | ||
end | ||
|
||
def assert_body(json_string=last_response.body, &block) | ||
assert_json(json_string, &block) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class BaseTest < Test::Unit::TestCase | ||
|
||
context "GET '/'" do | ||
setup { get '/' } | ||
|
||
should "return an error" do | ||
assert_status 400 | ||
assert_body { |json| json.element "error", "Wrong url format." } | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class DistrictTest < Test::Unit::TestCase | ||
|
||
def setup | ||
super | ||
@source = '/district' | ||
api_user.permissions = create_permissions_for(District, :read) | ||
FactoryGirl.create(:district, :number => 1, :name => "Zentrum") | ||
FactoryGirl.create(:district, :number => 2, :name => "West") | ||
end | ||
|
||
test :get, '/districts' do | ||
assert_status 200 | ||
assert_equal 2, last_result["data"].size | ||
end | ||
|
||
test :get, '/districts/1' do | ||
assert_status 200 | ||
assert_json(last_response.body) do | ||
has "id", 1 | ||
has "number", 1 | ||
has "name", "Zentrum" | ||
has "created_at" | ||
has "updated_at" | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FactoryGirl.define do | ||
|
||
factory :user do | ||
email "[email protected]" | ||
#password_salt { Authlogic::Random.hex_token } | ||
#crypted_password { Authlogic::CryptoProviders::Sha512.encrypt("test1234" + password_salt) } | ||
single_access_token { Authlogic::Random.friendly_token } | ||
persistence_token { Authlogic::Random.hex_token } | ||
perishable_token { Authlogic::Random.friendly_token } | ||
password "test" | ||
password_confirmation "test" | ||
end | ||
|
||
factory :permission do | ||
# nothing | ||
end | ||
|
||
factory :district do | ||
sequence(:name) {|n| "district-#{n}"} | ||
sequence(:number) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# add /lib to loadpath | ||
$LOAD_PATH.unshift File.expand_path("../lib", File.dirname(__FILE__)) | ||
|
||
ENV['RACK_ENV'] = 'test' | ||
|
||
require 'api' | ||
require 'test/unit' | ||
require 'assert_json' | ||
require 'rack/test' | ||
require 'turn' | ||
require 'shoulda-context' | ||
require 'assertions' | ||
require 'factory_girl' | ||
require 'authlogic/test_case' | ||
require 'factories' | ||
require 'database_cleaner' | ||
|
||
|
||
DatabaseCleaner.strategy = :transaction | ||
DatabaseCleaner.clean_with(:truncation) | ||
|
||
|
||
class Test::Unit::TestCase | ||
include Rack::Test::Methods | ||
include AssertJson | ||
include Api::Assertions | ||
|
||
def setup | ||
DatabaseCleaner.start | ||
end | ||
|
||
def teardown | ||
DatabaseCleaner.clean | ||
end | ||
|
||
def app | ||
Sinatra::Application | ||
end | ||
|
||
# | ||
# some dslish test stuff | ||
# | ||
|
||
def self.test(verb, resource, &block) | ||
define_method :"test #{verb.to_s.upcase} to \'#{resource}\'" do | ||
send(verb, [@source, resource].join) | ||
instance_eval(&block) | ||
end | ||
|
||
end | ||
|
||
# | ||
# helper and utility functions for testing | ||
# | ||
|
||
# creates and returns permissions of the given access type for all columns of the given model | ||
def create_permissions_for(klass, access) | ||
_, source, table = klass.table_name.split("_") | ||
klass.column_names.map do |cname| | ||
FactoryGirl.create(:permission, :access => access, :source => source, :table => table, :column => cname) | ||
end | ||
end | ||
|
||
# shortcut to the parsed JSON body of the last_response | ||
def last_result | ||
JSON.parse(last_response.body) | ||
end | ||
|
||
# returns the current @user or creates one with default values via FactoryGirl | ||
def api_user | ||
@user ||= FactoryGirl.create(:user) | ||
end | ||
|
||
# returns the api_user's api_key | ||
def api_key | ||
api_user && api_user.single_access_token | ||
end | ||
|
||
# override rack-test's get method in order to add some default values | ||
def get(url, opts={}) | ||
opts[:api_key] ||= api_key | ||
super(url, opts) | ||
end | ||
|
||
end |