2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-03 06:51:22 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe AdminUserListSerializer do
|
2020-02-02 22:37:46 -06:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2019-03-15 01:59:16 -05:00
|
|
|
let(:guardian) { Guardian.new(admin) }
|
|
|
|
|
|
|
|
let(:serializer) do
|
|
|
|
AdminUserListSerializer.new(user, scope: guardian, root: false)
|
|
|
|
end
|
|
|
|
|
2020-02-02 22:37:46 -06:00
|
|
|
context "when totp enabled" do
|
|
|
|
before do
|
|
|
|
Fabricate(:user_second_factor_totp, user: user)
|
|
|
|
end
|
|
|
|
it "returns the right values" do
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:second_factor_enabled]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2019-03-15 01:59:16 -05:00
|
|
|
|
2020-02-02 22:37:46 -06:00
|
|
|
context "when security keys enabled" do
|
|
|
|
before do
|
|
|
|
Fabricate(:user_security_key, user: user)
|
|
|
|
end
|
|
|
|
it "returns the right values" do
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:second_factor_enabled]).to eq(true)
|
|
|
|
end
|
2019-03-15 01:59:16 -05:00
|
|
|
end
|
2018-07-03 06:51:22 -05:00
|
|
|
|
|
|
|
context "emails" do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:admin) { Fabricate(:user, admin: true, email: "admin@email.com") }
|
|
|
|
fab!(:moderator) { Fabricate(:user, moderator: true, email: "moderator@email.com") }
|
|
|
|
fab!(:user) { Fabricate(:user, email: "user@email.com") }
|
2018-07-03 06:51:22 -05:00
|
|
|
|
2019-01-11 10:10:02 -06:00
|
|
|
def serialize(user, viewed_by, opts = nil)
|
|
|
|
AdminUserListSerializer.new(
|
|
|
|
user,
|
|
|
|
scope: Guardian.new(viewed_by),
|
|
|
|
root: false,
|
|
|
|
emails_desired: opts && opts[:emails_desired]
|
2018-12-19 03:24:57 -06:00
|
|
|
).as_json
|
|
|
|
end
|
|
|
|
|
2018-07-03 06:51:22 -05:00
|
|
|
def fabricate_secondary_emails_for(u)
|
2019-01-11 10:10:02 -06:00
|
|
|
Fabricate(:secondary_email, user: u, email: "first@email.com")
|
|
|
|
Fabricate(:secondary_email, user: u, email: "second@email.com")
|
2018-07-03 06:51:22 -05:00
|
|
|
end
|
|
|
|
|
2019-01-11 10:10:02 -06:00
|
|
|
it "contains an admin's own emails" do
|
|
|
|
fabricate_secondary_emails_for(admin)
|
|
|
|
json = serialize(admin, admin)
|
|
|
|
expect(json[:email]).to eq("admin@email.com")
|
|
|
|
expect(json[:secondary_emails]).to contain_exactly("first@email.com", "second@email.com")
|
2018-07-03 06:51:22 -05:00
|
|
|
end
|
|
|
|
|
2019-01-11 10:10:02 -06:00
|
|
|
it "doesn't include a regular user's emails" do
|
|
|
|
fabricate_secondary_emails_for(user)
|
|
|
|
json = serialize(user, user)
|
|
|
|
expect(json[:email]).to eq(nil)
|
|
|
|
expect(json[:secondary_emails]).to eq(nil)
|
2018-07-03 06:51:22 -05:00
|
|
|
end
|
|
|
|
|
2019-03-13 16:30:25 -05:00
|
|
|
it "doesn't return emails for a moderator request when moderators_view_emails is disabled" do
|
|
|
|
SiteSetting.moderators_view_emails = false
|
2019-01-11 10:10:02 -06:00
|
|
|
fabricate_secondary_emails_for(user)
|
|
|
|
json = serialize(user, moderator, emails_desired: true)
|
|
|
|
expect(json[:email]).to eq(nil)
|
|
|
|
expect(json[:secondary_emails]).to eq(nil)
|
2018-12-19 03:24:57 -06:00
|
|
|
end
|
|
|
|
|
2019-03-13 16:30:25 -05:00
|
|
|
it "returns emails for a moderator request when moderators_view_emails is enabled" do
|
|
|
|
SiteSetting.moderators_view_emails = true
|
2019-01-18 08:26:44 -06:00
|
|
|
fabricate_secondary_emails_for(user)
|
|
|
|
json = serialize(user, moderator, emails_desired: true)
|
|
|
|
expect(json[:email]).to eq("user@email.com")
|
|
|
|
expect(json[:secondary_emails]).to contain_exactly("first@email.com", "second@email.com")
|
|
|
|
end
|
|
|
|
|
2019-01-11 10:10:02 -06:00
|
|
|
it "returns emails for admins when emails_desired is true" do
|
|
|
|
fabricate_secondary_emails_for(user)
|
|
|
|
json = serialize(user, admin, emails_desired: true)
|
|
|
|
expect(json[:email]).to eq("user@email.com")
|
|
|
|
expect(json[:secondary_emails]).to contain_exactly("first@email.com", "second@email.com")
|
2018-07-03 06:51:22 -05:00
|
|
|
end
|
|
|
|
|
2019-01-11 10:10:02 -06:00
|
|
|
it "returns a staged user's emails" do
|
|
|
|
user.staged = true
|
|
|
|
fabricate_secondary_emails_for(user)
|
|
|
|
json = serialize(user, admin)
|
|
|
|
expect(json[:email]).to eq("user@email.com")
|
|
|
|
expect(json[:secondary_emails]).to contain_exactly("first@email.com", "second@email.com")
|
2018-07-03 06:51:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|