Permalink
Browse files

Write tests

  • Loading branch information...
1 parent 3cdd742 commit 0ed2deb012aa1394bb8aa74016cac61ce6f6b18c Jesse Stuart committed Sep 29, 2012
Showing with 156 additions and 24 deletions.
  1. +14 −18 lib/omniauth/strategies/tent.rb
  2. +1 −0 omniauth-tent.gemspec
  3. +141 −6 spec/omniauth/strategies/tent_spec.rb
@@ -11,14 +11,10 @@ class Tent
option :get_app_id, lambda { |entity| }
option :on_app_created, lambda { |app| }
option :on_discovery, lambda { |profile| }
- option :app_name, ""
- option :app_icon, ""
- option :app_description, ""
- option :scopes, {}
+ option :app, { :name => nil, :icon => nil, :description => nil, :scopes => {}, :redirect_uris => nil }
option :profile_info_types, []
option :post_types, []
option :notification_url, ""
- option :redirect_uris, nil
def request_params
Hashie::Mash.new(request.params)
@@ -75,25 +71,25 @@ def lookup_app(app_id)
app = client.app.get(app_id).body
if app && !app.kind_of?(::String)
@tent_app = Hashie::Mash.new(app)
+ set_state(:app_id, @tent_app.id)
else
create_app
end
end
def create_app
- client = ::TentClient.new
+ client = ::TentClient.new(@server_url)
res = client.app.create(
- :name => options[:name],
- :description => options[:description],
- :scopes => options[:scopes],
- :icon => options[:icon],
- :notification_url => options[:notification_url],
- :redirect_uris => options[:redirect_uris] || [callback_url]
+ :name => options.app.name,
+ :description => options.app.description,
+ :scopes => options.app.scopes,
+ :icon => options.app.icon,
+ :redirect_uris => options.app.redirect_uris || [callback_url]
)
- if (app = res.body) && !app.body.kind_of?(::String)
- options[:on_app_created].call(res.body)
- @tent_app = Hashie::Mash.new(res.body)
+ if (app = res.body) && !app.kind_of?(::String)
+ @tent_app = Hashie::Mash.new(app)
+ options[:on_app_created].call(@tent_app)
set_state(:app_id, @tent_app.id)
else
fail!(:app_create_failure)
@@ -123,18 +119,18 @@ def build_uri_params!(uri, params)
end
def verify_state!
- fail!(:state_missmatch) unless get_state(:state) == request.params[:state]
+ fail!(:state_missmatch) unless get_state(:state) == request.params['state']
end
def create_app_authorization!
client = ::TentClient.new(get_state(:server_url))
- res = client.app.authorization.create(get_state(:app_id), :code => request.params[:code])
+ res = client.app.authorization.create(get_state(:app_id), :code => request.params['code'])
fail!(:app_creation_failure) if res.body.kind_of?(::String)
@app_authorization = Hashie::Mash.new(res.body)
end
def build_auth_hash!
- Hashie::Mash.new(
+ env['omniauth.auth_hash'] = Hashie::Mash.new(
:provider => 'tent',
:uid => get_state(:entity),
:info => extract_basic_info(get_state(:profile)),
View
@@ -27,4 +27,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rake'
gem.add_development_dependency 'kicker'
gem.add_development_dependency 'mocha'
+ gem.add_development_dependency 'yajl-ruby'
end
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'yajl'
describe OmniAuth::Strategies::Tent do
attr_accessor :app
@@ -8,7 +9,6 @@
def set_app!(tent_options = {})
old_app = self.app
self.app = Rack::Builder.app do
- use Rack::Session::Cookie
use OmniAuth::Strategies::Tent, tent_options
run lambda{|env| [404, {'env' => env}, ["HELLO!"]]}
end
@@ -23,28 +23,163 @@ def set_app!(tent_options = {})
set_app!
end
+ let(:env) { {'rack.session' => {}} }
+
let(:fresh_strategy){ Class.new(OmniAuth::Strategies::Tent) }
let(:tent_entity) { 'https://example.com' }
let(:tent_server) { "#{tent_entity}/tent" }
+ let(:app_id) { 'app-id-123' }
let(:link_header) { %(<#{tent_server}/profile>; rel="%s") % TentClient::PROFILE_REL }
let(:tent_profile) { %({"https://tent.io/types/info/core/v0.1.0":{"licenses":["http://creativecommons.org/licenses/by/3.0/"],"entity":"#{tent_entity}","servers":["#{tent_server}"]}}) }
+ let(:app_attrs) do
+ {
+ :name => "Example App",
+ :description => "An example app",
+ :scopes => { "read_posts" => "Display your posts feed" },
+ :icon => "https://example.com/icon.png",
+ }
+ end
+ let(:app_json) { %({"name":"Example App","id":"#{app_id}"}) }
+
+ let(:token_code) { 'token-code-123abc' }
+
+ let(:access_token) { 'access-token-abc' }
+ let(:mac_key) { 'mac-key-312' }
+ let(:mac_algorithm) { 'hmac-sha-256' }
+ let(:token_type) { 'mac' }
+ let(:app_auth_json) { %({"access_token":"#{access_token}","mac_key":"#{mac_key}","mac_algorithm":"#{mac_algorithm}","token_type":"#{token_type}") }
+
+ let(:stub_head_discovery!) do
+ stub_request(:head, tent_entity).to_return(:headers => {'Link' => link_header})
+ end
+
+ let(:stub_profile_discovery!) do
+ stub_request(:get, "#{tent_server}/profile").to_return(:body => tent_profile, :headers => {'Content-Type' => TentClient::MEDIA_TYPE})
+ end
+
+ let(:stub_app_lookup_success!) do
+ stub_request(:get, "#{tent_server}/apps/#{app_id}").to_return(:body => app_json, :headers => { 'Content-Type' => TentClient::MEDIA_TYPE })
+ end
+
+ let(:stub_app_lookup_failure!) do
+ stub_request(:get, "#{tent_server}/apps/#{app_id}").to_return(:status => 404)
+ end
+
+ let(:stub_app_create_success!) do
+ stub_request(:post, "#{tent_server}/apps").with(:body => Yajl::Encoder.encode(app_attrs.merge(:redirect_uris => ["http://example.org/auth/tent/callback"]))).to_return(:body => app_json, :headers => { 'Content-Type' => TentClient::MEDIA_TYPE })
+ end
+
+ let(:stub_app_auth_create_success!) do
+ stub_request(:post, "#{tent_server}/apps/#{app_id}/authorizations").with(:body => Yajl::Encoder.encode({ :code => token_code })).to_return(:body => app_auth_json, :headers => { 'Content-Type' => TentClient::MEDIA_TYPE })
+ end
describe '#request_phase' do
it 'should display a form' do
- get '/auth/tent'
+ get '/auth/tent', {}, env
expect(last_response.body).to be_include("<form")
end
it 'should perform disvocery' do
- head_stub = stub_request(:head, tent_entity).to_return(:headers => {'Link' => link_header})
- profile_stub = stub_request(:get, "#{tent_server}/profile").to_return(:body => tent_profile, :headers => {'Content-Type' => TentClient::MEDIA_TYPE})
+ head_stub = stub_head_discovery!
+ profile_stub = stub_profile_discovery!
+
+ described_class.any_instance.stubs(:find_or_create_app!)
+ described_class.any_instance.stubs(:build_uri_and_redirect!).returns([200, {}, []])
- post '/auth/tent', :entity => tent_entity
- expect(last_response.status).to eq(200)
+ post '/auth/tent', { :entity => tent_entity }, env
expect(head_stub).to have_been_requested
expect(profile_stub).to have_been_requested
end
+
+ it 'should find existing app' do
+ set_app!(:get_app_id => lambda { |entity| app_id })
+ stub_head_discovery!
+ stub_profile_discovery!
+ app_lookup_stub = stub_app_lookup_success!
+ described_class.any_instance.stubs(:build_uri_and_redirect!).returns([200, {}, []])
+
+ post '/auth/tent', { :entity => tent_entity }, env
+
+ expect(app_lookup_stub).to have_been_requested
+ end
+
+ it 'should create app if app_id callback returns nil' do
+ set_app!(:app => app_attrs)
+ stub_head_discovery!
+ stub_profile_discovery!
+ app_create_stub = stub_app_create_success!
+ described_class.any_instance.stubs(:build_uri_and_redirect!).returns([200, {}, []])
+
+ post '/auth/tent', { :entity => tent_entity }, env
+
+ expect(app_create_stub).to have_been_requested
+ end
+
+ it 'should create app if not found' do
+ set_app!(:app => app_attrs)
+ stub_head_discovery!
+ stub_profile_discovery!
+ stub_app_lookup_failure!
+ app_create_stub = stub_app_create_success!
+ described_class.any_instance.stubs(:build_uri_and_redirect!).returns([200, {}, []])
+
+ post '/auth/tent', { :entity => tent_entity }, env
+
+ expect(app_create_stub).to have_been_requested
+ end
+
+ it 'should build uri and redirect' do
+ set_app!(:get_app_id => lambda { |entity| app_id })
+ stub_head_discovery!
+ stub_profile_discovery!
+ stub_app_lookup_success!
+
+ post '/auth/tent', { :entity => tent_entity }, env
+
+ expect(last_response.status).to eq(302)
+ expect(last_response.headers["Location"]).
+ to match(%r{^#{tent_server}/oauth\?client_id=#{app_id}})
+ end
+ end
+
+ describe '#callback_phase' do
+ it 'should create app authorization' do
+ state = 'abcdef'
+ session = {}
+ session['omniauth.state'] = state
+ session['omniauth.entity'] = tent_entity
+ session['omniauth.server_url'] = tent_server
+ session['omniauth.app_id'] = app_id
+ session['omniauth.profile'] = Yajl::Parser.parse(tent_profile)
+
+ stub_app_auth_create_success!
+
+ get '/auth/tent/callback', { :code => token_code, :state => state }, 'rack.session' => session
+
+ auth_hash = last_response['env']['omniauth.auth_hash']
+ expect(auth_hash).to_not be_nil
+ expect(auth_hash.provider).to eq('tent')
+ expect(auth_hash.uid).to eq(tent_entity)
+ expect(auth_hash.info).to eq(Hashie::Mash.new(
+ :name => nil,
+ :nickname => tent_entity
+ ))
+ expect(auth_hash.credentials).to eq(Hashie::Mash.new(
+ :token => access_token,
+ :secret => mac_key
+ ))
+ expect(auth_hash.extra.raw_info.profile).to eq(Hashie::Mash.new(Yajl::Parser.parse(tent_profile)))
+ expect(auth_hash.extra.credentials).to eq(Hashie::Mash.new(
+ :mac_key_id => access_token,
+ :mac_key => mac_key,
+ :mac_algorithm => mac_algorithm,
+ :token_type => token_type
+ ))
+ expect(auth_hash.extra.raw_info.app_authorization).to eq(Hashie::Mash.new(
+ Yajl::Parser.parse(app_auth_json)
+ ))
+ end
end
end

0 comments on commit 0ed2deb

Please sign in to comment.