Sha256: aa6f46432dee6463c40c1563da432ce5c488cd560b9af26ac55f2a9b578ed48e

Contents?: true

Size: 1.74 KB

Versions: 8

Compression:

Stored size: 1.74 KB

Contents

require "spec_helper"
require "omnicontacts/middleware/oauth2"

describe OmniContacts::Middleware::OAuth2 do

  before(:all) do
    class OAuth2Middleware < OmniContacts::Middleware::OAuth2
      def authorization_url
        "http://www.example.com"
      end

      def redirect_path
        "/redirect_path"
      end

      def self.mock_session
        @mock_session ||= {}
      end

      def session
        OAuth2Middleware.mock_session
      end

      def fetch_access_token code
        ["access_token", "token_type", "token_refresh"]
      end

      def fetch_contacts_using_access_token token, token_type
        [{:name => "John Doe", :email => "john@example.com"}]
      end
    end
  end

  let(:app) {
    Rack::Builder.new do |b|
      b.use OAuth2Middleware, "client_id", "client_secret"
      b.run lambda { |env| [200, {"Content-Type" => "text/html"}, ["Hello World"]] }
    end.to_app
  }

  context "visiting the listening path" do
    it "should redirect to authorization site when visiting the listening path" do
      get "/contacts/oauth2middleware"
      last_response.should be_redirect
      last_response.headers['location'].should eq("http://www.example.com")
    end
  end

  context "visiting the callback url after authorization" do
    it "should fetch the contacts" do
      get '/redirect_path?code=ABC'
      last_response.should be_ok
      last_request.env["omnicontacts.contacts"].size.should be(1)
    end

    it "should redirect to failure page because user did not allow access to contacts list" do
      get '/redirect_path?error=not_authorized'
      last_response.should be_redirect
      last_response.headers["location"].should eq("/contacts/failure?error_message=not_authorized&importer=oauth2middleware")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
omnicontacts-0.3.4 spec/omnicontacts/middleware/oauth2_spec.rb
omnicontacts-0.3.3 spec/omnicontacts/middleware/oauth2_spec.rb
omnicontacts-0.3.2 spec/omnicontacts/middleware/oauth2_spec.rb
omnicontacts-0.3.1 spec/omnicontacts/middleware/oauth2_spec.rb
omnicontacts-0.3.0 spec/omnicontacts/middleware/oauth2_spec.rb
omnicontacts-0.2.5 spec/omnicontacts/middleware/oauth2_spec.rb
omnicontacts-0.2.4 spec/omnicontacts/middleware/oauth2_spec.rb
omnicontacts-0.2.3 spec/omnicontacts/middleware/oauth2_spec.rb