Skip to content

A platform for community discussion. Free, open, simple.

908 B
Raw Blame
# frozen_string_literal: true

describe UserSerializer do
  fab!(:user)

  subject(:json) { described_class.new(user, scope: guardian).as_json }

  before do
    SiteSetting.discourse_events_enabled = true
    user.upsert_custom_fields(DiscourseEvents::REGION_CUSTOM_FIELD => "uk")
  end

  context "as another user" do
    let(:guardian) { Fabricate(:user).guardian }

    it "does not return user region" do
      expect(json[:user][:custom_fields]).to be_blank
    end
  end

  context "as current user" do
    let(:guardian) { user.guardian }

    it "returns user region" do
      expect(json[:user][:custom_fields]).to eq(DiscourseEvents::REGION_CUSTOM_FIELD => "uk")
    end
  end

  context "as staff" do
    let(:guardian) { Fabricate(:admin).guardian }

    it "returns user region" do
      expect(json[:user][:custom_fields]).to eq(DiscourseEvents::REGION_CUSTOM_FIELD => "uk")
    end
  end
end