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