cirandas.net

ref: master

test/unit/invite_member_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require_relative "../test_helper"

class InviteMemberTest < ActiveSupport::TestCase

  should 'be a task' do
    ok { InviteMember.new.kind_of?(Task) }
  end

  should 'actually add as member when confirmed' do
    person = fast_create(Person)
    friend = fast_create(Person)
    friend.stubs(:user).returns(User.new(:email => 'garotos@podres.punk.oi'))
    person.stubs(:user).returns(User.new(:email => 'suburbio-operario@podres.podres'))
    community = fast_create(Community)

    assert_equal [], community.members

    task = InviteMember.create!(:person => person, :friend => friend, :community_id => community.id)
    task.finish
    community.reload

    ok('friend is member of community') { community.members.include?(friend) }
  end

  should 'actually add as member when confirmed if the community is secret' do
    person = fast_create(Person)
    friend = fast_create(Person)
    friend.stubs(:user).returns(User.new(:email => 'garotos@podres.punk.oi'))
    person.stubs(:user).returns(User.new(:email => 'suburbio-operario@podres.podres'))
    community = fast_create(Community, secret: true)

    assert_equal [], community.members

    task = InviteMember.create!(:person => person, :friend => friend, :community_id => community.id)
    task.finish
    community.reload

    ok('friend is member of secret community') { community.members.include?(friend) }
  end

  should 'cancel other invitations for same community when confirmed' do
    friend = create_user('friend').person
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    community = fast_create(Community)

    task = InviteMember.create!(:person => p1, :friend => friend, :community_id => community.id)
    InviteMember.create!(:person => p2, :friend => friend, :community_id => community.id)

    assert_difference 'friend.tasks.pending.count', -2 do
      task.finish
    end
  end

  should 'require community (person inviting other to be a member)' do
    task = InviteMember.new
    task.valid?

    ok('community is required') { task.errors[:community_id.to_s].present? }
  end

  should 'require friend email if no target given (person being invited)' do
    task = InviteMember.new
    task.valid?

    ok('friend_email is required') { task.errors[:friend_email.to_s].present? }
  end

  should 'dont require friend email if target given (person being invited)' do
    task = InviteMember.new(:target => create_user('testuser2').person)
    task.valid?

    ok('friend_email isnt required') { !task.errors[:friend_email.to_s].present? }
  end

  should 'require target (person being invited) if no friend email given' do
    task = InviteMember.new
    task.valid?

    ok('target is required') { task.errors[:target_id.to_s].present? }
  end

  should 'dont require target (person being invited) if friend email given' do
    task = InviteMember.new(:friend_email => "test@test.com")
    task.valid?

    ok('target isn required') { !task.errors[:target_id.to_s].present? }
  end

  should 'not send e-mails to requestor' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person

    TaskMailer.expects(:deliver_task_finished).never
    TaskMailer.expects(:deliver_task_created).never

    task = InviteMember.create!(:person => p1, :friend => p2, :community_id => fast_create(Community).id)
    task.finish
  end

  should 'send e-mails to friend if friend_email given' do
    p1 = create_user('testuser1').person

    mailer = mock
    mailer.expects(:deliver).at_least_once
    TaskMailer.expects(:invitation_notification).returns(mailer).once

    task = InviteMember.create!(:person => p1, :friend_email => 'test@test.com', :message => '<url>', :community_id => fast_create(Community).id)
  end

  should 'send e-mails notification to friend if target given (person being invited)' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person

    mailer = mock
    mailer.expects(:deliver).at_least_once
    TaskMailer.expects(:target_notification).returns(mailer).once

    task = InviteMember.create!(:person => p1, :friend => p2, :community_id => fast_create(Community).id)
  end

  should 'not invite yourself' do
    p = create_user('testuser1').person

    task1 = build(InviteMember, :person => p, :friend => p, :message => 'click here: <url>')
    refute task1.save

    task2 = build(InviteMember, :person => p, :friend_name => 'Myself', :friend_email => p.user.email, :message => 'click here: <url>')
    refute task2.save
  end

  should 'have target notification description' do
    p = create_user('testuser1').person
    community = fast_create(Community)

    task = create(InviteMember, :person => p, :friend_email => 'test@test.com', :message => '<url>', :community_id => community.id)

    assert_match(/#{task.requestor.name} invited you to join #{community.name}/, task.target_notification_description)
  end

  should 'deliver invitation notification' do
    person = create_user('testuser1').person
    community = fast_create(Community)

    task = create(InviteMember, :person => person, :friend_email => 'test@test.com', :message => '<url>', :community_id => community.id)

    email = TaskMailer.invitation_notification(task).deliver

    assert_match(/#{task.requestor.name} invited you to join #{community.name}/, email.subject)
  end

  should 'destroy InviteMember task when the community is destroyed' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    p3 = create_user('testuser3').person
    community = fast_create(Community)

    t1 = InviteMember.create!(:person => p1, :friend => p2, :community_id => community.id)
    t2 = InviteMember.create!(:person => p1, :friend => p3, :community_id => community.id)
    community.destroy

    assert_raise ActiveRecord::RecordNotFound do; t1.reload; end
    assert_raise ActiveRecord::RecordNotFound do; t2.reload; end
  end

  should 'have target notification message only if target given (person being invited)' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person

    task = InviteMember.create!(:person => p1, :friend => p2, :community_id => fast_create(Community).id)
    assert_nothing_raised NotImplementedError do
      task.target_notification_message
    end

    task = InviteMember.create!(:person => p1, :friend_email => 'test@test.com', :message => '<url>', :community_id => fast_create(Community).id)
    assert_raise NotImplementedError do
      task.target_notification_message
    end
  end

  should 'deliver target notification message if target given (person being invited)' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person

    task = InviteMember.create!(:person => p1, :friend => p2, :community_id => fast_create(Community).id)

    email = TaskMailer.target_notification(task, task.target_notification_message).deliver
    assert_match(/#{task.requestor.name} invited you to join #{task.community.name}/, email.subject)
  end

  should 'not invite member if there is a pending invitation' do
    person = create_user('testuser1').person
    friend = create_user('testuser2').person
    community = fast_create(Community)

    assert_difference 'InviteMember.count' do
      InviteMember.create({:person => person, :target => friend, :community_id => community.id})
    end

    assert_no_difference 'InviteMember.count' do
      InviteMember.create({:person => person, :target => friend, :community_id => community.id})
    end
  end
end