discourse/spec/serializers/admin_user_list_serializer_...

100 lines
2.4 KiB
Ruby
Raw Normal View History

2018-07-03 07:51:22 -04:00
require 'rails_helper'
require_dependency 'user'
describe AdminUserListSerializer do
context "emails" do
let(:admin) { Fabricate(:user_single_email, admin: true, email: "admin@email.com") }
let(:moderator) { Fabricate(:user_single_email, moderator: true, email: "moderator@email.com") }
2018-07-03 07:51:22 -04:00
let(:user) { Fabricate(:user_single_email, email: "user@email.com") }
let(:guardian) { Guardian.new(admin) }
let(:mod_guardian) { Guardian.new(moderator) }
2018-07-03 07:51:22 -04:00
let(:json) do
AdminUserListSerializer.new(user,
scope: guardian,
root: false
).as_json
end
let(:mod_json) do
AdminUserListSerializer.new(user,
scope: mod_guardian,
root: false
).as_json
end
2018-07-03 07:51:22 -04:00
def fabricate_secondary_emails_for(u)
["first", "second"].each do |name|
Fabricate(:secondary_email, user: u, email: "#{name}@email.com")
end
end
shared_examples "shown" do |email|
it "contains emails" do
expect(json[:email]).to eq(email)
expect(json[:secondary_emails]).to contain_exactly(
"first@email.com",
"second@email.com"
)
end
end
shared_examples "not shown" do
it "doesn't contain emails" do
expect(json[:email]).to eq(nil)
expect(json[:secondary_emails]).to eq(nil)
end
end
context "with myself" do
let(:user) { admin }
before do
fabricate_secondary_emails_for(admin)
end
include_examples "shown", "admin@email.com"
end
context "with a normal user" do
before do
fabricate_secondary_emails_for(user)
end
include_examples "not shown"
end
context "when moderator makes a request with show_emails param set to true" do
before do
mod_guardian.can_see_emails = true
fabricate_secondary_emails_for(user)
end
it "doesn't contain emails" do
expect(mod_json[:email]).to eq(nil)
expect(mod_json[:secondary_emails]).to eq(nil)
end
end
2018-07-03 07:51:22 -04:00
context "with a normal user after clicking 'show emails'" do
before do
guardian.can_see_emails = true
fabricate_secondary_emails_for(user)
end
include_examples "shown", "user@email.com"
end
context "with a staged user" do
before do
user.staged = true
fabricate_secondary_emails_for(user)
end
include_examples "shown", "user@email.com"
end
end
end