Browse files

Added support for integration test

  • Loading branch information...
1 parent d526f68 commit 078faea749f7775fa91914dd4aec8f0d52e4e74b @Diego81 committed Jun 5, 2012
View
2 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- omnicontacts (0.1.7)
+ omnicontacts (0.2.0)
json
rack
View
3 Rakefile
@@ -1,6 +1,7 @@
require 'bundler'
-Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
+
+Bundler::GemHelper.install_tasks
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
task :test => :spec
View
13 lib/omnicontacts.rb
@@ -1,11 +1,16 @@
module OmniContacts
-
- VERSION = "0.1.7"
+
+ VERSION = "0.2.0"
autoload :Builder, "omnicontacts/builder"
autoload :Importer, "omnicontacts/importer"
+ autoload :IntegrationTest, "omnicontacts/integration_test"
class AuthorizationError < RuntimeError
end
-
-end
+
+ def self.integration_test
+ IntegrationTest.instance
+ end
+
+end
View
36 lib/omnicontacts/integration_test.rb
@@ -0,0 +1,36 @@
+require 'singleton'
+
+class IntegrationTest
+ include Singleton
+
+ attr_accessor :enabled
+
+ def initialize
+ enabled = false
+ clear_mocks
+ end
+
+ def clear_mocks
+ @mock = {}
+ end
+
+ def mock provider, mock
+ @mock[provider.to_sym] = mock
+ end
+
+ def mock_authorization_from_user provider
+ [302, {"location" => provider.redirect_path}, []]
+ end
+
+ def mock_fetch_contacts provider
+ result = @mock[provider.class_name.to_sym] || []
+ if result.is_a? Array
+ result
+ elsif result.is_a? Hash
+ [result]
+ else
+ raise result.to_s
+ end
+ end
+
+end
View
22 lib/omnicontacts/middleware/base_oauth.rb
@@ -17,16 +17,12 @@ def initialize app, options
@ssl_ca_file = options[:ssl_ca_file]
end
- private
-
def class_name
self.class.name.split('::').last.downcase
end
- public
-
# Rack callback. It handles three cases:
- # * user visit middleware entru point.
+ # * user visit middleware entry point.
# In this case request_authorization_from_user is called
# * user is redirected back to the application
# from the authorization site. In this case the list
@@ -47,16 +43,28 @@ def call env
end
private
+
+ def test_mode?
+ IntegrationTest.instance.enabled
+ end
def handle_initial_request
execute_and_rescue_exceptions do
- request_authorization_from_user
+ if test_mode?
+ IntegrationTest.instance.mock_authorization_from_user(self)
+ else
+ request_authorization_from_user
+ end
end
end
def handle_callback
execute_and_rescue_exceptions do
- @env["omnicontacts.contacts"] = fetch_contacts
+ @env["omnicontacts.contacts"] = if test_mode?
+ IntegrationTest.instance.mock_fetch_contacts(self)
+ else
+ fetch_contacts
+ end
@app.call(@env)
end
end
View
51 spec/omnicontacts/integration_test_spec.rb
@@ -0,0 +1,51 @@
+require "spec_helper"
+require "omnicontacts/integration_test"
+
+describe IntegrationTest do
+
+ context "mock_initial_request" do
+ it "should redirect to the provider's redirect_path" do
+ provider = mock
+ redirect_path = "/redirect_path"
+ provider.stub(:redirect_path => redirect_path)
+ IntegrationTest.instance.mock_authorization_from_user(provider)[1]["location"].should eq(redirect_path)
+ end
+ end
+
+ context "mock_callback" do
+
+ before(:each) {
+ @env = {}
+ @provider = self.mock
+ @provider.stub(:class_name => "test")
+ IntegrationTest.instance.clear_mocks
+ }
+
+ it "should return an empty contacts list" do
+ IntegrationTest.instance.mock_fetch_contacts(@provider).should be_empty
+ end
+
+ it "should return a configured list of contacts " do
+ contacts = [:name => 'John Doe', :email => 'john@doe.com']
+ IntegrationTest.instance.mock('test', contacts)
+ result = IntegrationTest.instance.mock_fetch_contacts(@provider)
+ result.size.should be(1)
+ result.first[:email].should eq(contacts.first[:email])
+ result.first[:name].should eq(contacts.first[:name])
+ end
+
+ it "should return a single element list of contacts " do
+ contact = {:name => 'John Doe', :email => 'john@doe.com'}
+ IntegrationTest.instance.mock('test', contact)
+ result = IntegrationTest.instance.mock_fetch_contacts(@provider)
+ result.size.should be(1)
+ result.first[:email].should eq(contact[:email])
+ result.first[:name].should eq(contact[:name])
+ end
+
+ it "should throw an exception" do
+ IntegrationTest.instance.mock('test', :some_error)
+ expect {IntegrationTest.instance.mock_fetch_contacts(@provider)}.should raise_error
+ end
+ end
+end
View
46 spec/omnicontacts/middleware/base_oauth_spec.rb
@@ -0,0 +1,46 @@
+require "spec_helper"
+require "omnicontacts/middleware/base_oauth"
+
+describe OmniContacts::Middleware::BaseOAuth do
+
+ before(:all) do
+ class TestProvider < OmniContacts::Middleware::BaseOAuth
+ def initialize app, consumer_key, consumer_secret, options = {}
+ super app, options
+ end
+
+ def redirect_path
+ "/contacts/testprovider/callback"
+ end
+ end
+ OmniContacts.integration_test.enabled = true
+ end
+
+ let(:app) {
+ Rack::Builder.new do |b|
+ b.use TestProvider, "consumer_id", "consumer_secret"
+ b.run lambda { |env| [200, {"Content-Type" => "text/html"}, ["Hello World"]] }
+ end.to_app
+ }
+
+ it "should return a preconfigured list of contacts" do
+ OmniContacts.integration_test.mock(:testprovider, :email => "user@example.com")
+ get "/contacts/testprovider"
+ get "/contacts/testprovider/callback"
+ last_request.env["omnicontacts.contacts"].first[:email].should eq("user@example.com")
+ end
+
+ it "should redurect to failure url" do
+ OmniContacts.integration_test.mock(:testprovider, "some_error" )
+ get "/contacts/testprovider"
+ get "/contacts/testprovider/callback"
+ last_response.should be_redirect
+ last_response.headers["location"].should eq("/contacts/failure?error_message=internal_error")
+ end
+
+ after(:all) do
+ OmniContacts.integration_test.enabled = false
+ OmniContacts.integration_test.clear_mocks
+ end
+
+end

0 comments on commit 078faea

Please sign in to comment.