rails_sso 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ class RailsSso::FetchUserTest < ActiveSupport::TestCase
4
+ class AccessToken
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def get(path)
10
+ OAuth2::Response.new(@client.get(path))
11
+ end
12
+ end
13
+
14
+ def setup
15
+ @access_token = AccessToken.new(setup_access_token_client)
16
+ end
17
+
18
+ test "call should fetch user with access token and yield parsed data" do
19
+ called = false
20
+
21
+ RailsSso::FetchUser.new(@access_token).call do |data|
22
+ called = true
23
+
24
+ assert_equal data['name'], user_data['name']
25
+ assert_equal data['email'], user_data['email']
26
+ end
27
+
28
+ assert called, 'RailsSso::FetchUser#call should yield fetched data'
29
+ end
30
+
31
+ def setup_access_token_client
32
+ Faraday.new do |builder|
33
+ builder.adapter :test do |stub|
34
+ stub.get('/api/v1/me') { |env| [200, { 'Content-Type' => 'application/json' }, user_data] }
35
+ end
36
+ end
37
+ end
38
+
39
+ def user_data
40
+ {
41
+ 'name' => 'Kowalski',
42
+ 'email' => 'jan@kowalski.pl',
43
+ 'key' => 'value'
44
+ }
45
+ end
46
+ end
@@ -0,0 +1,79 @@
1
+ require 'anima'
2
+ require 'test_helper'
3
+
4
+ class RailsSso::UpdateUserTest < ActiveSupport::TestCase
5
+ class User
6
+ include Anima.new('email', 'name')
7
+
8
+ attr_accessor :id
9
+ attr_writer :name, :email
10
+ end
11
+
12
+ class Repository
13
+ attr_reader :storage
14
+
15
+ def initialize
16
+ @storage = {}
17
+ end
18
+
19
+ def find_by_sso_id(id)
20
+ storage[id]
21
+ end
22
+
23
+ def create_with_sso_id(id, data)
24
+ @storage[id] = User.new(data).tap do |user|
25
+ user.id = id
26
+ end
27
+ end
28
+
29
+ def update(user, params)
30
+ params.each do |k, v|
31
+ user.send("#{k}=", v)
32
+ end
33
+ end
34
+ end
35
+
36
+ def setup
37
+ @repository = Repository.new
38
+
39
+ @options = {
40
+ repository: @repository,
41
+ fields: [:name, :email]
42
+ }
43
+
44
+ @data = {
45
+ 'id' => 1,
46
+ 'name' => 'Kowalski',
47
+ 'email' => 'jan@kowalski.pl',
48
+ 'key' => 'value'
49
+ }
50
+ end
51
+
52
+ test "call should create user if not exists in repository and return it" do
53
+ output = RailsSso::UpdateUser.new(@data, @options).call
54
+
55
+ assert_equal_user(@data, output)
56
+
57
+ user = @repository.find_by_sso_id(@data['id'])
58
+
59
+ assert_equal_user(@data, user)
60
+ end
61
+
62
+ test "call should update user if exists in repository and return it" do
63
+ @repository.create_with_sso_id(@data['id'], { 'email' => 'test@example.com', 'name' => 'Nowak' })
64
+
65
+ output = RailsSso::UpdateUser.new(@data, @options).call
66
+
67
+ assert_equal_user(@data, output)
68
+
69
+ user = @repository.find_by_sso_id(@data['id'])
70
+
71
+ assert_equal_user(@data, user)
72
+ end
73
+
74
+ def assert_equal_user(data, user)
75
+ assert_equal data['id'], user.id
76
+ assert_equal data['email'], user.email
77
+ assert_equal data['name'], user.name
78
+ end
79
+ end
data/test/test_helper.rb CHANGED
@@ -4,6 +4,7 @@ ENV["RAILS_ENV"] = "test"
4
4
  require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
5
  ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
6
6
  require "rails/test_help"
7
+ require "mocha/mini_test"
7
8
 
8
9
  # Filter out Minitest backtrace while allowing backtrace from other libraries
9
10
  # to be shown.
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.0.1
4
+ version: 0.1.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-01-14 00:00:00.000000000 Z
11
+ date: 2015-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: '4.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-http-cache
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: omniauth-oauth2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: sqlite3
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +66,34 @@ dependencies:
38
66
  - - ">="
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: anima
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
41
97
  description: Single Sign On solution via OAuth2 for Ruby on Rails.
42
98
  email:
43
99
  - jan@dudulski.pl
@@ -49,15 +105,17 @@ files:
49
105
  - README.md
50
106
  - Rakefile
51
107
  - app/controllers/rails_sso/sessions_controller.rb
