millsb-twitter-auth 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/README.markdown +96 -0
  2. data/Rakefile +31 -0
  3. data/VERSION.yml +4 -0
  4. data/app/controllers/sessions_controller.rb +64 -0
  5. data/app/models/twitter_auth/basic_user.rb +63 -0
  6. data/app/models/twitter_auth/generic_user.rb +93 -0
  7. data/app/models/twitter_auth/oauth_user.rb +43 -0
  8. data/app/views/sessions/_login_form.html.erb +17 -0
  9. data/app/views/sessions/new.html.erb +5 -0
  10. data/config/routes.rb +6 -0
  11. data/generators/twitter_auth/USAGE +12 -0
  12. data/generators/twitter_auth/templates/migration.rb +48 -0
  13. data/generators/twitter_auth/templates/twitter_auth.yml +44 -0
  14. data/generators/twitter_auth/templates/user.rb +5 -0
  15. data/generators/twitter_auth/twitter_auth_generator.rb +34 -0
  16. data/lib/twitter_auth.rb +87 -0
  17. data/lib/twitter_auth/controller_extensions.rb +65 -0
  18. data/lib/twitter_auth/cryptify.rb +30 -0
  19. data/lib/twitter_auth/dispatcher/basic.rb +46 -0
  20. data/lib/twitter_auth/dispatcher/oauth.rb +26 -0
  21. data/lib/twitter_auth/dispatcher/shared.rb +40 -0
  22. data/rails/init.rb +8 -0
  23. data/spec/controllers/controller_extensions_spec.rb +162 -0
  24. data/spec/controllers/sessions_controller_spec.rb +221 -0
  25. data/spec/fixtures/config/twitter_auth.yml +17 -0
  26. data/spec/fixtures/factories.rb +18 -0
  27. data/spec/fixtures/fakeweb.rb +18 -0
  28. data/spec/fixtures/twitter.rb +5 -0
  29. data/spec/models/twitter_auth/basic_user_spec.rb +122 -0
  30. data/spec/models/twitter_auth/generic_user_spec.rb +142 -0
  31. data/spec/models/twitter_auth/oauth_user_spec.rb +94 -0
  32. data/spec/schema.rb +41 -0
  33. data/spec/spec.opts +1 -0
  34. data/spec/spec_helper.rb +51 -0
  35. data/spec/twitter_auth/cryptify_spec.rb +51 -0
  36. data/spec/twitter_auth/dispatcher/basic_spec.rb +83 -0
  37. data/spec/twitter_auth/dispatcher/oauth_spec.rb +72 -0
  38. data/spec/twitter_auth/dispatcher/shared_spec.rb +26 -0
  39. data/spec/twitter_auth_spec.rb +154 -0
  40. metadata +127 -0
