twitter-auth-with-mongo-mapper 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/CHANGELOG.markdown +11 -0
  2. data/README.markdown +5 -0
  3. data/Rakefile +31 -0
  4. data/VERSION.yml +4 -0
  5. data/app/controllers/sessions_controller.rb +81 -0
  6. data/app/models/twitter_auth/basic_user.rb +64 -0
  7. data/app/models/twitter_auth/generic_user.rb +135 -0
  8. data/app/models/twitter_auth/oauth_user.rb +50 -0
  9. data/app/views/sessions/_login_form.html.erb +17 -0
  10. data/app/views/sessions/new.html.erb +5 -0
  11. data/config/routes.rb +6 -0
  12. data/generators/twitter_auth/USAGE +12 -0
  13. data/generators/twitter_auth/templates/migration.rb +49 -0
  14. data/generators/twitter_auth/templates/twitter_auth.yml +47 -0
  15. data/generators/twitter_auth/templates/user.rb +5 -0
  16. data/generators/twitter_auth/twitter_auth_generator.rb +34 -0
  17. data/lib/twitter_auth.rb +100 -0
  18. data/lib/twitter_auth/controller_extensions.rb +82 -0
  19. data/lib/twitter_auth/cryptify.rb +31 -0
  20. data/lib/twitter_auth/dispatcher/basic.rb +46 -0
  21. data/lib/twitter_auth/dispatcher/oauth.rb +27 -0
  22. data/lib/twitter_auth/dispatcher/shared.rb +40 -0
  23. data/rails/init.rb +8 -0
  24. data/spec/controllers/controller_extensions_spec.rb +162 -0
  25. data/spec/controllers/sessions_controller_spec.rb +221 -0
  26. data/spec/fixtures/config/twitter_auth.yml +17 -0
  27. data/spec/fixtures/factories.rb +20 -0
  28. data/spec/fixtures/fakeweb.rb +18 -0
  29. data/spec/fixtures/twitter.rb +5 -0
  30. data/spec/models/twitter_auth/basic_user_spec.rb +138 -0
  31. data/spec/models/twitter_auth/generic_user_spec.rb +146 -0
  32. data/spec/models/twitter_auth/oauth_user_spec.rb +100 -0
  33. data/spec/schema.rb +42 -0
  34. data/spec/spec.opts +1 -0
  35. data/spec/spec_helper.rb +51 -0
  36. data/spec/twitter_auth/cryptify_spec.rb +51 -0
  37. data/spec/twitter_auth/dispatcher/basic_spec.rb +83 -0
  38. data/spec/twitter_auth/dispatcher/oauth_spec.rb +72 -0
  39. data/spec/twitter_auth/dispatcher/shared_spec.rb +26 -0
  40. data/spec/twitter_auth_spec.rb +160 -0
  41. metadata +151 -0
