discourse/spec/lib/imap/providers/gmail_spec.rb
Martin Brennan 964da21817
FEATURE: Improve group email settings UI (#13083)
This overhauls the user interface for the group email settings management, aiming to make it a lot easier to test the settings entered and confirm they are correct before proceeding. We do this by forcing the user to test the settings before they can be saved to the database. It also includes some quality of life improvements around setting up IMAP and SMTP for our first supported provider, GMail. This PR does not remove the old group email config, that will come in a subsequent PR. This is related to https://meta.discourse.org/t/imap-support-for-group-inboxes/160588 so read that if you would like more backstory.

### UI

Both site settings of `enable_imap` and `enable_smtp` must be true to test this. You must enable SMTP first to enable IMAP.

You can prefill the SMTP settings with GMail configuration. To proceed with saving these settings you must test them, which is handled by the EmailSettingsValidator.

If there is an issue with the configuration or credentials a meaningful error message should be shown.

IMAP settings must also be validated when IMAP is enabled, before saving.

When saving IMAP, we fetch the mailboxes for that account and populate them. This mailbox must be selected and saved for IMAP to work (the feature acts as though it is disabled until the mailbox is selected and saved):

### Database & Backend

This adds several columns to the Groups table. The purpose of this change is to make it much more explicit that SMTP/IMAP is enabled for a group, rather than relying on settings not being null. Also included is an UPDATE query to backfill these columns. These columns are automatically filled when updating the group.

For GMail, we now filter the mailboxes returned. This is so users cannot use a mailbox like Sent or Trash for syncing, which would generally be disastrous.

There is a new group endpoint for testing email settings. This may be useful in the future for other places in our UI, at which point it can be extracted to a more generic endpoint or module to be included.
2021-05-28 09:28:18 +10:00

93 lines
3.0 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Imap::Providers::Gmail do
fab!(:username) { "test@generic.com" }
fab!(:password) { "test1!" }
fab!(:provider) do
described_class.new(
"imap.generic.com",
{
port: 993,
ssl: true,
username: username,
password: password
}
)
end
let(:imap_stub) { stub }
let(:x_gm_thrid) { Imap::Providers::Gmail::X_GM_THRID }
let(:x_gm_labels) { Imap::Providers::Gmail::X_GM_LABELS }
before do
described_class.any_instance.stubs(:imap).returns(imap_stub)
end
describe "#store" do
it "converts LABELS store to special X-GM-LABELS" do
Imap::Providers::Generic.any_instance.expects(:store).with(
63, x_gm_labels, ["\\Inbox"], ["\\Inbox", "test"]
)
provider.store(63, "LABELS", ["\\Inbox"], ["\\Inbox", "test"])
end
end
describe "#tag_to_label" do
it "converts important to special gmail label \\Important" do
expect(provider.tag_to_label("important")).to eq("\\Important")
end
it "converts starred to special gmail label \\Starred" do
expect(provider.tag_to_label("starred")).to eq("\\Starred")
end
end
describe "#archive" do
it "gets the thread ID for the UID, and removes the Inbox label from all UIDs in the thread" do
main_uid = 78
fake_thrid = '4398634986239754'
imap_stub.expects(:uid_fetch).with(main_uid, [x_gm_thrid]).returns(
[stub(attr: { x_gm_thrid => fake_thrid })]
)
imap_stub.expects(:uid_search).with("#{x_gm_thrid} #{fake_thrid}").returns([79, 80])
provider.expects(:emails).with([79, 80], ["UID", "LABELS"]).returns(
[
{
"UID" => 79,
"LABELS" => ["\\Inbox", "seen"]
},
{
"UID" => 80,
"LABELS" => ["\\Inbox", "seen"]
}
]
)
provider.expects(:store).with(79, "LABELS", ["\\Inbox", "seen"], ["seen"])
provider.expects(:store).with(80, "LABELS", ["\\Inbox", "seen"], ["seen"])
provider.archive(main_uid)
end
end
describe "#filter_mailboxes" do
it "filters down the gmail mailboxes to only show the relevant ones" do
mailboxes_with_attr = [
Net::IMAP::MailboxList.new([:Hasnochildren], "/", "INBOX"),
Net::IMAP::MailboxList.new([:All, :Hasnochildren], "/", "[Gmail]/All Mail"),
Net::IMAP::MailboxList.new([:Drafts, :Hasnochildren], "/", "[Gmail]/Drafts"),
Net::IMAP::MailboxList.new([:Hasnochildren, :Important], "/", "[Gmail]/Important"),
Net::IMAP::MailboxList.new([:Hasnochildren, :Sent], "/", "[Gmail]/Sent Mail"),
Net::IMAP::MailboxList.new([:Hasnochildren, :Junk], "/", "[Gmail]/Spam"),
Net::IMAP::MailboxList.new([:Flagged, :Hasnochildren], "/", "[Gmail]/Starred"),
Net::IMAP::MailboxList.new([:Hasnochildren, :Trash], "/", "[Gmail]/Trash")
]
expect(provider.filter_mailboxes(mailboxes_with_attr)).to match_array([
"INBOX", "[Gmail]/All Mail", "[Gmail]/Important"
])
end
end
end