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
File without changes
File without changes
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
data/spec/factories.rb ADDED
@@ -0,0 +1,33 @@
1
+ FactoryGirl.define do
2
+
3
+ factory :user, class: Maestrano::Connector::Rails::User do
4
+ email "email@example.com"
5
+ tenant "default"
6
+ end
7
+
8
+ factory :organization, class: Maestrano::Connector::Rails::Organization do
9
+ name "My company"
10
+ tenant "default"
11
+ end
12
+
13
+ factory :user_organization_rel, class: Maestrano::Connector::Rails::UserOrganizationRel do
14
+ association :user
15
+ association :organization
16
+ end
17
+
18
+ factory :idmap, class: Maestrano::Connector::Rails::IdMap do
19
+ connec_id '6798-ada6-te43'
20
+ connec_entity 'person'
21
+ external_id '4567ada66'
22
+ external_entity 'contact'
23
+ last_push_to_external 2.day.ago
24
+ last_push_to_connec 1.day.ago
25
+ association :organization
26
+ end
27
+
28
+ factory :synchronization, class: Maestrano::Connector::Rails::Synchronization do
29
+ association :organization
30
+ status 'SUCCESS'
31
+ partial false
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Maestrano::Connector::Rails::SynchronizationJob do
4
+ let(:organization) { create(:organization) }
5
+ subject { Maestrano::Connector::Rails::SynchronizationJob.new(organization, {}) }
6
+
7
+ describe 'perform' do
8
+ it 'creates a synchronization' do
9
+ expect{
10
+ subject.perform
11
+ }.to change{ Maestrano::Connector::Rails::Synchronization.count }.by(1)
12
+ end
13
+
14
+ it 'calls sync entity on all the organization synchronized entities set to true' do
15
+ organization.synchronized_entities[organization.synchronized_entities.keys.first] = false
16
+ expect(subject).to receive(:sync_entity).exactly(organization.synchronized_entities.count - 1).times
17
+ subject.perform
18
+ end
19
+
20
+ context 'with options' do
21
+ context 'with only_entities' do
22
+ subject { Maestrano::Connector::Rails::SynchronizationJob.new(organization, {only_entities: %w(people price)}) }
23
+
24
+ it 'calls sync entity on the specified entities' do
25
+ expect(subject).to receive(:sync_entity).twice
26
+ subject.perform
27
+ end
28
+
29
+ it 'set the current syncrhonization as partial' do
30
+ subject.perform
31
+ expect(Maestrano::Connector::Rails::Synchronization.last.partial).to be(true)
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ # def sync_entity(entity, organization, connec_client, external_client, last_synchronization, opts)
38
+ describe 'sync_entity' do
39
+ before {
40
+ class Entities::Person < Maestrano::Connector::Rails::Entity
41
+ end
42
+ }
43
+
44
+ it 'calls the five methods' do
45
+ expect_any_instance_of(Entities::Person).to receive(:get_connec_entities)
46
+ expect_any_instance_of(Entities::Person).to receive(:get_external_entities)
47
+ expect_any_instance_of(Entities::Person).to receive(:consolidate_and_map_data)
48
+ expect_any_instance_of(Entities::Person).to receive(:push_entities_to_external)
49
+ expect_any_instance_of(Entities::Person).to receive(:push_entities_to_connec)
50
+ subject.sync_entity('person', organization, nil, nil, nil, {})
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,353 @@
1
+ require 'spec_helper'
2
+
3
+ describe Maestrano::Connector::Rails::ComplexEntity do
4
+
5
+ #Connec specific methods
6
+ describe 'connec specific methods' do
7
+ describe 'connec_entities_names' do
8
+ it { expect{ subject.connec_entities_names }.to raise_error('Not implemented') }
9
+ end
10
+ describe 'external_entities_names' do
11
+ it { expect{ subject.external_entities_names }.to raise_error('Not implemented') }
12
+ end
13
+ describe 'connec_model_to_external_model!' do
14
+ it { expect{ subject.connec_model_to_external_model!({}) }.to raise_error('Not implemented') }
15
+ end
16
+ describe 'external_model_to_connec_model!' do
17
+ it { expect{ subject.external_model_to_connec_model!({}) }.to raise_error('Not implemented') }
18
+ end
19
+ end
20
+
21
+ describe 'general methods' do
22
+ subject { Maestrano::Connector::Rails::ComplexEntity.new }
23
+
24
+ describe 'map_to_external_with_idmap' do
25
+ let(:organization) { create(:organization) }
26
+ let(:id) { '322j-bbfg4' }
27
+ let(:entity) { {'id' => id, 'name' => 'John', 'updated_at' => 2.day.ago} }
28
+ let(:mapped_entity) { {'first_name' => 'John'} }
29
+ let(:connec_name) { 'connec_name' }
30
+ let(:external_name) { 'external_name' }
31
+ let(:sub_instance) { Maestrano::Connector::Rails::SubEntityBase.new }
32
+ before {
33
+ allow(sub_instance).to receive(:map_to).and_return(mapped_entity)
34
+ }
35
+
36
+ context 'when entity has no idmap' do
37
+ it 'creates one' do
38
+ expect{
39
+ subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)
40
+ }.to change{ Maestrano::Connector::Rails::IdMap.count }.by(1)
41
+ end
42
+ end
43
+ it 'returns the mapped entity with its idmap' do
44
+ 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})
45
+ end
46
+
47
+ context 'when entity has an idmap without last_push_to_external' do
48
+ let!(:idmap) { create(:idmap, organization: organization, connec_id: id, connec_entity: connec_name, last_push_to_external: nil, external_entity: external_name) }
49
+
50
+ it 'returns the mapped entity with its idmap' do
51
+ expect(subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: idmap})
52
+ end
53
+ end
54
+
55
+ context 'when entity has an idmap with an older last_push_to_external' do
56
+ let!(:idmap) { create(:idmap, organization: organization, connec_id: id, connec_entity: connec_name, last_push_to_external: 1.year.ago, external_entity: external_name) }
57
+
58
+ it 'returns the mapped entity with its idmap' do
59
+ expect(subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)).to eql({entity: mapped_entity, idmap: idmap})
60
+ end
61
+ end
62
+
63
+ context 'when entity has an idmap with a more recent last_push_to_external' do
64
+ let!(:idmap) { create(:idmap, organization: organization, connec_id: id, connec_entity: connec_name, last_push_to_external: 1.second.ago, external_entity: external_name) }
65
+
66
+ it 'discards the entity' do
67
+ expect(subject.map_to_external_with_idmap(entity, organization, connec_name, external_name, sub_instance)).to be_nil
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ describe 'methods with sub complex entities' do
75
+ before {
76
+ module Entities::SubEntities
77
+ end
78
+ class Entities::SubEntities::ScE1 < Maestrano::Connector::Rails::SubEntityBase
79
+ end
80
+ class Entities::SubEntities::ScE2 < Maestrano::Connector::Rails::SubEntityBase
81
+ end
82
+ }
83
+
84
+ describe 'get_connec_entities' do
85
+ before {
86
+ allow(subject).to receive(:connec_entities_names).and_return(%w(sc_e1 ScE2))
87
+ }
88
+
89
+ it 'calls get_connec_entities on each connec sub complex entities' do
90
+ expect_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_connec_entities).with(nil, nil, nil, {opts: true})
91
+ expect_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_connec_entities).with(nil, nil, nil, {opts: true})
92
+ subject.get_connec_entities(nil, nil, nil, {opts: true})
93
+ end
94
+
95
+ let(:arr1) { [{'name' => 'Water'}, {'name' => 'Sugar'}] }
96
+ let(:arr2) { [{'price' => 92}, {'price' => 300}] }
97
+ it 'returns an hash of the connec_entities keyed by connec_entity_name' do
98
+ allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_connec_entities).and_return(arr1)
99
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_connec_entities).and_return(arr2)
100
+ expect(subject.get_connec_entities(nil, nil, nil, {opts: true})).to eql({'sc_e1' => arr1, 'ScE2' => arr2})
101
+ end
102
+ end
103
+
104
+ describe 'get_external_entities' do
105
+ before {
106
+ allow(subject).to receive(:external_entities_names).and_return(%w(sc_e1 ScE2))
107
+ }
108
+
109
+ it 'calls get_external_entities on each connec sub complex entities' do
110
+ expect_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_external_entities).with(nil, nil, nil, {opts: true})
111
+ expect_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_external_entities).with(nil, nil, nil, {opts: true})
112
+ subject.get_external_entities(nil, nil, nil, {opts: true})
113
+ end
114
+
115
+ let(:arr1) { [{'name' => 'Water'}, {'name' => 'Sugar'}] }
116
+ let(:arr2) { [{'price' => 92}, {'price' => 300}] }
117
+ it 'returns an hash of the external_entities keyed by external_entity_name' do
118
+ allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_external_entities).and_return(arr1)
119
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_external_entities).and_return(arr2)
120
+ expect(subject.get_external_entities(nil, nil, nil, {opts: true})).to eql({'sc_e1' => arr1, 'ScE2' => arr2})
121
+ end
122
+ end
123
+
124
+
125
+ describe 'consolidate_and_map_data' do
126
+ let(:opt) { {opt: true} }
127
+ let(:organization) { create(:organization) }
128
+
129
+ it 'calls external_model_to_connec_model!' do
130
+ allow(subject).to receive(:connec_model_to_external_model!)
131
+ expect(subject).to receive(:external_model_to_connec_model!).with({a: {}})
132
+ subject.consolidate_and_map_data({}, {a: {}}, organization, opt)
133
+ end
134
+
135
+ it 'calls connec_model_to_external_model' do
136
+ allow(subject).to receive(:external_model_to_connec_model!)
137
+ expect(subject).to receive(:connec_model_to_external_model!).with({a: {}})
138
+ subject.consolidate_and_map_data({a: {}}, {}, organization, opt)
139
+ end
140
+
141
+ describe 'connec_entities treatment' do
142
+ #hash as it should be after connec_model_to_external_model!
143
+ let(:connec_hash) {
144
+ {
145
+ 'sc_e1' => {'ext1' => [{'name' => 'John'}, {'name' => 'Jane'}]},
146
+ 'ScE2' => {'ext1' => [{'name' => 'Robert'}], 'ext2' => [{'price' => 45}]}
147
+ }
148
+ }
149
+ before{
150
+ allow(subject).to receive(:external_model_to_connec_model!)
151
+ allow(subject).to receive(:connec_model_to_external_model!)
152
+ }
153
+
154
+ it 'calls map_to_external_with_idmap on each entity' do
155
+ expect(subject).to receive(:map_to_external_with_idmap).exactly(4).times
156
+ subject.consolidate_and_map_data(connec_hash, {}, organization, opt)
157
+ end
158
+ end
159
+
160
+ describe 'external_entities treatment' do
161
+ #hash as it should be after external_model_to_connec_model!
162
+ let(:id1) { '5678ttd3' }
163
+ let(:id2) { '5678taa3' }
164
+ let(:entity1) { {'id' => id1, 'name' => 'Robert'} }
165
+ let(:entity2) { {'id' => id2, 'price' => 45} }
166
+ let(:mapped_entity1) { {'first_name' => 'Robert'} }
167
+ let(:mapped_entity2) { {'net_price' => 45} }
168
+ let(:external_hash) {
169
+ {
170
+ 'sc_e1' => {'connec1' => [entity1]},
171
+ 'ScE2' => {'connec1' => [entity1], 'connec2' => [entity2]}
172
+ }
173
+ }
174
+ before{
175
+ allow(subject).to receive(:external_model_to_connec_model!)
176
+ allow(subject).to receive(:connec_model_to_external_model!)
177
+ allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_id_from_external_entity_hash).with(entity1).and_return(id1)
178
+ allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:get_last_update_date_from_external_entity_hash).and_return(1.minute.ago)
179
+ allow_any_instance_of(Entities::SubEntities::ScE1).to receive(:map_to).with('connec1', entity1, organization).and_return(mapped_entity1)
180
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_id_from_external_entity_hash).with(entity1).and_return(id1)
181
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_id_from_external_entity_hash).with(entity2).and_return(id2)
182
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:get_last_update_date_from_external_entity_hash).and_return(1.minute.ago)
183
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:map_to).with('connec1', entity1, organization).and_return(mapped_entity1)
184
+ allow_any_instance_of(Entities::SubEntities::ScE2).to receive(:map_to).with('connec2', entity2, organization).and_return(mapped_entity2)
185
+ }
186
+
187
+ context 'when entities have no idmaps' do
188
+ it 'creates an idmap for each entity' do
189
+ expect{
190
+ subject.consolidate_and_map_data({}, external_hash, organization, opt)
191
+ }.to change{ Maestrano::Connector::Rails::IdMap.count }.by(3)
192
+ end
193
+
194
+ it 'returns the entity with their new idmaps' do
195
+ subject.consolidate_and_map_data({}, external_hash, organization, opt)
196
+ expect(external_hash).to eql( {
197
+ 'sc_e1' => {'connec1' => [{entity: mapped_entity1, idmap: Maestrano::Connector::Rails::IdMap.first}]},
198
+ 'ScE2' => {
199
+ 'connec1' => [{entity: mapped_entity1, idmap: Maestrano::Connector::Rails::IdMap.all[1]}],
200
+ 'connec2' => [{entity: mapped_entity2, idmap: Maestrano::Connector::Rails::IdMap.last}],
201
+ }
202
+ })
203
+ end
204
+ end
205
+
206
+ context 'when entities have idmaps with more recent last_push_to_connec' do
207
+ let!(:idmap1) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sc_e1', connec_entity: 'connec1', last_push_to_connec: 1.second.ago) }
208
+ let!(:idmap21) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sce2', connec_entity: 'connec1', last_push_to_connec: 1.second.ago) }
209
+ let!(:idmap22) { create(:idmap, organization: organization, external_id: id2, external_entity: 'sce2', connec_entity: 'connec2', last_push_to_connec: 1.second.ago) }
210
+
211
+ it 'discards the entities' do
212
+ subject.consolidate_and_map_data({}, external_hash, organization, opt)
213
+ expect(external_hash).to eql( {
214
+ 'sc_e1' => {'connec1' => []},
215
+ 'ScE2' => {
216
+ 'connec1' => [],
217
+ 'connec2' => [],
218
+ }
219
+ })
220
+ end
221
+ end
222
+
223
+ context 'when entities have idmaps with older last_push_to_connec' do
224
+ before{
225
+ class Entities::SubEntities::Connec1 < Maestrano::Connector::Rails::SubEntityBase
226
+ end
227
+ class Entities::SubEntities::Connec2 < Maestrano::Connector::Rails::SubEntityBase
228
+ end
229
+ allow_any_instance_of(Entities::SubEntities::Connec1).to receive(:map_to).and_return({'name' => 'Jacob'})
230
+ }
231
+ let(:connec_id1) { '67ttf-5rr4d' }
232
+ 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) }
233
+ let!(:idmap21) { create(:idmap, organization: organization, external_id: id1, external_entity: 'sce2', connec_entity: 'connec1', last_push_to_connec: 1.year.ago) }
234
+ let!(:idmap22) { create(:idmap, organization: organization, external_id: id2, external_entity: 'sce2', connec_entity: 'connec2', last_push_to_connec: 1.year.ago) }
235
+ let(:connec_hash) { {'connec1' => {'sc_e1' => [{'id' => connec_id1, 'first_name' => 'Jacob', 'updated_at' => 1.hour.ago}]}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}} }
236
+
237
+ context 'without conflict' do
238
+ it 'returns the entity with their idmaps' do
239
+ subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
240
+ expect(external_hash).to eql({
241
+ 'sc_e1' => {'connec1' => [{entity: mapped_entity1, idmap: idmap1}]},
242
+ 'ScE2' => {
243
+ 'connec1' => [{entity: mapped_entity1, idmap: idmap21}],
244
+ 'connec2' => [{entity: mapped_entity2, idmap: idmap22}],
245
+ }
246
+ })
247
+ end
248
+ end
249
+
250
+ context 'with conflict' do
251
+ context 'with option connec_preemption' do
252
+ context 'set to true' do
253
+ let(:opt) { {connec_preemption: true} }
254
+
255
+ it 'keeps the connec entities' do
256
+ subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
257
+ expect(connec_hash).to eq({'connec1' => {'sc_e1' => [{entity: {'name' => 'Jacob'}, idmap: idmap1}]}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
258
+ expect(external_hash).to eql({
259
+ 'sc_e1' => {'connec1' => []},
260
+ 'ScE2' => {
261
+ 'connec1' => [{entity: mapped_entity1, idmap: idmap21}],
262
+ 'connec2' => [{entity: mapped_entity2, idmap: idmap22}],
263
+ }
264
+ })
265
+ end
266
+ end
267
+ context 'set to false' do
268
+ let(:opt) { {connec_preemption: false} }
269
+
270
+ it 'keeps the external entities' do
271
+ subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
272
+ expect(connec_hash).to eq({'connec1' => {'sc_e1' => []}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
273
+ expect(external_hash).to eql({
274
+ 'sc_e1' => {'connec1' => [{entity: mapped_entity1, idmap: idmap1}]},
275
+ 'ScE2' => {
276
+ 'connec1' => [{entity: mapped_entity1, idmap: idmap21}],
277
+ 'connec2' => [{entity: mapped_entity2, idmap: idmap22}],
278
+ }
279
+ })
280
+ end
281
+ end
282
+ end
283
+
284
+ context 'without option' do
285
+ context 'with a more recently updated external entity' do
286
+ it 'keeps the external entity' do
287
+ subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
288
+ expect(connec_hash).to eq({'connec1' => {'sc_e1' => []}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
289
+ expect(external_hash).to eql({
290
+ 'sc_e1' => {'connec1' => [{entity: mapped_entity1, idmap: idmap1}]},
291
+ 'ScE2' => {
292
+ 'connec1' => [{entity: mapped_entity1, idmap: idmap21}],
293
+ 'connec2' => [{entity: mapped_entity2, idmap: idmap22}],
294
+ }
295
+ })
296
+ end
297
+ end
298
+
299
+ context 'with a more recently updated connec entity' do
300
+ let(:connec_hash) { {'connec1' => {'sc_e1' => [{'id' => connec_id1, 'first_name' => 'Jacob', 'updated_at' => 1.second.ago}]}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}} }
301
+
302
+ it 'keeps the connec entities' do
303
+ subject.consolidate_and_map_data(connec_hash, external_hash, organization, opt)
304
+ expect(connec_hash).to eq({'connec1' => {'sc_e1' => [{entity: {'name' => 'Jacob'}, idmap: idmap1}]}, 'connec2' => {'sc_e1' => [], 'ScE2' => []}})
305
+ expect(external_hash).to eql({
306
+ 'sc_e1' => {'connec1' => []},
307
+ 'ScE2' => {
308
+ 'connec1' => [{entity: mapped_entity1, idmap: idmap21}],
309
+ 'connec2' => [{entity: mapped_entity2, idmap: idmap22}],
310
+ }
311
+ })
312
+ end
313
+ end
314
+ end
315
+ end
316
+ end
317
+ end
318
+ end
319
+
320
+ describe 'push_entities_to_connec' do
321
+ let(:mapped_entity_with_idmap) { {entity: {}, idmap: nil} }
322
+ let(:external_hash) {
323
+ {
324
+ 'sc_e1' => {'connec1' => [mapped_entity_with_idmap]},
325
+ 'ScE2' => {'connec1' => [mapped_entity_with_idmap, mapped_entity_with_idmap], 'connec2' => [mapped_entity_with_idmap]}
326
+ }
327
+ }
328
+
329
+ it 'calls push_entities_to_connec on each sub complex entity' do
330
+ expect_any_instance_of(Entities::SubEntities::ScE1).to receive(:push_entities_to_connec_to).once.with(nil, [mapped_entity_with_idmap], 'connec1', nil)
331
+ expect_any_instance_of(Entities::SubEntities::ScE2).to receive(:push_entities_to_connec_to).twice
332
+ subject.push_entities_to_connec(nil, external_hash, nil)
333
+ end
334
+ end
335
+
336
+
337
+ describe 'push_entities_to_external' do
338
+ let(:mapped_entity_with_idmap) { {entity: {}, idmap: nil} }
339
+ let(:connec_hash) {
340
+ {
341
+ 'sc_e1' => {'ext1' => [mapped_entity_with_idmap]},
342
+ 'ScE2' => {'ext1' => [mapped_entity_with_idmap, mapped_entity_with_idmap], 'ext2' => [mapped_entity_with_idmap]}
343
+ }
344
+ }
345
+
346
+ it 'calls push_entities_to_connec on each sub complex entity' do
347
+ expect_any_instance_of(Entities::SubEntities::ScE1).to receive(:push_entities_to_external_to).once.with(nil, [mapped_entity_with_idmap], 'ext1', nil)
348
+ expect_any_instance_of(Entities::SubEntities::ScE2).to receive(:push_entities_to_external_to).twice
349
+ subject.push_entities_to_external(nil, connec_hash, nil)
350
+ end
351
+ end
352
+ end
353
+ end