cirandas.net

ref: master

test/unit/activity_presenter_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require_relative "../test_helper"

class ActivityPresenterTest < ActiveSupport::TestCase
  def setup
    @user = fast_create(Person)
    @target = mock
    @owner = mock

    @target.stubs(:wall_access).returns(AccessLevels::LEVELS[:users])
  end

  should 'be available for ActionTracker::Record' do
    assert ActivityPresenter.available?(ActionTracker::Record.new)
  end

  should 'be available for ProfileActivity' do
    assert ActivityPresenter.available?(ProfileActivity.new)
  end

  should 'return correct target for ActionTracker::Record' do
    activity = ActionTracker::Record.new
    activity.stubs(:target).returns(@target)
    assert_equal @target, ActivityPresenter.target(activity)
  end

  should 'return correct target for ProfileActivity' do
    notification = ProfileActivity.new
    record = ActionTracker::Record.new
    notification.stubs(:activity).returns(record)
    record.stubs(:target).returns(@target)

    assert_equal @target, ActivityPresenter.target(notification)
  end

  should 'return correct owner for ActionTracker::Record' do
    activity = ActionTracker::Record.new
    activity.stubs(:user).returns(@owner)
    assert_equal @owner, ActivityPresenter.owner(activity)
  end

  should 'return correct owner for ProfileActivity' do
    notification = ProfileActivity.new
    notification.stubs(:profile).returns(@owner)

    assert_equal @owner, ActivityPresenter.owner(notification)
  end

  should 'not be hidden for user if target does not respond to display_to' do
    presenter = ActivityPresenter.new(@target)

    AccessLevels.stubs(:can_access?).returns(true)
    @target.stubs(:is_a?).with(Profile).returns(true)
    @target.stubs(:allow_followers?).returns(true)

    refute presenter.hidden_for?(@user)
  end

  should 'be hidden for user based on target display_to' do
    presenter = ActivityPresenter.new(@target)
    AccessLevels.stubs(:can_access?).returns(true)
    @target.stubs(:is_a?).with(Profile).returns(true)
    @target.stubs(:allow_followers?).returns(true)

    @target.stubs(:display_to?).with(@user).returns(false)
    assert presenter.hidden_for?(@user)

    @target.stubs(:display_to?).with(@user).returns(true)
    refute presenter.hidden_for?(@user)
  end

  should 'be hidden if user disabled the followers feature' do
    presenter = ActivityPresenter.new(@target)
    AccessLevels.stubs(:can_access?).returns(true)
    @target.stubs(:is_a?).with(Profile).returns(true)
    @target.stubs(:display_to?).with(@user).returns(true)

    @target.stubs(:allow_followers?).returns(false)
    assert presenter.hidden_for?(@user)

    @target.stubs(:allow_followers?).returns(true)
    refute presenter.hidden_for?(@user)
  end

  should 'verify if user is involved as target with the activity' do
    user = mock
    presenter = ActivityPresenter.new(@target)
    presenter.stubs(:target).returns(user)
    presenter.stubs(:owner).returns(nil)
    assert presenter.involved?(user)
  end

  should 'verify if user is involved as owner with the activity' do
    user = mock
    presenter = ActivityPresenter.new(@target)
    presenter.stubs(:target).returns(nil)
    presenter.stubs(:owner).returns(user)
    assert presenter.involved?(user)
  end

  should 'refute if user is not involved' do
    user = mock
    presenter = ActivityPresenter.new(@target)
    presenter.stubs(:target).returns(nil)
    presenter.stubs(:owner).returns(nil)
    refute presenter.involved?(user)
  end

  should 'be hidden if the target is a profile with a restricted wall' do
    target = create_user.person
    presenter = ActivityPresenter.new(target)

    Person.any_instance.stubs(:wall_access).returns(AccessLevels::LEVELS[:related])
    assert presenter.hidden_for?(@user)
  end

  should 'not be hidden if the target is a profile with a public wall' do
    target = create_user.person
    presenter = ActivityPresenter.new(target)

    Person.any_instance.stubs(:wall_access).returns(AccessLevels::LEVELS[:users])
    refute presenter.hidden_for?(@user)
  end

  should 'be hidden if the target is an article whose profile has a restricted wall' do
    profile = fast_create(Community)
    article = fast_create(Article, profile_id: profile.id)
    presenter = ActivityPresenter.new(article)

    Community.any_instance.stubs(:wall_access).returns(AccessLevels::LEVELS[:related])
    assert presenter.hidden_for?(@user)
  end

  should 'not be hidden if the target is an article whose profile has a public wall' do
    profile = fast_create(Community)
    article = fast_create(Article, profile_id: profile.id)
    presenter = ActivityPresenter.new(article)

    Community.any_instance.stubs(:wall_access).returns(AccessLevels::LEVELS[:users])
    refute presenter.hidden_for?(@user)
  end

  should 'be hidden if the target is a scrap whose receiver has a restricted wall' do
    receiver = create_user.person
    scrap = fast_create(Scrap, receiver_id: receiver.id)
    presenter = ActivityPresenter.new(scrap)

    Person.any_instance.stubs(:wall_access).returns(AccessLevels::LEVELS[:related])
    assert presenter.hidden_for?(@user)
  end

  should 'not be hidden if the target is a scrap whose receiver has a public wall' do
    receiver = create_user.person
    scrap = fast_create(Scrap, receiver_id: receiver.id)
    presenter = ActivityPresenter.new(scrap)

    Person.any_instance.stubs(:wall_access).returns(AccessLevels::LEVELS[:users])
    refute presenter.hidden_for?(@user)
  end

  should 'use the owner as target profile if the target class is unknown' do
    target = fast_create(ProfileFollower, profile_id: @user.id)
    presenter = ActivityPresenter.new(target)

    presenter.expects(:owner).at_least(1).returns(@owner)
    @owner.expects(:wall_access).returns(AccessLevels::LEVELS[:users])
    AccessLevels.expects(:can_access?).returns(false)
    presenter.hidden_for?(@user)
  end
end