@@ -0,0 +1,100 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe TwitterAuth::OauthUser do
4
+ before do
5
+ stub_oauth!
6
+ end
7
+
8
+ describe '.identify_or_create_from_access_token' do
9
+ before do
10
+ @token = OAuth::AccessToken.new(TwitterAuth.consumer, 'faketoken', 'fakesecret')
11
+ end
12
+
13
+ it 'should accept an OAuth::AccessToken' do
14
+ lambda{ User.identify_or_create_from_access_token(@token) }.should_not raise_error(ArgumentError)
15
+ end
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
+
23
+ it 'should accept two strings' do
24
+ lambda{ User.identify_or_create_from_access_token('faketoken', 'fakesecret') }.should_not raise_error(ArgumentError)
25
+ end
26
+
27
+ it 'should not accept one string' do
28
+ 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.')
29
+ end
30
+
31
+ it 'should make a call to verify_credentials' do
32
+ # this is in the before, just making it explicit
33
+ User.identify_or_create_from_access_token(@token)
34
+ end
35
+
36
+ it 'should try to find the user with that id' do
37
+ User.should_receive(:find_by_twitter_id).once.with('123')
38
+ User.identify_or_create_from_access_token(@token)
39
+ end
40
+
41
+ it 'should return the user if he/she exists' do
42
+ user = Factory.create(:twitter_oauth_user, :twitter_id => '123', :login => 'twitterman')
43
+ user.reload
44
+ User.identify_or_create_from_access_token(@token).should == user
45
+ end
46
+
47
+ it 'should update the access_token and access_secret for the user if he/she exists' do
48
+ user = Factory.create(:twitter_oauth_user, :twitter_id => '123', :login => 'twitterman', :access_token => 'someothertoken', :access_secret => 'someothersecret')
49
+ User.identify_or_create_from_access_token(@token)
50
+ user.reload
51
+ user.access_token.should == @token.token
52
+ user.access_secret.should == @token.secret
53
+ end
54
+
55
+ it 'should update the user\'s attributes based on the twitter info' do
56
+ user = Factory.create(:twitter_oauth_user, :login => 'twitterman', :name => 'Not Twitter Man')
57
+ User.identify_or_create_from_access_token(@token).name.should == 'Twitter Man'
58
+ end
59
+
60
+ it 'should create a user if one does not exist' do
61
+ lambda{User.identify_or_create_from_access_token(@token)}.should change(User, :count).by(1)
62
+ end
63
+
64
+ it 'should assign the oauth access token and secret' do
65
+ user = User.identify_or_create_from_access_token(@token)
66
+ user.access_token.should == @token.token
67
+ user.access_secret.should == @token.secret
68
+ end
69
+ end
70
+
71
+ describe '#token' do
72
+ before do
73
+ @user = Factory.create(:twitter_oauth_user, :access_token => 'token', :access_secret => 'secret')
74
+ end
75
+
76
+ it 'should return an AccessToken' do
77
+ @user.token.should be_a(OAuth::AccessToken)
78
+ end
79
+
80
+ it "should use the user's access_token and secret" do
81
+ @user.token.token.should == @user.access_token
82
+ @user.token.secret.should == @user.access_secret
83
+ end
84
+ end
85
+
86
+ describe '#twitter' do
87
+ before do
88
+ @user = Factory.create(:twitter_oauth_user, :access_token => 'token', :access_secret => 'secret')
89
+ end
90
+
91
+ it 'should return a TwitterAuth::Dispatcher::Oauth' do
92
+ @user.twitter.should be_a(TwitterAuth::Dispatcher::Oauth)
93
+ end
94
+
95
+ it 'should use my token and secret' do
96
+ @user.twitter.token.should == @user.access_token
97
+ @user.twitter.secret.should == @user.access_secret
98
+ end
99
+ end
100
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,42 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table :twitter_auth_users, :force => true do |t|
3
+ t.string :twitter_id
4
+ t.string :login
5
+
6
+ # OAuth fields
7
+ t.string :access_token
8
+ t.string :access_secret
9
+
10
+ # Basic fields
11
+ t.binary :crypted_password
12
+ t.string :salt
13
+
14
+ # Remember token fields
15
+ t.string :remember_token
16
+ t.datetime :remember_token_expires_at
17
+
18
+ # This information is automatically kept
19
+ # in-sync at each login of the user. You
20
+ # may remove any/all of these columns.
21
+ t.string :name
22
+ t.string :location
23
+ t.string :description
24
+ t.string :profile_image_url
25
+ t.string :url
26
+ t.boolean :protected
27
+ t.string :profile_background_color
28
+ t.string :profile_sidebar_fill_color
29
+ t.string :profile_link_color
30
+ t.string :profile_sidebar_border_color
31
+ t.string :profile_text_color
32
+ t.integer :friends_count
33
+ t.integer :statuses_count
34
+ t.integer :followers_count
35
+ t.integer :favourites_count
36
+ t.integer :utc_offset
37
+ t.string :time_zone
38
+
39
+ t.timestamps
40
+ end
41
+ end
42
+
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,51 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ rescue LoadError
4
+ puts "You need to install rspec in your base app"
5
+ exit
6
+ end
7
+
8
+ require File.dirname(__FILE__) + '/../app/models/twitter_auth/generic_user'
9
+
10
+ class TwitterAuth::GenericUser
11
+ def self.table_name; 'twitter_auth_users' end
12
+ end
13
+
14
+ class User < TwitterAuth::GenericUser; end
15
+
16
+ require 'remarkable'
17
+ require File.dirname(__FILE__) + '/fixtures/factories'
18
+ require File.dirname(__FILE__) + '/fixtures/fakeweb'
19
+ require File.dirname(__FILE__) + '/fixtures/twitter'
20
+
21
+ plugin_spec_dir = File.dirname(__FILE__)
22
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
23
+
24
+ load(File.dirname(__FILE__) + '/schema.rb')
25
+
26
+ def define_basic_user_class!
27
+ TwitterAuth::GenericUser.send :include, TwitterAuth::BasicUser
28
+ end
29
+
30
+ def define_oauth_user_class!
31
+ TwitterAuth::GenericUser.send :include, TwitterAuth::OauthUser
32
+ end
33
+
34
+ def stub_oauth!
35
+ TwitterAuth.stub!(:config).and_return({
36
+ 'strategy' => 'oauth',
37
+ 'oauth_consumer_key' => 'testkey',
38
+ 'oauth_consumer_secret' => 'testsecret'
39
+ })
40
+ define_oauth_user_class!
41
+ end
42
+
43
+ def stub_basic!
44
+ TwitterAuth.stub!(:config).and_return({
45
+ 'strategy' => 'basic',
46
+ 'encryption_key' => 'secretcode'
47
+ })
48
+ define_basic_user_class!
49
+ end
50
+
51
+ define_oauth_user_class!
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe TwitterAuth::Cryptify do
4
+ before do
5
+ stub_basic!
6
+ end
7
+
8
+ it 'should have encrypt and decrypt methods' do
9
+ TwitterAuth::Cryptify.should respond_to(:encrypt)
10
+ TwitterAuth::Cryptify.should respond_to(:decrypt)
11
+ end
12
+
13
+ describe '.encrypt' do
14
+ it 'should return a hash with :encrypted_data and :salt keys' do
15
+ result = TwitterAuth::Cryptify.encrypt('some string')
16
+ result.should be_a(Hash)
17
+ result.key?(:encrypted_data).should be_true
18
+ result.key?(:salt).should be_true
19
+ end
20
+
21
+ it 'should make a call to EzCrypto::Key.encrypt_with_password' do
22
+ EzCrypto::Key.should_receive(:encrypt_with_password).once.and_return('gobbledygook')
23
+ TwitterAuth::Cryptify.encrypt('some string')
24
+ end
25
+
26
+ it 'should not have the same encrypted as plaintext data' do
27
+ TwitterAuth::Cryptify.encrypt('some string')[:encrypted_data].should_not == 'some string'
28
+ end
29
+ end
30
+
31
+ describe '.decrypt' do
32
+ before do
33
+ @salt = TwitterAuth::Cryptify.generate_salt
34
+ TwitterAuth::Cryptify.stub!(:generate_salt).and_return(@salt)
35
+ @string = 'decrypted string'
36
+ @encrypted = TwitterAuth::Cryptify.encrypt(@string)
37
+ end
38
+
39
+ it 'should return the original string' do
40
+ TwitterAuth::Cryptify.decrypt(@encrypted).should == @string
41
+ end
42
+
43
+ it 'should raise an argument error if encrypted data is provided without a salt' do
44
+ lambda{TwitterAuth::Cryptify.decrypt('asodiaoie2')}.should raise_error(ArgumentError)
45
+ end
46
+
47
+ it 'should raise an argument error if a string or hash are not provided' do
48
+ lambda{TwitterAuth::Cryptify.decrypt(23)}.should raise_error(ArgumentError)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe TwitterAuth::Dispatcher::Basic do
4
+ before do
5
+ stub_basic!
6
+ @user = Factory.create(:twitter_basic_user, :login => 'twitterman', :password => 'test')
7
+ end
8
+
9
+ it 'should require a user as the initialization argument' do
10
+ lambda{TwitterAuth::Dispatcher::Basic.new(nil)}.should raise_error(TwitterAuth::Error, 'Dispatcher must be initialized with a User.')
11
+ end
12
+
13
+ it 'should store the user in an attr_accessor' do
14
+ TwitterAuth::Dispatcher::Basic.new(@user).user.should == @user
15
+ end
16
+
17
+ describe '#request' do
18
+ before do
19
+ @dispatcher = TwitterAuth::Dispatcher::Basic.new(@user)
20
+ FakeWeb.register_uri('https://twitter.com:443/fake.json', :string => {'fake' => true}.to_json)
21
+ FakeWeb.register_uri('https://twitter.com:443/fake.xml', :string => '<fake>true</fake>')
22
+ end
23
+
24
+ it 'should automatically parse JSON if valid' do
25
+ @dispatcher.request(:get, '/fake.json').should == {'fake' => true}
26
+ end
27
+
28
+ it 'should return XML as a string' do
29
+ @dispatcher.request(:get, '/fake.xml').should == "<fake>true</fake>"
30
+ end
31
+
32
+ it 'should append .json to the path if no extension is provided' do
33
+ @dispatcher.request(:get, '/fake.json').should == @dispatcher.request(:get, '/fake')
34
+ end
35
+
36
+ %w(get post put delete).each do |method|
37
+ it "should build a #{method} class based on a :#{method} http_method" do
38
+ @req = "Net::HTTP::#{method.capitalize}".constantize.new('/fake.json')
39
+ "Net::HTTP::#{method.capitalize}".constantize.should_receive(:new).and_return(@req)
40
+ @dispatcher.request(method.to_sym, '/fake')
41
+ end
42
+ end
43
+
44
+ it 'should start the HTTP session' do
45
+ @net = TwitterAuth.net
46
+ TwitterAuth.stub!(:net).and_return(@net)
47
+ @net.should_receive(:start)
48
+ lambda{@dispatcher.request(:get, '/fake')}.should raise_error(NoMethodError)
49
+ end
50
+
51
+ it "should raise a TwitterAuth::Dispatcher::Error if response code isn't 200" do
52
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized'])
53
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error)
54
+ end
55
+
56
+ it 'should set the error message to the JSON message' do
57
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['403', 'Forbidden'])
58
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
59
+ end
60
+
61
+ it 'should raise a TwitterAuth::Dispatcher::Unauthorized on 401' do
62
+ FakeWeb.register_uri('https://twitter.com:443/unauthenticated_response.xml', :string => "<hash>\n<request>/unauthenticated_response.xml</request>\n<error>bad response</error>\n</hash>", :status => ['401', 'Unauthorized'])
63
+ lambda{@dispatcher.request(:get, '/unauthenticated_response.xml')}.should raise_error(TwitterAuth::Dispatcher::Unauthorized)
64
+ end
65
+
66
+ it 'should set the error message to the XML message' do
67
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.xml', :string => "<hash>\n<request>/bad_response.xml</request>\n<error>bad response</error>\n</hash>", :status => ['403', 'Forbidden'])
68
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
69
+ end
70
+ end
71
+
72
+ %w(get post delete put).each do |method|
73
+ it "should have a ##{method} method that calls request(:#{method})" do
74
+ dispatcher = TwitterAuth::Dispatcher::Basic.new(@user)
75
+ if %w(get delete).include?(method)
76
+ dispatcher.should_receive(:request).with(method.to_sym, '/fake.json')
77
+ else
78
+ dispatcher.should_receive(:request).with(method.to_sym, '/fake.json', '')
79
+ end
80
+ dispatcher.send(method, '/fake.json')
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe TwitterAuth::Dispatcher::Oauth do
4
+ before do
5
+ stub_oauth!
6
+ @user = Factory.create(:twitter_oauth_user, :access_token => 'token', :access_secret => 'secret')
7
+ end
8
+
9
+ it 'should be a child class of OAuth::AccessToken' do
10
+ TwitterAuth::Dispatcher::Oauth.new(@user).should be_a(OAuth::AccessToken)
11
+ end
12
+
13
+ it 'should require initialization of an OauthUser' do
14
+ lambda{TwitterAuth::Dispatcher::Oauth.new(nil)}.should raise_error(TwitterAuth::Error, 'Dispatcher must be initialized with a User.')
15
+ end
16
+
17
+ it 'should store the user in an attr_accessor' do
18
+ TwitterAuth::Dispatcher::Oauth.new(@user).user.should == @user
19
+ end
20
+
21
+ it "should initialize with the user's token and secret" do
22
+ d = TwitterAuth::Dispatcher::Oauth.new(@user)
23
+ d.token.should == 'token'
24
+ d.secret.should == 'secret'
25
+ end
26
+
27
+ describe '#request' do
28
+ before do
29
+ @dispatcher = TwitterAuth::Dispatcher::Oauth.new(@user)
30
+ FakeWeb.register_uri(:get, 'https://twitter.com:443/fake.json', :string => {'fake' => true}.to_json)
31
+ FakeWeb.register_uri(:get, 'https://twitter.com:443/fake.xml', :string => "<fake>true</fake>")
32
+ end
33
+
34
+ it 'should automatically parse json' do
35
+ result = @dispatcher.request(:get, '/fake.json')
36
+ result.should be_a(Hash)
37
+ result['fake'].should be_true
38
+ end
39
+
40
+ it 'should return xml as a string' do
41
+ @dispatcher.request(:get, '/fake.xml').should == '<fake>true</fake>'
42
+ end
43
+
44
+ it 'should append .json to the path if no extension is provided' do
45
+ @dispatcher.request(:get, '/fake').should == @dispatcher.request(:get, '/fake.json')
46
+ end
47
+
48
+ it "should raise a TwitterAuth::Dispatcher::Error if response code isn't 200" do
49
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['401', 'Unauthorized'])
50
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error)
51
+ end
52
+
53
+ it 'should set the error message to the JSON message' do
54
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.json', :string => {'error' => 'bad response'}.to_json, :status => ['403', 'Forbidden'])
55
+ lambda{@dispatcher.request(:get, '/bad_response')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
56
+ end
57
+
58
+ it 'should set the error message to the XML message' do
59
+ FakeWeb.register_uri('https://twitter.com:443/bad_response.xml', :string => "<hash>\n<request>/bad_response.xml</request>\n<error>bad response</error>\n</hash>", :status => ['403', 'Forbidden'])
60
+ lambda{@dispatcher.request(:get, '/bad_response.xml')}.should raise_error(TwitterAuth::Dispatcher::Error, 'bad response')
61
+ end
62
+
63
+ it 'should raise a TwitterAuth::Dispatcher::Unauthorized on 401' do
64
+ FakeWeb.register_uri('https://twitter.com:443/unauthenticated_response.xml', :string => "<hash>\n<request>/unauthenticated_response.xml</request>\n<error>bad response</error>\n</hash>", :status => ['401', 'Unauthorized'])
65
+ lambda{@dispatcher.request(:get, '/unauthenticated_response.xml')}.should raise_error(TwitterAuth::Dispatcher::Unauthorized)
66
+ end
67
+
68
+ it 'should work with verb methods' do
69
+ @dispatcher.get('/fake').should == @dispatcher.request(:get, '/fake')
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe TwitterAuth::Dispatcher::Shared do
4
+ include TwitterAuth::Dispatcher::Shared
5
+
6
+ describe '#append_extension_to' do
7
+ it 'should leave extensions alone if they exist' do
8
+ append_extension_to('/fake.json').should == '/fake.json'
9
+ append_extension_to('/fake.xml').should == '/fake.xml'
10
+ end
11
+
12
+ it 'should append .json if no extension is provided' do
13
+ append_extension_to('/fake').should == '/fake.json'
14
+ append_extension_to('/verify/fake').should == '/verify/fake.json'
15
+ end
16
+
17
+ it 'should leave extensions alone even with query strings' do
18
+ append_extension_to('/fake.json?since_id=123').should == '/fake.json?since_id=123'
19
+ append_extension_to('/fake.xml?since_id=123').should == '/fake.xml?since_id=123'
20
+ end
21
+
22
+ it 'should add an extension even with query strings' do
23
+ append_extension_to('/fake?since_id=123').should == '/fake.json?since_id=123'
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,160 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe TwitterAuth do
4
+ describe '.base_url' do
5
+ it 'should have default to https://twitter.com' do
6
+ TwitterAuth.stub!(:config).and_return({})
7
+ TwitterAuth.base_url.should == 'https://twitter.com'
8
+ end
9
+
10
+ it 'should otherwise load from the config[base_url]' do
11
+ TwitterAuth.stub!(:config).and_return({'base_url' => 'https://example.com'})
12
+ TwitterAuth.base_url.should == 'https://example.com'
13
+ end
14
+
15
+ it 'should utilize oauth consumer settings' do
16
+ @config = TwitterAuth.config
17
+ TwitterAuth.stub!(:config).and_return(@config.merge('authorize_path' => '/somewhere_else'))
18
+ TwitterAuth.consumer.authorize_path.should == '/somewhere_else'
19
+ end
20
+ end
21
+
22
+ describe ".path_prefix" do
23
+ it 'should be blank if the base url does not have a path' do
24
+ TwitterAuth.stub!(:base_url).and_return("https://twitter.com:443")
25
+ TwitterAuth.path_prefix.should == ""
26
+ end
27
+
28
+ it 'should return the path prefix if one exists' do
29
+ TwitterAuth.stub!(:base_url).and_return("https://api.presentlyapp.com/api/twitter")
30
+ TwitterAuth.path_prefix.should == "/api/twitter"
31
+ end
32
+ end
33
+
34
+ describe '.api_timeout' do
35
+ it 'should default to 10' do
36
+ TwitterAuth.stub!(:config).and_return({})
37
+ TwitterAuth.api_timeout.should == 10
38
+ end
39
+
40
+ it 'should be settable via config' do
41
+ TwitterAuth.stub!(:config).and_return({'api_timeout' => 15})
42
+ TwitterAuth.api_timeout.should == 15
43
+ end
44
+ end
45
+
46
+ describe '.remember_for' do
47
+ it 'should default to 14' do
48
+ TwitterAuth.stub!(:config).and_return({})
49
+ TwitterAuth.remember_for.should == 14
50
+ end
51
+
52
+ it 'should be settable via config' do
53
+ TwitterAuth.stub!(:config).and_return({'remember_for' => '7'})
54
+ TwitterAuth.remember_for.should == 7
55
+ end
56
+ end
57
+
58
+ describe '.net' do
59
+ before do
60
+ stub_basic!
61
+ end
62
+
63
+ it 'should return a Net::HTTP object' do
64
+ TwitterAuth.net.should be_a(Net::HTTP)
65
+ end
66
+
67
+ it 'should be SSL if the base_url is' do
68
+ TwitterAuth.stub!(:config).and_return({'base_url' => 'http://twitter.com'})
69
+ TwitterAuth.net.use_ssl?.should be_false
70
+ TwitterAuth.stub!(:config).and_return({'base_url' => 'https://twitter.com'})
71
+ TwitterAuth.net.use_ssl?.should be_true
72
+ end
73
+
74
+ it 'should work from the base_url' do
75
+ @net = Net::HTTP.new('example.com',80)
76
+ Net::HTTP.should_receive(:new).with('example.com',80).and_return(@net)
77
+ TwitterAuth.stub!(:config).and_return({'base_url' => 'http://example.com'})
78
+ TwitterAuth.net
79
+ end
80
+ end
81
+
82
+ describe '#config' do
83
+ before do
84
+ TwitterAuth.send(:instance_variable_set, :@config, nil)
85
+ @config_file = File.open(File.dirname(__FILE__) + '/fixtures/config/twitter_auth.yml')
86
+ File.should_receive(:open).any_number_of_times.and_return(@config_file)
87
+ end
88
+
89
+ it 'should load a hash from RAILS_ROOT/config/twitter.yml' do
90
+ TwitterAuth.config.should be_a(Hash)
91
+ end
92
+
93
+ it 'should be able to override the RAILS_ENV' do
94
+ TwitterAuth.config('development')['oauth_consumer_key'].should == 'devkey'
95
+ end
96
+ end
97
+
98
+ describe '#consumer' do
99
+ before do
100
+ stub_oauth!
101
+ end
102
+
103
+ it 'should be an OAuth Consumer' do
104
+ TwitterAuth.consumer.should be_a(OAuth::Consumer)
105
+ end
106
+
107
+ it 'should use the credentials from #config' do
108
+ TwitterAuth.consumer.key.should == 'testkey'
109
+ TwitterAuth.consumer.secret.should == 'testsecret'
110
+ end
111
+
112
+ it 'should use the TwitterAuth base_url' do
113
+ TwitterAuth.stub!(:base_url).and_return('https://example.com')
114
+ TwitterAuth.consumer.site.should == TwitterAuth.base_url
115
+ TwitterAuth.consumer.site.should == 'https://example.com'
116
+ end
117
+ end
118
+
119
+ describe '#strategy' do
120
+ it 'should pull and symbolize from the config' do
121
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'oauth'})
122
+ TwitterAuth.strategy.should == TwitterAuth.config['strategy'].to_sym
123
+ end
124
+
125
+ it 'should raise an argument error if not oauth or basic' do
126
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'oauth'})
127
+ lambda{TwitterAuth.strategy}.should_not raise_error(ArgumentError)
128
+
129
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'basic'})
130
+ lambda{TwitterAuth.strategy}.should_not raise_error(ArgumentError)
131
+
132
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'invalid_strategy'})
133
+ lambda{TwitterAuth.strategy}.should raise_error(ArgumentError)
134
+ end
135
+ end
136
+
137
+ it '#oauth? should be true if strategy is :oauth' do
138
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'oauth'})
139
+ TwitterAuth.oauth?.should be_true
140
+ TwitterAuth.basic?.should be_false
141
+ end
142
+
143
+ it '#basic? should be true if strategy is :basic' do
144
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'basic'})
145
+ TwitterAuth.oauth?.should be_false
146
+ TwitterAuth.basic?.should be_true
147
+ end
148
+
149
+ describe '#encryption_key' do
150
+ it 'should raise a Cryptify error if none is found' do
151
+ TwitterAuth.stub!(:config).and_return({})
152
+ lambda{TwitterAuth.encryption_key}.should raise_error(TwitterAuth::Cryptify::Error, "You must specify an encryption_key in config/twitter_auth.yml")
153
+ end
154
+
155
+ it 'should return the config[encryption_key] value' do
156
+ TwitterAuth.stub!(:config).and_return({'encryption_key' => 'mickeymouse'})
157
+ TwitterAuth.encryption_key.should == 'mickeymouse'
158
+ end
159
+ end
160
+ end