@@ -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,154 @@
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
+ end
15
+
16
+ describe ".path_prefix" do
17
+ it 'should be blank if the base url does not have a path' do
18
+ TwitterAuth.stub!(:base_url).and_return("https://twitter.com:443")
19
+ TwitterAuth.path_prefix.should == ""
20
+ end
21
+
22
+ it 'should return the path prefix if one exists' do
23
+ TwitterAuth.stub!(:base_url).and_return("https://api.presentlyapp.com/api/twitter")
24
+ TwitterAuth.path_prefix.should == "/api/twitter"
25
+ end
26
+ end
27
+
28
+ describe '.api_timeout' do
29
+ it 'should default to 10' do
30
+ TwitterAuth.stub!(:config).and_return({})
31
+ TwitterAuth.api_timeout.should == 10
32
+ end
33
+
34
+ it 'should be settable via config' do
35
+ TwitterAuth.stub!(:config).and_return({'api_timeout' => 15})
36
+ TwitterAuth.api_timeout.should == 15
37
+ end
38
+ end
39
+
40
+ describe '.remember_for' do
41
+ it 'should default to 14' do
42
+ TwitterAuth.stub!(:config).and_return({})
43
+ TwitterAuth.remember_for.should == 14
44
+ end
45
+
46
+ it 'should be settable via config' do
47
+ TwitterAuth.stub!(:config).and_return({'remember_for' => '7'})
48
+ TwitterAuth.remember_for.should == 7
49
+ end
50
+ end
51
+
52
+ describe '.net' do
53
+ before do
54
+ stub_basic!
55
+ end
56
+
57
+ it 'should return a Net::HTTP object' do
58
+ TwitterAuth.net.should be_a(Net::HTTP)
59
+ end
60
+
61
+ it 'should be SSL if the base_url is' do
62
+ TwitterAuth.stub!(:config).and_return({'base_url' => 'http://twitter.com'})
63
+ TwitterAuth.net.use_ssl?.should be_false
64
+ TwitterAuth.stub!(:config).and_return({'base_url' => 'https://twitter.com'})
65
+ TwitterAuth.net.use_ssl?.should be_true
66
+ end
67
+
68
+ it 'should work from the base_url' do
69
+ @net = Net::HTTP.new('example.com',80)
70
+ Net::HTTP.should_receive(:new).with('example.com',80).and_return(@net)
71
+ TwitterAuth.stub!(:config).and_return({'base_url' => 'http://example.com'})
72
+ TwitterAuth.net
73
+ end
74
+ end
75
+
76
+ describe '#config' do
77
+ before do
78
+ TwitterAuth.send(:instance_variable_set, :@config, nil)
79
+ @config_file = File.open(File.dirname(__FILE__) + '/fixtures/config/twitter_auth.yml')
80
+ File.should_receive(:open).any_number_of_times.and_return(@config_file)
81
+ end
82
+
83
+ it 'should load a hash from RAILS_ROOT/config/twitter.yml' do
84
+ TwitterAuth.config.should be_a(Hash)
85
+ end
86
+
87
+ it 'should be able to override the RAILS_ENV' do
88
+ TwitterAuth.config('development')['oauth_consumer_key'].should == 'devkey'
89
+ end
90
+ end
91
+
92
+ describe '#consumer' do
93
+ before do
94
+ stub_oauth!
95
+ end
96
+
97
+ it 'should be an OAuth Consumer' do
98
+ TwitterAuth.consumer.should be_a(OAuth::Consumer)
99
+ end
100
+
101
+ it 'should use the credentials from #config' do
102
+ TwitterAuth.consumer.key.should == 'testkey'
103
+ TwitterAuth.consumer.secret.should == 'testsecret'
104
+ end
105
+
106
+ it 'should use the TwitterAuth base_url' do
107
+ TwitterAuth.stub!(:base_url).and_return('https://example.com')
108
+ TwitterAuth.consumer.site.should == TwitterAuth.base_url
109
+ TwitterAuth.consumer.site.should == 'https://example.com'
110
+ end
111
+ end
112
+
113
+ describe '#strategy' do
114
+ it 'should pull and symbolize from the config' do
115
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'oauth'})
116
+ TwitterAuth.strategy.should == TwitterAuth.config['strategy'].to_sym
117
+ end
118
+
119
+ it 'should raise an argument error if not oauth or basic' do
120
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'oauth'})
121
+ lambda{TwitterAuth.strategy}.should_not raise_error(ArgumentError)
122
+
123
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'basic'})
124
+ lambda{TwitterAuth.strategy}.should_not raise_error(ArgumentError)
125
+
126
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'invalid_strategy'})
127
+ lambda{TwitterAuth.strategy}.should raise_error(ArgumentError)
128
+ end
129
+ end
130
+
131
+ it '#oauth? should be true if strategy is :oauth' do
132
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'oauth'})
133
+ TwitterAuth.oauth?.should be_true
134
+ TwitterAuth.basic?.should be_false
135
+ end
136
+
137
+ it '#basic? should be true if strategy is :basic' do
138
+ TwitterAuth.stub!(:config).and_return({'strategy' => 'basic'})
139
+ TwitterAuth.oauth?.should be_false
140
+ TwitterAuth.basic?.should be_true
141
+ end
142
+
143
+ describe '#encryption_key' do
144
+ it 'should raise a Cryptify error if none is found' do
145
+ TwitterAuth.stub!(:config).and_return({})
146
+ lambda{TwitterAuth.encryption_key}.should raise_error(TwitterAuth::Cryptify::Error, "You must specify an encryption_key in config/twitter_auth.yml")
147
+ end
148
+
149
+ it 'should return the config[encryption_key] value' do
150
+ TwitterAuth.stub!(:config).and_return({'encryption_key' => 'mickeymouse'})
151
+ TwitterAuth.encryption_key.should == 'mickeymouse'
152
+ end
153
+ end
154
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: millsb-twitter-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.16
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-31 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: oauth
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ezcrypto
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.2
34
+ version:
35
+ description: TwitterAuth is a Rails plugin gem that provides Single Sign-On capabilities for Rails applications via Twitter. Both OAuth and HTTP Basic are supported.
36
+ email: michael@intridea.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.markdown
43
+ files:
44
+ - Rakefile
45
+ - README.markdown
46
+ - VERSION.yml
47
+ - generators/twitter_auth
48
+ - generators/twitter_auth/templates
49
+ - generators/twitter_auth/templates/migration.rb
50
+ - generators/twitter_auth/templates/twitter_auth.yml
51
+ - generators/twitter_auth/templates/user.rb
52
+ - generators/twitter_auth/twitter_auth_generator.rb
53
+ - generators/twitter_auth/USAGE
54
+ - lib/twitter_auth
55
+ - lib/twitter_auth/controller_extensions.rb
56
+ - lib/twitter_auth/cryptify.rb
57
+ - lib/twitter_auth/dispatcher
58
+ - lib/twitter_auth/dispatcher/basic.rb
59
+ - lib/twitter_auth/dispatcher/oauth.rb
60
+ - lib/twitter_auth/dispatcher/shared.rb
61
+ - lib/twitter_auth.rb
62
+ - spec/controllers
63
+ - spec/controllers/controller_extensions_spec.rb
64
+ - spec/controllers/sessions_controller_spec.rb
65
+ - spec/fixtures
66
+ - spec/fixtures/config
67
+ - spec/fixtures/config/twitter_auth.yml
68
+ - spec/fixtures/factories.rb
69
+ - spec/fixtures/fakeweb.rb
70
+ - spec/fixtures/twitter.rb
71
+ - spec/models
72
+ - spec/models/twitter_auth
73
+ - spec/models/twitter_auth/basic_user_spec.rb
74
+ - spec/models/twitter_auth/generic_user_spec.rb
75
+ - spec/models/twitter_auth/oauth_user_spec.rb
76
+ - spec/schema.rb
77
+ - spec/spec.opts
78
+ - spec/spec_helper.rb
79
+ - spec/twitter_auth
80
+ - spec/twitter_auth/cryptify_spec.rb
81
+ - spec/twitter_auth/dispatcher
82
+ - spec/twitter_auth/dispatcher/basic_spec.rb
83
+ - spec/twitter_auth/dispatcher/oauth_spec.rb
84
+ - spec/twitter_auth/dispatcher/shared_spec.rb
85
+ - spec/twitter_auth_spec.rb
86
+ - config/routes.rb
87
+ - app/controllers
88
+ - app/controllers/sessions_controller.rb
89
+ - app/models
90
+ - app/models/twitter_auth
91
+ - app/models/twitter_auth/basic_user.rb
92
+ - app/models/twitter_auth/generic_user.rb
93
+ - app/models/twitter_auth/oauth_user.rb
94
+ - app/views
95
+ - app/views/sessions
96
+ - app/views/sessions/_login_form.html.erb
97
+ - app/views/sessions/new.html.erb
98
+ - rails/init.rb
99
+ has_rdoc: true
100
+ homepage: http://github.com/mbleigh/twitter-auth
101
+ post_install_message:
102
+ rdoc_options:
103
+ - --inline-source
104
+ - --charset=UTF-8
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: "0"
112
+ version:
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ version:
119
+ requirements: []
120
+
121
+ rubyforge_project: twitter-auth
122
+ rubygems_version: 1.2.0
123
+ signing_key:
124
+ specification_version: 2
125
+ summary: TwitterAuth is a Rails plugin gem that provides Single Sign-On capabilities for Rails applications via Twitter.
126
+ test_files: []
127
+