channel_email_account_uniqueness_validator_spec.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012-2024 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. end
  59. end