2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2015-01-24 01:25:43 +08:00
|
|
|
|
|
|
|
describe Jobs::AutomaticGroupMembership do
|
|
|
|
|
|
|
|
it "raises an error when the group id is missing" do
|
|
|
|
expect { Jobs::AutomaticGroupMembership.new.execute({}) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the membership" do
|
2017-06-15 01:20:18 +08:00
|
|
|
user1 = Fabricate(:user, email: "no@bar.com")
|
|
|
|
user2 = Fabricate(:user, email: "no@wat.com")
|
|
|
|
user3 = Fabricate(:user, email: "noo@wat.com", staged: true)
|
2018-05-15 07:48:30 +08:00
|
|
|
EmailToken.confirm(user3.email_tokens.last.token)
|
2017-06-15 01:20:18 +08:00
|
|
|
user4 = Fabricate(:user, email: "yes@wat.com")
|
|
|
|
EmailToken.confirm(user4.email_tokens.last.token)
|
2018-05-15 07:48:30 +08:00
|
|
|
user5 = Fabricate(:user, email: "sso@wat.com")
|
|
|
|
user5.create_single_sign_on_record(external_id: 123, external_email: "hacker@wat.com", last_payload: "")
|
|
|
|
user6 = Fabricate(:user, email: "sso2@wat.com")
|
|
|
|
user6.create_single_sign_on_record(external_id: 456, external_email: "sso2@wat.com", last_payload: "")
|
2017-06-15 01:20:18 +08:00
|
|
|
|
2020-04-23 00:37:39 +08:00
|
|
|
group = Fabricate(:group, automatic_membership_email_domains: "wat.com")
|
2015-01-24 01:25:43 +08:00
|
|
|
|
2020-11-12 03:46:13 +08:00
|
|
|
automatic = nil
|
|
|
|
called = false
|
2019-09-11 00:58:08 +08:00
|
|
|
|
2020-11-12 03:46:13 +08:00
|
|
|
blk = Proc.new do |_u, _g, options|
|
|
|
|
automatic = options[:automatic]
|
|
|
|
called = true
|
|
|
|
end
|
2019-09-11 00:58:08 +08:00
|
|
|
|
2020-11-12 03:46:13 +08:00
|
|
|
begin
|
|
|
|
DiscourseEvent.on(:user_added_to_group, &blk)
|
2019-09-11 00:58:08 +08:00
|
|
|
Jobs::AutomaticGroupMembership.new.execute(group_id: group.id)
|
|
|
|
|
|
|
|
expect(automatic).to eql(true)
|
|
|
|
expect(called).to eq(true)
|
|
|
|
ensure
|
2020-11-12 03:46:13 +08:00
|
|
|
DiscourseEvent.off(:user_added_to_group, &blk)
|
2019-09-11 00:58:08 +08:00
|
|
|
end
|
2015-01-24 01:25:43 +08:00
|
|
|
|
|
|
|
group.reload
|
2017-06-15 01:20:18 +08:00
|
|
|
expect(group.users.include?(user1)).to eq(false)
|
2015-01-24 01:25:43 +08:00
|
|
|
expect(group.users.include?(user2)).to eq(false)
|
2016-12-21 18:04:26 +08:00
|
|
|
expect(group.users.include?(user3)).to eq(false)
|
2017-06-15 01:20:18 +08:00
|
|
|
expect(group.users.include?(user4)).to eq(true)
|
2018-05-15 07:48:30 +08:00
|
|
|
expect(group.users.include?(user5)).to eq(false)
|
|
|
|
expect(group.users.include?(user6)).to eq(true)
|
2015-01-24 01:25:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|