108
+ - app/services/rails_sso/fetch_user.rb
109
+ - app/services/rails_sso/update_user.rb
52
110
  - config/initializers/omniauth.rb
53
111
  - config/routes.rb
54
112
  - lib/rails_sso.rb
113
+ - lib/rails_sso/access_token.rb
55
114
  - lib/rails_sso/engine.rb
56
- - lib/rails_sso/fetch_user.rb
57
115
  - lib/rails_sso/helpers.rb
58
- - lib/rails_sso/update_user.rb
59
116
  - lib/rails_sso/version.rb
60
117
  - lib/tasks/rails_sso_tasks.rake
118
+ - test/controllers/rails_sso/sessions_controller_test.rb
61
119
  - test/dummy/README.rdoc
62
120
  - test/dummy/Rakefile
63
121
  - test/dummy/app/assets/javascripts/application.js
@@ -84,15 +142,20 @@ files:
84
142
  - test/dummy/config/initializers/inflections.rb
85
143
  - test/dummy/config/initializers/mime_types.rb
86
144
  - test/dummy/config/initializers/session_store.rb
145
+ - test/dummy/config/initializers/sso.rb
87
146
  - test/dummy/config/initializers/wrap_parameters.rb
88
147
  - test/dummy/config/locales/en.yml
89
148
  - test/dummy/config/routes.rb
90
149
  - test/dummy/config/secrets.yml
150
+ - test/dummy/db/test.sqlite3
151
+ - test/dummy/log/test.log
91
152
  - test/dummy/public/404.html
92
153
  - test/dummy/public/422.html
93
154
  - test/dummy/public/500.html
94
155
  - test/dummy/public/favicon.ico
95
156
  - test/rails_sso_test.rb
157
+ - test/services/rails_sso/fetch_user_test.rb
158
+ - test/services/rails_sso/update_user_test.rb
96
159
  - test/test_helper.rb
97
160
  homepage: https://github.com/monterail/rails_sso
98
161
  licenses:
@@ -119,9 +182,13 @@ signing_key:
119
182
  specification_version: 4
120
183
  summary: SSO Rails Engine
121
184
  test_files:
185
+ - test/controllers/rails_sso/sessions_controller_test.rb
122
186
  - test/rails_sso_test.rb
123
187
  - test/test_helper.rb
188
+ - test/services/rails_sso/fetch_user_test.rb
189
+ - test/services/rails_sso/update_user_test.rb
124
190
  - test/dummy/README.rdoc
191
+ - test/dummy/db/test.sqlite3
125
192
  - test/dummy/public/favicon.ico
126
193
  - test/dummy/public/500.html
127
194
  - test/dummy/public/422.html
@@ -138,6 +205,7 @@ test_files:
138
205
  - test/dummy/config/initializers/session_store.rb
139
206
  - test/dummy/config/initializers/backtrace_silencers.rb
140
207
  - test/dummy/config/initializers/wrap_parameters.rb
208
+ - test/dummy/config/initializers/sso.rb
141
209
  - test/dummy/config/initializers/mime_types.rb
142
210
  - test/dummy/config/initializers/cookies_serializer.rb
143
211
  - test/dummy/config/initializers/inflections.rb
@@ -154,4 +222,5 @@ test_files:
154
222
  - test/dummy/app/controllers/application_controller.rb
155
223
  - test/dummy/app/assets/javascripts/application.js
156
224
  - test/dummy/app/assets/stylesheets/application.css
225
+ - test/dummy/log/test.log
157
226
  - test/dummy/Rakefile
@@ -1,45 +0,0 @@
1
- module RailsSso
2
- class FetchUser
3
- def initialize(access_token, storage)
4
- @access_token, @storage = access_token, storage
5
- end
6
-
7
- def get_and_cache
8
- updater(get).call
9
- end
10
-
11
- private
12
-
13
- attr_reader :access_token, :storage
14
-
15
- def get(tries = 1)
16
- access_token.get(RailsSso.provider_profile_path).parsed
17
- rescue ::OAuth2::Error => e
18
- if tries > 0
19
- refresh_token!
20
-
21
- get(0)
22
- else
23
- raise e
24
- end
25
- end
26
-
27
- def updater(data)
28
- RailsSso::UpdateUser.new(data, updater_options)
29
- end
30
-
31
- def updater_options
32
- {
33
- fields: RailsSso.user_fields,
34
- repository: RailsSso.user_repository.new
35
- }
36
- end
37
-
38
- def refresh_token!
39
- @access_token = access_token.refresh!
40
-
41
- storage[:access_token] = access_token.token
42
- storage[:refresh_token] = access_token.refresh_token
43
- end
44
- end
45
- end