rails_sso 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,74 @@
1
+ require 'test_helper'
2
+
3
+ class RailsSso::HelpersTest < ActiveSupport::TestCase
4
+ class DummyController
5
+ def self.helper_method(*list)
6
+ end
7
+
8
+ include RailsSso::Helpers
9
+
10
+ attr_reader :session
11
+
12
+ def initialize(session)
13
+ @session = session
14
+ end
15
+ end
16
+
17
+ AccessToken = Struct.new(:token, :refresh_token)
18
+
19
+ def setup
20
+ RailsSso.provider_name = 'OAuth2'
21
+ RailsSso.provider_key = 'provider_key'
22
+ RailsSso.provider_secret = 'provider_secret'
23
+
24
+ @controller = DummyController.new(session)
25
+ end
26
+
27
+ def session
28
+ @session ||= {}
29
+ end
30
+
31
+ test "#current_user_data will return nil if not authenticated" do
32
+ assert_nil @controller.current_user_data
33
+ end
34
+
35
+ test "#current_user_data will return user data if authenticated" do
36
+ # TODO: pending
37
+ end
38
+
39
+ test "#user_signed_in? returns false if not authenticated" do
40
+ refute @controller.user_signed_in?
41
+ end
42
+
43
+ test "#user_signed_in? returns true if authenticated" do
44
+ # TODO: pending
45
+ end
46
+
47
+ test "#access_token returns a new OAuth2::AccessToken object" do
48
+ session[:access_token] = 'abc'
49
+ session[:refresh_token] = 'def'
50
+
51
+ access_token = @controller.access_token
52
+
53
+ assert_instance_of OAuth2::AccessToken, access_token
54
+ assert_equal 'abc', access_token.token
55
+ assert_equal 'def', access_token.refresh_token
56
+ end
57
+
58
+ test "#invalidate_access_token! will call Single Sign-Out with access token" do
59
+ # @TODO: pending
60
+ end
61
+
62
+ test "save_access_token! will copy access and refresh token to session" do
63
+ access_token = AccessToken.new('abc', '1337')
64
+
65
+ @controller.save_access_token!(access_token)
66
+
67
+ assert_equal 'abc', session[:access_token]
68
+ assert_equal '1337', session[:refresh_token]
69
+ end
70
+
71
+ test "#refresh_access_token! will refresh token and copy new values" do
72
+ # @TODO: pending
73
+ end
74
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ class RailsSso::ResponseErrorTest < ActiveSupport::TestCase
4
+ test "assigns error code" do
5
+ err = RailsSso::ResponseError.new(:err_code)
6
+
7
+ assert_equal err.code, :err_code
8
+ end
9
+
10
+ test "assigns unauthenticated error message from locales" do
11
+ err = RailsSso::ResponseError.new(:unauthenticated)
12
+
13
+ assert_equal err.message, "You're not authenticated"
14
+ end
15
+
16
+ test "assigns unknown error message from locales" do
17
+ err = RailsSso::ResponseError.new(:unknown)
18
+
19
+ assert_equal err.message, "Something wrong happened"
20
+ end
21
+ end
@@ -1,39 +1,55 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class RailsSso::FetchUserTest < ActiveSupport::TestCase
4
- class AccessToken
5
- def initialize(client)
6
- @client = client
7
- end
4
+ test "success call should fetch user with access token and return parsed data" do
5
+ data = RailsSso::FetchUser.new(success_client).call
8
6
 
9
- def get(path)
10
- OAuth2::Response.new(@client.get(path))
11
- end
7
+ assert_equal data['name'], user_data['name']
8
+ assert_equal data['email'], user_data['email']
12
9
  end
13
10
 
14
- def setup
15
- @access_token = AccessToken.new(setup_access_token_client)
11
+ test "unauthenticated call should raise error" do
12
+ err = assert_raises(RailsSso::ResponseError) { RailsSso::FetchUser.new(unauthenticated_client).call }
13
+ assert_equal :unauthenticated, err.code
16
14
  end
17
15
 
18
- test "call should fetch user with access token and return parsed data" do
19
- data = RailsSso::FetchUser.new(@access_token).call
16
+ test "unknown call should raise error" do
17
+ err = assert_raises(RailsSso::ResponseError) { RailsSso::FetchUser.new(unknown_client).call }
18
+ assert_equal :unknown, err.code
19
+ end
20
20
 
21
- assert_equal data['name'], user_data['name']
22
- assert_equal data['email'], user_data['email']
21
+ def user_data
22
+ {
23
+ 'name' => 'Kowalski',
24
+ 'email' => 'jan@kowalski.pl'
25
+ }
23
26
  end
24
27
 
25
- def setup_access_token_client
28
+ def response_headers
29
+ { 'Content-Type' => 'application/json' }
30
+ end
31
+
32
+ def success_client
26
33
  Faraday.new do |builder|
27
34
  builder.adapter :test do |stub|
28
- stub.get('/api/v1/me') { |env| [200, { 'Content-Type' => 'application/json' }, user_data] }
35
+ stub.get('/api/v1/me') { |env| [200, response_headers, user_data] }
29
36
  end
30
37
  end
31
38
  end
32
39
 
