summaryrefslogtreecommitdiff
path: root/test/support
diff options
context:
space:
mode:
Diffstat (limited to 'test/support')
-rw-r--r--test/support/api_integration_test.rb26
-rw-r--r--test/support/assert_responses.rb30
-rw-r--r--test/support/auth_test_helper.rb9
-rw-r--r--test/support/browser_integration_test.rb1
4 files changed, 59 insertions, 7 deletions
diff --git a/test/support/api_integration_test.rb b/test/support/api_integration_test.rb
new file mode 100644
index 0000000..bd10f11
--- /dev/null
+++ b/test/support/api_integration_test.rb
@@ -0,0 +1,26 @@
+class ApiIntegrationTest < ActionDispatch::IntegrationTest
+
+ DUMMY_TOKEN = Token.new
+ RACK_ENV = {'HTTP_AUTHORIZATION' => %Q(Token token="#{DUMMY_TOKEN.to_s}")}
+
+ def login(user = nil)
+ @user ||= user ||= FactoryGirl.create(:user)
+ # DUMMY_TOKEN will be frozen. So let's use a dup
+ @token ||= DUMMY_TOKEN.dup
+ # make sure @token is up to date if it already exists
+ @token.reload if @token.persisted?
+ @token.user_id = @user.id
+ @token.last_seen_at = Time.now
+ @token.save
+ end
+
+ teardown do
+ if @user && @user.persisted?
+ Identity.destroy_all_for @user
+ @user.reload.destroy
+ end
+ if @token && @token.persisted?
+ @token.reload.destroy
+ end
+ end
+end
diff --git a/test/support/assert_responses.rb b/test/support/assert_responses.rb
index b01166f..19c2768 100644
--- a/test/support/assert_responses.rb
+++ b/test/support/assert_responses.rb
@@ -8,21 +8,27 @@ module AssertResponses
@response || last_response
end
- def assert_attachement_filename(name)
- assert_equal %Q(attachment; filename="#{name}"),
- get_response.headers["Content-Disposition"]
+ def content_type
+ get_response.content_type.to_s.split(';').first
end
def json_response
+ return nil unless content_type == 'application/json'
response = JSON.parse(get_response.body)
response.respond_to?(:with_indifferent_access) ?
response.with_indifferent_access :
response
end
+ def assert_text_response(body = nil)
+ assert_equal 'text/plain', content_type
+ unless body.nil?
+ assert_equal body, get_response.body
+ end
+ end
+
def assert_json_response(object)
- assert_equal 'application/json',
- get_response.content_type.to_s.split(';').first
+ assert_equal 'application/json', content_type
if object.is_a? Hash
object.stringify_keys! if object.respond_to? :stringify_keys!
assert_equal object, json_response
@@ -35,6 +41,20 @@ module AssertResponses
object.stringify_keys! if object.respond_to? :stringify_keys!
assert_json_response :errors => object
end
+
+ # checks for the presence of a key in a json response
+ # or a string in a text response
+ def assert_response_includes(string_or_key)
+ response = json_response || get_response.body
+ assert response.include?(string_or_key),
+ "response should have included #{string_or_key}"
+ end
+
+ def assert_attachement_filename(name)
+ assert_equal %Q(attachment; filename="#{name}"),
+ get_response.headers["Content-Disposition"]
+ end
+
end
class ::ActionController::TestCase
diff --git a/test/support/auth_test_helper.rb b/test/support/auth_test_helper.rb
index 57f9f9b..38c2ea1 100644
--- a/test/support/auth_test_helper.rb
+++ b/test/support/auth_test_helper.rb
@@ -19,6 +19,10 @@ module AuthTestHelper
return @current_user
end
+ def assert_login_required
+ assert_access_denied(true, false)
+ end
+
def assert_access_denied(denied = true, logged_in = true)
if denied
if @response.content_type == 'application/json'
@@ -46,8 +50,9 @@ module AuthTestHelper
protected
def header_for_token_auth
- @token = find_record(:token, :authenticate => @current_user)
- ActionController::HttpAuthentication::Token.encode_credentials @token.id
+ @token = stub_record(:token, :authenticate => @current_user)
+ Token.stubs(:find_by_token).with(@token.token).returns(@token)
+ ActionController::HttpAuthentication::Token.encode_credentials @token.token
end
def expect_warden_logout
diff --git a/test/support/browser_integration_test.rb b/test/support/browser_integration_test.rb
index 1c872ff..4fec59f 100644
--- a/test/support/browser_integration_test.rb
+++ b/test/support/browser_integration_test.rb
@@ -54,6 +54,7 @@ class BrowserIntegrationTest < ActionDispatch::IntegrationTest
end
# currently this only works for tests with poltergeist.
+ # ApiIntegrationTest has a working implementation for RackTest
def login(user = nil)
@user ||= user ||= FactoryGirl.create(:user)
token = Token.create user_id: user.id