add_member_test.rb
3.99 KB
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
require File.dirname(__FILE__) + '/../test_helper'
class AddMemberTest < ActiveSupport::TestCase
  def setup
    @person = fast_create(Person)
    @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.invalid?(:requestor_id) }
    task.requestor = Person.new
    task.valid?
    ok('must validate when requestor is given') { task.errors.invalid?(:requestor_id)}
  end
  should 'require target' do
    task = AddMember.new
    task.valid?
    ok('must not validate with empty target') { task.errors.invalid?(:target_id) }
    task.target = Person.new
    task.valid?
    ok('must validate when target is given') { task.errors.invalid?(:target_id)}
  end
  should 'send e-mails' do
    community.update_attribute(:closed, true)
    community.stubs(:notification_emails).returns(["adm@example.com"])
    TaskMailer.expects(:deliver_target_notification).at_least_once
    task = AddMember.create!(:person => person, :organization => community)
  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 this community.*[\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 this community/, task.target_notification_description)
  end
  should 'deliver target notification message' do
    task = AddMember.new(:person => person, :organization => community)
    email = TaskMailer.deliver_target_notification(task, task.target_notification_message)
    assert_match(/#{task.requestor.name} wants to be a member of this community/, email.subject)
  end
end