cirandas.net

ref: master

test/unit/add_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
require_relative "../test_helper"

class AddMemberTest < ActiveSupport::TestCase

  def setup
    @user = fast_create(User)
    @person = fast_create(Person,:user_id => @user.id)
    @community = fast_create(Community)
  end
  attr_reader :person, :community


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

  should 'actually add memberships when confirmed' do
    community.update_attribute(:closed, true)
    TaskMailer.stubs(:deliver_target_notification)
    task = fast_create(AddMember, :requestor_id => person.id, :target_id => community.id, :target_type => 'Community')
    task.finish

    assert_equal [person], community.members
  end

  should 'make member role the default role' do
    TaskMailer.stubs(:deliver_target_notification)
    task = AddMember.create!(:roles => ["0", "0", nil], :person => person, :organization => community)
    task.finish

    assert_equal [person], community.members
  end

  should 'require requestor' do
    task = AddMember.new
    task.valid?

    ok('must not validate with empty requestor') { task.errors[:requestor_id.to_s].present? }

    task.requestor = Person.new
    task.valid?
    ok('must validate when requestor is given') { task.errors[:requestor_id.to_s].present?}
  end

  should 'require target' do
    task = AddMember.new
    task.valid?

    ok('must not validate with empty target') { task.errors[:target_id.to_s].present? }

    task.target = Person.new
    task.valid?
    ok('must validate when target is given') { task.errors[:target_id.to_s].present?}
  end

  should 'send e-mails' do
    community.update_attribute(:closed, true)
    community.stubs(:notification_emails).returns(["adm@example.com"])

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

    task = AddMember.create!(:person => person, :organization => community)
  end

  should 'send e-mails to requestor' do
    community.update_attribute(:closed, true)
    community.stubs(:notification_emails).returns(["adm@example.com"])

    task = AddMember.create!(:person => person, :organization => community)
    assert_difference "ActionMailer::Base.deliveries.size" do
      task.finish
    end
  end

  should 'has permission to manage members' do
    t = AddMember.new
    assert_equal :manage_memberships, t.permission
  end

  should 'have roles' do
    TaskMailer.stubs(:deliver_target_notification)
    task = AddMember.create!(:roles => [1,2,3], :person => person, :organization => community)
    assert_equal [1,2,3], task.roles
  end

  should 'put member with the right roles' do
    roles = [Profile::Roles.member(community.environment.id), Profile::Roles.admin(community.environment.id)]
    TaskMailer.stubs(:deliver_target_notification)
    task = AddMember.create!(:roles => roles.map(&:id), :person => person, :organization => community)
    task.finish

    current_roles = person.find_roles(community).map(&:role)
    assert_includes current_roles, roles[0]
    assert_includes current_roles, roles[1]
  end

  should 'override target notification message method from Task' do
    task = AddMember.new(:person => person, :organization => community)
    assert_nothing_raised NotImplementedError do
      task.target_notification_message
    end
  end

  should 'ignore roles with id zero' do
    role = Profile::Roles.member(community.environment.id)
    TaskMailer.stubs(:deliver_target_notification)
    task = AddMember.create!(:roles => ["0", role.id, nil], :person => person, :organization => community)
    task.finish

    current_roles = person.find_roles(community).map(&:role)
    assert_includes current_roles, role
  end

  should 'have target notification message' do
    task = AddMember.new(:person => person, :organization => community)

    assert_match(/#{person.name} wants to be a member of '#{community.name}'.*[\n]*.*to accept or reject/, task.target_notification_message)
  end

  should 'have target notification description' do
    task = AddMember.new(:person => person, :organization => community)

    assert_match(/#{task.requestor.name} wants to be a member of '#{community.name}'/, task.target_notification_description)
  end

  should 'deliver target notification message' do
    task = AddMember.new(:person => person, :organization => community)

    email = TaskMailer.target_notification(task, task.target_notification_message).deliver
    assert_match(/#{task.requestor.name} wants to be a member of '#{community.name}'/, email.subject)
  end

  should 'notification description with requestor email if requestor email is public' do
    new_person = create_user('testuser').person
    new_person.update_attributes!({:fields_privacy => {:email => 'public'}})

    task = AddMember.new(:person => new_person, :organization => community)

    assert_match(/\(#{task.requestor.email}\)/, task.target_notification_description)
  end

  should 'notification description without requestor email if requestor email is not public' do
    new_person = create_user('testuser').person
    new_person.update_attributes!({:fields_privacy => {:email => '0'}})

    task = AddMember.new(:person => new_person, :organization => community)

    assert_no_match(/\(#{task.requestor.email}\)/, task.target_notification_description)
  end

  should 'have cancel notification message with explanation' do
    explanation_message = 'some explanation'
    task = AddMember.new(:person => person, :organization => community,
                         :reject_explanation => explanation_message)
    assert_match(/#{explanation_message}/, task.task_cancelled_message)
  end
end