channel_email_account_uniqueness_validator_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Validations::ChannelEmailAccountUniquenessValidator do
  4. subject(:validator) { described_class.new }
  5. before { Channel.destroy_all }
  6. it 'a new record in empty database passes' do
  7. channel = build(:email_channel, :smtp, :imap)
  8. validator.validate(channel)
  9. expect(channel.errors).to be_blank
  10. end
  11. context 'with an existing record' do
  12. let(:mail_server_user) { 'user@example.com' }
  13. before { create(:email_channel, :smtp, :imap, mail_server_user:) }
  14. it 'identical record fails' do
  15. channel = build(:email_channel, :smtp, :imap, mail_server_user:)
  16. validator.validate(channel)
  17. expect(channel.errors).to be_present
  18. end
  19. it 'record with a different inbound server passes' do
  20. channel = build(:email_channel, :smtp, :pop3, mail_server_user:)
  21. validator.validate(channel)
  22. expect(channel.errors).to be_blank
  23. end
  24. context 'with another existing record' do
  25. let(:another_channel) { create(:email_channel, :smtp, :imap, mail_server_user: 'other@example.com') }
  26. it 'editing a persisted record to be identical fails' do
  27. another_channel.options[:inbound][:options][:user] = mail_server_user
  28. validator.validate(another_channel)
  29. expect(another_channel.errors).to be_present
  30. end
  31. it 'editing a persisted record passes' do
  32. another_channel.options[:inbound][:options][:folder] = 'foobar'
  33. validator.validate(another_channel)
  34. expect(another_channel.errors).to be_blank
  35. end
  36. end
  37. # https://github.com/zammad/zammad/issues/5111
  38. context 'with multiple identical channels' do
  39. let(:duplicate_channel) { create(:google_channel, gmail_user: 'email@example.com') }
  40. let(:editable_channel) do
  41. build(:google_channel, gmail_user: 'email@example.com')
  42. .tap { _1.save!(validate: false) }
  43. end
  44. let(:new_token) { 'new_xoauth2_token' }
  45. before do
  46. duplicate_channel
  47. allow(ExternalCredential)
  48. .to receive(:refresh_token).and_return(access_token: new_token)
  49. end
  50. it 'allows to edit XOauth2 token if identical channel exists' do
  51. editable_channel.refresh_xoauth2!(force: true)
  52. expect(editable_channel.options).to include(
  53. inbound: include(options: include(password: new_token)),
  54. outbound: include(options: include(password: new_token)),
  55. )
  56. end
  57. end
  58. context 'with existing microsoft shared mailbox channel' do
  59. before do
  60. create(:microsoft_graph_channel, microsoft_user: 'email@example.com', microsoft_shared_mailbox: 'shared@example.com')
  61. end
  62. it 'record with user mailbox passes' do
  63. channel = build(:microsoft_graph_channel, microsoft_user: 'email@example.com')
  64. validator.validate(channel)
  65. expect(channel.errors).to be_blank
  66. end
  67. it 'record with a diffeen shared mailbox passes' do
  68. channel = build(:microsoft_graph_channel, microsoft_user: 'email@example.com', microsoft_shared_mailbox: 'another-shared@example.com')
  69. validator.validate(channel)
  70. expect(channel.errors).to be_blank
  71. end
  72. it 'record with same shared mailbox fails' do
  73. channel = build(:microsoft_graph_channel, microsoft_user: 'email@example.com', microsoft_shared_mailbox: 'shared@example.com')
  74. validator.validate(channel)
  75. expect(channel.errors).to be_present
  76. end
  77. end
  78. end
  79. end