mbleigh-twitter-auth 0.1.18 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ Change Log
2
+ ==========
3
+
4
+ == 0.1.19 - April 23, 2009
5
+
6
+ - Added this changelog to keep track of additions to TwitterAuth
7
+ - All authentication is now keyed via `id` instead of `screen_name`
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 18
4
+ :patch: 20
@@ -35,7 +35,8 @@ module TwitterAuth
35
35
  end
36
36
 
37
37
  def identify_or_create_from_twitter_hash_and_password(twitter_hash, password)
38
- if user = User.find_by_login(twitter_hash['screen_name'])
38
+ if user = User.find_by_twitter_id(twitter_hash['id'].to_s)
39
+ user.login = twitter_hash['screen_name']
39
40
  user.assign_twitter_attributes(twitter_hash)
40
41
  user.password = password
41
42
  user.save
@@ -1,6 +1,6 @@
1
1
  module TwitterAuth
2
2
  class GenericUser < ActiveRecord::Base
3
- attr_protected :login, :remember_token, :remember_token_expires_at
3
+ attr_protected :twitter_id, :remember_token, :remember_token_expires_at
4
4
 
5
5
  TWITTER_ATTRIBUTES = [
6
6
  :name,
@@ -24,10 +24,11 @@ module TwitterAuth
24
24
  :utc_offset
25
25
  ]
26
26
 
27
- validates_presence_of :login
27
+ validates_presence_of :login, :twitter_id
28
28
  validates_format_of :login, :with => /\A[a-z0-9_]+\z/i
29
29
  validates_length_of :login, :in => 1..15
30
30
  validates_uniqueness_of :login, :case_sensitive => false
31
+ validates_uniqueness_of :twitter_id, :message => "ID has already been taken."
31
32
  validates_uniqueness_of :remember_token, :allow_blank => true
32
33
 
33
34
  def self.table_name; 'users' end
@@ -35,7 +36,10 @@ module TwitterAuth
35
36
  def self.new_from_twitter_hash(hash)
36
37
  raise ArgumentError, 'Invalid hash: must include screen_name.' unless hash.key?('screen_name')
37
38
 
39
+ raise ArgumentError, 'Invalid hash: must include id.' unless hash.key?('id')
40
+
38
41
  user = User.new
42
+ user.twitter_id = hash['id'].to_s
39
43
  user.login = hash['screen_name']
40
44
 
41
45
  TWITTER_ATTRIBUTES.each do |att|
@@ -16,7 +16,8 @@ module TwitterAuth
16
16
  response = token.get(TwitterAuth.path_prefix + '/account/verify_credentials.json')
17
17
  user_info = handle_response(response)
18
18
 
19
- if user = User.find_by_login(user_info['screen_name'])
19
+ if user = User.find_by_twitter_id(user_info['id'].to_s)
20
+ user.login = user_info['screen_name']
20
21
  user.assign_twitter_attributes(user_info)
21
22
  user.access_token = token.token
22
23
  user.access_secret = token.secret
@@ -1,6 +1,7 @@
1
1
  class TwitterAuthMigration < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :users do |t|
4
+ t.string :twitter_id
4
5
  t.string :login
5
6
  <% if options[:oauth] -%>
6
7
  t.string :access_token
@@ -81,7 +81,7 @@ describe SessionsController do
81
81
 
82
82
  describe 'with proper info' do
83
83
  before do
84
- @user = Factory.create(:twitter_oauth_user)
84
+ @user = Factory.create(:twitter_oauth_user, :twitter_id => '123')
85
85
  @time = Time.now
86
86
  @remember_token = ActiveSupport::SecureRandom.hex(10)
87
87
 
@@ -177,7 +177,7 @@ describe SessionsController do
177
177
 
178
178
  describe '#create' do
179
179
  before do
180
- @user = Factory.create(:twitter_basic_user)
180
+ @user = Factory.create(:twitter_basic_user, :twitter_id => '123')
181
181
  end
182
182
 
183
183
  it 'should call logout_keeping_session! to remove session info' do
