Skip to content
This repository
tree: 38e50bb77e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 73 lines (60 sloc) 2.634 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
require "spec_helper"
require "omnicontacts/middleware/oauth1"

describe OmniContacts::Middleware::OAuth1 do

  before(:all) do
    class OAuth1Middleware < OmniContacts::Middleware::OAuth1
      def self.mock_auth_token_resp
        @mock_auth_token_resp ||= Object.new
      end

      def fetch_authorization_token
        OAuth1Middleware.mock_auth_token_resp.body
      end

      def authorization_url auth_token
        "http://www.example.com"
      end

      def fetch_contacts_from_token_and_verifier oauth_token, ouath_token_secret, oauth_verifier
        [{:name => "John Doe", :email => "john@example.com"}]
      end

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

      def session
        OAuth1Middleware.mock_session
      end
    end
  end

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

  context "visiting the listening path" do
    it "should save the authorization token and redirect to the authorization url" do
      OAuth1Middleware.mock_auth_token_resp.should_receive(:body).and_return(["auth_token", "auth_token_secret"])
      get "/contacts/oauth1middleware"
      last_response.should be_redirect
      last_response.headers['location'].should eq("http://www.example.com")
    end

    it "should redirect to failure url if fetching the request token does not succeed" do
      OAuth1Middleware.mock_auth_token_resp.should_receive(:body).and_raise("Request failed")
      get "contacts/oauth1middleware"
      last_response.should be_redirect
      last_response.headers["location"].should eq("/contacts/failure?error_message=internal_error&importer=oauth1middleware")
    end
  end

  context "visiting the callback url after authorization" do
    it "should return the list of contacts" do
      OAuth1Middleware.mock_session.should_receive(:[]).and_return("oauth_token_secret")
      get "/contacts/oauth1middleware/callback?oauth_token=token&oauth_verifier=verifier"
      last_response.should be_ok
      last_request.env["omnicontacts.contacts"].size.should be(1)
    end

    it "should redirect to failure url if oauth_token_secret is not found in the session" do
      OAuth1Middleware.mock_session.should_receive(:[]).and_return(nil)
      get "/contacts/oauth1middleware/callback?oauth_token=token&oauth_verifier=verifier"
      last_response.should be_redirect
      last_response.headers["location"].should eq("/contacts/failure?error_message=not_authorized&importer=oauth1middleware")
    end
  end
end
Something went wrong with that request. Please try again.