2017-07-14 23:44:44 +03:00
|
|
|
require_relative 'iam_helper'
|
|
|
|
|
|
|
|
describe MozillaIAM do
|
2018-06-06 18:22:19 +03:00
|
|
|
before do
|
|
|
|
NotificationEmailer.enable
|
|
|
|
SiteSetting.queue_jobs = false
|
|
|
|
end
|
2018-07-31 16:19:46 +03:00
|
|
|
|
2017-07-14 23:44:44 +03:00
|
|
|
context 'new post in restricted category' do
|
|
|
|
|
2019-04-24 19:18:15 +03:00
|
|
|
let(:poster) { Fabricate(:user_with_secondary_email) }
|
|
|
|
let(:user) { Fabricate(:user_with_secondary_email) }
|
2017-07-14 23:44:44 +03:00
|
|
|
let(:group) { Fabricate(:group, users: [user]) }
|
|
|
|
let(:category) { Fabricate(:private_category, group: group) }
|
|
|
|
let(:topic) { Fabricate(:topic, category: category, user: poster) }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic, user: poster) }
|
|
|
|
let(:reply) { Fabricate(:post, topic: topic, user: poster) }
|
|
|
|
let(:uid) { create_uid(user.username) }
|
|
|
|
let(:last_refresh) { Time.now - 16.minutes }
|
|
|
|
|
|
|
|
before do
|
|
|
|
MozillaIAM::GroupMapping.new(iam_group_name: 'iam_group',
|
|
|
|
group: group).save!
|
|
|
|
TopicUser.change(user.id, topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
user.custom_fields['mozilla_iam_uid'] = uid
|
|
|
|
user.custom_fields['mozilla_iam_last_refresh'] = last_refresh
|
|
|
|
user.save_custom_fields
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user in correct IAM group' do
|
2018-08-24 17:23:45 +03:00
|
|
|
before { stub_apis_profile_request(uid, groups: ['iam_group']) }
|
2017-07-14 23:44:44 +03:00
|
|
|
|
|
|
|
it 'refreshes the user profile' do
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
user.clear_custom_fields
|
|
|
|
expect(Time.parse(user.custom_fields['mozilla_iam_last_refresh'])).to be_within(5.seconds).of Time.now
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'alerts the user' do
|
|
|
|
expect {
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
}.to change(user.notifications, :count).by 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'emails the user' do
|
|
|
|
PostAlerter.post_created(reply)
|
2018-07-31 16:19:46 +03:00
|
|
|
expect(EmailLog.where(user: user, post: reply, email_type: 'user_posted').count).to eq(1)
|
2017-07-14 23:44:44 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends the user a mailing list email' do
|
|
|
|
user.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 1)
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: reply.id)
|
2018-07-31 16:19:46 +03:00
|
|
|
expect(EmailLog.where(user: user, post: reply, email_type: 'mailing_list').count).to eq(1)
|
2017-07-14 23:44:44 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user removed from IAM group' do
|
2018-08-24 17:23:45 +03:00
|
|
|
before { stub_apis_profile_request(uid, groups: []) }
|
2017-07-14 23:44:44 +03:00
|
|
|
|
|
|
|
it 'refreshes the user profile' do
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
user.clear_custom_fields
|
|
|
|
expect(Time.parse(user.custom_fields['mozilla_iam_last_refresh'])).to be_within(5.seconds).of Time.now
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not alert the user' do
|
|
|
|
expect {
|
|
|
|
PostAlerter.post_created(reply)
|
|
|
|
}.to_not change(user.notifications, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not email the user' do
|
|
|
|
PostAlerter.post_created(reply)
|
2018-07-31 16:19:46 +03:00
|
|
|
expect(EmailLog.where(user: user, post: reply, email_type: 'user_posted').count).to eq(0)
|
2017-07-14 23:44:44 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not send the user a mailing list email' do
|
|
|
|
user.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 1)
|
|
|
|
Jobs::NotifyMailingListSubscribers.new.execute(post_id: reply.id)
|
2018-07-31 16:19:46 +03:00
|
|
|
expect(EmailLog.where(user: user, post: reply, email_type: 'mailing_list').count).to eq(0)
|
2017-07-14 23:44:44 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-06 17:57:14 +03:00
|
|
|
|
|
|
|
context 'new private message' do
|
2019-04-24 19:18:15 +03:00
|
|
|
let(:author) { Fabricate(:user_with_secondary_email) }
|
|
|
|
let(:user) { Fabricate(:user_with_secondary_email) }
|
2017-09-06 17:57:14 +03:00
|
|
|
let(:post) do
|
|
|
|
PostCreator.create(author, title: 'private message test',
|
|
|
|
raw: 'this is my private message',
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: user.username)
|
|
|
|
end
|
|
|
|
let(:uid) { create_uid(user.username) }
|
|
|
|
let(:last_refresh) { Time.now - 16.minutes }
|
|
|
|
|
|
|
|
before do
|
|
|
|
TopicUser.change(user.id, post.topic.id, notification_level: TopicUser.notification_levels[:watching])
|
|
|
|
user.custom_fields['mozilla_iam_uid'] = uid
|
|
|
|
user.custom_fields['mozilla_iam_last_refresh'] = last_refresh
|
|
|
|
user.save_custom_fields
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not refresh the user profile' do
|
|
|
|
user.clear_custom_fields
|
|
|
|
expect(Time.parse(user.custom_fields['mozilla_iam_last_refresh'])).to be_within(5.seconds).of last_refresh
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'alerts the user' do
|
|
|
|
expect(user.notifications.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'emails the user' do
|
2018-07-31 16:19:46 +03:00
|
|
|
expect(EmailLog.where(user: user, post: post, email_type: 'user_private_message').count).to eq(1)
|
2017-09-06 17:57:14 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-14 23:44:44 +03:00
|
|
|
end
|