2015-10-11 10:41:23 +01:00
require 'rails_helper'
2013-10-30 15:45:13 -04:00
require_dependency 'user'
describe UserSerializer do
2014-11-26 19:20:03 +01: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
2014-12-31 11:55:03 -03:00
untrusted_attributes . each { | attr | expect ( json ) . not_to have_key ( attr ) }
2014-11-26 19:20:03 +01:00
end
end
2016-02-17 15:46:19 +11:00
context " as current user " do
it " serializes options correctly " do
# so we serialize more stuff
SiteSetting . edit_history_visible_to_public = false
2016-02-18 16:57:22 +11:00
SiteSetting . default_other_auto_track_topics_after_msecs = 0
SiteSetting . default_other_new_topic_duration_minutes = 60 * 24
2016-02-17 15:46:19 +11:00
user = Fabricate . build ( :user ,
user_profile : Fabricate . build ( :user_profile ) ,
user_option : UserOption . new ( edit_history_public : true ) ,
user_stat : UserStat . new
)
json = UserSerializer . new ( user , scope : Guardian . new ( user ) , root : false ) . as_json
expect ( json [ :user_option ] [ :edit_history_public ] ) . to eq ( true )
2016-02-18 16:57:22 +11:00
expect ( json [ :user_option ] [ :new_topic_duration_minutes ] ) . to eq ( 60 * 24 )
expect ( json [ :user_option ] [ :auto_track_topics_after_msecs ] ) . to eq ( 0 )
2016-02-17 15:46:19 +11:00
end
end
2013-10-30 15:45:13 -04:00
context " with a user " do
2014-06-07 12:52:51 -07:00
let ( :user ) { Fabricate . build ( :user , user_profile : Fabricate . build ( :user_profile ) ) }
2013-10-30 15:45:13 -04:00
let ( :serializer ) { UserSerializer . new ( user , scope : Guardian . new , root : false ) }
let ( :json ) { serializer . as_json }
it " produces json " do
2014-12-31 11:55:03 -03:00
expect ( json ) . to be_present
2013-10-30 15:45:13 -04:00
end
context " with `enable_names` true " do
before do
2016-02-17 15:46:19 +11:00
SiteSetting . enable_names = true
2013-10-30 15:45:13 -04:00
end
it " has a name " do
2014-12-31 11:55:03 -03:00
expect ( json [ :name ] ) . to be_present
2013-10-30 15:45:13 -04:00
end
end
2016-02-17 15:46:19 +11:00
2013-10-30 15:45:13 -04:00
context " with `enable_names` false " do
before do
SiteSetting . stubs ( :enable_names? ) . returns ( false )
end
it " has a name " do
2014-12-31 11:55:03 -03:00
expect ( json [ :name ] ) . to be_blank
2013-10-30 15:45:13 -04:00
end
end
2014-10-20 12:11:36 -04:00
context " with filled out card background " do
2014-10-16 15:05:36 -04:00
before do
2014-10-20 12:11:36 -04:00
user . user_profile . card_background = 'http://card.com'
2014-10-16 15:05:36 -04:00
end
it " has a profile background " do
2014-10-20 12:11:36 -04:00
expect ( json [ :card_background ] ) . to eq 'http://card.com'
2014-10-16 15:05:36 -04:00
end
end
2014-06-11 18:52:50 -07: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-07 12:52:51 -07:00
context " with filled out website " do
before do
2015-08-10 13:37:53 +05:30
user . user_profile . website = 'http://example.com/user'
2014-06-07 12:52:51 -07:00
end
2013-10-30 15:45:13 -04:00
2014-06-07 12:52:51 -07:00
it " has a website " do
2015-08-10 13:37:53 +05:30
expect ( json [ :website ] ) . to eq 'http://example.com/user'
end
context " has a website name " do
it " returns website host name when instance domain is not same as website domain " do
Discourse . stubs ( :current_hostname ) . returns ( 'discourse.org' )
expect ( json [ :website_name ] ) . to eq 'example.com'
end
it " returns complete website path when instance domain is same as website domain " do
Discourse . stubs ( :current_hostname ) . returns ( 'example.com' )
expect ( json [ :website_name ] ) . to eq 'example.com/user'
end
it " returns complete website path when website domain is parent of instance domain " do
Discourse . stubs ( :current_hostname ) . returns ( 'forums.example.com' )
expect ( json [ :website_name ] ) . to eq 'example.com/user'
end
2014-06-07 12:52:51 -07:00
end
end
2014-06-10 01:19:08 -04: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-30 15:45:13 -04:00
end
2014-08-19 11:05:35 -04: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 ]
2014-12-31 11:55:03 -03:00
expect ( json [ :custom_fields ] ) . to be_empty
2014-08-19 11:05:35 -04:00
end
it " serializes the fields listed in public_user_custom_fields site setting " do
SiteSetting . stubs ( :public_user_custom_fields ) . returns ( 'public_field' )
2014-12-31 11:55:03 -03:00
expect ( json [ :custom_fields ] [ 'public_field' ] ) . to eq ( user . custom_fields [ 'public_field' ] )
expect ( json [ :custom_fields ] [ 'secret_field' ] ) . to eq ( nil )
2014-08-19 11:05:35 -04:00
end
end
2013-10-30 15:45:13 -04:00
end