models_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Models do
  4. describe '.merge' do
  5. context 'when ExternalSync references are present' do
  6. shared_examples 'migrates entries' do |model|
  7. let(:factory_name) { model.downcase.to_sym }
  8. let(:source) { create(factory_name) }
  9. let(:target) { create(factory_name) }
  10. it 'sends ExternalSync.migrate' do
  11. allow(ExternalSync).to receive(:migrate)
  12. described_class.merge(model, source.id, target.id)
  13. expect(ExternalSync).to have_received(:migrate).with(model, source.id, target.id)
  14. end
  15. end
  16. it_behaves_like 'migrates entries', 'User'
  17. end
  18. end
  19. describe '.searchable' do
  20. it 'lists all needed models' do
  21. expect(described_class.searchable).to contain_exactly(Ticket, User, Organization, Chat::Session, KnowledgeBase::Answer::Translation)
  22. end
  23. end
  24. describe '.references' do
  25. # User object is handled slightly differently
  26. # Due to not having Rails relations for performance reasons
  27. context 'with User' do
  28. let(:role) { create(:role) }
  29. let(:group1) { create(:group) }
  30. let(:group2) { create(:group) }
  31. let(:user1) { create(:user, roles: [role], groups: [group1, group2]) }
  32. let(:user2) do
  33. create(:user, roles: [role], groups: [group1, group2],
  34. updated_by_id: user1.id)
  35. end
  36. let(:user3) do
  37. create(:user, roles: [role], groups: [group1, group2],
  38. updated_by_id: user1.id, created_by_id: user1.id)
  39. end
  40. let(:organization) { create(:organization, updated_by_id: user1.id) }
  41. before do
  42. user1 && user2 && user3 && organization
  43. end
  44. it 'returns existing references' do
  45. references = described_class.references('User', user1.id)
  46. expect(references)
  47. .to eq({
  48. 'History' => { 'created_by_id' => 1 },
  49. 'Organization' => { 'updated_by_id' => 1 },
  50. 'User' => {
  51. 'created_by_id' => 1,
  52. 'updated_by_id' => 2
  53. },
  54. 'UserGroup' => { 'user_id' => 2 }
  55. })
  56. end
  57. it 'returns existing references and includes attributes with no references found' do
  58. references = described_class.references('User', user1.id, true)
  59. expect(references).to include(
  60. 'History' => { 'created_by_id' => 1, },
  61. 'Organization' => {
  62. 'created_by_id' => 0,
  63. 'updated_by_id' => 1
  64. },
  65. 'User' => {
  66. 'created_by_id' => 1, 'out_of_office_replacement_id' => 0, 'updated_by_id' => 2
  67. },
  68. 'UserGroup' => { 'user_id' => 2 },
  69. 'Taskbar' => { 'user_id'=>0 },
  70. )
  71. end
  72. end
  73. context 'with an example object' do
  74. let(:organization) { create(:organization) }
  75. let(:user) { create(:user, organization: organization) }
  76. before { user }
  77. it 'returns existing references' do
  78. references = described_class.references('Organization', organization.id, false)
  79. expect(references).to eq({
  80. 'User' => { 'organization_id'=>1 },
  81. })
  82. end
  83. it 'returns existing references and includes attributes with no references found' do
  84. references = described_class.references('Organization', organization.id, true)
  85. expect(references).to eq({
  86. 'Ticket' => { 'organization_id'=>0 },
  87. 'User' => { 'organization_id'=>1 },
  88. })
  89. end
  90. it 'raises an error if non existing object is given' do
  91. expect { described_class.references('Organization', 123_456) }
  92. .to raise_error(ActiveRecord::RecordNotFound)
  93. end
  94. end
  95. end
  96. describe '.references_total' do
  97. it 'returns total count' do
  98. allow(described_class)
  99. .to receive(:references)
  100. .and_return({
  101. 'Klass' => { 'attr_id' => 1 },
  102. 'AnotherKlass' => { 'attr_id' => 2, 'something_id' => 4 },
  103. })
  104. count = described_class.references_total('User', 123)
  105. expect(count).to eq(7)
  106. end
  107. end
  108. end