spec/omniauth/strategies/square_spec.rb in omniauth-square-0.1.1 vs spec/omniauth/strategies/square_spec.rb in omniauth-square-0.9
- old
+ new
@@ -1,133 +1,105 @@
require 'spec_helper'
-require 'omniauth-square'
describe OmniAuth::Strategies::Square do
before :each do
- @request = double('Request')
+ @request = double('Request', :scheme => '', :url => '')
@request.stub(:params) { {} }
- @client_id = '123'
- @client_secret = 'afalsf'
- @raw_info = {
- 'name' => 'Sebastian Rabuini',
- 'email' => 'sebas@wasabit.com.ar',
- 'bio' => 'Sebas',
- 'blog_url' => 'sebas_blog',
- 'online_bio_url' => 'http://wasabitlabs.com',
- 'twitter_url' => 'http://twitter.com/#!/sebasr',
- 'facebook_url' => 'http://www.facebook.com/sebastian.rabuini',
- 'linkedin_url' => 'http://www.linkedin.com/in/srabuini',
- 'follower_count' => 6,
- 'investor' => false,
- 'locations' => [
- {'id' => 1963, 'tag_type' => 'LocationTag', 'name' => 'buenos aires',
- 'display_name' => 'Buenos Aires',
- 'angellist_url' => 'https://angel.co/buenos-aires'}
- ],
- 'roles' => [
- {'id' => 14726, 'tag_type' => 'RoleTag', 'name' => 'developer',
- 'display_name' => 'Developer',
- 'angellist_url' => 'https://angel.co/developer'},
- {'id' => 14725, 'tag_type' => 'RoleTag', 'name' => 'entrepreneur',
- 'display_name' => 'Entrepreneur',
- 'angellist_url' => 'https://angel.co/entrepreneur-1'}
- ],
- 'skills' => [
- {"id" => 82532, "tag_type" => "SkillTag", "name" => "ruby on rails",
- "display_name" => "Ruby on Rails",
- "angellist_url" => "https://angel.co/ruby-on-rails-1"}
- ],
- 'scopes' => ["email","comment","message","talent"],
- 'angellist_url' => 'https://angel.co/sebasr',
- 'image' => 'https://s3.amazonaws.com/photos.angel.co/users/90585-medium_jpg?1327684569'
- }
end
subject do
- args = [@client_id, @client_secret, @options].compact
- OmniAuth::Strategies::Square.new(nil, *args).tap do |strategy|
+ OmniAuth::Strategies::Square.new(nil, @options || {}).tap do |strategy|
strategy.stub(:request) { @request }
end
end
- it_should_behave_like 'an oauth2 strategy'
-
describe '#client' do
- it 'has correct AngelList site' do
- subject.client.site.should eq('https://angel.co/')
+ it 'has correct Square site' do
+ subject.client.site.should eq('https://squareup.com/')
end
it 'has correct authorize url' do
- subject.client.options[:authorize_url].should eq('https://angel.co/api/oauth/authorize')
+ subject.client.options[:authorize_url].should eq('/oauth2/authorize')
end
it 'has correct token url' do
- subject.client.options[:token_url].should eq('https://angel.co/api/oauth/token')
+ subject.client.options[:token_url].should eq('/oauth2/token')
end
end
describe '#info' do
before :each do
+ @raw_info = {
+ "id" => "JGHJ0343",
+ "name" => "Dave Davis",
+ "email" => "dave@example.com",
+ "country_code" => "US",
+ "language_code" => "en-US",
+ "business_name" => "Dave's Milkshakes",
+ "business_address" => {
+ "address_line_1" => "1455 Market St",
+ "locality" => "San Francisco",
+ "administrative_district_level_1" => "CA",
+ "postal_code" => "94103"
+ },
+ "business_phone" => {
+ "calling_code" => "+1",
+ "number" => "4155551234"
+ },
+ "business_type" => "restaurants"
+ }
+
subject.stub(:raw_info) { @raw_info }
end
context 'when data is present in raw info' do
- it 'returns the combined name' do
- subject.info['name'].should eq('Sebastian Rabuini')
+ it 'returns the name' do
+ subject.info[:name].should eq('Dave Davis')
end
- it 'returns the bio' do
- subject.info['bio'].should eq('Sebas')
+ it 'returns the email' do
+ subject.info[:email].should eq('dave@example.com')
end
- it 'returns the image' do
- subject.info['image'].should eq(@raw_info['image'])
+ it 'returns the phone number' do
+ subject.info[:phone].should eq('+14155551234')
end
- it "return the email" do
- subject.info['email'].should eq('sebas@wasabit.com.ar')
+ it 'returns the user location' do
+ subject.info[:location].should eq('San Francisco')
end
- it "return skills" do
- subject.info['skills'].first['name'].should eq("ruby on rails")
+ it 'sets the location blank if business_address isnt provided' do
+ @raw_info.delete('business_address')
+ subject.info[:location].should be_nil
end
- end
- end
- describe '#authorize_params' do
- before :each do
- subject.stub(:session => {})
+ it 'returns raw info' do
+ subject.extra[:raw_info]['business_name'].should eq("Dave's Milkshakes")
+ end
end
-
- it 'includes default scope for email' do
- subject.authorize_params['scope'].should eq('email')
- end
end
describe '#credentials' do
before :each do
@access_token = double('OAuth2::AccessToken')
- @access_token.stub(:token) { '123' } # Token is always required
+ @access_token.stub(:token)
@access_token.stub(:expires?)
@access_token.stub(:expires_at)
@access_token.stub(:refresh_token)
subject.stub(:access_token) { @access_token }
- subject.stub(:raw_info) { @raw_info }
end
it 'returns a Hash' do
subject.credentials.should be_a(Hash)
end
it 'returns the token' do
+ @access_token.stub(:token) { '123' }
subject.credentials['token'].should eq('123')
end
- it "return scopes" do
- subject.credentials['scope'].should eq("email comment message talent")
- end
-
it 'returns the expiry status' do
@access_token.stub(:expires?) { true }
subject.credentials['expires'].should eq(true)
@access_token.stub(:expires?) { false }
@@ -153,8 +125,38 @@
it 'does not return the refresh token when not expiring' do
@access_token.stub(:expires?) { false }
@access_token.stub(:refresh_token) { 'XXX' }
subject.credentials['refresh_token'].should be_nil
subject.credentials.should_not have_key('refresh_token')
+ end
+ end
+
+ describe '#build_access_token' do
+ it 'converts iso8601 expires_at to an integer' do
+ now = Time.now
+ subject.stub(:fetch_access_token).and_return({'expires_at' => now.iso8601})
+ token = subject.send(:build_access_token)
+ expect(token.expires_at).to eq(now.to_i)
+ end
+ end
+
+ describe '#access_token_request_payload' do
+ before do
+ @request.stub(:params).and_return('code' => '11111')
+ subject.stub(:callback_url).and_return('http://example.com')
+ end
+
+ let!(:payload) { subject.send(:access_token_request_payload) }
+
+ it 'sets the Content-Type header' do
+ expect(payload[:headers]['Content-Type']).to eq('application/x-www-form-urlencoded')
+ end
+
+ it 'sets the redirect_uri' do
+ expect(payload[:body][:redirect_uri]).to eq('http://example.com')
+ end
+
+ it 'sets the authorization code' do
+ expect(payload[:body][:code]).to eq('11111')
end
end
end