spec/models/entity_spec.rb in maestrano-connector-rails-1.3.5 vs spec/models/entity_spec.rb in maestrano-connector-rails-1.4.0
- old
+ new
@@ -5,14 +5,14 @@
describe 'class methods' do
subject { Maestrano::Connector::Rails::Entity }
# IdMap methods
describe 'idmaps methods' do
- before {
+ before do
allow(subject).to receive(:connec_entity_name).and_return('Ab')
allow(subject).to receive(:external_entity_name).and_return('Ab')
- }
+ end
let(:n_hash) { {connec_entity: 'ab', external_entity: 'ab'} }
it { expect(subject.names_hash).to eql(n_hash) }
it {
@@ -30,17 +30,17 @@
subject.create_idmap({id: 'lala'})
}
end
describe 'normalized_connec_entity_name' do
- before {
+ before do
allow(subject).to receive(:connec_entity_name).and_return(connec_name)
- }
+ end
context 'for a singleton resource' do
- before {
+ before do
allow(subject).to receive(:singleton?).and_return(true)
- }
+ end
context 'for a simple name' do
let(:connec_name) { 'Person' }
it { expect(subject.normalized_connec_entity_name).to eql('person') }
end
@@ -50,13 +50,13 @@
it { expect(subject.normalized_connec_entity_name).to eql('credit_note') }
end
end
context 'for a non singleton resource' do
- before {
+ before do
allow(subject).to receive(:singleton?).and_return(false)
- }
+ end
context 'for a simple name' do
let(:connec_name) { 'Person' }
it { expect(subject.normalized_connec_entity_name).to eql('people') }
end
@@ -122,13 +122,13 @@
allow(subject).to receive(:connec_entity_name).and_return('tree')
expect(subject.public_connec_entity_name).to eql('trees')
end
context 'when singleton' do
- before {
+ before do
allow(subject).to receive(:singleton?).and_return(true)
- }
+ end
it 'returns the connec_entity_name' do
allow(subject).to receive(:connec_entity_name).and_return('tree')
expect(subject.public_connec_entity_name).to eql('tree')
end
@@ -149,39 +149,54 @@
let!(:external_client) { Object.new }
let(:opts) { {} }
subject { Maestrano::Connector::Rails::Entity.new(organization, connec_client, external_client, opts) }
let(:connec_name) { 'Person' }
let(:external_name) { 'external_name' }
- before {
+ before do
allow(subject.class).to receive(:connec_entity_name).and_return(connec_name)
allow(subject.class).to receive(:external_entity_name).and_return(external_name)
- }
+ end
describe 'Mapper methods' do
- before(:each) {
+ before(:each) do
class AMapper
extend HashMapper
end
allow(subject.class).to receive(:mapper_class).and_return(AMapper)
- }
+ allow(subject.class).to receive(:creation_mapper_class).and_call_original
+ end
describe 'map_to_external' do
it 'calls the mapper normalize' do
- expect(AMapper).to receive(:normalize).with({}).and_return({})
+ expect(AMapper).to receive(:normalize).with({}, subject.instance_values).and_return({})
subject.map_to_external({})
end
+
+ it 'calls the creation_mapper normalize if passed true as second argument' do
+ expect(subject.class).to receive(:creation_mapper_class)
+ #if not overridden #creation_mapper_class calls #mapper_class
+ expect(AMapper).to receive(:normalize).with({}, subject.instance_values).and_return({})
+ subject.map_to_external({}, true)
+ end
end
describe 'map_to_connec' do
- before {
+ before do
allow(subject.class).to receive(:id_from_external_entity_hash).and_return('this id')
- }
+ end
it 'calls the mapper denormalize' do
- expect(AMapper).to receive(:denormalize).with({}).and_return({})
+ expect(AMapper).to receive(:denormalize).with({}, subject.instance_values).and_return({})
subject.map_to_connec({})
end
+ it 'calls the creation_mapper denormalize if passed true as second argument' do
+ expect(subject.class).to receive(:creation_mapper_class)
+ #if not overridden #creation_mapper_class calls #mapper_class
+ expect(AMapper).to receive(:denormalize).with({}, subject.instance_values).and_return({})
+ subject.map_to_connec({}, true)
+ end
+
it 'calls for reference folding' do
refs = %w(organization_id person_id)
allow(subject.class).to receive(:references).and_return(refs)
expect(Maestrano::Connector::Rails::ConnecHelper).to receive(:fold_references).with({id: 'this id'}, refs, organization)
subject.map_to_connec({})
@@ -205,14 +220,14 @@
end
end
describe 'get_connec_entities' do
describe 'when write only' do
- before {
+ before do
allow(subject.class).to receive(:can_read_connec?).and_return(false)
allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {people: [{first_name: 'Lea'}]}.to_json, {}))
- }
+ end
it { expect(subject.get_connec_entities(nil)).to eql([]) }
end
describe 'when skip_connec' do
@@ -220,31 +235,31 @@
it { expect(subject.get_connec_entities(nil)).to eql([]) }
end
describe 'with response' do
context 'for a singleton resource' do
- before {
+ before do
allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {person: []}.to_json, {}))
allow(subject.class).to receive(:singleton?).and_return(true)
- }
+ end
it 'calls get with a singularize url' do
expect(connec_client).to receive(:get).with("#{connec_name.downcase}?")
subject.get_connec_entities(nil)
end
end
context 'for a non singleton resource' do
- before {
+ before do
allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {people: []}.to_json, {}))
- }
+ end
context 'with limit and skip opts' do
let(:opts) { {__skip: 100, __limit: 50} }
- before {
+ before do
allow(connec_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, {}))
- }
+ end
it 'performs a size limited date and do not paginate' do
uri_param = {"$filter" => "updated_at gt '#{sync.updated_at.iso8601}'", "$skip" => 100, "$top" => 50}.to_query
expect(connec_client).to receive(:get).once.with("#{connec_name.downcase.pluralize}?#{uri_param}")
subject.get_connec_entities(sync.updated_at)
@@ -296,13 +311,13 @@
subject.get_connec_entities(sync.updated_at)
end
end
context 'with pagination' do
- before {
+ before do
allow(connec_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, {}))
- }
+ end
it 'calls get multiple times' do
expect(connec_client).to receive(:get).with('people?')
expect(connec_client).to receive(:get).with('people?%24skip=10&%24top=10')
subject.get_connec_entities(nil)
@@ -320,34 +335,34 @@
end
end
describe 'failures' do
context 'when no response' do
- before {
+ before do
allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, nil, {}))
- }
+ end
it { expect{ subject.get_connec_entities(nil) }.to raise_error(RuntimeError) }
end
context 'when invalid response' do
- before {
+ before do
allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {not_an_entity: []}.to_json, {}))
- }
+ end
it { expect{ subject.get_connec_entities(nil) }.to raise_error(RuntimeError) }
end
context 'when no response in pagination' do
- before {
+ before do
allow(connec_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, {}, nil, {}))
- }
+ end
it { expect{ subject.get_connec_entities(nil) }.to raise_error(RuntimeError) }
end
context 'when invalid response in pagination' do
- before {
+ before do
allow(connec_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, {}, {not_an_entity: []}.to_json, {}))
- }
+ end
it { expect{ subject.get_connec_entities(nil) }.to raise_error(RuntimeError) }
end
end
end
@@ -366,38 +381,38 @@
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] }
context 'when read only' do
- before {
+ before do
allow(subject.class).to receive(:can_write_connec?).and_return(false)
- }
+ end
it 'does nothing' do
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 {
+ before do
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, {}))
- }
+ end
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 {
+ before do
allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(200, {}, {results: [result200, result201]}.to_json, {}))
- }
+ end
let(:batch_request) {
{
sequential: true,
ops: [
@@ -439,33 +454,33 @@
describe 'batch batch calls' do
let(:entities) { [] }
let(:results) { [] }
context 'when 100 entities' do
- before {
+ before do
100.times do
entities << entity_with_idmap1
results << result200
end
allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(200, {}, {results: results}.to_json, {}))
- }
+ end
it 'does one call' do
expect(connec_client).to receive(:batch).once
subject.push_entities_to_connec_to(entities, connec_name)
end
end
context 'when more than 100 entities' do
- before {
+ before do
100.times do
entities << entity_with_idmap1
results << result200
end
entities << entity_with_idmap2
allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(200, {}, {results: results}.to_json, {}), ActionDispatch::Response.new(200, {}, {results: [result201]}.to_json, {}))
- }
+ end
it 'does several call' do
expect(connec_client).to receive(:batch).twice
subject.push_entities_to_connec_to(entities, connec_name)
end
@@ -480,13 +495,13 @@
end
context 'with errors' do
let(:err_msg) { 'Not Found' }
let(:result400) { {status: 400, body: err_msg} }
- before {
+ before do
allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(200, {}, {results: [result400, result400]}.to_json, {}))
- }
+ end
it 'stores the errr in the idmap' do
subject.push_entities_to_connec_to(entities_with_idmaps, '')
idmap2.reload
expect(idmap2.message).to eq result400[:body]
@@ -504,13 +519,13 @@
end
end
# External methods
describe 'external methods' do
- before {
+ before do
allow(subject.class).to receive(:id_from_external_entity_hash).and_return('id')
- }
+ end
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(:id_refs_only_connec_entity1) { {} }
@@ -569,15 +584,15 @@
expect(subject).to receive(:push_entity_to_external).twice
subject.push_entities_to_external_to(entities_with_idmaps, external_name)
end
describe 'ids' do
- before {
+ before do
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)
- }
+ end
context 'when ids to send to connec' do
let(:batch_param) {
{:sequential=>true, :ops=>[{:method=>"put", :url=>"/api/v2/cld-123/people/#{idmap2.connec_id}", :params=>{:people=>{id: [{:id=>"id", :provider=>organization.oauth_provider, :realm=>organization.oauth_uid}]}}}]}
}
@@ -587,13 +602,13 @@
subject.push_entities_to_external_to(entities_with_idmaps, external_name)
end
end
context 'when no id to send to connec' do
- before {
+ before do
idmap2.update(external_id: 'id')
- }
+ end
it 'does not do a call on connec' do
expect(connec_client).to_not receive(:batch)
subject.push_entities_to_external_to(entities_with_idmaps, external_name)
end
@@ -723,21 +738,21 @@
end
describe 'consolidate_and_map methods' do
let(:id) { '56882' }
let(:date) { 2.hour.ago }
- before {
+ before do
allow(subject.class).to receive(:id_from_external_entity_hash).and_return(id)
allow(subject.class).to receive(:last_update_date_from_external_entity_hash).and_return(date)
allow(subject.class).to receive(:creation_date_from_external_entity_hash).and_return(date)
- }
+ end
describe 'consolidate_and_map_data' do
context 'singleton' do
- before {
+ before do
allow(subject.class).to receive(:singleton?).and_return(true)
- }
+ end
it 'returns the consolidate_and_map_singleton method result' do
expect(subject).to receive(:consolidate_and_map_singleton).with({}, {}).and_return({result: 1})
expect(subject.consolidate_and_map_data({}, {})).to eql({result: 1})
end
@@ -752,16 +767,16 @@
end
end
describe 'consolidate_and_map_singleton' do
let(:connec_id) { [{'id' => 'lala', 'provider' => 'connec', 'realm' => 'realm'}] }
- before {
+ before do
allow(subject).to receive(:map_to_connec).and_return({map: 'connec'})
allow(subject).to receive(:map_to_external).and_return({map: 'external'})
allow(subject.class).to receive(:object_name_from_connec_entity_hash).and_return('connec human name')
allow(subject.class).to receive(:object_name_from_external_entity_hash).and_return('external human name')
- }
+ end
it { expect(subject.consolidate_and_map_singleton([], [])).to eql({connec_entities: [], external_entities: []}) }
context 'with no idmap' do
it 'creates one for connec' do
@@ -897,13 +912,13 @@
end
end
end
context 'when before date_filtering_limit' do
- before {
+ before do
organization.update(date_filtering_limit: 5.minutes.ago)
- }
+ end
it 'discards the entity' do
expect(subject.consolidate_and_map_connec_entities(entities, [], [], external_name)).to eql([])
end
@@ -920,13 +935,13 @@
context 'when conflict' do
let(:entities) { [entity1] }
let(:external_entity_1) { {'id' => id1} }
let(:external_entities) { [external_entity_1] }
- before {
+ before do
allow(subject.class).to receive(:id_from_external_entity_hash).and_return(id1)
- }
+ end
context 'with opts' do
context 'with connec preemption false' do
it 'discards the entity and keep the external one' do
subject.instance_variable_set(:@opts, {connec_preemption: false})
@@ -943,13 +958,13 @@
end
end
end
context 'without opts' do
- before {
+ before do
allow(subject.class).to receive(:last_update_date_from_external_entity_hash).and_return(external_date)
- }
+ end
context 'with connec one more recent' do
let(:external_date) { 1.year.ago }
let(:date) { 1.day.ago }
@@ -974,15 +989,15 @@
describe 'consolidate_and_map_external_entities' do
let(:entity) { {'id' => id, 'name' => 'Jane'} }
let(:id) { 'id' }
let(:external_human_name) { 'external human name' }
- before {
+ before do
allow(subject.class).to receive(:id_from_external_entity_hash).and_return(id)
allow(subject.class).to receive(:object_name_from_external_entity_hash).and_return(external_human_name)
allow(subject).to receive(:map_to_connec).and_return({mapped: 'ext_entity'})
- }
+ end
context 'when idmap exists' do
let!(:idmap) { create(:idmap, organization: organization, connec_entity: connec_name.downcase, external_entity: external_name.downcase, external_id: id) }
it 'does not create one' do
@@ -1002,13 +1017,13 @@
expect(subject.consolidate_and_map_external_entities([entity], connec_name)).to eql([])
end
end
context 'when entity is inactive' do
- before {
+ before do
allow(subject.class).to receive(:inactive_from_external_entity_hash?).and_return(true)
- }
+ end
it 'discards the entity' do
expect(subject.consolidate_and_map_external_entities([entity], connec_name)).to eql([])
end
@@ -1033,12 +1048,12 @@
end
end
end
context 'when before date_filtering_limit' do
- before {
+ before do
organization.update(date_filtering_limit: 5.minutes.ago)
- }
+ end
it 'discards the entity' do
expect(subject.consolidate_and_map_external_entities([entity], connec_name)).to eql([])
end