2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe BasicUserSerializer do
|
2024-02-01 11:28:10 -05:00
|
|
|
fab!(:user)
|
2024-02-26 08:40:48 -05:00
|
|
|
let(:serializer) { BasicUserSerializer.new(user, scope: Guardian.new(user), root: false) }
|
|
|
|
|
2019-01-11 19:00:22 -05:00
|
|
|
describe "#as_json" do
|
2018-08-21 07:58:11 -04:00
|
|
|
let(:json) { serializer.as_json }
|
|
|
|
|
|
|
|
it "returns the username" do
|
2019-01-11 19:00:22 -05:00
|
|
|
expect(json[:username]).to eq(user.username)
|
|
|
|
expect(json[:name]).to eq(user.name)
|
|
|
|
expect(json[:avatar_template]).to eq(user.avatar_template)
|
2018-08-21 07:58:11 -04:00
|
|
|
end
|
|
|
|
|
2019-12-19 12:48:01 -05:00
|
|
|
describe "extended serializers" do
|
|
|
|
let(:post_action) { Fabricate(:post_action, user: user) }
|
|
|
|
let(:serializer) do
|
|
|
|
PostActionUserSerializer.new(post_action, scope: Guardian.new(user), root: false)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2019-12-19 12:48:01 -05:00
|
|
|
it "returns the user correctly" do
|
|
|
|
expect(serializer.user.username).to eq(user.username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-21 07:58:11 -04:00
|
|
|
it "doesn't return the name it when `enable_names` is false" do
|
|
|
|
SiteSetting.enable_names = false
|
2019-01-11 19:00:22 -05:00
|
|
|
expect(json[:name]).to eq(nil)
|
2019-01-11 13:09:06 -05:00
|
|
|
end
|
2018-08-21 07:58:11 -04:00
|
|
|
end
|
2024-02-26 08:40:48 -05:00
|
|
|
|
|
|
|
describe "#status" do
|
2024-02-01 11:28:10 -05:00
|
|
|
fab!(:user_status)
|
2024-02-26 08:40:48 -05:00
|
|
|
|
|
|
|
before { user.user_status = user_status }
|
|
|
|
|
|
|
|
describe "when status is enabled in settings" do
|
|
|
|
before { SiteSetting.enable_user_status = true }
|
|
|
|
|
|
|
|
it "doesn't add status by default" do
|
|
|
|
serializer = BasicUserSerializer.new(user, root: false)
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json.keys).not_to include :status
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when including user status" do
|
|
|
|
let(:serializer) { BasicUserSerializer.new(user, root: false, include_status: true) }
|
|
|
|
|
|
|
|
it "adds status if `include_status: true` has been passed" do
|
|
|
|
json = serializer.as_json
|
|
|
|
expect(json[:status]).to_not be_nil do |status|
|
|
|
|
expect(status.description).to eq(user_status.description)
|
|
|
|
expect(status.emoji).to eq(user_status.emoji)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't add expired user status" do
|
|
|
|
user.user_status.ends_at = 1.minutes.ago
|
|
|
|
json = serializer.as_json
|
|
|
|
expect(json.keys).not_to include :status
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return status if user doesn't have it set" do
|
|
|
|
user.clear_status!
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json.keys).not_to include :status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when status is disabled in settings" do
|
|
|
|
before { SiteSetting.enable_user_status = false }
|
|
|
|
|
|
|
|
it "doesn't add user status" do
|
|
|
|
serializer = BasicUserSerializer.new(user, root: false, include_status: true)
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json.keys).not_to include :status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-21 07:58:11 -04:00
|
|
|
end
|