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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b69f6c8db91a764df6878e0de53feb9879f7509e79bf0d406e6fe9522f96e61
4
- data.tar.gz: f44a4f4dfce641e54493730c9f183c9bd2f6a2e73fe4cd0c7a60d084b6d0d09a
3
+ metadata.gz: 2d8bc1e0dc872a16d7406a72e5028da49b62c80c5297d6c4428ea45efd5293a1
4
+ data.tar.gz: 71d87358a0e584eba16ada273a582caa7e560b84ba58b06d6b5752b7d1769f9e
5
5
  SHA512:
6
- metadata.gz: 17551dd1de906fd5813214f4fdd98bd49f7d4503138cfa5cd9beedc235707dc00b000d356e090a154b7de0c08e692553154b273d50236b31f455903044c97594
7
- data.tar.gz: ee07b9c1b821bc601ac2c62d4268392783e5f687da1d9b810d6160f64c5b7f3546ffd7e5998f05a5a03fc2326bd162e338e0be2ceda461d2cae1152072e44ec8
6
+ metadata.gz: c22e85bfa4b48b5f5614314ae5cf2f696342fdf3ca204ca92a9d23107a1097da897dcd23a67d7635f1a2cd83b8ecaacbf4bdf20bfeba785fb0c9a1328fb671e9
7
+ data.tar.gz: def01df4152f2fa0e0fc3991e1438d5898af2d3485be0824f0d59e83987d65d27268897e90308e353fdc04e7b8658ff88f0e54fd92dad41d263292fed80756d9
@@ -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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CognitoRails
4
4
  # @return [String] gem version
5
- VERSION = '1.1.0'
5
+ VERSION = '1.2.0'
6
6
  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
- it '#sync_from_cognito!' do
147
- expect(fake_cognito_client).to receive(:list_users).and_return(
148
- OpenStruct.new(
149
- users: [
150
- build_cognito_user_data('some@example.com'),
151
- build_cognito_user_data('some2@example.com')
152
- ],
153
- pagination_token: nil
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
- expect do
158
- users = User.sync_from_cognito!
159
-
160
- expect(users).to be_a(Array)
161
- expect(users.size).to eq(2)
162
- expect(users.first).to be_a(User)
163
- end.to change { User.count }.by(2)
164
-
165
- expect(User.pluck(:email)).to match_array(['some@example.com', 'some2@example.com'])
166
- expect(User.pluck(:name)).to match_array(['Giovanni', 'Giovanni'])
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
@@ -54,7 +54,7 @@ module CognitoRails::Helpers
54
54
  ),
55
55
  OpenStruct.new(
56
56
  name: 'custom:name',
57
- value: 'Giovanni'
57
+ value: 'John Doe'
58
58
  )
59
59
  ],
60
60
  mfa_options: []
@@ -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.1.0
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-04-27 00:00:00.000000000 Z
11
+ date: 2023-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport