maestrano-connector-rails 0.2.16 → 0.2.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/Gemfile.lock +27 -0
  4. data/VERSION +1 -1
  5. data/app/helpers/maestrano/connector/rails/session_helper.rb +5 -1
  6. data/app/models/maestrano/connector/rails/complex_entity.rb +14 -33
  7. data/app/models/maestrano/connector/rails/concerns/entity.rb +161 -73
  8. data/app/models/maestrano/connector/rails/sub_entity_base.rb +44 -4
  9. data/lib/generators/connector/install_generator.rb +20 -24
  10. data/lib/generators/connector/templates/home_controller.rb +33 -10
  11. data/lib/generators/connector/templates/home_controller_spec.rb +141 -0
  12. data/lib/generators/connector/templates/home_index.haml +103 -0
  13. data/lib/generators/connector/templates/layouts.haml +45 -0
  14. data/lib/generators/connector/templates/oauth_controller.rb +3 -6
  15. data/lib/generators/connector/templates/shared_entities_controller.rb +7 -0
  16. data/lib/generators/connector/templates/shared_entities_controller_spec.rb +23 -0
  17. data/lib/generators/connector/templates/shared_entities_index.haml +41 -0
  18. data/lib/generators/connector/templates/stylesheets/application.sass +24 -0
  19. data/lib/generators/connector/templates/stylesheets/banners.sass +59 -0
  20. data/lib/generators/connector/templates/stylesheets/home.sass +25 -0
  21. data/lib/generators/connector/templates/stylesheets/layout.sass +125 -0
  22. data/lib/generators/connector/templates/stylesheets/spacers.sass +46 -0
  23. data/lib/generators/connector/templates/stylesheets/variables.sass +57 -0
  24. data/lib/generators/connector/templates/sychronizations_controller_spec.rb +22 -0
  25. data/lib/generators/connector/templates/synchronizations_controller.rb +7 -0
  26. data/lib/generators/connector/templates/synchronizations_index.haml +42 -0
  27. data/maestrano-connector-rails.gemspec +29 -6
  28. data/pkg/maestrano-connector-rails-0.2.16.gem +0 -0
  29. data/spec/models/complex_entity_spec.rb +46 -12
  30. data/spec/models/entity_spec.rb +212 -113
  31. data/spec/models/sub_entity_base_spec.rb +59 -0
  32. data/template/maestrano-connector-template.rb +4 -3
  33. data/template/routes.rb +14 -0
  34. metadata +61 -5
  35. data/lib/generators/connector/templates/admin_controller.rb +0 -58
  36. data/lib/generators/connector/templates/admin_index.html.erb +0 -55
  37. data/lib/generators/connector/templates/home_index.html.erb +0 -48
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe SynchronizationsController, :type => :controller do
4
+ describe 'index' do
5
+ subject { get :index }
6
+
7
+ it { expect(subject).to be_success }
8
+
9
+ context 'when user is logged in' do
10
+ let(:organization) { create(:organization) }
11
+ let(:synchronization) { create(:synchronization, organization: organization) }
12
+ before {
13
+ allow_any_instance_of(Maestrano::Connector::Rails::SessionHelper).to receive(:current_organization).and_return(organization)
14
+ }
15
+
16
+ it 'assigns the synchronizations' do
17
+ subject
18
+ expect(assigns(:synchronizations)).to eq([synchronization])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ class SynchronizationsController < ApplicationController
2
+ def index
3
+ if current_organization
4
+ @synchronizations = Maestrano::Connector::Rails::Synchronization.where(organization_id: current_organization.id).order(updated_at: :desc).limit(40)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,42 @@
1
+ .home
2
+ .banners
3
+ .container
4
+ - if current_user
5
+ .row
6
+ .col-md-3
7
+ %h3 Synchronization history
8
+ - if current_organization.oauth_uid && is_admin
9
+ .col-md-2
10
+ = link_to "Synchronize", home_synchronize_path, method: :post, class: "btn btn-warning btn-lg"
11
+
12
+ - if @synchronizations
13
+ %table.table.table-condensed
14
+ %th Synchronization date
15
+ %th Status
16
+ %th Message
17
+
18
+ - @synchronizations.each do |sync|
19
+ - if sync.status == 'ERROR'
20
+ %tr.warning
21
+ %td= sync.updated_at
22
+ %td= sync.status
23
+ %td= sync.message
24
+ - elsif sync.status == 'SUCCESS'
25
+ %tr.success
26
+ %td= sync.updated_at
27
+ %td= sync.status
28
+ %td= sync.message
29
+ - else
30
+ %tr.active
31
+ %td= sync.updated_at
32
+ %td= sync.status
33
+ %td= sync.message
34
+ - else
35
+ .row
36
+ .col-md-12
37
+ %strong No synchronization yet
38
+
39
+ - else
40
+ .row.center
41
+ %h3 You need to be logged in to access this page
42
+ = button_to "Go back", home_index_path, method: :get, class: 'btn btn-warning'
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: maestrano-connector-rails 0.2.16 ruby lib
5
+ # stub: maestrano-connector-rails 0.2.17 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "maestrano-connector-rails"
9
- s.version = "0.2.16"
9
+ s.version = "0.2.17"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Pierre Berard"]
14
- s.date = "2016-03-07"
14
+ s.date = "2016-03-08"
15
15
  s.description = "Maestrano is the next generation marketplace for SME applications. See https://maestrano.com for details."
16
16
  s.email = "pierre.berard@maestrano.com"
17
17
  s.executables = ["rails"]
@@ -62,8 +62,6 @@ Gem::Specification.new do |s|
62
62
  "lib/generators/connector/USAGE",
63
63
  "lib/generators/connector/complex_entity_generator.rb",
64
64
  "lib/generators/connector/install_generator.rb",
65
- "lib/generators/connector/templates/admin_controller.rb",
66
- "lib/generators/connector/templates/admin_index.html.erb",
67
65
  "lib/generators/connector/templates/complex_entity_example/contact.rb",
68
66
  "lib/generators/connector/templates/complex_entity_example/contact_and_lead.rb",
69
67
  "lib/generators/connector/templates/complex_entity_example/contact_mapper.rb",
@@ -75,13 +73,28 @@ Gem::Specification.new do |s|
75
73
  "lib/generators/connector/templates/example_entity.rb",
76
74
  "lib/generators/connector/templates/external.rb",
77
75
  "lib/generators/connector/templates/home_controller.rb",
78
- "lib/generators/connector/templates/home_index.html.erb",
76
+ "lib/generators/connector/templates/home_controller_spec.rb",
77
+ "lib/generators/connector/templates/home_index.haml",
78
+ "lib/generators/connector/templates/layouts.haml",
79
79
  "lib/generators/connector/templates/oauth_controller.rb",
80
+ "lib/generators/connector/templates/shared_entities_controller.rb",
81
+ "lib/generators/connector/templates/shared_entities_controller_spec.rb",
82
+ "lib/generators/connector/templates/shared_entities_index.haml",
83
+ "lib/generators/connector/templates/stylesheets/application.sass",
84
+ "lib/generators/connector/templates/stylesheets/banners.sass",
85
+ "lib/generators/connector/templates/stylesheets/home.sass",
86
+ "lib/generators/connector/templates/stylesheets/layout.sass",
87
+ "lib/generators/connector/templates/stylesheets/spacers.sass",
88
+ "lib/generators/connector/templates/stylesheets/variables.sass",
89
+ "lib/generators/connector/templates/sychronizations_controller_spec.rb",
90
+ "lib/generators/connector/templates/synchronizations_controller.rb",
91
+ "lib/generators/connector/templates/synchronizations_index.haml",
80
92
  "lib/maestrano-connector-rails.rb",
81
93
  "lib/maestrano/connector/rails.rb",
82
94
  "maestrano-connector-rails.gemspec",
83
95
  "maestrano.png",
84
96
  "pkg/maestrano-connector-rails-0.0.1.gem",
97
+ "pkg/maestrano-connector-rails-0.2.16.gem",
85
98
  "pkg/maestrano-connector-rails-0.2.4.gem",
86
99
  "spec/controllers/connec_controller_spec.rb",
87
100
  "spec/dummy/README.md",
@@ -156,6 +169,7 @@ Gem::Specification.new do |s|
156
169
  "template/factories.rb",
157
170
  "template/gitignore",
158
171
  "template/maestrano-connector-template.rb",
172
+ "template/routes.rb",
159
173
  "template/spec_helper.rb"
160
174
  ]
161
175
  s.homepage = "http://github.com/maestrano/maestrano-connector-rails"
@@ -170,6 +184,9 @@ Gem::Specification.new do |s|
170
184
  s.add_runtime_dependency(%q<maestrano-rails>, [">= 0"])
171
185
  s.add_runtime_dependency(%q<hash_mapper>, [">= 0"])
172
186
  s.add_runtime_dependency(%q<sidekiq>, [">= 0"])
187
+ s.add_runtime_dependency(%q<haml-rails>, [">= 0"])
188
+ s.add_runtime_dependency(%q<bootstrap-sass>, [">= 0"])
189
+ s.add_runtime_dependency(%q<autoprefixer-rails>, [">= 0"])
173
190
  s.add_development_dependency(%q<shoulda>, [">= 0"])
174
191
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
175
192
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
@@ -183,6 +200,9 @@ Gem::Specification.new do |s|
183
200
  s.add_dependency(%q<maestrano-rails>, [">= 0"])
184
201
  s.add_dependency(%q<hash_mapper>, [">= 0"])
185
202
  s.add_dependency(%q<sidekiq>, [">= 0"])
203
+ s.add_dependency(%q<haml-rails>, [">= 0"])
204
+ s.add_dependency(%q<bootstrap-sass>, [">= 0"])
205
+ s.add_dependency(%q<autoprefixer-rails>, [">= 0"])
186
206
  s.add_dependency(%q<shoulda>, [">= 0"])
187
207
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
188
208
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -197,6 +217,9 @@ Gem::Specification.new do |s|
197
217
  s.add_dependency(%q<maestrano-rails>, [">= 0"])
198
218
  s.add_dependency(%q<hash_mapper>, [">= 0"])
199
219
  s.add_dependency(%q<sidekiq>, [">= 0"])
220
+ s.add_dependency(%q<haml-rails>, [">= 0"])
221
+ s.add_dependency(%q<bootstrap-sass>, [">= 0"])
222
+ s.add_dependency(%q<autoprefixer-rails>, [">= 0"])
200
223
  s.add_dependency(%q<shoulda>, [">= 0"])
201
224
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
202
225
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -31,26 +31,28 @@ describe Maestrano::Connector::Rails::ComplexEntity do
31
31
  let(:sub_instance) { Maestrano::Connector::Rails::SubEntityBase.new }
32
32
  let(:human_name) { 'ET' }
33
33
  before {
34
+ allow(sub_instance).to receive(:external?).and_return(false)
35
+ allow(sub_instance).to receive(:entity_name).and_return(connec_name)
34
36
  allow(sub_instance).to receive(:map_to).and_return(mapped_entity)
35
37
  allow(sub_instance).to receive(:object_name_from_connec_entity_hash).and_return(human_name)
36
38
  }
37
39
 
38
40
  context 'when entity has no idmap' do
39
41
  it 'creates one' do
40
- expect{
41
- subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)
42
- }.to change{ Maestrano::Connector::Rails::IdMap.count }.by(1)
42
+ expect {
43
+ subject.map_to_external_with_idmap(entity, organization, external_name, sub_instance)
44
+ }.to change{ Maestrano::Connector::Rails::IdMap.count }.by(1)
43
45
  end
44
46
  end
45
47
  it 'returns the mapped entity with its idmap' do
46
- expect(subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: Maestrano::Connector::Rails::IdMap.last})
48
+ expect(subject.map_to_external_with_idmap(entity, organization, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: Maestrano::Connector::Rails::IdMap.last})
47
49
  end
48
50
 
49
51
  context 'when entity has an idmap without last_push_to_external' do
50
52
  let!(:idmap) { create(:idmap, organization: organization, connec_id: id, connec_entity: connec_name, last_push_to_external: nil, external_entity: external_name) }
51
53
 
52
54
  it 'returns the mapped entity with its idmap' do
53
- expect(subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: idmap})
55
+ expect(subject.map_to_external_with_idmap(entity, organization, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: idmap})
54
56
  end
55
57
  end
56
58
 
@@ -58,7 +60,7 @@ describe Maestrano::Connector::Rails::ComplexEntity do
58
60
  let!(:idmap) { create(:idmap, organization: organization, connec_id: id, connec_entity: connec_name, last_push_to_external: 1.year.ago, external_entity: external_name) }
59
61
 
60
62
  it 'returns the mapped entity with its idmap' do
61
- expect(subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: idmap})
63
+ expect(subject.map_to_external_with_idmap(entity, organization, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: idmap})
62
64
  end
63
65
  end
64
66
 
@@ -66,7 +68,15 @@ describe Maestrano::Connector::Rails::ComplexEntity do
66
68
  let!(:idmap) { create(:idmap, organization: organization, connec_id: id, connec_entity: connec_name, last_push_to_external: 1.second.ago, external_entity: external_name) }
67
69
 
68
70
  it 'discards the entity' do
69
- expect(subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)).to be_nil
71
+ expect(subject.map_to_external_with_idmap(entity, organization, external_name, sub_instance)).to be_nil
72
+ end
73
+ end
74
+
75
+ context 'when entity has an idmap with to_external set to false' do
76
+ let!(:idmap) { create(:idmap, organization: organization, connec_id: id, connec_entity: connec_name, to_external: false, external_entity: external_name) }
77
+
78
+ it 'discards the entity' do
79
+ expect(subject.map_to_external_with_idmap(entity, organization, external_name, sub_instance)).to be_nil
70
80
  end
71
81
  end
72
82
  end
@@ -177,9 +187,13 @@ describe Maestrano::Connector::Rails::ComplexEntity do
177
187
  before{
178
188
  allow(subject).to receive(:external_model_to_connec_model).and_return(external_hash)
179
189
  allow(subject).to receive(:connec_model_to_external_model).and_return(connec_hash)
190
+ allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:external?).and_return(true)
191
+ allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:entity_name).and_return('sc_e1')
180
192
  allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_id_from_external_entity_hash).with(entity1).and_return(id1)
181
193
  allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_last_update_date_from_external_entity_hash).and_return(1.minute.ago)
182
194
  allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:map_to).with('connec1', entity1, organization).and_return(mapped_entity1)
195
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:external?).and_return(true)
196
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:entity_name).and_return('Sce2')
183
197
  allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_id_from_external_entity_hash).with(entity1).and_return(id1)
184
198
  allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_id_from_external_entity_hash).with(entity2).and_return(id2)
185
199
  allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_last_update_date_from_external_entity_hash).and_return(1.minute.ago)
@@ -213,6 +227,24 @@ describe Maestrano::Connector::Rails::ComplexEntity do
213
227
  end
214
228
  end
215
229
 
230
+ context 'when entities have idmaps with to_connec set to false' do
231
+ let!(:idmap1) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sc_e1', connec_entity: 'connec1', to_connec: false) }
232
+ let!(:idmap21) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sce2', connec_entity: 'connec1', to_connec: false) }
233
+ let!(:idmap22) { create(:idmap, organization: organization, external_id: id2, external_entity: 'sce2', connec_entity: 'connec2', to_connec: false) }
234
+
235
+ it 'discards the entities' do
236
+ mapped_entities = subject.consolidate_and_map_data({}, external_hash, organization, opt)
237
+ expect(mapped_entities).to eql(external_entities: {
238
+ 'sc_e1' => {'connec1' => []},
239
+ 'ScE2' => {
240
+ 'connec1' => [],
241
+ 'connec2' => [],
242
+ }
243
+ },
244
+ connec_entities: {})
245
+ end
246
+ end
247
+
216
248
  context 'when entities have idmaps with more recent last_push_to_connec' do
217
249
  let!(:idmap1) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sc_e1', connec_entity: 'connec1', last_push_to_connec: 1.second.ago) }
218
250
  let!(:idmap21) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sce2', connec_entity: 'connec1', last_push_to_connec: 1.second.ago) }
@@ -238,6 +270,8 @@ describe Maestrano::Connector::Rails::ComplexEntity do
238
270
  class Entities::SubEntities::Connec2 < Maestrano::Connector::Rails::SubEntityBase
239
271
  end
240
272
  allow_any_instance_of(Entities::SubEntities::Connec1).to receive(:map_to).and_return({'name' => 'Jacob'})
273
+ allow_any_instance_of(Entities::SubEntities::Connec1).to receive(:external?).and_return(false)
274
+ allow_any_instance_of(Entities::SubEntities::Connec1).to receive(:entity_name).and_return('connec1')
241
275
  }
242
276
  let(:connec_id1) { '67ttf-5rr4d' }
243
277
  let!(:idmap1) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sc_e1', connec_entity: 'connec1', last_push_to_connec: 1.year.ago, connec_id: connec_id1) }
@@ -247,7 +281,7 @@ describe Maestrano::Connector::Rails::ComplexEntity do
247
281
 
248
282
  context 'without conflict' do
249
283
  it 'returns the entity with their idmaps' do
250
- subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
284
+ subject.consolidate_and_map_data({'connec1' => []}, {'sc_e1' => []}, organization, opt)
251
285
  expect(external_hash).to eql({
252
286
  'sc_e1' => {'connec1' => [{entity: mapped_entity1, idmap: idmap1}]},
253
287
  'ScE2' => {
@@ -264,7 +298,7 @@ describe Maestrano::Connector::Rails::ComplexEntity do
264
298
  let(:opt) { {connec_preemption: true} }
265
299
 
266
300
  it 'keeps the connec entities' do
267
- mapped_entities = subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
301
+ mapped_entities = subject.consolidate_and_map_data({'connec1' => []}, {'sc_e1' => []}, organization, opt)
268
302
  expect(mapped_entities[:connec_entities]).to eq({'connec1' => {'sc_e1' => [{entity: {'name' => 'Jacob'}, idmap: idmap1}]}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
269
303
  expect(mapped_entities[:external_entities]).to eql({
270
304
  'sc_e1' => {'connec1' => []},
@@ -280,7 +314,7 @@ describe Maestrano::Connector::Rails::ComplexEntity do
280
314
  let(:opt) { {connec_preemption: false} }
281
315
 
282
316
  it 'keeps the external entities' do
283
- mapped_entities = subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
317
+ mapped_entities = subject.consolidate_and_map_data({'connec1' => []}, {'sc_e1' => []}, organization, opt)
284
318
  expect(mapped_entities[:connec_entities]).to eq({'connec1' => {'sc_e1' => []}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
285
319
  expect(mapped_entities[:external_entities]).to eql({
286
320
  'sc_e1' => {'connec1' => [{entity: mapped_entity1, idmap: idmap1}]},
@@ -296,7 +330,7 @@ describe Maestrano::Connector::Rails::ComplexEntity do
296
330
  context 'without option' do
297
331
  context 'with a more recently updated external entity' do
298
332
  it 'keeps the external entity' do
299
- mapped_entities = subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
333
+ mapped_entities = subject.consolidate_and_map_data({'connec1' => []}, {'sc_e1' => []}, organization, opt)
300
334
  expect(mapped_entities[:connec_entities]).to eq({'connec1' => {'sc_e1' => []}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
301
335
  expect(mapped_entities[:external_entities]).to eql({
302
336
  'sc_e1' => {'connec1' => [{entity: mapped_entity1, idmap: idmap1}]},
@@ -312,7 +346,7 @@ describe Maestrano::Connector::Rails::ComplexEntity do
312
346
  let(:connec_hash) { {'connec1' => {'sc_e1' => [{'id' => connec_id1, 'first_name' => 'Jacob', 'updated_at' => 1.second.ago}]}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}} }
313
347
 
314
348
  it 'keeps the connec entities' do
315
- mapped_entities = subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
349
+ mapped_entities = subject.consolidate_and_map_data({'connec1' => []}, {'sc_e1' => []}, organization, opt)
316
350
  expect(mapped_entities[:connec_entities]).to eq({'connec1' => {'sc_e1' => [{entity: {'name' => 'Jacob'}, idmap: idmap1}]}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
317
351
  expect(mapped_entities[:external_entities]).to eql({
318
352
  'sc_e1' => {'connec1' => []},
@@ -38,6 +38,43 @@ describe Maestrano::Connector::Rails::Entity do
38
38
  end
39
39
  end
40
40
 
41
+ # IdMap methods
42
+ describe 'idmaps mehtods' do
43
+ before {
44
+ allow(subject).to receive(:connec_entity_name).and_return('Ab')
45
+ allow(subject).to receive(:external_entity_name).and_return('Ab')
46
+ }
47
+ let(:n_hash) { {connec_entity: 'ab', external_entity: 'ab'} }
48
+
49
+ it { expect(subject.names_hash).to eql(n_hash) }
50
+ it {
51
+ expect(Maestrano::Connector::Rails::IdMap).to receive(:find_or_create_by).with(n_hash.merge(id: 'lala'))
52
+ subject.find_or_create_idmap({id: 'lala'})
53
+ }
54
+ it {
55
+ expect(Maestrano::Connector::Rails::IdMap).to receive(:find_by).with(n_hash.merge(id: 'lala'))
56
+ subject.find_idmap({id: 'lala'})
57
+ }
58
+ describe 'creates' do
59
+ let(:organization) { create(:organization) }
60
+ let(:connec_entity) { {'id' => 'lala'} }
61
+ before {
62
+ allow(subject).to receive(:object_name_from_external_entity_hash).and_return('name_e')
63
+ allow(subject).to receive(:object_name_from_connec_entity_hash).and_return('name_c')
64
+ allow(subject).to receive(:get_id_from_external_entity_hash).and_return('id')
65
+ }
66
+
67
+ it {
68
+ expect(Maestrano::Connector::Rails::IdMap).to receive(:create).with(n_hash.merge(connec_id: 'lala', name: 'name_c', organization_id: organization.id))
69
+ subject.create_idmap_from_connec_entity(connec_entity, organization)
70
+ }
71
+ it {
72
+ expect(Maestrano::Connector::Rails::IdMap).to receive(:create).with(n_hash.merge(external_id: 'id', name: 'name_e', organization_id: organization.id))
73
+ subject.create_idmap_from_external_entity({}, organization)
74
+ }
75
+ end
76
+
77
+ end
41
78
 
42
79
  # Connec! methods
43
80
  describe 'connec_methods' do
@@ -181,26 +218,17 @@ describe Maestrano::Connector::Rails::Entity do
181
218
  describe 'create_connec_entity' do
182
219
  let(:entity) { {name: 'John'} }
183
220
 
184
- describe 'with a response' do
185
- before {
186
- allow(client).to receive(:post).and_return(ActionDispatch::Response.new(200, {}, {people: entity}.to_json, {}))
187
- }
188
-
189
- it 'sends a post to connec' do
190
- expect(client).to receive(:post).with("/#{connec_name.downcase.pluralize}", {"#{connec_name.downcase.pluralize}".to_sym => entity})
191
- subject.create_connec_entity(client, entity, connec_name, organization)
192
- end
221
+ before {
222
+ allow(client).to receive(:post).and_return(ActionDispatch::Response.new(200, {}, {people: entity}.to_json, {}))
223
+ }
193
224
 
194
- it 'returns the created entity' do
195
- expect(subject.create_connec_entity(client, entity, connec_name, organization)).to eql(JSON.parse(entity.to_json))
196
- end
225
+ it 'sends a post to connec' do
226
+ expect(client).to receive(:post).with("/#{connec_name.downcase.pluralize}", {"#{connec_name.downcase.pluralize}".to_sym => entity})
227
+ subject.create_connec_entity(client, entity, connec_name, organization)
197
228
  end
198
229
 
199
- describe 'without response' do
200
- before {
201
- allow(client).to receive(:post).and_return(nil)
202
- }
203
- it { expect{ subject.create_connec_entity(client, entity, connec_name, organization) }.to raise_error("No response received from Connec! when trying to create a #{connec_name}") }
230
+ it 'returns the created entity' do
231
+ expect(subject.create_connec_entity(client, entity, connec_name, organization)).to eql(JSON.parse(entity.to_json))
204
232
  end
205
233
  end
206
234
 
@@ -208,23 +236,13 @@ describe Maestrano::Connector::Rails::Entity do
208
236
  let(:organization) { create(:organization) }
209
237
  let(:entity) { {name: 'John'} }
210
238
  let(:id) { '88ye-777ab' }
239
+ before {
240
+ allow(client).to receive(:put).and_return(ActionDispatch::Response.new(200, {}, {}.to_json, {}))
241
+ }
211
242
 
212
- describe 'with a response' do
213
- before {
214
- allow(client).to receive(:put).and_return(ActionDispatch::Response.new(200, {}, {}.to_json, {}))
215
- }
216
-
217
- it 'sends a put to connec' do
218
- expect(client).to receive(:put).with("/#{connec_name.downcase.pluralize}/#{id}", {"#{connec_name.downcase.pluralize}".to_sym => entity})
219
- subject.update_connec_entity(client, entity, id, connec_name, organization)
220
- end
221
- end
222
-
223
- describe 'without response' do
224
- before {
225
- allow(client).to receive(:put).and_return(nil)
226
- }
227
- it { expect{ subject.update_connec_entity(client, entity, id, connec_name, organization) }.to raise_error("No response received from Connec! when trying to update a #{connec_name}") }
243
+ it 'sends a put to connec' do
244
+ expect(client).to receive(:put).with("/#{connec_name.downcase.pluralize}/#{id}", {"#{connec_name.downcase.pluralize}".to_sym => entity})
245
+ subject.update_connec_entity(client, entity, id, connec_name, organization)
228
246
  end
229
247
  end
230
248
 
@@ -234,11 +252,14 @@ describe Maestrano::Connector::Rails::Entity do
234
252
  let(:mapped_entity) { {'first_name' => 'John'} }
235
253
  before {
236
254
  allow(subject).to receive(:connec_entity_name).and_return(connec_name)
255
+ allow(subject).to receive(:external_entity_name).and_return(external_name)
237
256
  allow(subject).to receive(:map_to_external).and_return(mapped_entity)
257
+ allow(subject).to receive(:object_name_from_connec_entity_hash).and_return('name')
258
+ allow(subject).to receive(:object_name_from_external_entity_hash).and_return('name')
238
259
  }
239
260
 
240
261
  context 'when entity has an idmap' do
241
- let!(:idmap) { create(:idmap, organization: organization, connec_entity: connec_name.downcase, connec_id: id, last_push_to_external: 3.hour.ago)}
262
+ let!(:idmap) { create(:idmap, organization: organization, external_entity: external_name.downcase, connec_entity: connec_name.downcase, connec_id: id, last_push_to_external: 3.hour.ago)}
242
263
 
243
264
  context 'when updated_at field is most recent than idmap last_push_to_external' do
244
265
  let(:entity) { {'id' => id, 'name' => 'John', 'updated_at' => 2.hour.ago } }
@@ -255,6 +276,17 @@ describe Maestrano::Connector::Rails::Entity do
255
276
  expect(subject.map_to_external_with_idmap(entity, organization)).to be_nil
256
277
  end
257
278
  end
279
+
280
+ context 'when to_external is set to false' do
281
+ let(:entity) { {'id' => id, 'name' => 'John' } }
282
+ before {
283
+ idmap.update(to_external: false)
284
+ }
285
+
286
+ it 'discards the entity' do
287
+ expect(subject.map_to_external_with_idmap(entity, organization)).to be_nil
288
+ end
289
+ end
258
290
  end
259
291
 
260
292
  context 'when entity has no idmap' do
@@ -368,125 +400,192 @@ describe Maestrano::Connector::Rails::Entity do
368
400
 
369
401
  # General methods
370
402
  describe 'consolidate_and_map_data' do
371
- # subject.consolidate_and_map_data(connec_entities, external_entities, organization, opts)
372
403
  let(:organization) { create(:organization) }
404
+ let(:external_name) { 'External_name' }
405
+ let(:connec_name) { 'Connec_name' }
406
+ let(:id) { '56882' }
407
+ let(:date) { 2.hour.ago }
408
+ before {
409
+ allow(subject).to receive(:get_id_from_external_entity_hash).and_return(id)
410
+ allow(subject).to receive(:get_last_update_date_from_external_entity_hash).and_return(date)
411
+ allow(subject).to receive(:external_entity_name).and_return(external_name)
412
+ allow(subject).to receive(:connec_entity_name).and_return(connec_name)
413
+ }
373
414
 
374
- describe 'connec_entities treatment' do
375
- let(:entity1) { {name: 'John'} }
376
- let(:entity2) { {name: 'Jane'} }
415
+ context 'for a singleton method' do
416
+ before {
417
+ allow(subject).to receive(:singleton?).and_return(true)
418
+ allow(subject).to receive(:map_to_connec).and_return({map: 'connec'})
419
+ allow(subject).to receive(:map_to_external).and_return({map: 'external'})
420
+ }
377
421
 
378
- it 'calls map_to_external_with_idmap for each entity' do
379
- expect(subject).to receive(:map_to_external_with_idmap).with(entity1, organization)
380
- expect(subject).to receive(:map_to_external_with_idmap).with(entity2, organization)
381
- subject.consolidate_and_map_data([entity1, entity2], [], organization)
422
+ it { expect(subject.consolidate_and_map_data([], [], organization)).to eql({connec_entities: [], external_entities: []}) }
423
+
424
+ context 'with no idmap' do
425
+ it 'creates one for connec' do
426
+ subject.consolidate_and_map_data([{'id' => 'lala'}], [], organization)
427
+ idmap = Maestrano::Connector::Rails::IdMap.last
428
+ expect(idmap.connec_entity).to eql(connec_name.downcase)
429
+ expect(idmap.external_entity).to eql(external_name.downcase)
430
+ expect(idmap.connec_id).to eql('lala')
431
+ end
432
+
433
+ it 'creates one for external' do
434
+ subject.consolidate_and_map_data([], [{}], organization)
435
+ idmap = Maestrano::Connector::Rails::IdMap.last
436
+ expect(idmap.connec_entity).to eql(connec_name.downcase)
437
+ expect(idmap.external_entity).to eql(external_name.downcase)
438
+ expect(idmap.external_id).to eql(id)
439
+ end
382
440
  end
383
- end
384
441
 
385
- describe 'external_entities treatment' do
386
- let(:external_name) { 'external_name' }
387
- let(:connec_name) { 'connec_name' }
388
- let(:id) { '56882' }
389
- let(:date) { 2.hour.ago }
390
- let(:entity) { {id: id, name: 'John', modifiedDate: date} }
391
- let(:mapped_entity) { {first_name: 'John'} }
392
- let(:entities) { [entity] }
393
-
394
- before{
395
- allow(subject).to receive(:get_id_from_external_entity_hash).and_return(id)
396
- allow(subject).to receive(:get_last_update_date_from_external_entity_hash).and_return(date)
397
- allow(subject).to receive(:external_entity_name).and_return(external_name)
398
- allow(subject).to receive(:connec_entity_name).and_return(connec_name)
399
- allow(subject).to receive(:map_to_connec).and_return(mapped_entity)
400
- }
442
+ context 'with an idmap' do
443
+ let!(:idmap) { create(:idmap, connec_entity: connec_name.downcase, external_entity: external_name.downcase, organization: organization) }
401
444
 
402
- context 'when entity has no idmap' do
403
- let(:human_name) { 'alien' }
404
- before {
405
- allow(subject).to receive(:object_name_from_external_entity_hash).and_return(human_name)
406
- }
445
+ it { expect{ subject.consolidate_and_map_data([{}], [], organization) }.to_not change{ Maestrano::Connector::Rails::IdMap } }
446
+ end
447
+
448
+ context 'with conflict' do
449
+ let!(:idmap) { create(:idmap, connec_entity: connec_name.downcase, external_entity: external_name.downcase, organization: organization, external_id: id, connec_id: 'lala') }
450
+ let(:updated) { 3.hour.ago }
451
+ let(:connec_entity) { {'id' => 'lala', 'updated_at' => updated} }
407
452
 
408
- it 'creates an idmap and returns the mapped entity with its new idmap' do
409
- mapped_entities = subject.consolidate_and_map_data([], entities, organization)
410
- expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: Maestrano::Connector::Rails::IdMap.last}]})
453
+ context 'with options' do
454
+ it { expect(subject.consolidate_and_map_data([connec_entity], [{}], organization, connec_preemption: true)).to eql({connec_entities: [{entity: {map: 'external'}, idmap: idmap}], external_entities: []}) }
455
+ it { expect(subject.consolidate_and_map_data([connec_entity], [{}], organization, connec_preemption: false)).to eql({connec_entities: [], external_entities: [{entity: {map: 'connec'}, idmap: idmap}]}) }
411
456
  end
412
457
 
413
- it 'save the name in the idmap' do
414
- subject.consolidate_and_map_data([], entities, organization)
415
- expect(Maestrano::Connector::Rails::IdMap.last.name).to eql(human_name)
458
+ context 'without options' do
459
+ context 'with a more recent external one' do
460
+ it { expect(subject.consolidate_and_map_data([connec_entity], [{}], organization)).to eql({connec_entities: [], external_entities: [{entity: {map: 'connec'}, idmap: idmap}]}) }
461
+ end
462
+ context 'with a more recent connec one' do
463
+ let(:updated) { 2.minute.ago }
464
+ it { expect(subject.consolidate_and_map_data([connec_entity], [{}], organization)).to eql({connec_entities: [{entity: {map: 'external'}, idmap: idmap}], external_entities: []}) }
465
+ end
416
466
  end
417
467
  end
468
+ end
469
+
470
+ context 'for a non singleton method' do
418
471
 
419
- context 'when entity has an idmap with a last_push_to_connec more recent than date' do
420
- let!(:idmap) { create(:idmap, external_entity: external_name, external_id: id, organization: organization, last_push_to_connec: 2.minute.ago) }
472
+ describe 'connec_entities treatment' do
473
+ let(:entity1) { {name: 'John'} }
474
+ let(:entity2) { {name: 'Jane'} }
421
475
 
422
- it 'discards the entity' do
423
- mapped_entities = subject.consolidate_and_map_data([], entities, organization)
424
- expect(mapped_entities).to eql({connec_entities: [], external_entities: []})
476
+ it 'calls map_to_external_with_idmap for each entity' do
477
+ expect(subject).to receive(:map_to_external_with_idmap).with(entity1, organization)
478
+ expect(subject).to receive(:map_to_external_with_idmap).with(entity2, organization)
479
+ subject.consolidate_and_map_data([entity1, entity2], [], organization)
425
480
  end
426
481
  end
427
482
 
428
- context 'when entity has an idmap with a last_push_to_connec older than date' do
483
+ describe 'external_entities treatment' do
484
+ let(:entity) { {id: id, name: 'John', modifiedDate: date} }
485
+ let(:mapped_entity) { {first_name: 'John'} }
486
+ let(:entities) { [entity] }
429
487
 
430
- context 'with no conflict' do
431
- let!(:idmap) { create(:idmap, external_entity: external_name, external_id: id, organization: organization, last_push_to_connec: 2.day.ago) }
488
+ before {
489
+ allow(subject).to receive(:map_to_connec).and_return(mapped_entity)
490
+ }
432
491
 
433
- it 'returns the mapped entity with its idmap' do
492
+ context 'when entity has no idmap' do
493
+ let(:human_name) { 'alien' }
494
+ before {
495
+ allow(subject).to receive(:object_name_from_external_entity_hash).and_return(human_name)
496
+ }
497
+
498
+ it 'creates an idmap and returns the mapped entity with its new idmap' do
434
499
  mapped_entities = subject.consolidate_and_map_data([], entities, organization)
435
- expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: idmap}]})
500
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: Maestrano::Connector::Rails::IdMap.last}]})
501
+ end
502
+
503
+ it 'save the name in the idmap' do
504
+ subject.consolidate_and_map_data([], entities, organization)
505
+ expect(Maestrano::Connector::Rails::IdMap.last.name).to eql(human_name)
436
506
  end
437
507
  end
438
508
 
439
- context 'with conflict' do
440
- let(:connec_id) { '34uuu-778aa' }
441
- let!(:idmap) { create(:idmap, connec_id: connec_id, external_entity: external_name, external_id: id, organization: organization, last_push_to_connec: 2.day.ago) }
442
- before {
443
- allow(subject).to receive(:map_to_external_with_idmap)
444
- }
509
+ context 'when entity has an idmap with to_connec set to false' do
510
+ let!(:idmap) { create(:idmap, external_entity: external_name.downcase, connec_entity: connec_name.downcase, external_id: id, organization: organization, to_connec: false) }
511
+ it 'discards the entity' do
512
+ mapped_entities = subject.consolidate_and_map_data([], entities, organization)
513
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: []})
514
+ end
515
+ end
445
516
 
446
- context 'with connec_preemption opt' do
517
+ context 'when entity has an idmap with a last_push_to_connec more recent than date' do
518
+ let!(:idmap) { create(:idmap, connec_entity: connec_name.downcase, external_entity: external_name.downcase, external_id: id, organization: organization, last_push_to_connec: 2.minute.ago) }
447
519
 
448
- context 'set to true' do
449
- let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.day.ago} }
450
- it 'discards the entity' do
451
- mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: true})
452
- expect(mapped_entities).to eql({connec_entities: [], external_entities: []})
453
- end
454
- end
520
+ it 'discards the entity' do
521
+ mapped_entities = subject.consolidate_and_map_data([], entities, organization)
522
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: []})
523
+ end
524
+ end
455
525
 
456
- context 'set to false' do
457
- let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.second.ago} }
458
- it 'returns the mapped entity with its idmap' do
459
- mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: false})
460
- expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: idmap}]})
461
- end
526
+ context 'when entity has an idmap with a last_push_to_connec older than date' do
527
+
528
+ context 'with no conflict' do
529
+ let!(:idmap) { create(:idmap, connec_entity: connec_name.downcase, external_entity: external_name.downcase, external_id: id, organization: organization, last_push_to_connec: 2.day.ago) }
530
+
531
+ it 'returns the mapped entity with its idmap' do
532
+ mapped_entities = subject.consolidate_and_map_data([], entities, organization)
533
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: idmap}]})
462
534
  end
463
535
  end
464
536
 
465
- context 'without opt' do
466
- context 'with a more recent connec entity' do
467
- let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.second.ago} }
537
+ context 'with conflict' do
538
+ let(:connec_id) { '34uuu-778aa' }
539
+ let!(:idmap) { create(:idmap, connec_id: connec_id, connec_entity: connec_name.downcase, external_entity: external_name.downcase, external_id: id, organization: organization, last_push_to_connec: 2.day.ago) }
540
+ before {
541
+ allow(subject).to receive(:map_to_external_with_idmap)
542
+ }
543
+
544
+ context 'with connec_preemption opt' do
468
545
 
