spec/models/entity_spec.rb in maestrano-connector-rails-1.2.3 vs spec/models/entity_spec.rb in maestrano-connector-rails-1.3.0

- old
+ new

@@ -142,14 +142,10 @@ describe 'map_to_external' do it 'calls the mapper normalize' do expect(AMapper).to receive(:normalize).with({}).and_return({}) subject.map_to_external({}) end - - it 'preserve the __connec_id' do - expect(subject.map_to_external({__connec_id: 'connec id'})).to eql({__connec_id: 'connec id'}.with_indifferent_access) - end end describe 'map_to_connec' do before { allow(subject.class).to receive(:id_from_external_entity_hash).and_return('this id') @@ -337,11 +333,11 @@ end end describe 'push_entities_to_connec_to' do let(:idmap1) { create(:idmap, organization: organization) } - let(:idmap2) { create(:idmap, organization: organization, last_push_to_connec: nil) } + let(:idmap2) { create(:idmap, organization: organization, connec_id: nil) } let(:entity1) { {name: 'John'} } let(:entity2) { {name: 'Jane'} } let(:entity_with_idmap1) { {entity: entity1, idmap: idmap1} } let(:entity_with_idmap2) { {entity: entity2, idmap: idmap2} } let(:entities_with_idmaps) { [entity_with_idmap1, entity_with_idmap2] } @@ -355,10 +351,22 @@ expect(subject).to_not receive(:batch_op) subject.push_entities_to_connec_to(entities_with_idmaps, connec_name) end end + context 'when no update' do + before { + allow(subject.class).to receive(:can_update_connec?).and_return(false) + allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(200, {}, {results: []}.to_json, {})) + } + + it 'filters out the one with a connec_id' do + expect(subject).to receive(:batch_op).once.with('post', entity2, nil, 'people') + subject.push_entities_to_connec_to(entities_with_idmaps, connec_name) + end + end + context 'without errors' do let(:result200) { {status: 200, body: {connec_name.downcase.pluralize.to_sym => {id: [{provider: 'connec', id: 'id1'}]}}} } let(:result201) { {status: 201, body: {connec_name.downcase.pluralize.to_sym => {id: [{provider: 'connec', id: 'id2'}]}}} } before { allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(200, {}, {results: [result200, result201]}.to_json, {})) @@ -471,17 +479,22 @@ end end # External methods describe 'external methods' do + before { + allow(subject.class).to receive(:id_from_external_entity_hash).and_return('id') + } let(:idmap1) { create(:idmap, organization: organization) } let(:idmap2) { create(:idmap, organization: organization, external_id: nil, external_entity: nil, last_push_to_external: nil) } let(:entity1) { {name: 'John'} } let(:entity2) { {name: 'Jane'} } - let(:entity_with_idmap1) { {entity: entity1, idmap: idmap1} } + let(:id_refs_only_connec_entity1) { {} } + let(:id_refs_only_connec_entity2) { {} } + let(:entity_with_idmap1) { {entity: entity1, idmap: idmap1, id_refs_only_connec_entity: id_refs_only_connec_entity1} } let(:connec_id2) { 'connec_id2' } - let(:entity_with_idmap2) { {entity: entity2, idmap: idmap2} } + let(:entity_with_idmap2) { {entity: entity2, idmap: idmap2, id_refs_only_connec_entity: id_refs_only_connec_entity2} } let(:entities_with_idmaps) { [entity_with_idmap1, entity_with_idmap2] } describe 'get_external_entities_wrapper' do context 'when write only' do before { allow(subject.class).to receive(:can_read_external?).and_return(false) } @@ -506,11 +519,11 @@ subject.get_external_entities_wrapper(nil) end end describe 'get_external_entities' do - it { expect{ subject.get_external_entities(nil) }.to raise_error('Not implemented') } + it { expect{ subject.get_external_entities('') }.to raise_error('Not implemented') } end describe 'push_entities_to_external' do it 'calls push_entities_to_external_to' do expect(subject).to receive(:push_entities_to_external_to).with(entities_with_idmaps, external_name) @@ -532,11 +545,12 @@ subject.push_entities_to_external_to(entities_with_idmaps, external_name) end describe 'ids' do before { - allow(subject).to receive(:create_external_entity).and_return('id') + allow(subject.class).to receive(:id_from_external_entity_hash).and_return('id') + allow(subject).to receive(:create_external_entity).and_return({'id' => 'id'}) allow(subject).to receive(:update_external_entity).and_return(nil) } context 'when ids to send to connec' do let(:batch_param) { @@ -558,10 +572,48 @@ expect(connec_client).to_not receive(:batch) subject.push_entities_to_external_to(entities_with_idmaps, external_name) end end end + + describe 'id_references' do + let(:connec_line_id1) { 'connec_line_id1' } + let(:connec_line_id2) { 'connec_line_id2' } + let(:ext_line_id1) { 'ext_line_id1' } + let(:ext_line_id2) { 'ext_line_id2' } + let(:id_refs_only_connec_entity1) { {lines: [{id: [{provider: 'connec', realm: 'org', id: connec_line_id1}]}]}.with_indifferent_access } + let(:id_refs_only_connec_entity2) { {lines: [{id: [{provider: 'connec', realm: 'org', id: connec_line_id2}]}]}.with_indifferent_access } + before { + allow(subject.class).to receive(:id_from_external_entity_hash).and_return('id') + allow(subject.class).to receive(:references).and_return({record_references: [], id_references: ['lines/id']}) + allow(subject).to receive(:create_external_entity).and_return({'id' => 'id', invoice_lines: [{ID: ext_line_id1}]}) + allow(subject).to receive(:update_external_entity).and_return({'id' => 'id', invoice_lines: [{ID: ext_line_id2}]}) + allow(subject).to receive(:map_to_connec).and_return({lines: [{id: [{id: ext_line_id1, provider: organization.oauth_provider, realm: organization.oauth_uid}]}]}, {lines: [{id: [{id: ext_line_id2, provider: organization.oauth_provider, realm: organization.oauth_uid}]}]}) + } + let(:batch_param) { + { + :sequential=>true, + :ops=> [ + { + :method=>"put", + :url=>"/api/v2/cld-123/people/#{idmap1.connec_id}", + :params=>{:people=>{id: [{:id=>idmap1.external_id, :provider=>organization.oauth_provider, :realm=>organization.oauth_uid}], lines: [{id: [{provider: 'connec', realm: 'org', id: connec_line_id1}, {id: ext_line_id1, provider: organization.oauth_provider, realm: organization.oauth_uid}]}]}.with_indifferent_access} + }, + { + :method=>"put", + :url=>"/api/v2/cld-123/people/#{idmap2.connec_id}", + :params=>{:people=>{id: [{:id=>'id', :provider=>organization.oauth_provider, :realm=>organization.oauth_uid}], lines: [{id: [{provider: 'connec', realm: 'org', id: connec_line_id2}, {id: ext_line_id2, provider: organization.oauth_provider, realm: organization.oauth_uid}]}]}.with_indifferent_access} + } + ] + } + } + + it 'send both the id and the id references to connec' do + expect(connec_client).to receive(:batch).with(batch_param).and_return(ActionDispatch::Response.new(200, {}, {results: []}.to_json, {})) + subject.push_entities_to_external_to(entities_with_idmaps, external_name) + end + end end describe 'push_entity_to_external' do context 'when the entity idmap has an external id' do it 'does not calls update if create_only' do @@ -589,20 +641,22 @@ expect(subject).to receive(:create_external_entity).with(entity2, external_name) subject.push_entity_to_external(entity_with_idmap2, external_name) end it 'updates the idmap external id, entity and last push' do - allow(subject).to receive(:create_external_entity).and_return('999111') + allow(subject).to receive(:create_external_entity).and_return({'id' => '999111'}) + allow(subject.class).to receive(:id_from_external_entity_hash).and_return('999111') subject.push_entity_to_external(entity_with_idmap2, external_name) idmap2.reload expect(idmap2.external_id).to eql('999111') expect(idmap2.last_push_to_external).to_not be_nil end it 'returns the idmap' do - allow(subject).to receive(:create_external_entity).and_return('999111') - expect(subject.push_entity_to_external(entity_with_idmap2, external_name)).to eql(idmap2) + allow(subject).to receive(:create_external_entity).and_return({'id' => '999111'}) + allow(subject.class).to receive(:id_from_external_entity_hash).and_return('999111') + expect(subject.push_entity_to_external(entity_with_idmap2, external_name)).to eql({idmap: idmap2, completed_hash: nil}) end end describe 'failures' do @@ -720,11 +774,11 @@ context 'when connec preemption is true' do let(:opts) { {connec_preemption: true} } it 'keep the connec one' do - expect(subject.consolidate_and_map_singleton([connec_entity], [{}])).to eql({connec_entities: [{entity: {map: 'external'}, idmap: idmap}], external_entities: []}) + expect(subject.consolidate_and_map_singleton([connec_entity], [{}])).to eql({connec_entities: [{entity: {map: 'external'}, idmap: idmap, id_refs_only_connec_entity: {}}], external_entities: []}) end it 'map with the unfolded references' do expect(subject).to receive(:map_to_external).with('id' => nil, 'updated_at' => updated) subject.consolidate_and_map_singleton([connec_entity], [{}]) @@ -736,11 +790,11 @@ context 'with a more recent external one' do it { expect(subject.consolidate_and_map_singleton([connec_entity], [{}])).to eql({connec_entities: [], external_entities: [{entity: {map: 'connec'}, idmap: idmap}]}) } end context 'with a more recent connec one' do let(:updated) { 2.minute.ago } - it { expect(subject.consolidate_and_map_singleton([connec_entity], [{}])).to eql({connec_entities: [{entity: {map: 'external'}, idmap: idmap}], external_entities: []}) } + it { expect(subject.consolidate_and_map_singleton([connec_entity], [{}])).to eql({connec_entities: [{entity: {map: 'external'}, idmap: idmap, id_refs_only_connec_entity: {}}], external_entities: []}) } end end end end @@ -752,20 +806,21 @@ let(:connec_id2) { 'connec-id-2' } let(:entity1) { {'id' => id1, 'name' => 'John', 'updated_at' => date, 'created_at' => date} } let(:entity2) { {'id' => id2, 'name' => 'Jane', 'updated_at' => date, 'created_at' => date} } let(:entity_without_refs) { {} } let(:entities) { [entity1, entity2] } + let(:id_refs_only_connec_entity) { {a:1} } before { allow(subject.class).to receive(:object_name_from_connec_entity_hash).and_return(connec_human_name) allow(subject).to receive(:map_to_external).and_return({mapped: 'entity'}) - allow(Maestrano::Connector::Rails::ConnecHelper).to receive(:unfold_references).and_return(entity1.merge(__connec_id: connec_id1), entity2.merge(__connec_id: connec_id2), nil) + allow(Maestrano::Connector::Rails::ConnecHelper).to receive(:unfold_references).and_return({entity: entity1, connec_id: connec_id1, id_refs_only_connec_entity: id_refs_only_connec_entity}, {entity: entity2, connec_id: connec_id2, id_refs_only_connec_entity: id_refs_only_connec_entity}, {entity: nil}) } context 'when idmaps do not exist' do it 'creates the idmaps with a name and returns the mapped entities with their idmaps' do expect{ - expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.first}, {entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.last}]) + expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.first, id_refs_only_connec_entity: id_refs_only_connec_entity}, {entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.last, id_refs_only_connec_entity: id_refs_only_connec_entity}]) }.to change{ Maestrano::Connector::Rails::IdMap.count }.by(2) expect(Maestrano::Connector::Rails::IdMap.last.name).to eql(connec_human_name) end end @@ -778,11 +833,11 @@ subject.consolidate_and_map_connec_entities(entities, [], [], external_name) }.to_not change{ Maestrano::Connector::Rails::IdMap.count } end it 'returns the entity with its idmap' do - expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: idmap1}]) + expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: idmap1, id_refs_only_connec_entity: id_refs_only_connec_entity}]) end context 'when external inactive' do before { idmap1.update(external_inactive: true) } it 'discards the entity' do @@ -805,11 +860,11 @@ context 'with full synchronization opts' do let(:opts) { {full_sync: true} } it 'keeps the entity' do - expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: idmap1}]) + expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: idmap1, id_refs_only_connec_entity: id_refs_only_connec_entity}]) end end end context 'when before date_filtering_limit' do @@ -823,11 +878,11 @@ context 'with full synchronization opts' do let(:opts) { {full_sync: true} } it 'keeps the entity' do - expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: idmap1}]) + expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: idmap1, id_refs_only_connec_entity: id_refs_only_connec_entity}]) end end end end @@ -850,11 +905,11 @@ end context 'with connec preemption true' do it 'keeps the entity and discards the external one' do subject.instance_variable_set(:@opts, {connec_preemption: true}) - expect(subject.consolidate_and_map_connec_entities(entities, external_entities, [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.first}]) + expect(subject.consolidate_and_map_connec_entities(entities, external_entities, [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.first, id_refs_only_connec_entity: id_refs_only_connec_entity}]) expect(external_entities).to be_empty end end end @@ -866,10 +921,10 @@ context 'with connec one more recent' do let(:external_date) { 1.year.ago } let(:date) { 1.day.ago } it 'keeps the entity and discards the external one' do - expect(subject.consolidate_and_map_connec_entities(entities, external_entities, [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.first}]) + expect(subject.consolidate_and_map_connec_entities(entities, external_entities, [], external_name)).to eql([{entity: {mapped: 'entity'}, idmap: Maestrano::Connector::Rails::IdMap.first, id_refs_only_connec_entity: id_refs_only_connec_entity}]) expect(external_entities).to be_empty end end context 'with external one more recent' do \ No newline at end of file