33
- def user_data
34
- {
35
- 'name' => 'Kowalski',
36
- 'email' => 'jan@kowalski.pl'
37
- }
40
+ def unauthenticated_client
41
+ Faraday.new do |builder|
42
+ builder.adapter :test do |stub|
43
+ stub.get('/api/v1/me') { |env| [401, response_headers, {}] }
44
+ end
45
+ end
46
+ end
47
+
48
+ def unknown_client
49
+ Faraday.new do |builder|
50
+ builder.adapter :test do |stub|
51
+ stub.get('/api/v1/me') { |env| [500, response_headers, {}] }
52
+ end
53
+ end
38
54
  end
39
55
  end
data/test/test_helper.rb CHANGED
@@ -1,8 +1,9 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
1
3
  # Configure Rails Environment
2
4
  ENV["RAILS_ENV"] = "test"
3
5
 
4
6
  require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
- ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
6
7
  require "rails/test_help"
7
8
  require "mocha/mini_test"
8
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_sso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Dudulski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-17 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -107,11 +107,15 @@ files:
107
107
  - app/controllers/rails_sso/sessions_controller.rb
108
108
  - app/services/rails_sso/fetch_user.rb
109
109
  - config/initializers/omniauth.rb
110
+ - config/locales/en.yml
110
111
  - config/routes.rb
112
+ - lib/generators/rails_sso_generator.rb
113
+ - lib/generators/templates/sso.rb
111
114
  - lib/rails_sso.rb
112
- - lib/rails_sso/access_token.rb
115
+ - lib/rails_sso/client.rb
113
116
  - lib/rails_sso/engine.rb
114
117
  - lib/rails_sso/helpers.rb
118
+ - lib/rails_sso/response_error.rb
115
119
  - lib/rails_sso/version.rb
116
120
  - lib/tasks/rails_sso_tasks.rake
117
121
  - test/controllers/rails_sso/sessions_controller_test.rb
@@ -146,12 +150,14 @@ files:
146
150
  - test/dummy/config/locales/en.yml
147
151
  - test/dummy/config/routes.rb
148
152
  - test/dummy/config/secrets.yml
149
- - test/dummy/db/test.sqlite3
153
+ - test/dummy/log/development.log
150
154
  - test/dummy/log/test.log
151
155
  - test/dummy/public/404.html
152
156
  - test/dummy/public/422.html
153
157
  - test/dummy/public/500.html
154
158
  - test/dummy/public/favicon.ico
159
+ - test/lib/rails_sso/helpers_test.rb
160
+ - test/lib/rails_sso/response_error_test.rb
155
161
  - test/rails_sso_test.rb
156
162
  - test/routes/sso_routes_test.rb
157
163
  - test/services/rails_sso/fetch_user_test.rb
@@ -183,10 +189,11 @@ summary: SSO Rails Engine
183
189
  test_files:
184
190
  - test/controllers/rails_sso/sessions_controller_test.rb
185
191
  - test/rails_sso_test.rb
192
+ - test/lib/rails_sso/response_error_test.rb
193
+ - test/lib/rails_sso/helpers_test.rb
186
194
  - test/test_helper.rb
187
195
  - test/services/rails_sso/fetch_user_test.rb
188
196
  - test/dummy/README.rdoc
189
- - test/dummy/db/test.sqlite3
190
197
  - test/dummy/public/favicon.ico
191
198
  - test/dummy/public/500.html
192
199
  - test/dummy/public/422.html
@@ -221,5 +228,6 @@ test_files:
221
228
  - test/dummy/app/assets/javascripts/application.js
222
229
  - test/dummy/app/assets/stylesheets/application.css
223
230
  - test/dummy/log/test.log
231
+ - test/dummy/log/development.log
224
232
  - test/dummy/Rakefile
225
233
  - test/routes/sso_routes_test.rb
@@ -1,52 +0,0 @@
1
- require 'faraday-http-cache'
2
-
3
- module RailsSso
4
- class AccessToken
5
- attr_reader :token, :refresh_token
6
-
7
- delegate :get, :patch, :post, :put, :delete, to: :access_token
8
-
9
- def self.from_access_token(access_token)
10
- new(access_token.token, access_token.refresh_token)
11
- end
12
-
13
- def initialize(token, refresh_token)
14
- @token, @refresh_token = token, refresh_token
15
- end
16
-
17
- def refresh!
18
- self.class.from_access_token(access_token.refresh!)
19
- end
20
-
21
- def access_token
22
- @access_token ||= OAuth2::AccessToken.new(client, token, {
23
- refresh_token: refresh_token
24
- })
25
- end
26
-
27
- private
28
-
29
- def client(cache = RailsSso.use_cache)
30
- strategy.client.tap do |c|
31
- if cache
32
- c.options[:connection_build] = Proc.new do |conn|
33
- conn.use :http_cache,
34
- store: Rails.cache,
35
- logger: Rails.logger,
36
- shared_cache: false
37
-
38
- conn.adapter Faraday.default_adapter
39
- end
40
- end
41
- end
42
- end
43
-
44
- def strategy
45
- @strategy ||= strategy_class.new(nil, RailsSso.provider_key, RailsSso.provider_secret)
46
- end
47
-
48
- def strategy_class
49
- "OmniAuth::Strategies::#{RailsSso.provider_name.camelize}".constantize
50
- end
51
- end
52
- end
File without changes