Browse Source

Maintenance: Port create_or_update_with_ref test

Mantas 1 year ago
parent
commit
0cb999b0a7

+ 0 - 17
app/models/application_model/can_creates_and_updates.rb

@@ -47,23 +47,6 @@ returns
 
 =begin
 
-Model.create_if_not_exists with ref lookups
-
-  result = Model.create_if_not_exists_with_ref(attributes)
-
-returns
-
-  result = model # with all attributes
-
-=end
-
-    def create_if_not_exists_with_ref(data)
-      data = association_name_to_id_convert(data)
-      create_or_update(data)
-    end
-
-=begin
-
 Model.create_or_update with ref lookups
 
   result = Model.create_or_update(attributes)

+ 1 - 0
spec/factories/token.rb

@@ -3,6 +3,7 @@
 FactoryBot.define do
   factory :token, aliases: %i[token_api api_token] do
     user
+    name { Faker::Lorem.unique.word }
     action { 'api' }
     persistent { true }
     preferences do

+ 135 - 0
spec/models/application_model/can_associations_spec.rb

@@ -0,0 +1,135 @@
+# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe ApplicationModel::CanAssociations, type: :model do
+  describe '.association_name_to_id_convert' do
+    it 'converts has many users association by login' do
+      user   = create(:user)
+      params = { name: 'org', members: [user.login] }
+
+      converted_params = Organization.association_name_to_id_convert(params)
+
+      expect(converted_params)
+        .to eq({
+                 name:       'org',
+                 member_ids: [user.id]
+               })
+    end
+
+    it 'converts has many users association by email' do
+      user   = create(:user)
+      params = { name: 'org', members: [user.email] }
+
+      converted_params = Organization.association_name_to_id_convert(params)
+
+      expect(converted_params)
+        .to eq({
+                 name:       'org',
+                 member_ids: [user.id]
+               })
+    end
+
+    it 'keeps IDs for has many associations if given in non _id field' do
+      params           = { name: 'org', members: [123] }
+      converted_params = Organization.association_name_to_id_convert(params)
+
+      expect(converted_params)
+        .to eq({
+                 name:    'org',
+                 members: [123]
+               })
+    end
+
+    it 'raises error if has many association is given non-existant identifier' do
+      params = { name: 'org', members: ['nonexistantstring'] }
+
+      expect { Organization.association_name_to_id_convert(params) }
+        .to raise_error Exceptions::UnprocessableEntity, %r{No lookup value found}
+    end
+
+    it 'converts non-user has many association by name' do
+      token  = create(:token)
+      params = { email: 'email@example.org', tokens: [token.name] }
+
+      converted_params = User.association_name_to_id_convert(params)
+
+      expect(converted_params)
+        .to eq({
+                 email:     'email@example.org',
+                 token_ids: [token.id]
+               })
+    end
+
+    it 'does not convert named association if ids are given' do
+      user   = create(:user)
+      params = { name: 'org', members: [user.login], member_ids: [512] }
+
+      converted_params = Organization.association_name_to_id_convert(params)
+
+      expect(converted_params).to eq(params)
+    end
+
+    it 'converts belongs to users association by login' do
+      user   = create(:user)
+      params = { name: 'token', user: user.login }
+
+      converted_params = Token.association_name_to_id_convert(params)
+
+      expect(converted_params)
+        .to eq({
+                 name:    'token',
+                 user_id: user.id
+               })
+    end
+
+    it 'converts belongs to users association by email' do
+      user   = create(:user)
+      params = { name: 'token', user: user.email }
+
+      converted_params = Token.association_name_to_id_convert(params)
+
+      expect(converted_params).to eq({
+                                       name:    'token',
+                                       user_id: user.id
+                                     })
+    end
+
+    it 'raises error if belongs to association is given a non-existant identifier' do
+      params = { name: 'token', user: 'nonexistantstring' }
+
+      expect { Token.association_name_to_id_convert(params) }
+        .to raise_error Exceptions::UnprocessableEntity, %r{No lookup value found}
+    end
+
+    it 'converts non-user belongs to association by name' do
+      organization = create(:organization)
+      params       = { email: 'email@example.org', organization: organization.name }
+
+      converted_params = User.association_name_to_id_convert(params)
+
+      expect(converted_params)
+        .to eq({
+                 email:           'email@example.org',
+                 organization_id: organization.id
+               })
+    end
+
+    it 'overrides belongs to association if both name and ID are given' do
+      organization = create(:organization)
+      params       = { email: 'email@example.org', organization: organization.name, organization_id: 123 }
+
+      converted_params = User.association_name_to_id_convert(params)
+
+      expect(converted_params).to eq(params)
+    end
+
+    it 'raises error if ID is given as belongs to identifier' do
+      organization = create(:organization)
+      params       = { email: 'email@example.org', organization: organization.id }
+
+      expect { User.association_name_to_id_convert(params) }
+        .to raise_error Exceptions::UnprocessableEntity, %r{No lookup value found}
+    end
+  end
+end

