cognito_rails 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cognito_rails/model.rb +6 -1
- data/lib/cognito_rails/version.rb +1 -1
- data/spec/cognito_rails/user_spec.rb +43 -20
- data/spec/support/cognito_helpers.rb +1 -1
- data/spec/support/schema.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d8bc1e0dc872a16d7406a72e5028da49b62c80c5297d6c4428ea45efd5293a1
|
4
|
+
data.tar.gz: 71d87358a0e584eba16ada273a582caa7e560b84ba58b06d6b5752b7d1769f9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c22e85bfa4b48b5f5614314ae5cf2f696342fdf3ca204ca92a9d23107a1097da897dcd23a67d7635f1a2cd83b8ecaacbf4bdf20bfeba785fb0c9a1328fb671e9
|
7
|
+
data.tar.gz: def01df4152f2fa0e0fc3991e1438d5898af2d3485be0824f0d59e83987d65d27268897e90308e353fdc04e7b8658ff88f0e54fd92dad41d263292fed80756d9
|
data/lib/cognito_rails/model.rb
CHANGED
@@ -28,10 +28,13 @@ module CognitoRails
|
|
28
28
|
# @return [Array<ActiveRecord::Base>] all users
|
29
29
|
# @raise [CognitoRails::Error] if failed to fetch users
|
30
30
|
# @raise [ActiveRecord::RecordInvalid] if failed to save user
|
31
|
+
# @yield [user, user_data] yields user and user_data just before saving
|
31
32
|
def sync_from_cognito!
|
32
33
|
response = User.all
|
33
34
|
response.users.map do |user_data|
|
34
|
-
sync_user!(user_data)
|
35
|
+
sync_user!(user_data) do |user|
|
36
|
+
yield user, user_data if block_given?
|
37
|
+
end
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
@@ -56,6 +59,8 @@ module CognitoRails
|
|
56
59
|
user.phone = User.extract_cognito_attribute(user_data.attributes, :phone_number) if user.respond_to?(:phone)
|
57
60
|
_cognito_resolve_custom_attribute(user, user_data)
|
58
61
|
|
62
|
+
yield user if block_given?
|
63
|
+
|
59
64
|
user.save!
|
60
65
|
user
|
61
66
|
end
|
@@ -143,27 +143,50 @@ RSpec.describe CognitoRails::User, type: :model do
|
|
143
143
|
expect(CognitoRails::User).to receive(:cognito_client).at_least(:once).and_return(fake_cognito_client)
|
144
144
|
end
|
145
145
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
146
|
+
context '#sync_from_cognito!' do
|
147
|
+
before do
|
148
|
+
expect(fake_cognito_client).to receive(:list_users).and_return(
|
149
|
+
OpenStruct.new(
|
150
|
+
users: [
|
151
|
+
build_cognito_user_data('some@example.com'),
|
152
|
+
build_cognito_user_data('some2@example.com')
|
153
|
+
],
|
154
|
+
pagination_token: nil
|
155
|
+
)
|
154
156
|
)
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
157
|
+
end
|
158
|
+
it 'imports all users correctly' do
|
159
|
+
expect do
|
160
|
+
users = User.sync_from_cognito!
|
161
|
+
|
162
|
+
expect(users).to be_a(Array)
|
163
|
+
expect(users.size).to eq(2)
|
164
|
+
expect(users.first).to be_a(User)
|
165
|
+
end.to change { User.count }.by(2)
|
166
|
+
|
167
|
+
expect(User.pluck(:email)).to match_array(['some@example.com', 'some2@example.com'])
|
168
|
+
expect(User.pluck(:name)).to match_array(['John Doe', 'John Doe'])
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'allows to specify a block with extra changes applied pre-save' do
|
172
|
+
expect do
|
173
|
+
i = 0
|
174
|
+
users = EnrichedUser.sync_from_cognito! do |user, cognito_user|
|
175
|
+
i += 1
|
176
|
+
name = cognito_user.attributes.find { |a| a.name == 'custom:name' }
|
177
|
+
user.first_name = name.value.split(' ').first + i.to_s
|
178
|
+
user.last_name = name.value.split(' ').last
|
179
|
+
end
|
180
|
+
|
181
|
+
expect(users).to be_a(Array)
|
182
|
+
expect(users.size).to eq(2)
|
183
|
+
expect(users.first).to be_a(EnrichedUser)
|
184
|
+
end.to change { EnrichedUser.count }.by(2)
|
185
|
+
|
186
|
+
expect(EnrichedUser.pluck(:email)).to match_array(['some@example.com', 'some2@example.com'])
|
187
|
+
expect(EnrichedUser.order(:id).pluck(:first_name)).to match_array(['John1', 'John2'])
|
188
|
+
expect(EnrichedUser.order(:id).pluck(:last_name)).to match_array(['Doe', 'Doe'])
|
189
|
+
end
|
167
190
|
end
|
168
191
|
|
169
192
|
it '#sync_to_cognito!' do
|
data/spec/support/schema.rb
CHANGED
@@ -17,6 +17,19 @@ class User < ActiveRecord::Base
|
|
17
17
|
attr_accessor :password
|
18
18
|
end
|
19
19
|
|
20
|
+
class EnrichedUser < ActiveRecord::Base
|
21
|
+
validates :email, presence: true
|
22
|
+
validates :email, uniqueness: true
|
23
|
+
validates :first_name, :last_name, presence: true
|
24
|
+
|
25
|
+
as_cognito_user
|
26
|
+
cognito_verify_email
|
27
|
+
define_cognito_attribute 'role', 'user'
|
28
|
+
|
29
|
+
attr_accessor :password
|
30
|
+
end
|
31
|
+
|
32
|
+
|
20
33
|
class Admin < ActiveRecord::Base
|
21
34
|
validates :email, presence: true
|
22
35
|
validates :email, uniqueness: true
|
@@ -41,6 +54,14 @@ module Schema
|
|
41
54
|
t.timestamps null: false
|
42
55
|
end
|
43
56
|
|
57
|
+
create_table :enriched_users, force: true do |t|
|
58
|
+
t.string "email", null: false
|
59
|
+
t.string "first_name", null: false
|
60
|
+
t.string "last_name", null: false
|
61
|
+
t.string "external_id", null: false
|
62
|
+
t.timestamps null: false
|
63
|
+
end
|
64
|
+
|
44
65
|
create_table :admins, force: true do |t|
|
45
66
|
t.string "email", null: false
|
46
67
|
t.string "phone", null: false
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cognito_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mònade
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|