From 05fa609eb585415df18d86e8cc6a4f91375a9e7f Mon Sep 17 00:00:00 2001 From: Dominik Klein Date: Wed, 15 Sep 2021 16:27:40 +0200 Subject: [PATCH] Fixes #3203 - Removal of Google-Channel not possible via UI when having a normal email channel of same account. --- app/controllers/channels_google_controller.rb | 4 +- spec/requests/channels_google_spec.rb | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 spec/requests/channels_google_spec.rb diff --git a/app/controllers/channels_google_controller.rb b/app/controllers/channels_google_controller.rb index ebd008639..7c2388252 100644 --- a/app/controllers/channels_google_controller.rb +++ b/app/controllers/channels_google_controller.rb @@ -54,8 +54,8 @@ class ChannelsGoogleController < ApplicationController def destroy channel = Channel.find_by(id: params[:id], area: 'Google::Account') - email = EmailAddress.find_by(channel_id: channel.id) - email.destroy! + email = EmailAddress.find_by(channel_id: channel.id) + email&.destroy! channel.destroy! render json: {} end diff --git a/spec/requests/channels_google_spec.rb b/spec/requests/channels_google_spec.rb new file mode 100644 index 000000000..47b1100d9 --- /dev/null +++ b/spec/requests/channels_google_spec.rb @@ -0,0 +1,50 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe 'Google channel API endpoints', type: :request do + let(:admin) { create(:admin) } + let!(:google_channel) { create(:google_channel) } + + describe 'DELETE /api/v1/channels_google', authenticated_as: :admin do + context 'without a email address relation' do + let(:params) do + { + id: google_channel.id + } + end + + it 'responds 200 OK' do + delete '/api/v1/channels_google', params: params, as: :json + + expect(response).to have_http_status(:ok) + end + + it 'google channel deleted' do + expect { delete '/api/v1/channels_google', params: params, as: :json }.to change(Channel, :count).by(-1) + end + end + + context 'with a email address relation' do + let(:params) do + { + id: google_channel.id + } + end + + before do + create(:email_address, channel: google_channel) + end + + it 'responds 200 OK' do + delete '/api/v1/channels_google', params: params, as: :json + + expect(response).to have_http_status(:ok) + end + + it 'google channel and related email address deleted' do + expect { delete '/api/v1/channels_google', params: params, as: :json }.to change(Channel, :count).by(-1).and change(EmailAddress, :count).by(-1) + end + end + end +end