+ 20 - 0
spec/models/application_model/can_creates_and_updates_examples.rb

@@ -272,4 +272,24 @@ RSpec.shared_examples 'ApplicationModel::CanCreatesAndUpdates' do |unique_name:
     include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
     include_examples 'for #locale attribute' if described_class.attribute_names.include?('locale')
   end
+
+  describe '.create_or_update_with_ref' do
+    before do
+      allow(described_class)
+        .to receive(:association_name_to_id_convert).and_return(converted_params)
+
+      allow(described_class)
+        .to receive(:create_or_update)
+    end
+
+    let(:given_params) { { given: 'attr' } }
+    let(:converted_params) { { converted: 'attr' } }
+
+    it 'calls create_or_update with given data', aggregate_failures: true do
+      described_class.create_or_update_with_ref(given_params)
+
+      expect(described_class).to have_received(:association_name_to_id_convert).with(given_params)
+      expect(described_class).to have_received(:create_or_update).with(converted_params)
+    end
+  end
 end

+ 0 - 170
test/unit/object_create_update_with_ref_name_test.rb

@@ -1,170 +0,0 @@
-# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
-
-require 'test_helper'
-
-class ObjectCreateUpdateWithRefNameTest < ActiveSupport::TestCase
-  test 'organization' do
-    roles  = Role.where(name: %w[Agent Admin])
-    groups = Group.all
-    user1 = User.create_or_update(
-      login:         'object_ref_name1@example.org',
-      firstname:     'object_ref_name1',
-      lastname:      'object_ref_name1',
-      email:         'object_ref_name1@example.org',
-      password:      'some_pass',
-      active:        true,
-      updated_by_id: 1,
-      created_by_id: 1,
-      roles:         roles,
-      groups:        groups,
-    )
-    user2 = User.create_or_update(
-      login:         'object_ref_name2@example.org',
-      firstname:     'object_ref_name2',
-      lastname:      'object_ref_name2',
-      email:         'object_ref_name2@example.org',
-      password:      'some_pass',
-      active:        true,
-      updated_by_id: 1,
-      created_by_id: 1,
-      roles:         roles,
-      groups:        groups,
-    )
-
-    org1 = Organization.create_if_not_exists_with_ref(
-      name:          'some org update_with_ref member',
-      members:       ['object_ref_name1@example.org'],
-      updated_by_id: 1,
-      created_by_id: 1,
-    )
-
-    assert(org1.member_ids.sort.include?(user1.id))
-    assert_not(org1.member_ids.sort.include?(user2.id))
-
-    org2 = Organization.create_or_update_with_ref(
-      name:          'some org update_with_ref member',
-      members:       ['object_ref_name2@example.org'],
-      updated_by_id: 1,
-      created_by_id: 1,
-    )
-
-    assert_not(org2.member_ids.sort.include?(user1.id))
-    assert(org2.member_ids.sort.include?(user2.id))
-    assert_equal(org1.id, org2.id)
-
-    org3 = Organization.create_or_update_with_ref(
-      name:          'some org update_with_ref member2',
-      members:       ['object_ref_name2@example.org'],
-      updated_by_id: 1,
-      created_by_id: 1,
-    )
-
-    assert_not(org3.member_ids.sort.include?(user1.id))
-    assert(org3.member_ids.sort.include?(user2.id))
-    assert_not_equal(org2.id, org3.id)
-
-    assert_raises(ActiveRecord::AssociationTypeMismatch) do
-      Organization.create_or_update_with_ref(
-        name:          'some org update_with_ref member2',
-        members:       ['object_ref_name2@example.org'],
-        member_ids:    [2],
-        updated_by_id: 1,
-        created_by_id: 1,
-      )
-    end
-
-  end
-
-  test 'user' do
-    Organization.create_if_not_exists_with_ref(
-      name:          'some org update_with_ref user',
-      updated_by_id: 1,
-      created_by_id: 1,
-    )
-    user1 = User.create_or_update_with_ref(
-      login:         'object_ref_name1@example.org',
-      firstname:     'object_ref_name1',
-      lastname:      'object_ref_name1',
-      email:         'object_ref_name1@example.org',
-      password:      'some_pass',
-      active:        true,
-      organization:  'some org update_with_ref user',
-      updated_by_id: 1,
-      created_by_id: 1,
-      roles:         %w[Agent Admin],
-      groups:        ['Users'],
-    )
-    user2 = User.create_or_update_with_ref(
-      login:           'object_ref_name2@example.org',
-      firstname:       'object_ref_name2',
-      lastname:        'object_ref_name2',
-      email:           'object_ref_name2@example.org',
-      password:        'some_pass',
-      organization_id: nil,
-      active:          true,
-      updated_by_id:   1,
-      created_by_id:   1,
-      roles:           ['Customer'],
-      groups:          [],
-    )
-    admin_role = Role.lookup(name: 'Admin')
-    agent_role = Role.lookup(name: 'Agent')
-    customer_role = Role.lookup(name: 'Customer')
-
-    users_group = Group.lookup(name: 'Users')
-
-    assert(user1.organization.name, 'some org update_with_ref user')
-    assert(user1.group_ids.include?(users_group.id))
-    assert(user1.role_ids.include?(admin_role.id))
-    assert(user1.role_ids.include?(agent_role.id))
-    assert_not(user1.role_ids.include?(customer_role.id))
-
-    assert_nil(user2.organization_id)
-    assert_not(user2.group_ids.include?(users_group.id))
-    assert_not(user2.role_ids.include?(admin_role.id))
-    assert_not(user2.role_ids.include?(agent_role.id))
-    assert(user2.role_ids.include?(customer_role.id))
-
-  end
-
-  test 'group' do
-    user1 = User.create_or_update_with_ref(
-      login:           'object_ref_name1@example.org',
-      firstname:       'object_ref_name1',
-      lastname:        'object_ref_name1',
-      email:           'object_ref_name1@example.org',
-      password:        'some_pass',
-      active:          true,
-      organization_id: nil,
-      updated_by_id:   1,
-      created_by_id:   1,
-      roles:           %w[Agent Admin],
-      groups:          [],
-    )
-    user2 = User.create_or_update_with_ref(
-      login:           'object_ref_name2@example.org',
-      firstname:       'object_ref_name2',
-      lastname:        'object_ref_name2',
-      email:           'object_ref_name2@example.org',
-      password:        'some_pass',
-      organization_id: nil,
-      active:          true,
-      updated_by_id:   1,
-      created_by_id:   1,
-      roles:           ['Customer'],
-      groups:          [],
-    )
-
-    group1 = Group.create_if_not_exists_with_ref(
-      name:          'some group update_with_ref',
-      users:         ['object_ref_name1@example.org'],
-      updated_by_id: 1,
-      created_by_id: 1,
-    )
-
-    assert(group1.name, 'some group update_with_ref')
-    assert(group1.user_ids.include?(user1.id))
-    assert_not(group1.user_ids.include?(user2.id))
-
-  end
-end