Created
June 7, 2010 11:58
-
-
Save h-lame/428589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
def stub_oauth! | |
TwitterAuth.stub!(:config).and_return({ | |
'strategy' => 'oauth', | |
'oauth_consumer_key' => 'testkey', | |
'oauth_consumer_secret' => 'testsecret' | |
}) | |
end | |
-- | |
require 'fake_web' | |
FakeWeb.allow_net_connect = false | |
FakeWeb.register_uri(:post, 'https://twitter.com:443/oauth/request_token', :body => 'oauth_token=faketoken&oauth_token_secret=faketokensecret') | |
FakeWeb.register_uri(:post, 'https://twitter.com:443/oauth/access_token', :body => 'oauth_token=fakeaccesstoken&oauth_token_secret=fakeaccesstokensecret') | |
FakeWeb.register_uri(:get, 'https://twitter.com:443/account/verify_credentials.json', :body => "{\"profile_image_url\":\"http:\\/\\/static.twitter.com\\/images\\/default_profile_normal.png\",\"description\":\"Saving the world for all Twitter kind.\",\"utc_offset\":null,\"favourites_count\":0,\"profile_sidebar_fill_color\":\"e0ff92\",\"screen_name\":\"twitterman\",\"statuses_count\":0,\"profile_background_tile\":false,\"profile_sidebar_border_color\":\"87bc44\",\"friends_count\":2,\"url\":null,\"name\":\"Twitter Man\",\"time_zone\":null,\"protected\":false,\"profile_background_image_url\":\"http:\\/\\/static.twitter.com\\/images\\/themes\\/theme1\\/bg.gif\",\"profile_background_color\":\"9ae4e8\",\"created_at\":\"Fri Feb 06 18:10:32 +0000 2009\",\"profile_text_color\":\"000000\",\"followers_count\":2,\"location\":null,\"id\":123,\"profile_link_color\":\"0000ff\"}") | |
-- | |
Factory.define(:twitter_oauth_user, :class => User) do |user| | |
user.twitter_id { User.count + 1 } | |
user.twitter_login { Factory.next(:login) } | |
user.access_token 'fakeaccesstoken' | |
user.access_secret 'fakeaccesstokensecret' | |
user.name 'Twitter Man' | |
user.description 'Saving the world for all Twitter kind.' | |
user.password "password" | |
user.password_confirmation "password" | |
user.email { Factory.next(:email) } | |
user.terms true | |
end | |
-- | |
require File.dirname(__FILE__) + '/../../spec_helper' | |
describe TwitterAuth::TwitterUser do | |
before do | |
stub_oauth! | |
end | |
it 'should allow capital letters in the username' do | |
Factory.build(:twitter_oauth_user, :twitter_login => 'TwitterMan').should have(:no).errors_on(:login) | |
end | |
describe '.new_from_twitter_hash' do | |
it 'should raise an argument error if the hash does not have a screen_name attribute' do | |
lambda{User.new_from_twitter_hash({'id' => '123'})}.should raise_error(ArgumentError, 'Invalid hash: must include screen_name.') | |
end | |
it 'should raise an argument error if the hash does not have an id attribute' do | |
lambda{User.new_from_twitter_hash({'screen_name' => 'abc123'})}.should raise_error(ArgumentError, 'Invalid hash: must include id.') | |
end | |
it 'should return a user' do | |
User.new_from_twitter_hash({'id' => '123', 'screen_name' => 'twitterman'}).should be_a(User) | |
end | |
it 'should assign login to the screen_name' do | |
User.new_from_twitter_hash({'id' => '123', 'screen_name' => 'twitterman'}).twitter_login.should == 'twitterman' | |
end | |
it 'should assign twitter attributes that are provided' do | |
u = User.new_from_twitter_hash({'id' => '4566', 'screen_name' => 'twitterman', 'name' => 'Twitter Man', 'description' => 'Saving the world for all Tweet kind.'}) | |
u.name.should == 'Twitter Man' | |
u.description.should == 'Saving the world for all Tweet kind.' | |
end | |
end | |
describe '#update_twitter_attributes' do | |
it 'should assign values to the user' do | |
user = Factory.create(:twitter_oauth_user, :name => "Dude", :description => "Awesome, man.") | |
user.update_twitter_attributes({'name' => 'Twitter Man', 'description' => 'Works.'}) | |
user.reload | |
user.name.should == 'Twitter Man' | |
user.description.should == 'Works.' | |
end | |
it 'should not throw an error with extraneous info' do | |
user = Factory.create(:twitter_oauth_user, :name => "Dude", :description => "Awesome, man.") | |
lambda{user.update_twitter_attributes({'name' => 'Twitter Man', 'description' => 'Works.', 'whoopsy' => 'noworks.'})}.should_not raise_error | |
end | |
end | |
describe '.identify_or_create_from_access_token' do | |
before do | |
@token = OAuth::AccessToken.new(TwitterAuth.consumer, 'faketoken', 'fakesecret') | |
end | |
it 'should accept an OAuth::AccessToken' do | |
lambda{ User.identify_or_create_from_access_token(@token) }.should_not raise_error(ArgumentError) | |
end | |
it 'should change the login when the screen_name changes' do | |
@user = Factory(:twitter_oauth_user, :twitter_id => '123') | |
User.stub!(:handle_response).and_return({'id' => 123, 'screen_name' => 'dude'}) | |
User.identify_or_create_from_access_token(@token).should == @user.reload | |
end | |
it 'should accept two strings' do | |
lambda{ User.identify_or_create_from_access_token('faketoken', 'fakesecret') }.should_not raise_error(ArgumentError) | |
end | |
it 'should not accept one string' do | |
lambda{ User.identify_or_create_from_access_token('faketoken') }.should raise_error(ArgumentError, 'Must authenticate with an OAuth::AccessToken or the string access token and secret.') | |
end | |
it 'should make a call to verify_credentials' do | |
# this is in the before, just making it explicit | |
User.identify_or_create_from_access_token(@token) | |
end | |
it 'should try to find the user with that id' do | |
User.should_receive(:find_by_twitter_id).once.with('123') | |
User.identify_or_create_from_access_token(@token) | |
end | |
it 'should return the user if he/she exists' do | |
user = Factory.create(:twitter_oauth_user, :twitter_id => '123', :twitter_login => 'twitterman') | |
user.reload | |
User.identify_or_create_from_access_token(@token).should == user | |
end | |
it 'should update the access_token and access_secret for the user if he/she exists' do | |
user = Factory.create(:twitter_oauth_user, :twitter_id => '123', :twitter_login => 'twitterman', :access_token => 'someothertoken', :access_secret => 'someothersecret') | |
User.identify_or_create_from_access_token(@token) | |
user.reload | |
user.access_token.should == @token.token | |
user.access_secret.should == @token.secret | |
end | |
it 'should update the user\'s attributes based on the twitter info' do | |
user = Factory.create(:twitter_oauth_user, :twitter_login => 'twitterman', :name => 'Not Twitter Man') | |
User.identify_or_create_from_access_token(@token).name.should == 'Twitter Man' | |
end | |
it 'should create a user if one does not exist' do | |
user = User.new | |
User.should_receive(:new).and_return(user) | |
user.should_receive(:save) | |
User.identify_or_create_from_access_token(@token) | |
end | |
it 'should assign the oauth access token and secret' do | |
user = User.identify_or_create_from_access_token(@token) | |
user.access_token.should == @token.token | |
user.access_secret.should == @token.secret | |
end | |
end | |
describe 'adding twitter auth to an existing user' do | |
before do | |
@user = Factory.create(:user) | |
@token = OAuth::AccessToken.new(TwitterAuth.consumer, 'faketoken', 'fakesecret') | |
end | |
it 'should accept an OAuth::AccessToken' do | |
lambda{ @user.update_twitter_auth(@token) }.should_not raise_error(ArgumentError) | |
end | |
it 'should accept two strings' do | |
lambda{ @user.update_twitter_auth('faketoken', 'fakesecret') }.should_not raise_error(ArgumentError) | |
end | |
it 'should not accept one string' do | |
lambda{ @user.update_twitter_auth('faketoken') }.should raise_error(ArgumentError, 'Must authenticate with an OAuth::AccessToken or the string access token and secret.') | |
end | |
it 'should make a call to verify_credentials' do | |
# this is in the before, just making it explicit | |
@user.update_twitter_auth(@token) | |
end | |
it 'should assign the oauth access token and secret' do | |
@user.update_twitter_auth(@token) | |
@user.access_token.should == @token.token | |
@user.access_secret.should == @token.secret | |
end | |
it 'should update the access_token and access_secret for the user if he/she exists' do | |
@user = Factory.create(:twitter_oauth_user, :twitter_id => '123', :twitter_login => 'twitterman', :access_token => 'someothertoken', :access_secret => 'someothersecret') | |
@user.update_twitter_auth(@token) | |
@user.access_token.should == @token.token | |
@user.access_secret.should == @token.secret | |
end | |
it 'should update the user\'s attributes based on the twitter info' do | |
@user = Factory.create(:twitter_oauth_user, :twitter_login => 'twitterman', :name => 'Not Twitter Man') | |
@user.update_twitter_auth(@token) | |
@user.name.should == 'Twitter Man' | |
end | |
end | |
it "shoudl be able to remove all twitter credentials" do | |
@user = Factory.create(:twitter_oauth_user) | |
@user.disconnect_twitter! | |
@user.twitter_id.should == nil | |
@user.twitter_login.should == nil | |
@user.access_token.should == nil | |
@user.access_secret.should == nil | |
@user.name.should == "Twitter Man" | |
@user.location.should == nil | |
@user.description.should == nil | |
@user.profile_image_url.should == nil | |
@user.url.should == nil | |
@user.protected.should == nil | |
@user.profile_background_color.should == nil | |
@user.profile_sidebar_fill_color.should == nil | |
@user.profile_link_color.should == nil | |
@user.profile_sidebar_border_color.should == nil | |
@user.profile_text_color.should == nil | |
@user.profile_background_image_url.should == nil | |
@user.profile_background_tiled.should == nil | |
@user.friends_count.should == nil | |
@user.statuses_count.should == nil | |
@user.followers_count.should == nil | |
@user.favourites_count.should == nil | |
@user.utc_offset.should == nil | |
@user.time_zone.should == nil | |
end | |
describe '#token' do | |
before do | |
@user = Factory.create(:twitter_oauth_user, :access_token => 'token', :access_secret => 'secret') | |
end | |
it 'should return an AccessToken' do | |
@user.token.should be_a(OAuth::AccessToken) | |
end | |
it "should use the user's access_token and secret" do | |
@user.token.token.should == @user.access_token | |
@user.token.secret.should == @user.access_secret | |
end | |
end | |
describe '#twitter' do | |
before do | |
@user = Factory.create(:twitter_oauth_user, :access_token => 'token', :access_secret => 'secret') | |
end | |
it 'should return a TwitterAuth::Dispatcher::Oauth' do | |
@user.twitter.should be_a(TwitterAuth::Dispatcher::Oauth) | |
end | |
it 'should use my token and secret' do | |
@user.twitter.token.should == @user.access_token | |
@user.twitter.secret.should == @user.access_secret | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment