DEV: Apply syntax_tree formatting to spec/*

This commit is contained in:
David Taylor
2023-01-09 11:18:21 +00:00
parent 0cf6421716
commit cb932d6ee1
907 changed files with 58693 additions and 45909 deletions

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'imap/sync'
require "imap/sync"
RSpec.describe Imap::Sync do
before do
@ -15,32 +15,31 @@ RSpec.describe Imap::Sync do
let(:group) do
Fabricate(
:group,
imap_server: 'imap.gmail.com',
imap_server: "imap.gmail.com",
imap_port: 993,
email_username: 'groupemailusername@example.com',
email_password: 'password',
imap_mailbox_name: '[Gmail]/All Mail'
email_username: "groupemailusername@example.com",
email_password: "password",
imap_mailbox_name: "[Gmail]/All Mail",
)
end
let(:sync_handler) { Imap::Sync.new(group) }
before do
mocked_imap_provider = MockedImapProvider.new(
group.imap_server,
port: group.imap_port,
ssl: group.imap_ssl,
username: group.email_username,
password: group.email_password
)
Imap::Providers::Detector.stubs(:init_with_detected_provider).returns(
mocked_imap_provider
)
mocked_imap_provider =
MockedImapProvider.new(
group.imap_server,
port: group.imap_port,
ssl: group.imap_ssl,
username: group.email_username,
password: group.email_password,
)
Imap::Providers::Detector.stubs(:init_with_detected_provider).returns(mocked_imap_provider)
end
describe 'no previous sync' do
let(:from) { 'john@free.fr' }
let(:subject) { 'Testing email post' }
describe "no previous sync" do
let(:from) { "john@free.fr" }
let(:subject) { "Testing email post" }
let(:message_id) { "#{SecureRandom.hex}@example.com" }
let(:email) do
@ -48,7 +47,8 @@ RSpec.describe Imap::Sync do
from: from,
to: group.email_username,
subject: subject,
message_id: message_id)
message_id: message_id,
)
end
before do
@ -60,20 +60,19 @@ RSpec.describe Imap::Sync do
provider.stubs(:emails).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Important test-label],
'FLAGS' => %i[Seen],
'RFC822' => email
}
]
"UID" => 100,
"LABELS" => %w[\\Important test-label],
"FLAGS" => %i[Seen],
"RFC822" => email,
},
],
)
end
it 'creates a topic from an incoming email' do
expect { sync_handler.process }
.to change { Topic.count }.by(1)
.and change { Post.where(post_type: Post.types[:regular]).count }.by(1)
.and change { IncomingEmail.count }.by(1)
it "creates a topic from an incoming email" do
expect { sync_handler.process }.to change { Topic.count }.by(1).and change {
Post.where(post_type: Post.types[:regular]).count
}.by(1).and change { IncomingEmail.count }.by(1)
expect(IncomingEmail.last.created_via).to eq(IncomingEmail.created_via_types[:imap])
@ -86,7 +85,7 @@ RSpec.describe Imap::Sync do
expect(topic.tags.pluck(:name)).to contain_exactly("seen", "important", "test-label")
post = topic.first_post
expect(post.raw).to eq('This is an email *body*. :smile:')
expect(post.raw).to eq("This is an email *body*. :smile:")
incoming_email = post.incoming_email
expect(incoming_email.raw.lines.map(&:strip)).to eq(email.lines.map(&:strip))
@ -100,15 +99,12 @@ RSpec.describe Imap::Sync do
end
context "when tagging not enabled" do
before do
SiteSetting.tagging_enabled = false
end
before { SiteSetting.tagging_enabled = false }
it "creates a topic from an incoming email but with no tags added" do
expect { sync_handler.process }
.to change { Topic.count }.by(1)
.and change { Post.where(post_type: Post.types[:regular]).count }.by(1)
.and change { IncomingEmail.count }.by(1)
expect { sync_handler.process }.to change { Topic.count }.by(1).and change {
Post.where(post_type: Post.types[:regular]).count
}.by(1).and change { IncomingEmail.count }.by(1)
expect(group.imap_uid_validity).to eq(1)
expect(group.imap_last_uid).to eq(100)
@ -120,25 +116,22 @@ RSpec.describe Imap::Sync do
end
end
it 'does not duplicate topics' do
expect { sync_handler.process }
.to change { Topic.count }.by(1)
.and change { Post.where(post_type: Post.types[:regular]).count }.by(1)
.and change { IncomingEmail.count }.by(1)
it "does not duplicate topics" do
expect { sync_handler.process }.to change { Topic.count }.by(1).and change {
Post.where(post_type: Post.types[:regular]).count
}.by(1).and change { IncomingEmail.count }.by(1)
expect { sync_handler.process }
.to not_change { Topic.count }
.and not_change { Post.where(post_type: Post.types[:regular]).count }
.and not_change { IncomingEmail.count }
expect { sync_handler.process }.to not_change { Topic.count }.and not_change {
Post.where(post_type: Post.types[:regular]).count
}.and not_change { IncomingEmail.count }
end
it 'creates a new incoming email if the message ID does not match the receiver post id regex' do
it "creates a new incoming email if the message ID does not match the receiver post id regex" do
incoming_email = Fabricate(:incoming_email, message_id: message_id)
expect { sync_handler.process }
.to change { Topic.count }.by(1)
.and change { Post.where(post_type: Post.types[:regular]).count }.by(1)
.and change { IncomingEmail.count }.by(1)
expect { sync_handler.process }.to change { Topic.count }.by(1).and change {
Post.where(post_type: Post.types[:regular]).count
}.by(1).and change { IncomingEmail.count }.by(1)
last_incoming = IncomingEmail.where(message_id: message_id).last
expect(last_incoming.message_id).to eq(message_id)
@ -150,13 +143,12 @@ RSpec.describe Imap::Sync do
context "when the message id matches the receiver post id regex" do
let(:message_id) { "topic/999/324@test.localhost" }
it 'does not duplicate incoming email' do
it "does not duplicate incoming email" do
incoming_email = Fabricate(:incoming_email, message_id: message_id)
expect { sync_handler.process }
.to not_change { Topic.count }
.and not_change { Post.where(post_type: Post.types[:regular]).count }
.and not_change { IncomingEmail.count }
expect { sync_handler.process }.to not_change { Topic.count }.and not_change {
Post.where(post_type: Post.types[:regular]).count
}.and not_change { IncomingEmail.count }
incoming_email.reload
expect(incoming_email.message_id).to eq(message_id)
@ -168,44 +160,47 @@ RSpec.describe Imap::Sync do
end
end
describe 'previous sync' do
let(:subject) { 'Testing email post' }
describe "previous sync" do
let(:subject) { "Testing email post" }
let(:first_from) { 'john@free.fr' }
let(:first_from) { "john@free.fr" }
let(:first_message_id) { SecureRandom.hex }
let(:first_body) { 'This is the first message of this exchange.' }
let(:first_body) { "This is the first message of this exchange." }
let(:second_from) { 'sam@free.fr' }
let(:second_from) { "sam@free.fr" }
let(:second_message_id) { SecureRandom.hex }
let(:second_body) { '<p>This is an <b>answer</b> to this message.</p>' }
let(:second_body) { "<p>This is an <b>answer</b> to this message.</p>" }
it 'continues with new emails' do
it "continues with new emails" do
provider = MockedImapProvider.any_instance
provider.stubs(:open_mailbox).returns(uid_validity: 1)
provider.stubs(:uids).with.returns([100])
provider.stubs(:emails).with([100], ['UID', 'FLAGS', 'LABELS', 'RFC822'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen],
'RFC822' => EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body
)
}
]
)
provider
.stubs(:emails)
.with([100], %w[UID FLAGS LABELS RFC822], anything)
.returns(
[
{
"UID" => 100,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Seen],
"RFC822" =>
EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body,
),
},
],
)
expect { sync_handler.process }
.to change { Topic.count }.by(1)
.and change { Post.where(post_type: Post.types[:regular]).count }.by(1)
.and change { IncomingEmail.count }.by(1)
expect { sync_handler.process }.to change { Topic.count }.by(1).and change {
Post.where(post_type: Post.types[:regular]).count
}.by(1).and change { IncomingEmail.count }.by(1)
topic = Topic.last
expect(topic.title).to eq(subject)
@ -219,37 +214,35 @@ RSpec.describe Imap::Sync do
provider.stubs(:uids).with(to: 100).returns([100])
provider.stubs(:uids).with(from: 101).returns([200])
provider.stubs(:emails).with([100], ['UID', 'FLAGS', 'LABELS', 'ENVELOPE'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen]
}
]
)
provider.stubs(:emails).with([200], ['UID', 'FLAGS', 'LABELS', 'RFC822'], anything).returns(
[
{
'UID' => 200,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Recent],
'RFC822' => EmailFabricator(
message_id: SecureRandom.hex,
in_reply_to: first_message_id,
from: second_from,
to: group.email_username,
subject: "Re: #{subject}",
body: second_body
)
}
]
)
provider
.stubs(:emails)
.with([100], %w[UID FLAGS LABELS ENVELOPE], anything)
.returns([{ "UID" => 100, "LABELS" => %w[\\Inbox], "FLAGS" => %i[Seen] }])
provider
.stubs(:emails)
.with([200], %w[UID FLAGS LABELS RFC822], anything)
.returns(
[
{
"UID" => 200,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Recent],
"RFC822" =>
EmailFabricator(
message_id: SecureRandom.hex,
in_reply_to: first_message_id,
from: second_from,
to: group.email_username,
subject: "Re: #{subject}",
body: second_body,
),
},
],
)
expect { sync_handler.process }
.to not_change { Topic.count }
.and change { Post.where(post_type: Post.types[:regular]).count }.by(1)
.and change { IncomingEmail.count }.by(1)
expect { sync_handler.process }.to not_change { Topic.count }.and change {
Post.where(post_type: Post.types[:regular]).count
}.by(1).and change { IncomingEmail.count }.by(1)
post = Post.where(post_type: Post.types[:regular]).last
expect(post.user.email).to eq(second_from)
@ -259,25 +252,19 @@ RSpec.describe Imap::Sync do
provider.stubs(:uids).with(to: 200).returns([100, 200])
provider.stubs(:uids).with(from: 201).returns([])
provider.stubs(:emails).with([100, 200], ['UID', 'FLAGS', 'LABELS', 'ENVELOPE'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[],
'FLAGS' => %i[Seen]
},
{
'UID' => 200,
'LABELS' => %w[],
'FLAGS' => %i[Recent],
}
]
)
provider
.stubs(:emails)
.with([100, 200], %w[UID FLAGS LABELS ENVELOPE], anything)
.returns(
[
{ "UID" => 100, "LABELS" => %w[], "FLAGS" => %i[Seen] },
{ "UID" => 200, "LABELS" => %w[], "FLAGS" => %i[Recent] },
],
)
expect { sync_handler.process }
.to not_change { Topic.count }
.and not_change { Post.where(post_type: Post.types[:regular]).count }
.and not_change { IncomingEmail.count }
expect { sync_handler.process }.to not_change { Topic.count }.and not_change {
Post.where(post_type: Post.types[:regular]).count
}.and not_change { IncomingEmail.count }
topic = Topic.last
expect(topic.title).to eq(subject)
@ -292,24 +279,27 @@ RSpec.describe Imap::Sync do
provider.stubs(:open_mailbox).returns(uid_validity: 1)
provider.stubs(:uids).with.returns([100])
provider.stubs(:emails).with([100], ['UID', 'FLAGS', 'LABELS', 'RFC822'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen],
'RFC822' => EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body
)
}
]
)
provider
.stubs(:emails)
.with([100], %w[UID FLAGS LABELS RFC822], anything)
.returns(
[
{
"UID" => 100,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Seen],
"RFC822" =>
EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body,
),
},
],
)
end
it "detects previously synced UIDs are missing and deletes the posts if they are in the trash mailbox" do
@ -322,14 +312,9 @@ RSpec.describe Imap::Sync do
provider.stubs(:find_spam_by_message_ids).returns(stub(spam_emails: []))
provider.stubs(:find_trashed_by_message_ids).returns(
stub(
trashed_emails: [
stub(
uid: 10,
message_id: incoming_100.message_id
)
],
trash_uid_validity: 99
)
trashed_emails: [stub(uid: 10, message_id: incoming_100.message_id)],
trash_uid_validity: 99,
),
)
sync_handler.process
@ -350,14 +335,9 @@ RSpec.describe Imap::Sync do
provider.stubs(:find_trashed_by_message_ids).returns(stub(trashed_emails: []))
provider.stubs(:find_spam_by_message_ids).returns(
stub(
spam_emails: [
stub(
uid: 10,
message_id: incoming_100.message_id
)
],
spam_uid_validity: 99
)
spam_emails: [stub(uid: 10, message_id: incoming_100.message_id)],
spam_uid_validity: 99,
),
)
sync_handler.process
@ -394,31 +374,31 @@ RSpec.describe Imap::Sync do
provider.stubs(:uids).with(from: 101).returns([])
PostDestroyer.new(Discourse.system_user, incoming_100.post).destroy
provider.stubs(:emails).with([100], ['UID', 'FLAGS', 'LABELS', 'ENVELOPE'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen],
'RFC822' => EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body
)
}
]
)
provider.stubs(:emails).with(100, ['FLAGS', 'LABELS']).returns(
[
{
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen]
}
]
)
provider
.stubs(:emails)
.with([100], %w[UID FLAGS LABELS ENVELOPE], anything)
.returns(
[
{
"UID" => 100,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Seen],
"RFC822" =>
EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body,
),
},
],
)
provider
.stubs(:emails)
.with(100, %w[FLAGS LABELS])
.returns([{ "LABELS" => %w[\\Inbox], "FLAGS" => %i[Seen] }])
provider.expects(:trash).with(100)
sync_handler.process
@ -430,14 +410,9 @@ RSpec.describe Imap::Sync do
provider.stubs(:find_spam_by_message_ids).returns(stub(spam_emails: []))
provider.stubs(:find_trashed_by_message_ids).returns(
stub(
trashed_emails: [
stub(
uid: 10,
message_id: incoming_100.message_id
)
],
trash_uid_validity: 99
)
trashed_emails: [stub(uid: 10, message_id: incoming_100.message_id)],
trash_uid_validity: 99,
),
)
PostDestroyer.expects(:new).never
@ -456,23 +431,27 @@ RSpec.describe Imap::Sync do
provider.stubs(:open_mailbox).returns(uid_validity: 1)
provider.stubs(:uids).with.returns([100])
provider.stubs(:emails).with([100], ['UID', 'FLAGS', 'LABELS', 'RFC822'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen],
'RFC822' => EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body
)
}
]
)
provider
.stubs(:emails)
.with([100], %w[UID FLAGS LABELS RFC822], anything)
.returns(
[
{
"UID" => 100,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Seen],
"RFC822" =>
EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body,
),
},
],
)
sync_handler.process
@incoming_email = IncomingEmail.find_by(message_id: first_message_id)
@ -480,34 +459,23 @@ RSpec.describe Imap::Sync do
provider.stubs(:uids).with(to: 100).returns([100])
provider.stubs(:uids).with(from: 101).returns([101])
provider.stubs(:emails).with([100], ['UID', 'FLAGS', 'LABELS', 'ENVELOPE'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen]
}
]
)
provider.stubs(:emails).with([101], ['UID', 'FLAGS', 'LABELS', 'RFC822'], anything).returns(
[]
)
provider.stubs(:emails).with(100, ['FLAGS', 'LABELS']).returns(
[
{
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen]
}
]
)
provider
.stubs(:emails)
.with([100], %w[UID FLAGS LABELS ENVELOPE], anything)
.returns([{ "UID" => 100, "LABELS" => %w[\\Inbox], "FLAGS" => %i[Seen] }])
provider.stubs(:emails).with([101], %w[UID FLAGS LABELS RFC822], anything).returns([])
provider
.stubs(:emails)
.with(100, %w[FLAGS LABELS])
.returns([{ "LABELS" => %w[\\Inbox], "FLAGS" => %i[Seen] }])
end
it "archives an email on the IMAP server when archived in discourse" do
GroupArchivedMessage.archive!(group.id, @topic, skip_imap_sync: false)
@incoming_email.update(imap_sync: true)
provider.stubs(:store).with(100, 'FLAGS', anything, anything)
provider.stubs(:store).with(100, 'LABELS', ["\\Inbox"], ["seen"])
provider.stubs(:store).with(100, "FLAGS", anything, anything)
provider.stubs(:store).with(100, "LABELS", ["\\Inbox"], ["seen"])
provider.expects(:archive).with(100)
sync_handler.process
@ -515,111 +483,118 @@ RSpec.describe Imap::Sync do
it "does not archive email if not archived in discourse, it unarchives it instead" do
@incoming_email.update(imap_sync: true)
provider.stubs(:store).with(100, 'FLAGS', anything, anything)
provider.stubs(:store).with(100, 'LABELS', ["\\Inbox"], ["\\Inbox", "seen"])
provider.stubs(:store).with(100, "FLAGS", anything, anything)
provider.stubs(:store).with(100, "LABELS", ["\\Inbox"], ["\\Inbox", "seen"])
provider.expects(:archive).with(100).never
provider.expects(:unarchive).with(100)
sync_handler.process
end
end
end
describe 'invalidated previous sync' do
let(:subject) { 'Testing email post' }
describe "invalidated previous sync" do
let(:subject) { "Testing email post" }
let(:first_from) { 'john@free.fr' }
let(:first_from) { "john@free.fr" }
let(:first_message_id) { SecureRandom.hex }
let(:first_body) { 'This is the first message of this exchange.' }
let(:first_body) { "This is the first message of this exchange." }
let(:second_from) { 'sam@free.fr' }
let(:second_from) { "sam@free.fr" }
let(:second_message_id) { SecureRandom.hex }
let(:second_body) { '<p>This is an <b>answer</b> to this message.</p>' }
let(:second_body) { "<p>This is an <b>answer</b> to this message.</p>" }
# TODO: Improve the invalidating flow for mailbox change. This is a destructive
# action so it should not be done often.
xit 'is updated' do
xit "is updated" do
provider = MockedImapProvider.any_instance
provider.stubs(:open_mailbox).returns(uid_validity: 1)
provider.stubs(:uids).with.returns([100, 200])
provider.stubs(:emails).with([100, 200], ['UID', 'FLAGS', 'LABELS', 'RFC822'], anything).returns(
[
{
'UID' => 100,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen],
'RFC822' => EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body
)
},
{
'UID' => 200,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Recent],
'RFC822' => EmailFabricator(
message_id: second_message_id,
in_reply_to: first_message_id,
from: second_from,
to: group.email_username,
subject: "Re: #{subject}",
body: second_body
)
}
]
)
provider
.stubs(:emails)
.with([100, 200], %w[UID FLAGS LABELS RFC822], anything)
.returns(
[
{
"UID" => 100,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Seen],
"RFC822" =>
EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body,
),
},
{
"UID" => 200,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Recent],
"RFC822" =>
EmailFabricator(
message_id: second_message_id,
in_reply_to: first_message_id,
from: second_from,
to: group.email_username,
subject: "Re: #{subject}",
body: second_body,
),
},
],
)
expect { sync_handler.process }
.to change { Topic.count }.by(1)
.and change { Post.where(post_type: Post.types[:regular]).count }.by(2)
.and change { IncomingEmail.count }.by(2)
expect { sync_handler.process }.to change { Topic.count }.by(1).and change {
Post.where(post_type: Post.types[:regular]).count
}.by(2).and change { IncomingEmail.count }.by(2)
imap_data = Topic.last.incoming_email.pluck(:imap_uid_validity, :imap_uid, :imap_group_id)
expect(imap_data).to contain_exactly([1, 100, group.id], [1, 200, group.id])
provider.stubs(:open_mailbox).returns(uid_validity: 2)
provider.stubs(:uids).with.returns([111, 222])
provider.stubs(:emails).with([111, 222], ['UID', 'FLAGS', 'LABELS', 'RFC822'], anything).returns(
[
{
'UID' => 111,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Seen],
'RFC822' => EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body
)
},
{
'UID' => 222,
'LABELS' => %w[\\Inbox],
'FLAGS' => %i[Recent],
'RFC822' => EmailFabricator(
message_id: second_message_id,
in_reply_to: first_message_id,
from: second_from,
to: group.email_username,
subject: "Re: #{subject}",
body: second_body
)
}
]
)
provider
.stubs(:emails)
.with([111, 222], %w[UID FLAGS LABELS RFC822], anything)
.returns(
[
{
"UID" => 111,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Seen],
"RFC822" =>
EmailFabricator(
message_id: first_message_id,
from: first_from,
to: group.email_username,
cc: second_from,
subject: subject,
body: first_body,
),
},
{
"UID" => 222,
"LABELS" => %w[\\Inbox],
"FLAGS" => %i[Recent],
"RFC822" =>
EmailFabricator(
message_id: second_message_id,
in_reply_to: first_message_id,
from: second_from,
to: group.email_username,
subject: "Re: #{subject}",
body: second_body,
),
},
],
)
expect { sync_handler.process }
.to not_change { Topic.count }
.and not_change { Post.where(post_type: Post.types[:regular]).count }
.and not_change { IncomingEmail.count }
expect { sync_handler.process }.to not_change { Topic.count }.and not_change {
Post.where(post_type: Post.types[:regular]).count
}.and not_change { IncomingEmail.count }
imap_data = Topic.last.incoming_email.pluck(:imap_uid_validity, :imap_uid, :imap_group_id)
expect(imap_data).to contain_exactly([2, 111, group.id], [2, 222, group.id])