2013-10-31 03:45:13 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
require_dependency 'user'
|
|
|
|
|
|
|
|
describe UserSerializer do
|
|
|
|
|
2014-11-27 02:20:03 +08:00
|
|
|
context "with a TL0 user seen as anonymous" do
|
|
|
|
let(:user) { Fabricate.build(:user, trust_level: 0, user_profile: Fabricate.build(:user_profile)) }
|
|
|
|
let(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) }
|
|
|
|
let(:json) { serializer.as_json }
|
|
|
|
|
|
|
|
let(:untrusted_attributes) { %i{bio_raw bio_cooked bio_excerpt location website profile_background card_background} }
|
|
|
|
|
|
|
|
it "doesn't serialize untrusted attributes" do
|
|
|
|
untrusted_attributes.each { |attr| json.should_not have_key(attr) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
context "with a user" do
|
2014-06-08 03:52:51 +08:00
|
|
|
let(:user) { Fabricate.build(:user, user_profile: Fabricate.build(:user_profile) ) }
|
2013-10-31 03:45:13 +08:00
|
|
|
let(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) }
|
|
|
|
let(:json) { serializer.as_json }
|
|
|
|
|
|
|
|
it "produces json" do
|
|
|
|
json.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with `enable_names` true" do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:enable_names?).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a name" do
|
2013-10-31 04:04:26 +08:00
|
|
|
json[:name].should be_present
|
2013-10-31 03:45:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with `enable_names` false" do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:enable_names?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a name" do
|
|
|
|
json[:name].should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-21 00:11:36 +08:00
|
|
|
context "with filled out card background" do
|
2014-10-17 03:05:36 +08:00
|
|
|
before do
|
2014-10-21 00:11:36 +08:00
|
|
|
user.user_profile.card_background = 'http://card.com'
|
2014-10-17 03:05:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has a profile background" do
|
2014-10-21 00:11:36 +08:00
|
|
|
expect(json[:card_background]).to eq 'http://card.com'
|
2014-10-17 03:05:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-12 09:52:50 +08:00
|
|
|
context "with filled out profile background" do
|
|
|
|
before do
|
|
|
|
user.user_profile.profile_background = 'http://background.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a profile background" do
|
|
|
|
expect(json[:profile_background]).to eq 'http://background.com'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-08 03:52:51 +08:00
|
|
|
context "with filled out website" do
|
|
|
|
before do
|
|
|
|
user.user_profile.website = 'http://example.com'
|
|
|
|
end
|
2013-10-31 03:45:13 +08:00
|
|
|
|
2014-06-08 03:52:51 +08:00
|
|
|
it "has a website" do
|
|
|
|
expect(json[:website]).to eq 'http://example.com'
|
|
|
|
end
|
|
|
|
end
|
2014-06-10 13:19:08 +08:00
|
|
|
|
|
|
|
context "with filled out bio" do
|
|
|
|
before do
|
|
|
|
user.user_profile.bio_raw = 'my raw bio'
|
|
|
|
user.user_profile.bio_cooked = 'my cooked bio'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a bio" do
|
|
|
|
expect(json[:bio_raw]).to eq 'my raw bio'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a cooked bio" do
|
|
|
|
expect(json[:bio_cooked]).to eq 'my cooked bio'
|
|
|
|
end
|
|
|
|
end
|
2013-10-31 03:45:13 +08:00
|
|
|
end
|
2014-08-19 23:05:35 +08:00
|
|
|
|
|
|
|
context "with custom_fields" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:json) { UserSerializer.new(user, scope: Guardian.new, root: false).as_json }
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.custom_fields['secret_field'] = 'Only for me to know'
|
|
|
|
user.custom_fields['public_field'] = 'Everyone look here'
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't serialize the fields by default" do
|
|
|
|
json[:custom_fields]
|
|
|
|
json[:custom_fields].should be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it "serializes the fields listed in public_user_custom_fields site setting" do
|
|
|
|
SiteSetting.stubs(:public_user_custom_fields).returns('public_field')
|
|
|
|
json[:custom_fields]['public_field'].should == user.custom_fields['public_field']
|
2014-09-25 23:44:48 +08:00
|
|
|
json[:custom_fields]['secret_field'].should == nil
|
2014-08-19 23:05:35 +08:00
|
|
|
end
|
|
|
|
end
|
2013-10-31 03:45:13 +08:00
|
|
|
end
|