@@ -1,6 +1,7 @@
1
1
  require 'factory_girl'
2
2
 
3
3
  Factory.define(:twitter_oauth_user, :class => User) do |u|
4
+ u.twitter_id { User.count + 1 }
4
5
  u.login 'twitterman'
5
6
  u.access_token 'fakeaccesstoken'
6
7
  u.access_secret 'fakeaccesstokensecret'
@@ -10,6 +11,7 @@ Factory.define(:twitter_oauth_user, :class => User) do |u|
10
11
  end
11
12
 
12
13
  Factory.define(:twitter_basic_user, :class => User) do |u|
14
+ u.twitter_id { User.count + 1 }
13
15
  u.login 'twitterman'
14
16
  u.password 'test'
15
17
 
@@ -13,6 +13,6 @@ FakeWeb.register_uri(:post, 'https://twitter.com:443/oauth/request_token', :stri
13
13
 
14
14
  FakeWeb.register_uri(:post, 'https://twitter.com:443/oauth/access_token', :string => 'oauth_token=fakeaccesstoken&oauth_token_secret=fakeaccesstokensecret')
15
15
 
16
- FakeWeb.register_uri(:get, 'https://twitter.com:443/account/verify_credentials.json', :string => "{\"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\":20256865,\"profile_link_color\":\"0000ff\"}")
16
+ FakeWeb.register_uri(:get, 'https://twitter.com:443/account/verify_credentials.json', :string => "{\"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\"}")
17
17
 
18
18
  #FakeWeb.register_uri(:get, 'https://twitter.com:443/)
@@ -7,7 +7,7 @@ describe TwitterAuth::BasicUser do
7
7
 
8
8
  describe '#password=' do
9
9
  before do
10
- @user = Factory.build(:twitter_basic_user)
10
+ @user = Factory.build(:twitter_basic_user, :twitter_id => '123')
11
11
  end
12
12
 
13
13
  it 'should change the value of crypted_password' do
@@ -58,7 +58,7 @@ describe TwitterAuth::BasicUser do
58
58
 
59
59
  describe '.authenticate' do
60
60
  before do
61
- @user = Factory.create(:twitter_basic_user)
61
+ @user = Factory.create(:twitter_basic_user, :twitter_id => '123')
62
62
  end
63
63
 
64
64
  it 'should make a call to verify_credentials' do
@@ -72,34 +72,35 @@ describe TwitterAuth::BasicUser do
72
72
  end
73
73
 
74
74
  it 'should return the user if verify_credentials succeeds' do
75
- User.stub!(:verify_credentials).and_return(JSON.parse("{\"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\":20256865,\"profile_link_color\":\"0000ff\"}"))
75
+ User.stub!(:verify_credentials).and_return(JSON.parse("{\"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\"}"))
76
76
  User.authenticate('twitterman','test').should == @user
77
77
  end
78
78
  end
79
79
 
80
80
  describe '.find_or_create_by_twitter_hash_and_password' do
81
81
  before do
82
- @user = Factory.create(:twitter_basic_user)
82
+ @user = Factory.create(:twitter_basic_user, :twitter_id => '123')
83
83
  end
84
84
 
85
85
  it 'should return the existing user if there is one' do
86
- User.identify_or_create_from_twitter_hash_and_password({'screen_name' => 'twitterman'},'test').should == @user
86
+ User.identify_or_create_from_twitter_hash_and_password({'id' => '123', 'screen_name' => 'twitterman'},'test').should == @user
87
87
  end
88
88
 
89
89
  it 'should update the attributes from the hash' do
90
- User.identify_or_create_from_twitter_hash_and_password({'screen_name' => 'twitterman', 'name' => 'New Name'}, 'test').name.should == 'New Name'
90
+ User.identify_or_create_from_twitter_hash_and_password({'id' => 123, 'screen_name' => 'twitterman', 'name' => 'New Name'}, 'test').name.should == 'New Name'
91
91
  end
92
92
 
93
93
  it 'should update the password from the argument' do
94
- User.identify_or_create_from_twitter_hash_and_password({'screen_name' => 'twitterman', 'name' => 'New Name'}, 'test2').password.should == 'test2'
94
+ User.identify_or_create_from_twitter_hash_and_password({'id' => '123', 'screen_name' => 'twitterman', 'name' => 'New Name'}, 'test2').password.should == 'test2'
95
95
  end
96
96
 
97
97
  it 'should create a user if one does not exist' do
98
- lambda{User.identify_or_create_from_twitter_hash_and_password({'screen_name' => 'dude', 'name' => "Lebowski"}, 'test')}.should change(User, :count).by(1)
98
+ lambda{User.identify_or_create_from_twitter_hash_and_password({'id' => 124, 'screen_name' => 'dude', 'name' => "Lebowski"}, 'test')}.should change(User, :count).by(1)
99
99
  end
100
100
 
101
101
  it 'should assign the attributes from the hash to a created user' do
102
- user = User.identify_or_create_from_twitter_hash_and_password({'screen_name' => 'dude', 'name' => "Lebowski"}, 'test')
102
+ user = User.identify_or_create_from_twitter_hash_and_password({'id' => 124, 'screen_name' => 'dude', 'name' => "Lebowski"}, 'test')
103
+ user.twitter_id.should == '124'
103
104
  user.login.should == 'dude'
104
105
  user.name.should == 'Lebowski'
105
106
  user.password.should == 'test'
@@ -108,7 +109,7 @@ describe TwitterAuth::BasicUser do
108
109
 
109
110
  describe '#twitter' do
110
111
  before do
111
- @user = Factory.create(:twitter_basic_user)
112
+ @user = Factory.create(:twitter_basic_user, :twitter_id => '123')
112
113
  end
113
114
 
114
115
  it 'should be an instance of TwitterAuth::Dispatcher::Basic' do
@@ -119,4 +120,19 @@ describe TwitterAuth::BasicUser do
119
120
  @user.twitter.user.should == @user
120
121
  end
121
122
  end
123
+
124
+ describe 'changing usernames' do
125
+ before do
126
+ @user = Factory.create(:twitter_basic_user, :twitter_id => '123')
127
+ end
128
+
129
+ it 'should not create a new record when a screen_name has changed' do
130
+ lambda{User.identify_or_create_from_twitter_hash_and_password({'id' => '123', 'screen_name' => 'dude'},'awesome')}.should_not change(User,:count)
131
+ end
132
+
133
+ it 'should update the record with the new screen name' do
134
+ User.identify_or_create_from_twitter_hash_and_password({'id' => '123', 'screen_name' => 'dude'},'awesome').should == @user.reload
135
+ @user.login.should == 'dude'
136
+ end
137
+ end
122
138
  end
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe TwitterAuth::GenericUser do
4
- should_validate_presence_of :login
4
+ should_validate_presence_of :login, :twitter_id
5
5
  should_validate_format_of :login, 'some_guy', 'awesome', 'cool_man'
6
6
  should_not_validate_format_of :login, 'with-dashes', 'with.periods', 'with spaces'
7
7
  should_validate_length_of :login, :in => 1..15
@@ -27,19 +27,23 @@ describe TwitterAuth::GenericUser do
27
27
 
28
28
  describe '.new_from_twitter_hash' do
29
29
  it 'should raise an argument error if the hash does not have a screen_name attribute' do
30
- lambda{User.new_from_twitter_hash({})}.should raise_error(ArgumentError, 'Invalid hash: must include screen_name.')
30
+ lambda{User.new_from_twitter_hash({'id' => '123'})}.should raise_error(ArgumentError, 'Invalid hash: must include screen_name.')
31
+ end
32
+
33
+ it 'should raise an argument error if the hash does not have an id attribute' do
34
+ lambda{User.new_from_twitter_hash({'screen_name' => 'abc123'})}.should raise_error(ArgumentError, 'Invalid hash: must include id.')
31
35
  end
32
36
 
33
37
  it 'should return a user' do
34
- User.new_from_twitter_hash({'screen_name' => 'twitterman'}).should be_a(User)
38
+ User.new_from_twitter_hash({'id' => '123', 'screen_name' => 'twitterman'}).should be_a(User)
35
39
  end
36
40
 
37
41
  it 'should assign login to the screen_name' do
38
- User.new_from_twitter_hash({'screen_name' => 'twitterman'}).login.should == 'twitterman'
42
+ User.new_from_twitter_hash({'id' => '123', 'screen_name' => 'twitterman'}).login.should == 'twitterman'
39
43
  end
40
44
 
41
45
  it 'should assign twitter attributes that are provided' do
42
- u = User.new_from_twitter_hash({'screen_name' => 'twitterman', 'name' => 'Twitter Man', 'description' => 'Saving the world for all Tweet kind.'})
46
+ u = User.new_from_twitter_hash({'id' => '4566', 'screen_name' => 'twitterman', 'name' => 'Twitter Man', 'description' => 'Saving the world for all Tweet kind.'})
43
47
  u.name.should == 'Twitter Man'
44
48
  u.description.should == 'Saving the world for all Tweet kind.'
45
49
  end
@@ -14,6 +14,12 @@ describe TwitterAuth::OauthUser do
14
14
  lambda{ User.identify_or_create_from_access_token(@token) }.should_not raise_error(ArgumentError)
15
15
  end
16
16
 
17
+ it 'should change the login when the screen_name changes' do
18
+ @user = Factory(:twitter_oauth_user, :twitter_id => '123')
19
+ User.stub!(:handle_response).and_return({'id' => 123, 'screen_name' => 'dude'})
20
+ User.identify_or_create_from_access_token(@token).should == @user.reload
21
+ end
22
+
17
23
  it 'should accept two strings' do
18
24
  lambda{ User.identify_or_create_from_access_token('faketoken', 'fakesecret') }.should_not raise_error(ArgumentError)
19
25
  end
@@ -27,19 +33,19 @@ describe TwitterAuth::OauthUser do
27
33
  User.identify_or_create_from_access_token(@token)
28
34
  end
29
35
 
30
- it 'should try to find the user with that login' do
31
- User.should_receive(:find_by_login).once.with('twitterman')
36
+ it 'should try to find the user with that id' do
37
+ User.should_receive(:find_by_twitter_id).once.with('123')
32
38
  User.identify_or_create_from_access_token(@token)
33
39
  end
34
40
 
35
41
  it 'should return the user if he/she exists' do
36
- user = Factory.create(:twitter_oauth_user, :login => 'twitterman')
42
+ user = Factory.create(:twitter_oauth_user, :twitter_id => '123', :login => 'twitterman')
37
43
  user.reload
38
44
  User.identify_or_create_from_access_token(@token).should == user
39
45
  end
40
46
 
41
47
  it 'should update the access_token and access_secret for the user if he/she exists' do
42
- user = Factory.create(:twitter_oauth_user, :login => 'twitterman', :access_token => 'someothertoken', :access_secret => 'someothersecret')
48
+ user = Factory.create(:twitter_oauth_user, :twitter_id => '123', :login => 'twitterman', :access_token => 'someothertoken', :access_secret => 'someothersecret')
43
49
  User.identify_or_create_from_access_token(@token)
44
50
  user.reload
45
51
  user.access_token.should == @token.token
data/spec/schema.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  ActiveRecord::Schema.define :version => 0 do
2
2
  create_table :twitter_auth_users, :force => true do |t|
3
+ t.string :twitter_id
3
4
  t.string :login
4
5
 
5
6
  # OAuth fields
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbleigh-twitter-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.18
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-17 00:00:00 -07:00
12
+ date: 2009-04-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -41,6 +41,7 @@ extensions: []
41
41
  extra_rdoc_files:
42
42
  - README.markdown
43
43
  files:
44
+ - CHANGELOG.markdown
44
45
  - Rakefile
45
46
  - README.markdown
46
47
  - VERSION.yml