diff options
-rw-r--r-- | app/models/info_request.rb | 4 | ||||
-rw-r--r-- | app/models/info_request_event.rb | 2 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 29 |
3 files changed, 34 insertions, 1 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index aaf171c4c..a7d482930 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -156,6 +156,10 @@ class InfoRequest < ActiveRecord::Base end end + def user_json_for_api + is_external? ? { :name => user_name || _("Anonymous user") } : user.json_for_api + end + @@custom_states_loaded = false begin if !Rails.env.test? diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index 469aabc4a..0967e3940 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -420,7 +420,7 @@ class InfoRequestEvent < ActiveRecord::Base if deep ret[:info_request] = self.info_request.json_for_api(false) ret[:public_body] = self.info_request.public_body.json_for_api - ret[:user] = self.info_request.user.json_for_api + ret[:user] = self.info_request.user_json_for_api end return ret diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 3eb88b2bb..b193f8bc2 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -562,7 +562,36 @@ describe InfoRequest do @info_request.prominence = 'requester_only' @info_request.all_can_view?.should == false end + end +end + +describe InfoRequest, 'when generating json for the api' do + + before do + @user = mock_model(User, :json_for_api => { :id => 20, + :url_name => 'alaveteli_user', + :name => 'Alaveteli User', + :ban_text => '', + :about_me => 'Hi' }) + end + + it 'should return full user info for an internal request' do + @info_request = InfoRequest.new(:user => @user) + @info_request.user_json_for_api.should == { :id => 20, + :url_name => 'alaveteli_user', + :name => 'Alaveteli User', + :ban_text => '', + :about_me => 'Hi' } + end + it "should return a hash with the user's name for an external request" do + @info_request = InfoRequest.new(:external_url => 'http://www.example.com', + :external_user_name => 'External User') + @info_request.user_json_for_api.should == {:name => 'External User'} end + it 'should return "Anonymous user" for an anonymous external user' do + @info_request = InfoRequest.new(:external_url => 'http://www.example.com') + @info_request.user_json_for_api.should == {:name => 'Anonymous user'} + end end |