invite_controller_test.rb
10.3 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
require File.dirname(__FILE__) + '/../test_helper'
class InviteControllerTest < ActionController::TestCase
def setup
@profile = create_user('testuser').person
@friend = create_user('thefriend').person
@community = fast_create(Community)
login_as ('testuser')
end
attr_accessor :profile, :friend, :community
should 'add manually invitation of an added address with friend object on a queue and process it later' do
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
assert_difference InviteFriend, :count, 1 do
process_delayed_job_queue
end
end
should 'add manually invitation of an added address with only email on a queue and process it later' do
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "test@test.com", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
assert_difference InviteFriend, :count, 1 do
process_delayed_job_queue
end
end
should 'add manually invitation of an added address with email and other format on a queue and process it later' do
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "test@test.cz.com", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
assert_difference InviteFriend, :count, 1 do
process_delayed_job_queue
end
end
should 'add manually invitation of more than one added address on a queue and process it later' do
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "Some Friend <somefriend@email.com>\r\notherperson@bleble.net\r\n", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
assert_difference InviteFriend, :count, 2 do
process_delayed_job_queue
end
end
should 'add manually invitation of an added address with name and e-mail on a queue and process it later' do
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "Test Name <test@test.com>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
assert_difference InviteFriend, :count, 1 do
process_delayed_job_queue
end
end
should 'add invitation of yourself on a queue and not process it later' do
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{profile.name} <#{profile.user.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
assert_no_difference InviteFriend, :count do
process_delayed_job_queue
end
end
should 'add invitation of an already friend on a queue and not process it later' do
friend = create_user('testfriend', :email => 'friend@noosfero.org')
friend.person.add_friend(profile)
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
assert_no_difference InviteFriend, :count do
process_delayed_job_queue
end
end
should 'display invitation page' do
get :select_address_book, :profile => profile.identifier
assert_response :success
assert_tag :tag => 'h1', :content => 'Invite your friends'
end
should 'get mail template to invite members' do
community.add_admin(profile)
contact_list = ContactList.create
get :select_friends, :profile => community.identifier, :contact_list => contact_list.id
assert_equal InviteMember.mail_template, assigns(:mail_template)
end
should 'get mail template to invite friends' do
community.add_admin(profile)
contact_list = ContactList.create
get :select_friends, :profile => profile.identifier, :contact_list => contact_list.id
assert_equal InviteFriend.mail_template, assigns(:mail_template)
end
should 'deny select_address_book f user has no rights to invite members' do
get :select_address_book, :profile => community.identifier
assert_response 403 # forbidden
end
should 'deny select_friends if user has no rights to invite members' do
get :select_friends, :profile => community.identifier
assert_response 403 # forbidden
end
should 'deny select_address_book access when trying to invite friends to another user' do
get :select_address_book, :profile => friend.identifier
assert_response 403 # forbidden
end
should 'deny select_friends access when trying to invite friends to another user' do
get :select_address_book, :profile => friend.identifier
assert_response 403 # forbidden
end
should 'redirect to profile after invitation if profile is a person' do
contact_list = ContactList.create
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'friends'
end
should 'redirect to profile after invitation if profile is not a person' do
community.add_admin(profile)
contact_list = ContactList.create
post :select_friends, :profile => community.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_redirected_to :controller => 'profile', :action => 'members'
end
should 'create a job to get emails after choose address book' do
community.add_admin(profile)
contact_list = ContactList.create
assert_difference Delayed::Job, :count, 1 do
post :select_address_book, :profile => community.identifier, :contact_list => contact_list.id, :import_from => 'gmail'
end
end
should 'destroy contact_list after invitation when import is manual' do
contact_list = ContactList.create
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert ContactList.exists?(contact_list.id)
process_delayed_job_queue
assert !ContactList.exists?(contact_list.id)
end
should 'destroy contact_list after invitation when import is not manual' do
contact_list = ContactList.create
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "not_manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert ContactList.exists?(contact_list.id)
process_delayed_job_queue
assert !ContactList.exists?(contact_list.id)
end
should 'return empty hash as invitation data if contact list was not fetched' do
contact_list = ContactList.create
get :invitation_data, :profile => profile.identifier, :contact_list => contact_list.id
assert_equal 'application/javascript', @response.content_type
assert_equal '{}', @response.body
end
should 'return hash as invitation data if contact list was fetched' do
contact_list = ContactList.create(:fetched => true)
get :invitation_data, :profile => profile.identifier, :contact_list => contact_list.id
assert_equal 'application/javascript', @response.content_type
assert_match /\{\"fetched\":\s*true,\s*\"contact_list\":\s*#{contact_list.id},\s*\"error\":\s*null\}/, @response.body
end
should 'render empty list of contacts' do
contact_list = ContactList.create(:fetched => true)
get :add_contact_list, :profile => profile.identifier, :contact_list => contact_list.id
assert_response :success
assert_template '_contact_list'
assert_no_tag(:tag => 'input', :attributes => { :type => 'checkbox', :name => 'webmail_import_addresses[]'})
end
should 'render list of contacts' do
contact_list = ContactList.create(:fetched => true, :list => ['email1@noosfero.org', 'email2@noosfero.org'])
get :add_contact_list, :profile => profile.identifier, :contact_list => contact_list.id
assert_response :success
assert_template '_contact_list'
i = 0
contact_list.list.each do |contact|
i += 1
assert_tag(:tag => 'input', :attributes => { :type => 'checkbox', :name => 'webmail_import_addresses[]', :id => "contacts_to_invite_#{i}", :value => contact[2]})
end
end
should 'destroy contact_list when cancel_fetching_emails' do
contact_list = ContactList.create
assert_difference ContactList, :count, -1 do
get :cancel_fetching_emails, :profile => profile.identifier, :contact_list => contact_list.id
end
assert_redirected_to :action => 'select_address_book'
end
should 'set locale in the background job' do
@controller.stubs(:locale).returns('pt')
contact_list = ContactList.create
post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: <url>", :contact_list => contact_list.id
assert_equal 'pt', Delayed::Job.first.payload_object.locale
end
end