require 'spec_helper' require 'yajl' describe OmniAuth::Strategies::Tent do attr_accessor :app # customize rack app for testing, if block is given, reverts to default # rack app after testing is done def set_app!(tent_options = {}) old_app = self.app self.app = Rack::Builder.app do use OmniAuth::Strategies::Tent, tent_options run lambda{|env| [404, {'env' => env}, ["HELLO!"]]} end if block_given? yield self.app = old_app end self.app end before(:all) do 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", :url => "https://example.com" } end let(:app_json) { %({"name":"Example App","id":"#{app_id}"}) } let(:app_hash) { Yajl::Parser.parse(app_json) } 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").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', {}, env expect(last_response.body).to be_include("