469
- it 'discards the entity' do
470
- mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: true})
471
- expect(mapped_entities).to eql({connec_entities: [], external_entities: []})
546
+ context 'set to true' do
547
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.day.ago} }
548
+ it 'discards the entity' do
549
+ mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: true})
550
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: []})
551
+ end
552
+ end
553
+
554
+ context 'set to false' do
555
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.second.ago} }
556
+ it 'returns the mapped entity with its idmap' do
557
+ mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: false})
558
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: idmap}]})
559
+ end
472
560
  end
473
561
  end
474
562
 
475
- context 'with a more recent external_entity' do
476
- let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.year.ago} }
563
+ context 'without opt' do
564
+ context 'with a more recent connec entity' do
565
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.second.ago} }
566
+
567
+ it 'discards the entity' do
568
+ mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: true})
569
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: []})
570
+ end
571
+ end
572
+
573
+ context 'with a more recent external_entity' do
574
+ let(:connec_entity) { {'id' => connec_id, 'first_name' => 'Richard', 'updated_at' => 1.year.ago} }
477
575
 
478
- it 'returns the mapped entity with its idmap' do
479
- mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: false})
480
- expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: idmap}]})
576
+ it 'returns the mapped entity with its idmap' do
577
+ mapped_entities = subject.consolidate_and_map_data([connec_entity], entities, organization, {connec_preemption: false})
578
+ expect(mapped_entities).to eql({connec_entities: [], external_entities: [{entity: mapped_entity, idmap: idmap}]})
579
+ end
481
580
  end
482
581
  end
483
- end
484
582
 
583
+ end
485
584
  end
585
+
486
586
  end
487
587
 
488
588
  end
489
-
490
589
  end
491
590
 
492
591