external_credential_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ExternalCredential, :aggregate_failures, current_user_id: 1, type: :model do
  4. describe '#update_client_secret' do
  5. let(:external_credential) { create(:external_credential, name: 'google', credentials:) }
  6. let(:credentials) do
  7. {
  8. 'client_secret' => 'dummy-1337',
  9. 'code' => 'code123',
  10. 'grant_type' => 'authorization_code',
  11. 'client_id' => 'dummy123',
  12. 'redirect_uri' => described_class.callback_url('google'),
  13. }
  14. end
  15. before do
  16. allow(ExternalCredential::Google).to receive(:update_client_secret).and_call_original
  17. end
  18. context 'when credentials are not changed' do
  19. it 'does not call update_client_secret on the backend module' do
  20. external_credential.update!(created_at: Time.zone.now)
  21. expect(ExternalCredential::Google).not_to have_received(:update_client_secret)
  22. end
  23. end
  24. context 'when credentials are changed' do
  25. context 'when client_secret is blank' do
  26. it 'does not call update_client_secret on the backend module' do
  27. external_credential.update!(credentials: credentials.merge('client_secret' => ''))
  28. expect(ExternalCredential::Google).not_to have_received(:update_client_secret)
  29. end
  30. end
  31. context 'when client_secret is not changed' do
  32. it 'does not call update_client_secret on the backend module' do
  33. external_credential.update!(credentials: credentials)
  34. expect(ExternalCredential::Google).not_to have_received(:update_client_secret)
  35. end
  36. end
  37. context 'when client_id is changed' do
  38. it 'does not call update_client_secret on the backend module' do
  39. external_credential.update!(credentials: credentials.merge('client_id' => 'dummy456'))
  40. expect(ExternalCredential::Google).not_to have_received(:update_client_secret)
  41. end
  42. end
  43. context 'when client_secret is changed' do
  44. it 'calls update_client_secret on the backend module' do
  45. external_credential.update!(credentials: credentials.merge('client_secret' => 'new-dummy-1337'))
  46. expect(ExternalCredential::Google).to have_received(:update_client_secret).with('dummy-1337', 'new-dummy-1337')
  47. end
  48. end
  49. end
  50. end
  51. end