aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2013-07-31 18:36:55 +0100
committerLouise Crow <louise.crow@gmail.com>2013-09-16 12:25:23 +0100
commitaf6bc758faf62eb44e5822ed0eb6c1871db6ac91 (patch)
tree613fc33e4c39e6e90c153a6c465b996aa2d08cea
parent49c346c1eecc4b785bf341e2746b7b458319d7bb (diff)
Add a method to ask whether a user can view an incoming message.
-rw-r--r--app/models/incoming_message.rb10
-rw-r--r--spec/models/incoming_message_spec.rb57
2 files changed, 67 insertions, 0 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index bf05cb0d1..6e85f86f0 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -71,6 +71,16 @@ class IncomingMessage < ActiveRecord::Base
self.info_request_events.detect{ |e| e.event_type == 'response' }
end
+ def user_can_view?(user)
+ if self.prominence == 'hidden'
+ return User.view_hidden?(user)
+ end
+ if self.prominence == 'requester_only'
+ return self.info_request.is_owning_user?(user)
+ end
+ return true
+ end
+
# Return a cached structured mail object
def mail(force = nil)
if (!force.nil? || @mail.nil?) && !self.raw_email.nil?
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index f249ee87e..a45bf303e 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -54,6 +54,63 @@ describe IncomingMessage, 'when getting a response event' do
end
+describe IncomingMessage, 'when asked if a user can view it', :focus => true do
+
+ before do
+ @user = mock_model(User)
+ @info_request = mock_model(InfoRequest)
+ @incoming_message = IncomingMessage.new(:info_request => @info_request)
+ end
+
+ context 'if the prominence is hidden' do
+
+ before do
+ @incoming_message.prominence = 'hidden'
+ end
+
+ it 'should return true if the user can view hidden things' do
+ User.stub!(:view_hidden?).with(@user).and_return(true)
+ @incoming_message.user_can_view?(@user).should be_true
+ end
+
+ it 'should return false if the user cannot view hidden things' do
+ User.stub!(:view_hidden?).with(@user).and_return(false)
+ @incoming_message.user_can_view?(@user).should be_false
+ end
+
+ end
+
+ context 'if the prominence is requester_only' do
+
+ before do
+ @incoming_message.prominence = 'requester_only'
+ end
+
+ it 'should return true if the user owns the associated request' do
+ @info_request.stub!(:is_owning_user?).with(@user).and_return(true)
+ @incoming_message.user_can_view?(@user).should be_true
+ end
+
+ it 'should return false if the user does not own the associated request' do
+ @info_request.stub!(:is_owning_user?).with(@user).and_return(false)
+ @incoming_message.user_can_view?(@user).should be_false
+ end
+ end
+
+ context 'if the prominence is normal' do
+
+ before do
+ @incoming_message.prominence = 'normal'
+ end
+
+ it 'should return true' do
+ @incoming_message.user_can_view?(@user).should be_true
+ end
+
+ end
+
+end
+
describe IncomingMessage, " when dealing with incoming mail" do
before(:each) do