maestrano-connector-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +1 -0
  3. data/DEVELOPER.md +10 -0
  4. data/Gemfile +18 -0
  5. data/Gemfile.lock +222 -0
  6. data/LICENSE +21 -0
  7. data/README.md +138 -0
  8. data/Rakefile +37 -0
  9. data/VERSION +1 -0
  10. data/app/controllers/maestrano/account/group_users_controller.rb +20 -0
  11. data/app/controllers/maestrano/account/groups_controller.rb +22 -0
  12. data/app/controllers/maestrano/application_controller.rb +7 -0
  13. data/app/controllers/maestrano/auth/saml_controller.rb +31 -0
  14. data/app/controllers/maestrano/sessions_controller.rb +15 -0
  15. data/app/helpers/maestrano/connector/rails/session_helper.rb +17 -0
  16. data/app/jobs/maestrano/connector/rails/synchronization_job.rb +49 -0
  17. data/app/models/maestrano/connector/rails/complex_entity.rb +175 -0
  18. data/app/models/maestrano/connector/rails/concerns/entity.rb +231 -0
  19. data/app/models/maestrano/connector/rails/concerns/external.rb +13 -0
  20. data/app/models/maestrano/connector/rails/connector_logger.rb +9 -0
  21. data/app/models/maestrano/connector/rails/entity.rb +5 -0
  22. data/app/models/maestrano/connector/rails/external.rb +5 -0
  23. data/app/models/maestrano/connector/rails/id_map.rb +6 -0
  24. data/app/models/maestrano/connector/rails/organization.rb +60 -0
  25. data/app/models/maestrano/connector/rails/sub_entity_base.rb +32 -0
  26. data/app/models/maestrano/connector/rails/synchronization.rb +38 -0
  27. data/app/models/maestrano/connector/rails/user.rb +25 -0
  28. data/app/models/maestrano/connector/rails/user_organization_rel.rb +9 -0
  29. data/bin/rails +12 -0
  30. data/config/routes.rb +28 -0
  31. data/db/migrate/20151122162100_create_maestrano_connector_rails_users.rb +16 -0
  32. data/db/migrate/20151122162414_create_maestrano_connector_rails_organizations.rb +21 -0
  33. data/db/migrate/20151122162613_create_maestrano_connector_rails_user_organization_rels.rb +12 -0
  34. data/db/migrate/20151122163325_create_maestrano_connector_rails_synchronizations.rb +13 -0
  35. data/db/migrate/20151122163449_create_maestrano_connector_rails_id_maps.rb +18 -0
  36. data/lib/generators/connector/USAGE +2 -0
  37. data/lib/generators/connector/complex_entity_generator.rb +19 -0
  38. data/lib/generators/connector/install_generator.rb +70 -0
  39. data/lib/generators/connector/templates/admin_controller.rb +48 -0
  40. data/lib/generators/connector/templates/admin_index.html.erb +51 -0
  41. data/lib/generators/connector/templates/complex_entity_example/contact.rb +22 -0
  42. data/lib/generators/connector/templates/complex_entity_example/contact_and_lead.rb +56 -0
  43. data/lib/generators/connector/templates/complex_entity_example/contact_mapper.rb +17 -0
  44. data/lib/generators/connector/templates/complex_entity_example/lead.rb +22 -0
  45. data/lib/generators/connector/templates/complex_entity_example/lead_mapper.rb +15 -0
  46. data/lib/generators/connector/templates/complex_entity_example/person.rb +23 -0
  47. data/lib/generators/connector/templates/contact_and_lead.rb +55 -0
  48. data/lib/generators/connector/templates/entity.rb +48 -0
  49. data/lib/generators/connector/templates/example_entity.rb +26 -0
  50. data/lib/generators/connector/templates/external.rb +19 -0
  51. data/lib/generators/connector/templates/home_controller.rb +17 -0
  52. data/lib/generators/connector/templates/home_index.html.erb +42 -0
  53. data/lib/generators/connector/templates/oauth_controller.rb +45 -0
  54. data/lib/maestrano-connector-rails.rb +1 -0
  55. data/lib/maestrano/connector/rails.rb +13 -0
  56. data/maestrano-connector-rails.gemspec +197 -0
  57. data/maestrano.png +0 -0
  58. data/spec/dummy/README.md +1 -0
  59. data/spec/dummy/Rakefile +6 -0
  60. data/spec/dummy/app/assets/images/.keep +0 -0
  61. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  62. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  63. data/spec/dummy/app/controllers/admin_controller.rb +48 -0
  64. data/spec/dummy/app/controllers/application_controller.rb +8 -0
  65. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  66. data/spec/dummy/app/controllers/home_controller.rb +17 -0
  67. data/spec/dummy/app/controllers/oauth_controller.rb +45 -0
  68. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  69. data/spec/dummy/app/mailers/.keep +0 -0
  70. data/spec/dummy/app/models/.keep +0 -0
  71. data/spec/dummy/app/models/concerns/.keep +0 -0
  72. data/spec/dummy/app/models/entities/example_entitiy.rb +26 -0
  73. data/spec/dummy/app/models/maestrano/connector/rails/entity.rb +10 -0
  74. data/spec/dummy/app/models/maestrano/connector/rails/external.rb +11 -0
  75. data/spec/dummy/app/views/admin/index.html.erb +51 -0
  76. data/spec/dummy/app/views/home/index.html.erb +36 -0
  77. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  78. data/spec/dummy/bin/bundle +3 -0
  79. data/spec/dummy/bin/rails +4 -0
  80. data/spec/dummy/bin/rake +4 -0
  81. data/spec/dummy/bin/setup +29 -0
  82. data/spec/dummy/config.ru +4 -0
  83. data/spec/dummy/config/application.rb +26 -0
  84. data/spec/dummy/config/boot.rb +5 -0
  85. data/spec/dummy/config/database.yml +25 -0
  86. data/spec/dummy/config/environment.rb +5 -0
  87. data/spec/dummy/config/environments/development.rb +41 -0
  88. data/spec/dummy/config/environments/production.rb +79 -0
  89. data/spec/dummy/config/environments/test.rb +42 -0
  90. data/spec/dummy/config/initializers/assets.rb +11 -0
  91. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  92. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  93. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  94. data/spec/dummy/config/initializers/inflections.rb +16 -0
  95. data/spec/dummy/config/initializers/maestrano.rb +135 -0
  96. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  97. data/spec/dummy/config/initializers/session_store.rb +3 -0
  98. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  99. data/spec/dummy/config/locales/en.yml +23 -0
  100. data/spec/dummy/config/routes.rb +9 -0
  101. data/spec/dummy/config/secrets.yml +22 -0
  102. data/spec/dummy/db/development.sqlite3 +0 -0
  103. data/spec/dummy/db/migrate/20151209134445_create_maestrano_connector_rails_users.maestrano_connector_rails.rb +17 -0
  104. data/spec/dummy/db/migrate/20151209134446_create_maestrano_connector_rails_organizations.maestrano_connector_rails.rb +22 -0
  105. data/spec/dummy/db/migrate/20151209134447_create_maestrano_connector_rails_user_organization_rels.maestrano_connector_rails.rb +13 -0
  106. data/spec/dummy/db/migrate/20151209134448_create_maestrano_connector_rails_synchronizations.maestrano_connector_rails.rb +14 -0
  107. data/spec/dummy/db/migrate/20151209134449_create_maestrano_connector_rails_id_maps.maestrano_connector_rails.rb +19 -0
  108. data/spec/dummy/db/schema.rb +83 -0
  109. data/spec/dummy/lib/assets/.keep +0 -0
  110. data/spec/dummy/log/.keep +0 -0
  111. data/spec/dummy/public/404.html +67 -0
  112. data/spec/dummy/public/422.html +67 -0
  113. data/spec/dummy/public/500.html +66 -0
  114. data/spec/dummy/public/favicon.ico +0 -0
  115. data/spec/factories.rb +33 -0
  116. data/spec/jobs/syncrhonization_job_spec.rb +53 -0
  117. data/spec/models/complex_entity_spec.rb +353 -0
  118. data/spec/models/connector_logger_spec.rb +21 -0
  119. data/spec/models/entity_spec.rb +457 -0
  120. data/spec/models/external_spec.rb +15 -0
  121. data/spec/models/id_map_spec.rb +12 -0
  122. data/spec/models/organizaztion_spec.rb +105 -0
  123. data/spec/models/sub_entity_base_spec.rb +51 -0
  124. data/spec/models/synchronization_spec.rb +75 -0
  125. data/spec/models/user_organization_rel_spec.rb +14 -0
  126. data/spec/models/user_spec.rb +15 -0
  127. data/spec/spec_helper.rb +19 -0
  128. metadata +327 -0
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Maestrano::Connector::Rails::ConnectorLogger do
4
+ subject { Maestrano::Connector::Rails::ConnectorLogger }
5
+
6
+ describe 'self.log' do
7
+ let(:organization) { create(:organization) }
8
+
9
+ it 'calls rails.logger' do
10
+ expect(Rails.logger).to receive(:info)
11
+ subject.log('info', organization, 'msg')
12
+ end
13
+
14
+ it 'includes the organization uid and tenant' do
15
+ expect(organization).to receive(:uid)
16
+ expect(organization).to receive(:tenant)
17
+ subject.log('info', organization, 'msg')
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,457 @@
1
+ require 'spec_helper'
2
+
3
+ describe Maestrano::Connector::Rails::Entity do
4
+
5
+ describe 'class methods' do
6
+ subject { Maestrano::Connector::Rails::Entity }
7
+
8
+ describe 'entities_list' do
9
+ it { expect(subject.entities_list).to eql(%w(entity1 entity2))}
10
+ end
11
+ end
12
+
13
+ describe 'instance methods' do
14
+ subject { Maestrano::Connector::Rails::Entity.new }
15
+
16
+ describe 'Mapper methods' do
17
+ before(:each) {
18
+ class AMapper
19
+ extend HashMapper
20
+ def self.set_organization(organization_id)
21
+ end
22
+ end
23
+ allow(subject).to receive(:mapper_class).and_return(AMapper)
24
+ }
25
+
26
+ describe 'map_to_external' do
27
+ it 'calls the setter normalize' do
28
+ expect(AMapper).to receive(:normalize).with({})
29
+ subject.map_to_external({}, nil)
30
+ end
31
+ end
32
+
33
+ describe 'map_to_connec' do
34
+ it 'calls the setter denormalize' do
35
+ expect(AMapper).to receive(:denormalize).with({})
36
+ subject.map_to_connec({}, nil)
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+ # Connec! methods
43
+ describe 'connec_methods' do
44
+ let(:organization) { create(:organization) }
45
+ let(:client) { Maestrano::Connec::Client.new(organization.uid) }
46
+ let(:connec_name) { 'person' }
47
+ let(:external_name) { 'external_name' }
48
+ let(:sync) { create(:synchronization) }
49
+ before {
50
+ allow(subject).to receive(:connec_entity_name).and_return(connec_name)
51
+ }
52
+
53
+ describe 'get_connec_entities' do
54
+
55
+ describe 'with response' do
56
+ before {
57
+ allow(client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {people: []}.to_json, {}))
58
+ }
59
+
60
+ context 'when opts[:full_sync] is true' do
61
+ it 'performs a full get' do
62
+ expect(client).to receive(:get).with("/#{connec_name.downcase.pluralize}")
63
+ subject.get_connec_entities(client, nil, organization, {full_sync: true})
64
+ end
65
+ end
66
+
67
+ context 'when there is no last sync' do
68
+ it 'performs a full get' do
69
+ expect(client).to receive(:get).with("/#{connec_name.downcase.pluralize}")
70
+ subject.get_connec_entities(client, nil, organization)
71
+ end
72
+ end
73
+
74
+ context 'when there is a last sync' do
75
+ it 'performs a time limited get' do
76
+ uri_param = URI.encode("$filter=updated_at gt '#{sync.updated_at.iso8601}'")
77
+ expect(client).to receive(:get).with("/#{connec_name.downcase.pluralize}?#{uri_param}")
78
+ subject.get_connec_entities(client, sync, organization)
79
+ end
80
+ end
81
+
82
+ context 'with pagination' do
83
+ before {
84
+ allow(client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {people: [], pagination: {next: "https://api-connec.maestrano.com/api/v2/cld-dkg601/people?%24skip=10&%24top=10"}}.to_json, {}), ActionDispatch::Response.new(200, {}, {people: []}.to_json, {}))
85
+ }
86
+
87
+ it 'calls get multiple times' do
88
+ expect(client).to receive(:get).twice
89
+ subject.get_connec_entities(client, nil, organization)
90
+ end
91
+ end
92
+
93
+ context 'with an actual response' do
94
+ let(:people) { [{first_name: 'John'}, {last_name: 'Durand'}, {job_title: 'Engineer'}] }
95
+
96
+ it 'returns an array of entities' do
97
+ allow(client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {people: people}.to_json, {}))
98
+ expect(subject.get_connec_entities(client, nil, organization)).to eql(JSON.parse(people.to_json))
99
+ end
100
+ end
101
+ end
102
+
103
+ describe 'without response' do
104
+ before {
105
+ allow(client).to receive(:get).and_return(nil)
106
+ }
107
+ it { expect{ subject.get_connec_entities(client, nil, organization) }.to raise_error("No data received from Connec! when trying to fetch #{connec_name.pluralize}") }
108
+ end
109
+ end
110
+
111
+ describe 'push_entities_to_connec' do
112
+ it 'calls push_entities_to_connec_to' do
113
+ allow(subject).to receive(:connec_entity_name).and_return(connec_name)
114
+ expect(subject).to receive(:push_entities_to_connec_to).with(client, [{entity: {}, idmap: nil}], connec_name, nil)
115
+ subject.push_entities_to_connec(client, [{entity: {}, idmap: nil}], nil)
116
+ end
117
+ end
118
+
119
+ describe 'push_entities_to_connec_to' do
120
+ let(:organization) { create(:organization) }
121
+ let(:idmap1) { create(:idmap, organization: organization) }
122
+ let(:idmap2) { create(:idmap, organization: organization, connec_id: nil, connec_entity: nil, last_push_to_connec: nil) }
123
+ let(:entity1) { {name: 'John'} }
124
+ let(:entity2) { {name: 'Jane'} }
125
+ let(:entity_with_idmap1) { {entity: entity1, idmap: idmap1} }
126
+ let(:entity_with_idmap2) { {entity: entity2, idmap: idmap2} }
127
+ let(:entities_with_idmaps) { [entity_with_idmap1, entity_with_idmap2] }
128
+ let(:id) { 'ab12-34re' }
129
+
130
+ it 'create or update the entities and idmaps according to their idmap state' do
131
+ allow(subject).to receive(:create_entity_to_connec).and_return({'id' => id})
132
+ allow(subject).to receive(:external_entity_name).and_return(external_name)
133
+
134
+ expect(subject).to receive(:create_entity_to_connec).with(client, entity2, connec_name, organization)
135
+ expect(subject).to receive(:update_entity_to_connec).with(client, entity1, idmap1.connec_id, connec_name, organization)
136
+ old_push_date = idmap1.last_push_to_connec
137
+
138
+ subject.push_entities_to_connec_to(client, entities_with_idmaps, connec_name, organization)
139
+
140
+ idmap1.reload
141
+ expect(idmap1.last_push_to_connec).to_not eql(old_push_date)
142
+ idmap2.reload
143
+ expect(idmap2.connec_id).to eql(id)
144
+ expect(idmap2.connec_entity).to eql(connec_name)
145
+ expect(idmap2.last_push_to_connec).to_not be_nil
146
+ end
147
+ end
148
+
149
+ describe 'create_entity_to_connec' do
150
+ let(:entity) { {name: 'John'} }
151
+
152
+ describe 'with a response' do
153
+ before {
154
+ allow(client).to receive(:post).and_return(ActionDispatch::Response.new(200, {}, {people: entity}.to_json, {}))
155
+ }
156
+
157
+ it 'sends a post to connec' do
158
+ expect(client).to receive(:post).with("/#{connec_name.pluralize}", {"#{connec_name.pluralize}".to_sym => entity})
159
+ subject.create_entity_to_connec(client, entity, connec_name, organization)
160
+ end
161
+
162
+ it 'returns the created entity' do
163
+ expect(subject.create_entity_to_connec(client, entity, connec_name, organization)).to eql(JSON.parse(entity.to_json))
164
+ end
165
+ end
166
+
167
+ describe 'without response' do
168
+ before {
169
+ allow(client).to receive(:post).and_return(nil)
170
+ }
171
+ it { expect{ subject.create_entity_to_connec(client, entity, connec_name, organization) }.to raise_error("No response received from Connec! when trying to create a #{connec_name}") }
172
+ end
173
+ end
174
+
175
+ describe 'update_entity_to_connec' do
176
+ let(:organization) { create(:organization) }
177
+ let(:entity) { {name: 'John'} }
178
+ let(:id) { '88ye-777ab' }
179
+
180
+ describe 'with a response' do
181
+ before {
182
+ allow(client).to receive(:put).and_return(ActionDispatch::Response.new(200, {}, {}.to_json, {}))
183
+ }
184
+
185
+ it 'sends a put to connec' do
186
+ expect(client).to receive(:put).with("/#{connec_name.pluralize}/#{id}", {"#{connec_name.pluralize}".to_sym => entity})
187
+ subject.update_entity_to_connec(client, entity, id, connec_name, organization)
188
+ end
189
+ end
190
+
191
+ describe 'without response' do
192
+ before {
193
+ allow(client).to receive(:put).and_return(nil)
194
+ }
195
+ it { expect{ subject.update_entity_to_connec(client, entity, id, connec_name, organization) }.to raise_error("No response received from Connec! when trying to update a #{connec_name}") }
196
+ end
197
+ end
198
+
199
+ describe 'map_to_external_with_idmap' do
200
+ let(:organization) { create(:organization) }
201
+ let(:id) { '765e-zer4' }
202
+ let(:mapped_entity) { {'first_name' => 'John'} }
203
+ before {
204
+ allow(subject).to receive(:connec_entity_name).and_return(connec_name)
205
+ allow(subject).to receive(:map_to_external).and_return(mapped_entity)
206
+ }
207
+
208
+ context 'when entity has an idmap' do
209
+ let!(:idmap) { create(:idmap, organization: organization, connec_entity: connec_name, connec_id: id, last_push_to_external: 3.hour.ago)}
210
+
211
+ context 'when updated_at field is most recent than idmap last_push_to_external' do
212
+ let(:entity) { {'id' => id, 'name' => 'John', 'updated_at' => 2.hour.ago } }
213
+
214
+ it 'returns the entity with its idmap' do
215
+ expect(subject.map_to_external_with_idmap(entity, organization)).to eql({entity: mapped_entity, idmap: idmap})
216
+ end
217
+ end
218
+
219
+ context 'when updated_at field is older than idmap last_push_to_external' do
220
+ let(:entity) { {'id' => id, 'name' => 'John', 'updated_at' => 5.hour.ago } }
221
+
222
+ it 'discards the entity' do
223
+ expect(subject.map_to_external_with_idmap(entity, organization)).to be_nil
224
+ end
225
+ end
226
+ end
227
+
228
+ context 'when entity has no idmap' do
229
+ let(:entity) { {'id' => id, 'name' => 'John', 'updated_at' => 5.hour.ago } }
230
+
231
+ it { expect{ subject.map_to_external_with_idmap(entity, organization) }.to change{Maestrano::Connector::Rails::IdMap.count}.by(1) }
232
+
233
+ it 'returns the entity with its new idmap' do
234
+ expect(subject.map_to_external_with_idmap(entity, organization)).to eql({entity: mapped_entity, idmap: Maestrano::Connector::Rails::IdMap.last})
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+
241
+ # External methods
242
+ describe 'external methods' do
243
+ let(:organization) { create(:organization) }
244
+ let(:connec_name) { 'connec_name' }
245
+ let(:external_name) { 'external_name' }
246
+ let(:idmap1) { create(:idmap, organization: organization) }
247
+ let(:idmap2) { create(:idmap, organization: organization, external_id: nil, external_entity: nil, last_push_to_external: nil) }
248
+ let(:entity1) { {name: 'John'} }
249
+ let(:entity2) { {name: 'Jane'} }
250
+ let(:entity_with_idmap1) { {entity: entity1, idmap: idmap1} }
251
+ let(:entity_with_idmap2) { {entity: entity2, idmap: idmap2} }
252
+ let(:entities_with_idmaps) { [entity_with_idmap1, entity_with_idmap2] }
253
+
254
+ describe 'get_external_entities' do
255
+ it { expect{ subject.get_external_entities(nil, nil, organization) }.to raise_error('Not implemented') }
256
+ end
257
+
258
+ describe 'push_entities_to_external' do
259
+ it 'calls push_entities_to_external_to' do
260
+ allow(subject).to receive(:external_entity_name).and_return(external_name)
261
+ expect(subject).to receive(:push_entities_to_external_to).with(nil, entities_with_idmaps, external_name, organization)
262
+ subject.push_entities_to_external(nil, entities_with_idmaps, organization)
263
+ end
264
+ end
265
+
266
+ describe 'push_entities_to_external_to' do
267
+ it 'calls push_entity_to_external for each entity' do
268
+ allow(subject).to receive(:connec_entity_name).and_return(connec_name)
269
+ expect(subject).to receive(:push_entity_to_external).twice
270
+ subject.push_entities_to_external_to(nil, entities_with_idmaps, external_name, organization)
271
+ end
272
+ end
273
+
274
+ describe 'push_entity_to_external' do
275
+ context 'when the entity idmap has an external id' do
276
+ it 'calls update_entity_to_external' do
277
+ expect(subject).to receive(:update_entity_to_external).with(nil, entity1, idmap1.external_id, external_name, organization)
278
+ subject.push_entity_to_external(nil, entity_with_idmap1, external_name, organization)
279
+ end
280
+
281
+ it 'updates the idmap last push to external' do
282
+ allow(subject).to receive(:update_entity_to_external)
283
+ time_before = idmap1.last_push_to_external
284
+ subject.push_entity_to_external(nil, entity_with_idmap1, external_name, organization)
285
+ idmap1.reload
286
+ expect(idmap1.last_push_to_external).to_not eql(time_before)
287
+ end
288
+ end
289
+
290
+ context 'when the entity idmap does not have an external id' do
291
+ it 'calls create_entity_to_external' do
292
+ expect(subject).to receive(:create_entity_to_external).with(nil, entity2, external_name, organization)
293
+ subject.push_entity_to_external(nil, entity_with_idmap2, external_name, organization)
294
+ end
295
+
296
+ it 'updates the idmap external id, entity and last push' do
297
+ allow(subject).to receive(:create_entity_to_external).and_return('999111')
298
+ subject.push_entity_to_external(nil, entity_with_idmap2, external_name, organization)
299
+ idmap2.reload
300
+ expect(idmap2.external_id).to eql('999111')
301
+ expect(idmap2.external_entity).to eql(external_name)
302
+ expect(idmap2.last_push_to_external).to_not be_nil
303
+ end
304
+ end
305
+ end
306
+
307
+ describe 'create_entity_to_external' do
308
+ let(:organization) { create(:organization) }
309
+
310
+ it { expect{ subject.create_entity_to_external(nil, nil, nil, organization) }.to raise_error('Not implemented') }
311
+ end
312
+
313
+ describe 'update_entity_to_external' do
314
+ let(:organization) { create(:organization) }
315
+
316
+ it { expect{ subject.update_entity_to_external(nil, nil, nil, nil, organization) }.to raise_error('Not implemented') }
317
+ end
318
+
319
+ describe 'get_id_from_external_entity_hash' do
320
+ it { expect{ subject.get_id_from_external_entity_hash(nil) }.to raise_error('Not implemented') }
321
+ end
322
+
323
+ describe 'get_last_update_date_from_external_entity_hash' do
324
+ it { expect{ subject.get_last_update_date_from_external_entity_hash(nil) }.to raise_error('Not implemented') }
325
+ end
326
+ end
327
+
328
+
329
+ # General methods
330
+ describe 'consolidate_and_map_data' do
331
+ # subject.consolidate_and_map_data(connec_entities, external_entities, organization, opts)
332
+ let(:organization) { create(:organization) }
333
+
334
+ describe 'connec_entities treatment' do
335
+ let(:entity1) { {name: 'John'} }
336
+ let(:entity2) { {name: 'Jane'} }
337
+
338
+ it 'calls map_to_external_with_idmap for each entity' do
339
+ expect(subject).to receive(:map_to_external_with_idmap).with(entity1, organization)
340
+ expect(subject).to receive(:map_to_external_with_idmap).with(entity2, organization)
341
+ subject.consolidate_and_map_data([entity1, entity2], [], organization)
342
+ end
343
+ end
344
+
345
+ describe 'external_entities treatment' do
346
+ let(:external_name) { 'external_name' }
347
+ let(:connec_name) { 'connec_name' }
348
+ let(:id) { '56882' }
349
+ let(:date) { 2.hour.ago }
350
+ let(:entity) { {id: id, name: 'John', modifiedDate: date} }
351
+ let(:mapped_entity) { {first_name: 'John'} }
352
+ let(:entities) { [entity] }
353
+
354
+ before{
355
+ allow(subject).to receive(:get_id_from_external_entity_hash).and_return(id)
356
+ allow(subject).to receive(:get_last_update_date_from_external_entity_hash).and_return(date)
357
+ allow(subject).to receive(:external_entity_name).and_return(external_name)
358
+ allow(subject).to receive(:connec_entity_name).and_return(connec_name)
359
+ allow(subject).to receive(:map_to_connec).and_return(mapped_entity)
360
+ }
361
+
362
+ context 'when entity has no idmap' do
363
+ it 'creates an idmap and returns the mapped entity with its new idmap' do
364
+ subject.consolidate_and_map_data([], entities, organization)
365
+ expect(entities).to eql([{entity: mapped_entity, idmap: Maestrano::Connector::Rails::IdMap.last}])
366
+ end
367
+ end
368
+
369
+ context 'when entity has an idmap with a last_push_to_connec more recent than date' do
370
+ let!(:idmap) { create(:idmap, external_entity: external_name, external_id: id, organization: organization, last_push_to_connec: 2.minute.ago) }
371
+
372
+ it 'discards the entity' do
373
+ subject.consolidate_and_map_data([], entities, organization)
374
+ expect(entities).to eql([])
375
+ end
376
+ end
377
+
378
+ context 'when entity has an idmap with a last_push_to_connec older than date' do
379
+
380
+ context 'with no conflict' do
381
+ let!(:idmap) { create(:idmap, external_entity: external_name, external_id: id, organization: organization, last_push_to_connec: 2.day.ago) }
382
+
383
+ it 'returns the mapped entity with its idmap' do
384
+ subject.consolidate_and_map_data([], entities, organization)
385
+ expect(entities).to eql([{entity: mapped_entity, idmap: idmap}])
386
+ end
387
+ end
388
+
389
+ context 'with conflict' do
390
+ let(:connec_id) { '34uuu-778aa' }
391
+ let!(:idmap) { create(:idmap, connec_id: connec_id, external_entity: external_name, external_id: id, organization: organization, last_push_to_connec: 2.day.ago) }
392
+ before {
393
+ allow(subject).to receive(:map_to_external_with_idmap)
394
+ }
395
+
396
+ context 'with connec_preemption opt' do
397
+
398
+ context 'set to true' do
399
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.day.ago} }
400
+ it 'discards the entity' do
401
+ subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: true})
402
+ expect(entities).to eql([])
403
+ end
404
+ end
405
+
406
+ context 'set to false' do
407
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.second.ago} }
408
+ it 'returns the mapped entity with its idmap' do
409
+ subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: false})
410
+ expect(entities).to eql([{entity: mapped_entity, idmap: idmap}])
411
+ end
412
+ end
413
+ end
414
+
415
+ context 'without opt' do
416
+ context 'with a more recent connec entity' do
417
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.second.ago} }
418
+
419
+ it 'discards the entity' do
420
+ subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: true})
421
+ expect(entities).to eql([])
422
+ end
423
+ end
424
+
425
+ context 'with a more recent external_entity' do
426
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.year.ago} }
427
+
428
+ it 'returns the mapped entity with its idmap' do
429
+ subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: false})
430
+ expect(entities).to eql([{entity: mapped_entity, idmap: idmap}])
431
+ end
432
+ end
433
+ end
434
+
435
+ end
436
+ end
437
+
438
+ end
439
+
440
+ end
441
+
442
+
443
+ # Entity specific methods
444
+ describe 'connec_entity_name' do
445
+ it { expect{ subject.connec_entity_name }.to raise_error('Not implemented') }
446
+ end
447
+
448
+ describe 'external_entity_name' do
449
+ it { expect{ subject.external_entity_name }.to raise_error('Not implemented') }
450
+ end
451
+
452
+ describe 'mapper_class' do
453
+ it { expect{ subject.mapper_class }.to raise_error('Not implemented') }
454
+ end
455
+ end
456
+
457
+ end