feat: add Google Email fetch and OAuth token refresh service (#9603)

This PR adds the following changes

1. Add `Imap::GoogleFetchEmailService` and
`Google::RefreshOauthTokenService`. The
`Google::RefreshOauthTokenService` uses
`OmniAuth::Strategies::GoogleOauth2` which is already added as a packge
2. Update `Inboxes::FetchImapEmailsJob` to handle Google inboxes
3. Add SMTP settings for Google in `ConversationReplyMailerHelper` to
allow sending emails


## Preview

#### Incoming emails

![CleanShot 2024-06-06 at 17 17
22@2x](https://github.com/chatwoot/chatwoot/assets/18097732/9d7d70d1-68e3-4c16-b1ca-e5a2e6f890e8)

#### Outgoing email

![CleanShot 2024-06-06 at 17 18
05@2x](https://github.com/chatwoot/chatwoot/assets/18097732/1b4abf0e-e311-493e-bdc8-386886afbb25)

---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Shivam Mishra
2024-06-11 14:22:03 +05:30
committed by GitHub
parent 9689bbf0dd
commit 650fee58a6
10 changed files with 236 additions and 68 deletions

View File

@@ -283,6 +283,22 @@ RSpec.describe ConversationReplyMailer do
end
end
context 'when smtp enabled for google email channel' do
let(:ms_smtp_email_channel) do
create(:channel_email, imap_login: 'smtp@gmail.com',
imap_enabled: true, account: account, provider: 'google', provider_config: { access_token: 'access_token' })
end
let(:conversation) { create(:conversation, assignee: agent, inbox: ms_smtp_email_channel.inbox, account: account).reload }
let(:message) { create(:message, conversation: conversation, account: account, message_type: 'outgoing', content: 'Outgoing Message 2') }
it 'use smtp mail server' do
mail = described_class.email_reply(message)
expect(mail.delivery_method.settings.empty?).to be false
expect(mail.delivery_method.settings[:address]).to eq 'smtp.gmail.com'
expect(mail.delivery_method.settings[:port]).to eq 587
end
end
context 'when smtp disabled for email channel', :test do
let(:conversation) { create(:conversation, assignee: agent, inbox: email_channel.inbox, account: account).reload }
let(:message) { create(:message, conversation: conversation, account: account, message_type: 'outgoing', content: 'Outgoing Message 2') }

View File

@@ -35,4 +35,15 @@ RSpec.describe Channel::Email do
expect(channel.microsoft?).to be(true)
end
end
context 'when google?' do
it 'returns false' do
expect(channel.google?).to be(false)
end
it 'returns true' do
channel.provider = 'google'
expect(channel.google?).to be(true)
end
end
end

View File

@@ -0,0 +1,89 @@
require 'rails_helper'
RSpec.describe Google::RefreshOauthTokenService do
let!(:google_channel) { create(:channel_email, :microsoft_email) }
let!(:google_channel_with_expired_token) do
create(
:channel_email, :microsoft_email, provider_config: {
expires_on: Time.zone.now - 3600,
access_token: SecureRandom.hex,
refresh_token: SecureRandom.hex
}
)
end
let(:new_tokens) do
{
access_token: SecureRandom.hex,
refresh_token: SecureRandom.hex,
expires_at: (Time.zone.now + 3600).to_i,
token_type: 'bearer'
}
end
context 'when token is not expired' do
it 'returns the existing access token' do
service = described_class.new(channel: google_channel)
expect(service.access_token).to eq(google_channel.provider_config['access_token'])
expect(google_channel.reload.provider_config['refresh_token']).to eq(google_channel.provider_config['refresh_token'])
end
end
describe 'on expired token or invalid expiry' do
before do
stub_request(:post, 'https://oauth2.googleapis.com/token').with(
body: { 'grant_type' => 'refresh_token', 'refresh_token' => google_channel_with_expired_token.provider_config['refresh_token'] }
).to_return(status: 200, body: new_tokens.to_json, headers: { 'Content-Type' => 'application/json' })
end
context 'when token is invalid' do
it 'fetches new access token and refresh tokens' do
with_modified_env GOOGLE_OAUTH_CLIENT_ID: SecureRandom.uuid, GOOGLE_OAUTH_CLIENT_SECRET: SecureRandom.hex do
provider_config = google_channel_with_expired_token.provider_config
service = described_class.new(channel: google_channel_with_expired_token)
expect(service.access_token).not_to eq(provider_config['access_token'])
new_provider_config = google_channel_with_expired_token.reload.provider_config
expect(new_provider_config['access_token']).to eq(new_tokens[:access_token])
expect(new_provider_config['refresh_token']).to eq(new_tokens[:refresh_token])
expect(new_provider_config['expires_on']).to eq(Time.at(new_tokens[:expires_at]).utc.to_s)
end
end
end
context 'when expiry time is missing' do
it 'fetches new access token and refresh tokens' do
with_modified_env GOOGLE_OAUTH_CLIENT_ID: SecureRandom.uuid, GOOGLE_OAUTH_CLIENT_SECRET: SecureRandom.hex do
google_channel_with_expired_token.provider_config['expires_on'] = nil
google_channel_with_expired_token.save!
provider_config = google_channel_with_expired_token.provider_config
service = described_class.new(channel: google_channel_with_expired_token)
expect(service.access_token).not_to eq(provider_config['access_token'])
new_provider_config = google_channel_with_expired_token.reload.provider_config
expect(new_provider_config['access_token']).to eq(new_tokens[:access_token])
expect(new_provider_config['refresh_token']).to eq(new_tokens[:refresh_token])
expect(new_provider_config['expires_on']).to eq(Time.at(new_tokens[:expires_at]).utc.to_s)
end
end
end
end
context 'when refresh token is not present in provider config and access token is expired' do
it 'throws an error' do
with_modified_env GOOGLE_OAUTH_CLIENT_ID: SecureRandom.uuid, GOOGLE_OAUTH_CLIENT_SECRET: SecureRandom.hex do
google_channel.update(
provider_config: {
access_token: SecureRandom.hex,
expires_on: Time.zone.now - 3600
}
)
expect do
described_class.new(channel: google_channel).access_token
end.to raise_error(RuntimeError, 'A refresh_token is not available')
end
end
end
end