global-registry-bindings 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +126 -0
  4. data/lib/global_registry_bindings.rb +8 -0
  5. data/lib/global_registry_bindings/entity/delete_entity_methods.rb +22 -0
  6. data/lib/global_registry_bindings/entity/entity_methods.rb +62 -0
  7. data/lib/global_registry_bindings/entity/entity_type_methods.rb +54 -0
  8. data/lib/global_registry_bindings/entity/mdm_methods.rb +43 -0
  9. data/lib/global_registry_bindings/entity/push_entity_methods.rb +78 -0
  10. data/lib/global_registry_bindings/entity/push_relationship_methods.rb +81 -0
  11. data/lib/global_registry_bindings/entity/relationship_type_methods.rb +59 -0
  12. data/lib/global_registry_bindings/exceptions.rb +9 -0
  13. data/lib/global_registry_bindings/global_registry_bindings.rb +71 -0
  14. data/lib/global_registry_bindings/options.rb +80 -0
  15. data/lib/global_registry_bindings/options/class_options.rb +58 -0
  16. data/lib/global_registry_bindings/options/instance_options.rb +61 -0
  17. data/lib/global_registry_bindings/railtie.rb +19 -0
  18. data/lib/global_registry_bindings/version.rb +7 -0
  19. data/lib/global_registry_bindings/workers/delete_gr_entity_worker.rb +22 -0
  20. data/lib/global_registry_bindings/workers/pull_mdm_id_worker.rb +30 -0
  21. data/lib/global_registry_bindings/workers/push_gr_entity_worker.rb +21 -0
  22. data/lib/global_registry_bindings/workers/push_relationship_worker.rb +21 -0
  23. data/spec/acceptance/global_registry_bindings_spec.rb +74 -0
  24. data/spec/factories/factories.rb +37 -0
  25. data/spec/fixtures/get_entities_person.json +8 -0
  26. data/spec/fixtures/get_entities_person_mdm.json +13 -0
  27. data/spec/fixtures/get_entities_person_relationship.json +32 -0
  28. data/spec/fixtures/get_entity_types.json +9 -0
  29. data/spec/fixtures/get_entity_types_address.json +59 -0
  30. data/spec/fixtures/get_entity_types_address_partial.json +43 -0
  31. data/spec/fixtures/get_entity_types_fancy_org.json +43 -0
  32. data/spec/fixtures/get_entity_types_fancy_org_partial.json +35 -0
  33. data/spec/fixtures/get_entity_types_person.json +42 -0
  34. data/spec/fixtures/get_entity_types_person_partial.json +34 -0
  35. data/spec/fixtures/get_relationship_types.json +9 -0
  36. data/spec/fixtures/get_relationship_types_person_fancy_org.json +41 -0
  37. data/spec/fixtures/get_relationship_types_person_fancy_org_partial.json +33 -0
  38. data/spec/fixtures/post_entities_fancy_org.json +8 -0
  39. data/spec/fixtures/post_entities_fancy_org_parent.json +8 -0
  40. data/spec/fixtures/post_entities_person.json +8 -0
  41. data/spec/fixtures/post_entity_types_address.json +9 -0
  42. data/spec/fixtures/post_entity_types_fancy_org.json +9 -0
  43. data/spec/fixtures/post_entity_types_person.json +9 -0
  44. data/spec/fixtures/post_relationship_types_person_fancy_org.json +16 -0
  45. data/spec/fixtures/put_entities_address.json +20 -0
  46. data/spec/fixtures/put_entities_person_relationship.json +29 -0
  47. data/spec/fixtures/put_entities_relationship.json +8 -0
  48. data/spec/fixtures/put_relationship_types_fields.json +33 -0
  49. data/spec/internal/app/models/address.rb +11 -0
  50. data/spec/internal/app/models/application_record.rb +5 -0
  51. data/spec/internal/app/models/assignment.rb +10 -0
  52. data/spec/internal/app/models/default.rb +6 -0
  53. data/spec/internal/app/models/namespaced/person.rb +18 -0
  54. data/spec/internal/app/models/organization.rb +12 -0
  55. data/spec/internal/config/database.yml +4 -0
  56. data/spec/internal/config/initializers/global_registry.rb +8 -0
  57. data/spec/internal/config/routes.rb +5 -0
  58. data/spec/internal/db/schema.rb +41 -0
  59. data/spec/internal/log/test.log +35717 -0
  60. data/spec/models/address_spec.rb +228 -0
  61. data/spec/models/assignment_spec.rb +200 -0
  62. data/spec/models/organization_spec.rb +127 -0
  63. data/spec/models/person_spec.rb +247 -0
  64. data/spec/spec_helper.rb +55 -0
  65. data/spec/workers/delete_gr_entity_worker_spec.rb +33 -0
  66. data/spec/workers/pull_mdm_id_worker_spec.rb +71 -0
  67. data/spec/workers/push_gr_entity_worker_spec.rb +27 -0
  68. data/spec/workers/push_relationship_worker_spec.rb +27 -0
  69. metadata +458 -0
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe 'Address' do
6
+ describe ':push_entity_to_global_registry_async' do
7
+ it 'should enqueue sidekiq job' do
8
+ address = build(:address)
9
+ expect do
10
+ address.push_entity_to_global_registry_async
11
+ end.to change(GlobalRegistry::Bindings::Workers::PushGrEntityWorker.jobs, :size).by(1)
12
+ end
13
+ end
14
+
15
+ describe ':delete_entity_from_global_registry_async' do
16
+ it 'should enqueue sidekiq job' do
17
+ address = build(:address, global_registry_id: '22527d88-3cba-11e7-b876-129bd0521531')
18
+ expect do
19
+ address.delete_entity_from_global_registry_async
20
+ end.to change(GlobalRegistry::Bindings::Workers::DeleteGrEntityWorker.jobs, :size).by(1)
21
+ end
22
+
23
+ it 'should not enqueue sidekiq job when missing global_registry_id' do
24
+ address = build(:address)
25
+ expect do
26
+ address.delete_entity_from_global_registry_async
27
+ end.not_to change(GlobalRegistry::Bindings::Workers::DeleteGrEntityWorker.jobs, :size)
28
+ end
29
+ end
30
+
31
+ describe ':push_entity_to_global_registry' do
32
+ around { |example| travel_to Time.utc(2001, 2, 3), &example }
33
+ context 'create' do
34
+ context '\'address\' entity_type does not exist' do
35
+ let!(:requests) do
36
+ [stub_request(:get, 'https://backend.global-registry.org/entity_types')
37
+ .with(query: { 'filters[name]' => 'person', 'filters[parent_id]' => nil })
38
+ .to_return(body: file_fixture('get_entity_types_person.json'), status: 200),
39
+ stub_request(:get, 'https://backend.global-registry.org/entity_types')
40
+ .with(query: { 'filters[name]' => 'address',
41
+ 'filters[parent_id]' => 'ee13a693-3ce7-4c19-b59a-30c8f137acd8' })
42
+ .to_return(body: file_fixture('get_entity_types.json'), status: 200),
43
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
44
+ .with(body: { entity_type: { name: 'address', parent_id: 'ee13a693-3ce7-4c19-b59a-30c8f137acd8',
45
+ field_type: 'entity' } })
46
+ .to_return(body: file_fixture('post_entity_types_address.json'), status: 200),
47
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
48
+ .with(body: { entity_type: { name: 'zip', parent_id: 'f5331684-3ca8-11e7-b937-129bd0521531',
49
+ field_type: 'string' } })
50
+ .to_return(status: 200),
51
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
52
+ .with(body: { entity_type: { name: 'line1', parent_id: 'f5331684-3ca8-11e7-b937-129bd0521531',
53
+ field_type: 'string' } })
54
+ .to_return(status: 200),
55
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
56
+ .with(body: { entity_type: { name: 'line2', parent_id: 'f5331684-3ca8-11e7-b937-129bd0521531',
57
+ field_type: 'string' } })
58
+ .to_return(status: 200),
59
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
60
+ .with(body: { entity_type: { name: 'primary', parent_id: 'f5331684-3ca8-11e7-b937-129bd0521531',
61
+ field_type: 'boolean' } })
62
+ .to_return(status: 200),
63
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
64
+ .with(body: { entity_type: { name: 'postal_code', parent_id: 'f5331684-3ca8-11e7-b937-129bd0521531',
65
+ field_type: 'string' } })
66
+ .to_return(status: 200)]
67
+ end
68
+
69
+ context '\'address\' record does not belong to a person' do
70
+ let(:address) { create(:address) }
71
+
72
+ it 'should create \'address\' entity_type and skip push of entity' do
73
+ address.push_entity_to_global_registry
74
+ requests.each { |r| expect(r).to have_been_requested.once }
75
+ end
76
+ end
77
+
78
+ context '\'address\' record belongs to a person without global registry id' do
79
+ let(:person) { create(:person) }
80
+ let(:address) { create(:address, person: person) }
81
+ let!(:sub_requests) do
82
+ [stub_request(:post, 'https://backend.global-registry.org/entities')
83
+ .with(body: { entity: { person: { first_name: 'Tony', last_name: 'Stark',
84
+ client_integration_id: person.id,
85
+ client_updated_at: '2001-02-03 00:00:00',
86
+ authentication: {
87
+ key_guid: '98711710-acb5-4a41-ba51-e0fc56644b53'
88
+ } } } })
89
+ .to_return(body: file_fixture('post_entities_person.json'), status: 200),
90
+ stub_request(:put,
91
+ 'https://backend.global-registry.org/entities/22527d88-3cba-11e7-b876-129bd0521531')
92
+ .with(body: { entity: { person: { client_integration_id: person.id,
93
+ address: {
94
+ zip: '90265', primary: 'true',
95
+ line1: '10880 Malibu Point', postal_code: '90265',
96
+ client_integration_id: address.id,
97
+ client_updated_at: '2001-02-03 00:00:00'
98
+ } } } })
99
+ .to_return(body: file_fixture('put_entities_address.json'), status: 200)]
100
+ end
101
+
102
+ it 'should create \'address\' entity_type and push person and address entities' do
103
+ address.push_entity_to_global_registry
104
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
105
+ expect(person.global_registry_id).to eq '22527d88-3cba-11e7-b876-129bd0521531'
106
+ expect(address.global_registry_id).to eq '0a594356-3f1c-11e7-bba6-129bd0521531'
107
+ end
108
+ end
109
+
110
+ context '\'address\' record belongs to an existing person entity' do
111
+ let(:person) { create(:person, global_registry_id: '22527d88-3cba-11e7-b876-129bd0521531') }
112
+ let(:address) { create(:address, person: person) }
113
+ let!(:sub_requests) do
114
+ [stub_request(:put,
115
+ 'https://backend.global-registry.org/entities/22527d88-3cba-11e7-b876-129bd0521531')
116
+ .with(body: { entity: { person: { client_integration_id: person.id,
117
+ address: {
118
+ zip: '90265', primary: 'true',
119
+ line1: '10880 Malibu Point', postal_code: '90265',
120
+ client_integration_id: address.id,
121
+ client_updated_at: '2001-02-03 00:00:00'
122
+ } } } })
123
+ .to_return(body: file_fixture('put_entities_address.json'), status: 200)]
124
+ end
125
+
126
+ it 'should create \'address\' entity_type and push address entity' do
127
+ address.push_entity_to_global_registry
128
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
129
+ expect(address.global_registry_id).to eq '0a594356-3f1c-11e7-bba6-129bd0521531'
130
+ end
131
+ end
132
+ end
133
+
134
+ context '\'address\' and \'person\' entity_types exist' do
135
+ let(:person) { create(:person, global_registry_id: '22527d88-3cba-11e7-b876-129bd0521531') }
136
+ let(:address) { create(:address, person: person) }
137
+ let!(:requests) do
138
+ [stub_request(:get, 'https://backend.global-registry.org/entity_types')
139
+ .with(query: { 'filters[name]' => 'person', 'filters[parent_id]' => nil })
140
+ .to_return(body: file_fixture('get_entity_types_person.json'), status: 200),
141
+ stub_request(:put,
142
+ 'https://backend.global-registry.org/entities/22527d88-3cba-11e7-b876-129bd0521531')
143
+ .with(body: { entity: { person: { client_integration_id: person.id,
144
+ address: {
145
+ zip: '90265', primary: 'true',
146
+ line1: '10880 Malibu Point', postal_code: '90265',
147
+ client_integration_id: address.id,
148
+ client_updated_at: '2001-02-03 00:00:00'
149
+ } } } })
150
+ .to_return(body: file_fixture('put_entities_address.json'), status: 200)]
151
+ end
152
+
153
+ context '\'address\' entity_type has partial fields' do
154
+ let!(:sub_requests) do
155
+ [stub_request(:get, 'https://backend.global-registry.org/entity_types')
156
+ .with(query: { 'filters[name]' => 'address',
157
+ 'filters[parent_id]' => 'ee13a693-3ce7-4c19-b59a-30c8f137acd8' })
158
+ .to_return(body: file_fixture('get_entity_types_address_partial.json'), status: 200),
159
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
160
+ .with(body: { entity_type: { name: 'line2', parent_id: 'f5331684-3ca8-11e7-b937-129bd0521531',
161
+ field_type: 'string' } })
162
+ .to_return(status: 200),
163
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
164
+ .with(body: { entity_type: { name: 'primary', parent_id: 'f5331684-3ca8-11e7-b937-129bd0521531',
165
+ field_type: 'boolean' } })
166
+ .to_return(status: 200)]
167
+ end
168
+
169
+ it 'should add missing fields and push address entity' do
170
+ address.push_entity_to_global_registry
171
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
172
+ expect(address.global_registry_id).to eq '0a594356-3f1c-11e7-bba6-129bd0521531'
173
+ end
174
+ end
175
+
176
+ context '\'address\' entity_type has all fields' do
177
+ let!(:sub_requests) do
178
+ [stub_request(:get, 'https://backend.global-registry.org/entity_types')
179
+ .with(query: { 'filters[name]' => 'address',
180
+ 'filters[parent_id]' => 'ee13a693-3ce7-4c19-b59a-30c8f137acd8' })
181
+ .to_return(body: file_fixture('get_entity_types_address.json'), status: 200)]
182
+ end
183
+
184
+ it 'should push address entity' do
185
+ address.push_entity_to_global_registry
186
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
187
+ expect(address.global_registry_id).to eq '0a594356-3f1c-11e7-bba6-129bd0521531'
188
+ end
189
+ end
190
+ end
191
+
192
+ context 'update' do
193
+ before :each do
194
+ person_entity_types = JSON.parse(file_fixture('get_entity_types_person.json').read)
195
+ Rails.cache.write('GlobalRegistry::Bindings::EntityType::person', person_entity_types['entity_types'].first)
196
+ address_entity_types = JSON.parse(file_fixture('get_entity_types_address.json').read)
197
+ Rails.cache.write('GlobalRegistry::Bindings::EntityType::address',
198
+ address_entity_types['entity_types'].first)
199
+ end
200
+ let(:person) { create(:person, global_registry_id: '22527d88-3cba-11e7-b876-129bd0521531') }
201
+ let(:address) do
202
+ create(:address, person: person, global_registry_id: '0a594356-3f1c-11e7-bba6-129bd0521531')
203
+ end
204
+ let!(:request) do
205
+ stub_request(:put,
206
+ 'https://backend.global-registry.org/entities/22527d88-3cba-11e7-b876-129bd0521531')
207
+ .with(body: { entity: { person: { client_integration_id: person.id,
208
+ address: {
209
+ zip: '90265', primary: 'false',
210
+ line1: '100 Sesame Street', postal_code: '90265',
211
+ client_integration_id: address.id,
212
+ client_updated_at: '2001-02-03 00:00:00'
213
+ } } } })
214
+ .to_return(body: file_fixture('put_entities_address.json'), status: 200)
215
+ end
216
+
217
+ it 'should push address entity' do
218
+ address.address1 = '100 Sesame Street'
219
+ address.primary = false
220
+ expect do
221
+ address.push_entity_to_global_registry
222
+ end.to_not(change { address.global_registry_id })
223
+ expect(request).to have_been_requested.once
224
+ end
225
+ end
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe 'Assignment' do
6
+ describe ':push_relationship_to_global_registry_async' do
7
+ it 'should enqueue sidekiq job' do
8
+ assignment = build(:assignment)
9
+ expect do
10
+ assignment.push_relationship_to_global_registry_async
11
+ end.to change(GlobalRegistry::Bindings::Workers::PushRelationshipWorker.jobs, :size).by(1)
12
+ end
13
+ end
14
+
15
+ describe ':push_relationship_to_global_registry' do
16
+ context '#create' do
17
+ let(:person) { create(:person, global_registry_id: '22527d88-3cba-11e7-b876-129bd0521531') }
18
+ let(:organization) { create(:organization, gr_id: 'aebb4170-3f34-11e7-bba6-129bd0521531') }
19
+ let(:assignment) { create(:assignment, person: person, organization: organization) }
20
+ let!(:requests) do
21
+ [stub_request(:get, 'https://backend.global-registry.org/entity_types')
22
+ .with(query: { 'filters[name]' => 'person', 'filters[parent_id]' => nil })
23
+ .to_return(body: file_fixture('get_entity_types_person.json'), status: 200),
24
+ stub_request(:get, 'https://backend.global-registry.org/entity_types')
25
+ .with(query: { 'filters[name]' => 'fancy_org', 'filters[parent_id]' => nil })
26
+ .to_return(body: file_fixture('get_entity_types_fancy_org.json'), status: 200),
27
+ stub_request(:put, "https://backend.global-registry.org/entities/#{person.global_registry_id}")
28
+ .with(body: { entity: { person: { 'fancy_org:relationship': {
29
+ role: 'leader', hired_at: '2000-12-03 00:00:00',
30
+ client_integration_id: assignment.id,
31
+ client_updated_at: '2001-02-03 00:00:00', fancy_org: organization.gr_id
32
+ } }, client_integration_id: person.id } }, query: { full_response: 'true',
33
+ fields: 'fancy_org:relationship' })
34
+ .to_return(body: file_fixture('put_entities_person_relationship.json'), status: 200),
35
+ stub_request(:put, 'https://backend.global-registry.org/entities/51a014a4-4252-11e7-944f-129bd0521531')
36
+ .with(body: { entity: { role: 'leader', hired_at: '2000-12-03 00:00:00',
37
+ client_integration_id: assignment.id,
38
+ client_updated_at: '2001-02-03 00:00:00' } })
39
+ .to_return(body: file_fixture('put_entities_relationship.json'), status: 200)]
40
+ end
41
+
42
+ context '\'assignment\' relationship_type does not exist' do
43
+ around { |example| travel_to Time.utc(2001, 2, 3), &example }
44
+ let!(:sub_requests) do
45
+ [stub_request(:get, 'https://backend.global-registry.org/relationship_types')
46
+ .with(query: { 'filters[between]' =>
47
+ 'ee13a693-3ce7-4c19-b59a-30c8f137acd8,025a1128-3f33-11e7-b876-129bd0521531' })
48
+ .to_return(body: file_fixture('get_relationship_types.json'), status: 200),
49
+ stub_request(:post, 'https://backend.global-registry.org/relationship_types')
50
+ .with(body: { relationship_type: { entity_type1_id: 'ee13a693-3ce7-4c19-b59a-30c8f137acd8',
51
+ entity_type2_id: '025a1128-3f33-11e7-b876-129bd0521531',
52
+ relationship1: 'person', relationship2: 'fancy_org' } })
53
+ .to_return(body: file_fixture('post_relationship_types_person_fancy_org.json'), status: 200),
54
+ stub_request(:put,
55
+ 'https://backend.global-registry.org/relationship_types/5d721db8-4248-11e7-90b4-129bd0521531')
56
+ .with(body: { relationship_type: { fields: [{ name: 'role', field_type: 'string' },
57
+ { name: 'hired_at', field_type: 'datetime' }] } })
58
+ .to_return(body: file_fixture('put_relationship_types_fields.json'), status: 200)]
59
+ end
60
+
61
+ it 'should create \'assignment\' relationship_type and push relationship' do
62
+ assignment.push_relationship_to_global_registry
63
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
64
+ expect(assignment.global_registry_id).to eq '51a014a4-4252-11e7-944f-129bd0521531'
65
+ end
66
+ end
67
+
68
+ context '\'assignment\' relationship_type partially exists' do
69
+ around { |example| travel_to Time.utc(2001, 2, 3), &example }
70
+ let!(:sub_requests) do
71
+ [stub_request(:get, 'https://backend.global-registry.org/relationship_types')
72
+ .with(query: { 'filters[between]' =>
73
+ 'ee13a693-3ce7-4c19-b59a-30c8f137acd8,025a1128-3f33-11e7-b876-129bd0521531' })
74
+ .to_return(body: file_fixture('get_relationship_types_person_fancy_org_partial.json'), status: 200),
75
+ stub_request(:put,
76
+ 'https://backend.global-registry.org/relationship_types/5d721db8-4248-11e7-90b4-129bd0521531')
77
+ .with(body: { relationship_type: { fields: [{ name: 'role', field_type: 'string' }] } })
78
+ .to_return(body: file_fixture('put_relationship_types_fields.json'), status: 200)]
79
+ end
80
+
81
+ it 'should add fields to relationship type and push relationship' do
82
+ assignment.push_relationship_to_global_registry
83
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
84
+ expect(assignment.global_registry_id).to eq '51a014a4-4252-11e7-944f-129bd0521531'
85
+ end
86
+ end
87
+
88
+ context '\'assignment\' relationship_type exists' do
89
+ around { |example| travel_to Time.utc(2001, 2, 3), &example }
90
+ let!(:sub_requests) do
91
+ [stub_request(:get, 'https://backend.global-registry.org/relationship_types')
92
+ .with(query: { 'filters[between]' =>
93
+ 'ee13a693-3ce7-4c19-b59a-30c8f137acd8,025a1128-3f33-11e7-b876-129bd0521531' })
94
+ .to_return(body: file_fixture('get_relationship_types_person_fancy_org.json'), status: 200)]
95
+ end
96
+
97
+ it 'should add fields to relationship type and push relationship' do
98
+ assignment.push_relationship_to_global_registry
99
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
100
+ expect(assignment.global_registry_id).to eq '51a014a4-4252-11e7-944f-129bd0521531'
101
+ end
102
+ end
103
+ end
104
+
105
+ context '#update' do
106
+ let(:person) { create(:person, global_registry_id: '22527d88-3cba-11e7-b876-129bd0521531') }
107
+ let(:organization) { create(:organization, gr_id: 'aebb4170-3f34-11e7-bba6-129bd0521531') }
108
+ let(:assignment) do
109
+ create(:assignment, person: person, organization: organization,
110
+ global_registry_id: '51a014a4-4252-11e7-944f-129bd0521531')
111
+ end
112
+
113
+ context '\'assignment\' relationship_type is cached' do
114
+ around { |example| travel_to Time.utc(2001, 2, 3), &example }
115
+ before :each do
116
+ person_entity_types = JSON.parse(file_fixture('get_entity_types_person.json').read)
117
+ organization_entity_types = JSON.parse(file_fixture('get_entity_types_fancy_org.json').read)
118
+ assignment_relationship_type = JSON.parse(file_fixture('get_relationship_types_person_fancy_org.json').read)
119
+ Rails.cache.write('GlobalRegistry::Bindings::EntityType::person', person_entity_types['entity_types'].first)
120
+ Rails.cache.write('GlobalRegistry::Bindings::EntityType::fancy_org',
121
+ organization_entity_types['entity_types'].first)
122
+ Rails.cache.write('GlobalRegistry::Bindings::RelationshipType::person::fancy_org::person',
123
+ assignment_relationship_type['relationship_types'].first)
124
+ end
125
+
126
+ it 'should push relationship' do
127
+ request = stub_request(:put,
128
+ 'https://backend.global-registry.org/entities/51a014a4-4252-11e7-944f-129bd0521531')
129
+ .with(body: { entity: { role: 'leader', hired_at: '2000-12-03 00:00:00',
130
+ client_integration_id: assignment.id,
131
+ client_updated_at: '2001-02-03 00:00:00' } })
132
+ .to_return(body: file_fixture('put_entities_relationship.json'), status: 200)
133
+
134
+ assignment.push_relationship_to_global_registry
135
+ expect(request).to have_been_requested.once
136
+ expect(assignment.global_registry_id).to eq '51a014a4-4252-11e7-944f-129bd0521531'
137
+ end
138
+ end
139
+ end
140
+
141
+ context 'related entities missing global_registry_id' do
142
+ context '\'person\' missing global_registry_id' do
143
+ let(:person) { build(:person) }
144
+ let(:organization) { build(:organization, gr_id: 'aebb4170-3f34-11e7-bba6-129bd0521531') }
145
+ let!(:assignment) { create(:assignment, person: person, organization: organization) }
146
+
147
+ it 'should raise an exception' do
148
+ # Drop sidekiq-unique-jobs locks
149
+ MOCK_REDIS.keys.each do |key|
150
+ MOCK_REDIS.del(key)
151
+ end
152
+
153
+ expect do
154
+ assignment.push_relationship_to_global_registry
155
+ end.to raise_error(GlobalRegistry::Bindings::RelatedEntityMissingGlobalRegistryId).and(
156
+ change(GlobalRegistry::Bindings::Workers::PushGrEntityWorker.jobs, :size).by(1)
157
+ )
158
+ end
159
+ end
160
+
161
+ context '\'organization\' missing global_registry_id' do
162
+ let(:person) { build(:person, global_registry_id: '22527d88-3cba-11e7-b876-129bd0521531') }
163
+ let(:organization) { build(:organization) }
164
+ let!(:assignment) { create(:assignment, person: person, organization: organization) }
165
+
166
+ it 'should raise an exception' do
167
+ # Drop sidekiq-unique-jobs locks
168
+ MOCK_REDIS.keys.each do |key|
169
+ MOCK_REDIS.del(key)
170
+ end
171
+
172
+ expect do
173
+ assignment.push_relationship_to_global_registry
174
+ end.to raise_error(GlobalRegistry::Bindings::RelatedEntityMissingGlobalRegistryId).and(
175
+ change(GlobalRegistry::Bindings::Workers::PushGrEntityWorker.jobs, :size).by(1)
176
+ )
177
+ end
178
+ end
179
+
180
+ context '\'person\' and \'organization\' missing global_registry_id' do
181
+ let(:person) { build(:person) }
182
+ let(:organization) { build(:organization) }
183
+ let!(:assignment) { create(:assignment, person: person, organization: organization) }
184
+
185
+ it 'should raise an exception' do
186
+ # Drop sidekiq-unique-jobs locks
187
+ MOCK_REDIS.keys.each do |key|
188
+ MOCK_REDIS.del(key)
189
+ end
190
+
191
+ expect do
192
+ assignment.push_relationship_to_global_registry
193
+ end.to raise_error(GlobalRegistry::Bindings::RelatedEntityMissingGlobalRegistryId).and(
194
+ change(GlobalRegistry::Bindings::Workers::PushGrEntityWorker.jobs, :size).by(2)
195
+ )
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe 'Organization' do
6
+ describe ':push_entity_to_global_registry_async' do
7
+ it 'should enqueue sidekiq job' do
8
+ org = build(:organization)
9
+ expect do
10
+ org.push_entity_to_global_registry_async
11
+ end.to change(GlobalRegistry::Bindings::Workers::PushGrEntityWorker.jobs, :size).by(1)
12
+ end
13
+ end
14
+
15
+ describe ':delete_entity_from_global_registry_async' do
16
+ it 'should enqueue sidekiq job' do
17
+ organization = build(:organization, gr_id: '22527d88-3cba-11e7-b876-129bd0521531')
18
+ expect do
19
+ organization.delete_entity_from_global_registry_async
20
+ end.to change(GlobalRegistry::Bindings::Workers::DeleteGrEntityWorker.jobs, :size).by(1)
21
+ end
22
+
23
+ it 'should not enqueue sidekiq job when missing global_registry_id' do
24
+ organization = build(:organization)
25
+ expect do
26
+ organization.delete_entity_from_global_registry_async
27
+ end.not_to change(GlobalRegistry::Bindings::Workers::DeleteGrEntityWorker.jobs, :size)
28
+ end
29
+ end
30
+
31
+ describe ':push_entity_to_global_registry' do
32
+ around { |example| travel_to Time.utc(2001, 2, 3), &example }
33
+ context 'create' do
34
+ context '\'fancy_org\' entity_type does not exist' do
35
+ let!(:requests) do
36
+ [stub_request(:get, 'https://backend.global-registry.org/entity_types')
37
+ .with(query: { 'filters[name]' => 'fancy_org', 'filters[parent_id]' => nil })
38
+ .to_return(body: file_fixture('get_entity_types.json'), status: 200),
39
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
40
+ .with(body: { entity_type: { name: 'fancy_org', parent_id: nil, field_type: 'entity' } })
41
+ .to_return(body: file_fixture('post_entity_types_fancy_org.json'), status: 200),
42
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
43
+ .with(body: { entity_type: { name: 'name', parent_id: '025a1128-3f33-11e7-b876-129bd0521531',
44
+ field_type: 'string' } })
45
+ .to_return(status: 200),
46
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
47
+ .with(body: { entity_type: { name: 'description', parent_id: '025a1128-3f33-11e7-b876-129bd0521531',
48
+ field_type: 'string' } })
49
+ .to_return(status: 200),
50
+ stub_request(:post, 'https://backend.global-registry.org/entity_types')
51
+ .with(body: { entity_type: { name: 'start_date', parent_id: '025a1128-3f33-11e7-b876-129bd0521531',
52
+ field_type: 'date' } })
53
+ .to_return(status: 200)]
54
+ end
55
+
56
+ context 'orphan record' do
57
+ let(:organization) { create(:organization) }
58
+ let!(:sub_requests) do
59
+ [stub_request(:post, 'https://backend.global-registry.org/entities')
60
+ .with(body: { entity: { fancy_org: { name: 'Organization', description: 'Fancy Organization',
61
+ start_date: '2001-02-03', parent_id: nil,
62
+ client_integration_id: organization.id,
63
+ client_updated_at: '2001-02-03 00:00:00' } } })
64
+ .to_return(body: file_fixture('post_entities_fancy_org.json'), status: 200)]
65
+ end
66
+
67
+ it 'should create \'fancy_org\' entity_type and push entity' do
68
+ organization.push_entity_to_global_registry
69
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
70
+ expect(organization.gr_id).to eq 'aebb4170-3f34-11e7-bba6-129bd0521531'
71
+ end
72
+ end
73
+
74
+ context 'record with a parent' do
75
+ let(:parent) { create(:organization, name: 'Parent', description: 'Parent Fancy Organization') }
76
+ let(:organization) { create(:organization, parent: parent) }
77
+ let!(:sub_requests) do
78
+ [stub_request(:post, 'https://backend.global-registry.org/entities')
79
+ .with(body: { entity: { fancy_org: { name: 'Parent', description: 'Parent Fancy Organization',
80
+ start_date: '2001-02-03', parent_id: nil,
81
+ client_integration_id: parent.id,
82
+ client_updated_at: '2001-02-03 00:00:00' } } })
83
+ .to_return(body: file_fixture('post_entities_fancy_org_parent.json'), status: 200),
84
+ stub_request(:post, 'https://backend.global-registry.org/entities')
85
+ .with(body: { entity: { fancy_org: { name: 'Organization', description: 'Fancy Organization',
86
+ start_date: '2001-02-03',
87
+ parent_id: 'cd5da38a-c336-46a7-b818-dcdd51c4acde',
88
+ client_integration_id: organization.id,
89
+ client_updated_at: '2001-02-03 00:00:00' } } })
90
+ .to_return(body: file_fixture('post_entities_fancy_org.json'), status: 200)]
91
+ end
92
+
93
+ it 'should create \'fancy_org\' entity_type and push both entities' do
94
+ organization.push_entity_to_global_registry
95
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
96
+ expect(parent.gr_id).to eq 'cd5da38a-c336-46a7-b818-dcdd51c4acde'
97
+ expect(organization.gr_id).to eq 'aebb4170-3f34-11e7-bba6-129bd0521531'
98
+ end
99
+ end
100
+
101
+ context 'record with an exiting parent' do
102
+ let(:parent) do
103
+ create :organization, name: 'Parent', description: 'Parent Fancy Organization',
104
+ gr_id: 'cd5da38a-c336-46a7-b818-dcdd51c4acde'
105
+ end
106
+ let(:organization) { create(:organization, parent: parent) }
107
+ let!(:sub_requests) do
108
+ [stub_request(:post, 'https://backend.global-registry.org/entities')
109
+ .with(body: { entity: { fancy_org: { name: 'Organization', description: 'Fancy Organization',
110
+ start_date: '2001-02-03',
111
+ parent_id: 'cd5da38a-c336-46a7-b818-dcdd51c4acde',
112
+ client_integration_id: organization.id,
113
+ client_updated_at: '2001-02-03 00:00:00' } } })
114
+ .to_return(body: file_fixture('post_entities_fancy_org.json'), status: 200)]
115
+ end
116
+
117
+ it 'should create \'fancy_org\' entity_type and push both entities' do
118
+ organization.push_entity_to_global_registry
119
+ (requests + sub_requests).each { |r| expect(r).to have_been_requested.once }
120
+ expect(parent.gr_id).to eq 'cd5da38a-c336-46a7-b818-dcdd51c4acde'
121
+ expect(organization.gr_id).to eq 'aebb4170-3f34-11e7-bba6-129bd0521531'
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end