Commit 59c529511586fb09fe9d236ce313dfd6198cbafd
1 parent
fc586dbf
Exists in
master
queueing email
Showing
3 changed files
with
571 additions
and
72 deletions
Show diff stats
controllers/email_article_plugin_myprofile_controller.rb
1 | -require 'nokogiri' | |
2 | -require 'open-uri' | |
3 | 1 | |
4 | 2 | class EmailArticlePluginMyprofileController < MyProfileController |
5 | 3 | |
6 | 4 | needs_profile |
7 | - | |
8 | - class Webpage < Nokogiri::HTML::Document | |
9 | - attr_accessor :url | |
10 | 5 | |
11 | - class << self | |
12 | 6 | |
13 | - def new(url) | |
14 | - html = open(url) | |
15 | - self.parse(html).tap do |d| | |
16 | - d.url = url | |
17 | - end | |
18 | - end | |
19 | - end | |
20 | - end | |
7 | + # def send_email | |
8 | + # if user.is_admin?(profile) | |
9 | + # article = profile.articles.find(params[:id]) | |
10 | + # #EmailArticlePluginMyprofileController::Sender.content(article).deliver | |
11 | + # Sender.content(article) | |
12 | + # render :text => "Email sent to queue" | |
13 | + # else | |
14 | + # render :status => :forbidden, :text => "Forbidden user" | |
15 | + # end | |
16 | + # end | |
21 | 17 | |
22 | 18 | def send_email |
23 | - if user.is_admin?(profile) | |
24 | - article = profile.articles.find(params[:id]) | |
25 | -# article = Article.find(params[:id]) | |
26 | - EmailArticlePluginMyprofileController::Sender.content(article).deliver | |
27 | - render :text => "Email sent to queue" | |
28 | - else | |
19 | + unless user.is_admin?(profile) | |
29 | 20 | render :status => :forbidden, :text => "Forbidden user" |
21 | + return | |
30 | 22 | end |
23 | + mailing_params = {"subject"=>"assunto", "body"=>"corpo"} | |
24 | + @mailing = profile.mailings.build(mailing_params) | |
25 | + # if request.post? | |
26 | + @mailing.locale = locale | |
27 | + @mailing.person = user | |
28 | + if @mailing.save | |
29 | + render :text => "Email sent to queue" | |
30 | + #session[:notice] = _('The e-mails are being sent') | |
31 | + # redirect_to_previous_location | |
32 | + else | |
33 | + render :status => :forbidden, :text => "Forbidden user" | |
34 | + # session[:notice] = _('Could not create the e-mail') | |
35 | + end | |
36 | + # end | |
31 | 37 | end |
32 | 38 | |
33 | - class Sender < ActionMailer::Base | |
34 | - def content(article) | |
35 | - doc = Nokogiri::HTML(article.body) | |
36 | - doc.css("a").each do |link| | |
37 | - unless link.attribute("href").nil? | |
38 | - relative_path = link.attribute("href").value | |
39 | - link.attribute("href").value = "http://#{article.environment.default_hostname}/#{relative_path}" | |
40 | - end | |
41 | - end | |
42 | - | |
43 | - doc.css("img").each do |link| | |
44 | - unless link.attribute("src").nil? | |
45 | - relative_path = link.attribute("src").value | |
46 | - link.attribute("src").value = "http://#{article.environment.default_hostname}/#{relative_path}" | |
47 | - end | |
48 | - end | |
49 | - | |
39 | + # before_filter :check_access_to_profile, :except => [:join, :join_not_logged, :index, :add] | |
40 | + # before_filter :store_location, :only => [:join, :join_not_logged, :report_abuse, :send_mail] | |
41 | + # before_filter :login_required, :only => [:add, :join, :leave, :unblock, :leave_scrap, :remove_scrap, :remove_activity, :view_more_activities, :view_more_network_activities, :report_abuse, :register_report, :leave_comment_on_activity, :send_mail] | |
42 | + # | |
43 | + # helper TagsHelper | |
44 | + # | |
45 | + # protect 'send_mail_to_members', :profile, :only => [:send_mail] | |
46 | + # | |
47 | + # def index | |
48 | + # @network_activities = !@profile.is_a?(Person) ? @profile.tracked_notifications.visible.paginate(:per_page => 15, :page => params[:page]) : [] | |
49 | + # if logged_in? && current_person.follows?(@profile) | |
50 | + # @network_activities = @profile.tracked_notifications.visible.paginate(:per_page => 15, :page => params[:page]) if @network_activities.empty? | |
51 | + # @activities = @profile.activities.paginate(:per_page => 15, :page => params[:page]) | |
52 | + # end | |
53 | + # @tags = profile.article_tags | |
54 | + # allow_access_to_page | |
55 | + # end | |
56 | + # | |
57 | + # def tags | |
58 | + # @tags_cache_key = "tags_profile_#{profile.id.to_s}" | |
59 | + # if is_cache_expired?(@tags_cache_key) | |
60 | + # @tags = profile.article_tags | |
61 | + # end | |
62 | + # end | |
63 | + # | |
64 | + # def content_tagged | |
65 | + # @tag = params[:id] | |
66 | + # @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_#{profile.id.to_s}_page_#{params[:npage]}" | |
67 | + # if is_cache_expired?(@tag_cache_key) | |
68 | + # @tagged = profile.tagged_with(@tag).paginate(:per_page => 20, :page => params[:npage]) | |
69 | + # end | |
70 | + # end | |
71 | + # | |
72 | + # def tag_feed | |
73 | + # @tag = params[:id] | |
74 | + # tagged = profile.articles.paginate(:per_page => 20, :page => 1, :order => 'published_at DESC', :include => :tags, :conditions => ['tags.name LIKE ?', @tag]) | |
75 | + # feed_writer = FeedWriter.new | |
76 | + # data = feed_writer.write( | |
77 | + # tagged, | |
78 | + # :title => _("%s's contents tagged with \"%s\"") % [profile.name, @tag], | |
79 | + # :description => _("%s's contents tagged with \"%s\"") % [profile.name, @tag], | |
80 | + # :link => url_for(profile.url) | |
81 | + # ) | |
82 | + # render :text => data, :content_type => "text/xml" | |
83 | + # end | |
84 | + # | |
85 | + # def communities | |
86 | + # if is_cache_expired?(profile.communities_cache_key(params)) | |
87 | + # @communities = profile.communities.includes(relations_to_include).paginate(:per_page => per_page, :page => params[:npage], :total_entries => profile.communities.count) | |
88 | + # end | |
89 | + # end | |
90 | + # | |
91 | + # def enterprises | |
92 | + # @enterprises = profile.enterprises.includes(relations_to_include) | |
93 | + # end | |
94 | + # | |
95 | + # def friends | |
96 | + # if is_cache_expired?(profile.friends_cache_key(params)) | |
97 | + # @friends = profile.friends.includes(relations_to_include).paginate(:per_page => per_page, :page => params[:npage], :total_entries => profile.friends.count) | |
98 | + # end | |
99 | + # end | |
100 | + # | |
101 | + # def members | |
102 | + # if is_cache_expired?(profile.members_cache_key(params)) | |
103 | + # @members = profile.members_by_name.includes(relations_to_include).paginate(:per_page => members_per_page, :page => params[:npage], :total_entries => profile.members.count) | |
104 | + # end | |
105 | + # end | |
106 | + # | |
107 | + # def fans | |
108 | + # @fans = profile.fans.includes(relations_to_include) | |
109 | + # end | |
110 | + # | |
111 | + # def favorite_enterprises | |
112 | + # @favorite_enterprises = profile.favorite_enterprises.includes(relations_to_include) | |
113 | + # end | |
114 | + # | |
115 | + # def sitemap | |
116 | + # @articles = profile.top_level_articles.includes([:profile, :parent]) | |
117 | + # end | |
118 | + # | |
119 | + # def join | |
120 | + # if !user.memberships.include?(profile) | |
121 | + # profile.add_member(user) | |
122 | + # if !profile.members.include?(user) | |
123 | + # render :text => {:message => _('%s administrator still needs to accept you as member.') % profile.name}.to_json | |
124 | + # else | |
125 | + # render :text => {:message => _('You just became a member of %s.') % profile.name}.to_json | |
126 | + # end | |
127 | + # else | |
128 | + # render :text => {:message => _('You are already a member of %s.') % profile.name}.to_json | |
129 | + # end | |
130 | + # end | |
131 | + # | |
132 | + # def join_not_logged | |
133 | + # session[:join] = profile.identifier | |
134 | + # | |
135 | + # if user | |
136 | + # redirect_to :controller => 'profile', :action => 'join' | |
137 | + # else | |
138 | + # redirect_to :controller => '/account', :action => 'login' | |
139 | + # end | |
140 | + # end | |
141 | + # | |
142 | + # def leave | |
143 | + # if current_person.memberships.include?(profile) | |
144 | + # if current_person.is_last_admin?(profile) | |
145 | + # render :text => {:redirect_to => url_for({:controller => 'profile_members', :action => 'last_admin', :person => current_person.id})}.to_json | |
146 | + # else | |
147 | + # render :text => current_person.leave(profile, params[:reload]) | |
148 | + # end | |
149 | + # else | |
150 | + # render :text => {:message => _('You are not a member of %s.') % profile.name}.to_json | |
151 | + # end | |
152 | + # end | |
153 | + # | |
154 | + # def check_membership | |
155 | + # unless logged_in? | |
156 | + # render :text => '' | |
157 | + # return | |
158 | + # end | |
159 | + # if user.memberships.include?(profile) | |
160 | + # render :text => 'true' | |
161 | + # else | |
162 | + # render :text => 'false' | |
163 | + # end | |
164 | + # end | |
165 | + # | |
166 | + # def add | |
167 | + # # FIXME this shouldn't be in Person model? | |
168 | + # if !user.memberships.include?(profile) | |
169 | + # AddFriend.create!(:person => user, :friend => profile) | |
170 | + # render :text => _('%s still needs to accept being your friend.') % profile.name | |
171 | + # else | |
172 | + # render :text => _('You are already a friend of %s.') % profile.name | |
173 | + # end | |
174 | + # end | |
175 | + # | |
176 | + # def check_friendship | |
177 | + # unless logged_in? | |
178 | + # render :text => '' | |
179 | + # return | |
180 | + # end | |
181 | + # if user == profile || user.already_request_friendship?(profile) || user.is_a_friend?(profile) | |
182 | + # render :text => 'true' | |
183 | + # else | |
184 | + # render :text => 'false' | |
185 | + # end | |
186 | + # end | |
187 | + # | |
188 | + # def unblock | |
189 | + # if current_user.person.is_admin?(profile.environment) | |
190 | + # profile.unblock | |
191 | + # session[:notice] = _("You have unblocked %s successfully. ") % profile.name | |
192 | + # redirect_to :controller => 'profile', :action => 'index' | |
193 | + # else | |
194 | + # message = _('You are not allowed to unblock enterprises in this environment.') | |
195 | + # render_access_denied(message) | |
196 | + # end | |
197 | + # end | |
198 | + # | |
199 | + # def leave_scrap | |
200 | + # sender = params[:sender_id].nil? ? current_user.person : Person.find(params[:sender_id]) | |
201 | + # receiver = params[:receiver_id].nil? ? @profile : Person.find(params[:receiver_id]) | |
202 | + # @scrap = Scrap.new(params[:scrap]) | |
203 | + # @scrap.sender= sender | |
204 | + # @scrap.receiver= receiver | |
205 | + # @tab_action = params[:tab_action] | |
206 | + # @message = @scrap.save ? _("Message successfully sent.") : _("You can't leave an empty message.") | |
207 | + # activities = @profile.activities.paginate(:per_page => 15, :page => params[:page]) if params[:not_load_scraps].nil? | |
208 | + # render :partial => 'profile_activities_list', :locals => {:activities => activities} | |
209 | + # end | |
210 | + # | |
211 | + # def leave_comment_on_activity | |
212 | + # @comment = Comment.new(params[:comment]) | |
213 | + # @comment.author = user | |
214 | + # @activity = ActionTracker::Record.find(params[:source_id]) | |
215 | + # @comment.source_type, @comment.source_id = (@activity.target_type == 'Article' ? ['Article', @activity.target_id] : [@activity.class.to_s, @activity.id]) | |
216 | + # @tab_action = params[:tab_action] | |
217 | + # @message = @comment.save ? _("Comment successfully added.") : _("You can't leave an empty comment.") | |
218 | + # if @tab_action == 'wall' | |
219 | + # activities = @profile.activities.paginate(:per_page => 15, :page => params[:page]) if params[:not_load_scraps].nil? | |
220 | + # render :partial => 'profile_activities_list', :locals => {:activities => activities} | |
221 | + # else | |
222 | + # network_activities = @profile.tracked_notifications.visible.paginate(:per_page => 15, :page => params[:page]) | |
223 | + # render :partial => 'profile_network_activities', :locals => {:network_activities => network_activities} | |
224 | + # end | |
225 | + # end | |
226 | + # | |
227 | + # def view_more_activities | |
228 | + # @activities = @profile.activities.paginate(:per_page => 10, :page => params[:page]) | |
229 | + # render :partial => 'profile_activities_list', :locals => {:activities => @activities} | |
230 | + # end | |
231 | + # | |
232 | + # def view_more_network_activities | |
233 | + # @activities = @profile.tracked_notifications.paginate(:per_page => 10, :page => params[:page]) | |
234 | + # render :partial => 'profile_network_activities', :locals => {:network_activities => @activities} | |
235 | + # end | |
236 | + # | |
237 | + # def more_comments | |
238 | + # profile_filter = @profile.person? ? {:user_id => @profile} : {:target_id => @profile} | |
239 | + # activity = ActionTracker::Record.find(:first, :conditions => {:id => params[:activity]}.merge(profile_filter)) | |
240 | + # comments_count = activity.comments.count | |
241 | + # comment_page = (params[:comment_page] || 1).to_i | |
242 | + # comments_per_page = 5 | |
243 | + # no_more_pages = comments_count <= comment_page * comments_per_page | |
244 | + # | |
245 | + # render :update do |page| | |
246 | + # page.insert_html :bottom, 'profile-wall-activities-comments-'+params[:activity], | |
247 | + # :partial => 'comment', :collection => activity.comments.flatten.paginate(:per_page => comments_per_page, :page => comment_page) | |
248 | + # | |
249 | + # if no_more_pages | |
250 | + # page.remove 'profile-wall-activities-comments-more-'+params[:activity] | |
251 | + # else | |
252 | + # page.replace_html 'profile-wall-activities-comments-more-'+params[:activity], | |
253 | + # :partial => 'more_comments', :locals => {:activity => activity, :comment_page => comment_page} | |
254 | + # end | |
255 | + # end | |
256 | + # end | |
257 | + # | |
258 | + # def more_replies | |
259 | + # activity = Scrap.find(:first, :conditions => {:id => params[:activity], :receiver_id => @profile, :scrap_id => nil}) | |
260 | + # comments_count = activity.replies.count | |
261 | + # comment_page = (params[:comment_page] || 1).to_i | |
262 | + # comments_per_page = 5 | |
263 | + # no_more_pages = comments_count <= comment_page * comments_per_page | |
264 | + # | |
265 | + # render :update do |page| | |
266 | + # page.insert_html :bottom, 'profile-wall-activities-comments-'+params[:activity], | |
267 | + # :partial => 'profile_scrap', :collection => activity.replies.paginate(:per_page => comments_per_page, :page => comment_page), :as => :scrap | |
268 | + # | |
269 | + # if no_more_pages | |
270 | + # page.remove 'profile-wall-activities-comments-more-'+params[:activity] | |
271 | + # else | |
272 | + # page.replace_html 'profile-wall-activities-comments-more-'+params[:activity], | |
273 | + # :partial => 'more_replies', :locals => {:activity => activity, :comment_page => comment_page} | |
274 | + # end | |
275 | + # end | |
276 | + # end | |
277 | + # | |
278 | + # def remove_scrap | |
279 | + # begin | |
280 | + # scrap = current_user.person.scraps(params[:scrap_id]) | |
281 | + # scrap.destroy | |
282 | + # finish_successful_removal 'Scrap successfully removed.' | |
283 | + # rescue | |
284 | + # finish_unsuccessful_removal 'You could not remove this scrap.' | |
285 | + # end | |
286 | + # end | |
287 | + # | |
288 | + # def remove_activity | |
289 | + # begin | |
290 | + # raise if !can_edit_profile | |
291 | + # activity = ActionTracker::Record.find(params[:activity_id]) | |
292 | + # if params[:only_hide] | |
293 | + # activity.update_attribute(:visible, false) | |
294 | + # else | |
295 | + # activity.destroy | |
296 | + # end | |
297 | + # finish_successful_removal 'Activity successfully removed.' | |
298 | + # rescue | |
299 | + # finish_unsuccessful_removal 'You could not remove this activity.' | |
300 | + # end | |
301 | + # end | |
302 | + # | |
303 | + # def remove_notification | |
304 | + # begin | |
305 | + # raise if !can_edit_profile | |
306 | + # notification = ActionTrackerNotification.find(:first, :conditions => {:profile_id => profile.id, :action_tracker_id => params[:activity_id]}) | |
307 | + # notification.destroy | |
308 | + # render :text => _('Notification successfully removed.') | |
309 | + # rescue | |
310 | + # render :text => _('You could not remove this notification.') | |
311 | + # end | |
312 | + # end | |
313 | + # | |
314 | + # def finish_successful_removal(msg) | |
315 | + # if request.xhr? | |
316 | + # render :text => {'ok' => true}.to_json, :content_type => 'application/json' | |
317 | + # else | |
318 | + # session[:notice] = _(msg) | |
319 | + # redirect_to :action => :index | |
320 | + # end | |
321 | + # end | |
322 | + # | |
323 | + # def finish_unsuccessful_removal(msg) | |
324 | + # session[:notice] = _(msg) | |
325 | + # if request.xhr? | |
326 | + # render :text => {'redirect' => url_for(:action => :index)}.to_json, :content_type => 'application/json' | |
327 | + # else | |
328 | + # redirect_to :action => :index | |
329 | + # end | |
330 | + # end | |
331 | + # | |
332 | + # def report_abuse | |
333 | + # @abuse_report = AbuseReport.new | |
334 | + # render :layout => false | |
335 | + # end | |
336 | + # | |
337 | + # def register_report | |
338 | + # unless user.is_admin? || verify_recaptcha | |
339 | + # render :text => { | |
340 | + # :ok => false, | |
341 | + # :error => { | |
342 | + # :code => 1, | |
343 | + # :message => _('You could not answer the captcha.') | |
344 | + # } | |
345 | + # }.to_json | |
346 | + # else | |
347 | + # begin | |
348 | + # abuse_report = AbuseReport.new(params[:abuse_report]) | |
349 | + # if !params[:content_type].blank? | |
350 | + # article = params[:content_type].constantize.find(params[:content_id]) | |
351 | + # abuse_report.content = article_reported_version(article) | |
352 | + # end | |
353 | + # | |
354 | + # user.register_report(abuse_report, profile) | |
355 | + # | |
356 | + # if !params[:content_type].blank? | |
357 | + # abuse_report = AbuseReport.find_by_reporter_id_and_abuse_complaint_id(user.id, profile.opened_abuse_complaint.id) | |
358 | + # Delayed::Job.enqueue DownloadReportedImagesJob.new(abuse_report, article) | |
359 | + # end | |
360 | + # | |
361 | + # render :text => { | |
362 | + # :ok => true, | |
363 | + # :message => _('Your abuse report was registered. The administrators are reviewing your report.'), | |
364 | + # }.to_json | |
365 | + # rescue Exception => exception | |
366 | + # logger.error(exception.to_s) | |
367 | + # render :text => { | |
368 | + # :ok => false, | |
369 | + # :error => { | |
370 | + # :code => 2, | |
371 | + # :message => _('Your report couldn\'t be saved due to some problem. Please contact the administrator.') | |
372 | + # } | |
373 | + # }.to_json | |
374 | + # end | |
375 | + # end | |
376 | + # end | |
377 | + # | |
378 | + # def remove_comment | |
379 | + # #FIXME Check whether these permissions are enough | |
380 | + # @comment = Comment.find(params[:comment_id]) | |
381 | + # if (user == @comment.author || user == profile || user.has_permission?(:moderate_comments, profile)) | |
382 | + # @comment.destroy | |
383 | + # finish_successful_removal 'Comment successfully removed.' | |
384 | + # else | |
385 | + # finish_unsuccessful_removal 'You could not remove this comment.' | |
386 | + # end | |
387 | + # end | |
50 | 388 | |
51 | -# link.attributes["href"].value = "http://myproxy.com/?url=#{CGI.escape link.attributes["href"].value}" | |
52 | -# http://#{environment.default_hostname}/#{relative_path[1]} | |
53 | - | |
54 | - | |
55 | - body = doc.to_s | |
56 | - sender = Person.find(article.author_id) | |
57 | - members = article.profile.members | |
58 | - emails = [] | |
59 | - members.each{ |m| | |
60 | - emails.push(m.user.email) | |
61 | - } | |
62 | - mail( | |
63 | - content_type: 'text/html', | |
64 | - to: emails, | |
65 | - from: "#{article.author_name} <#{sender.contact_email}>", | |
66 | - reply_to: article.author.user.email, | |
67 | - subject: "[Artigo] " + article.title, | |
68 | - body: body | |
69 | -# body: set_absolute_path(article.body, article.environment) | |
70 | - ) | |
71 | - end | |
72 | 389 | |
73 | - def set_absolute_path(body, environment) | |
74 | - parsed = Hpricot(body.to_s) | |
75 | - parsed.search('img[@src]').map { |i| change_element_path(i, 'src', environment) } | |
76 | - parsed.search('a[@href]').map { |i| change_element_path(i, 'href', environment) } | |
77 | - parsed.to_s | |
78 | - end | |
79 | 390 | |
80 | - def change_element_path(el, attribute, environment) | |
81 | - fullpath = /^http/.match(el[attribute]) | |
82 | - if not fullpath | |
83 | - relative_path = /\/?(.*)/.match(el[attribute]) | |
84 | - el[attribute] = "http://#{environment.default_hostname}/#{relative_path[1]}" | |
85 | - end | |
86 | - end | |
87 | - end | |
391 | + | |
392 | + # protected | |
393 | + # | |
394 | + # def check_access_to_profile | |
395 | + # unless profile.display_info_to?(user) | |
396 | + # redirect_to :action => 'index' | |
397 | + # end | |
398 | + # end | |
399 | + # | |
400 | + # def store_location | |
401 | + # if session[:previous_location].nil? | |
402 | + # session[:previous_location] = request.referer | |
403 | + # end | |
404 | + # end | |
405 | + # | |
406 | + # def redirect_to_previous_location | |
407 | + # back = session[:previous_location] | |
408 | + # if back | |
409 | + # session[:previous_location] = nil | |
410 | + # redirect_to back | |
411 | + # else | |
412 | + # redirect_to profile.url | |
413 | + # end | |
414 | + # end | |
415 | + # | |
416 | + # def per_page | |
417 | + # Noosfero::Constants::PROFILE_PER_PAGE | |
418 | + # end | |
419 | + # | |
420 | + # def members_per_page | |
421 | + # 20 | |
422 | + # end | |
423 | + # | |
424 | + # def can_edit_profile | |
425 | + # @can_edit_profile ||= user && user.has_permission?('edit_profile', profile) | |
426 | + # end | |
427 | + # helper_method :can_edit_profile | |
428 | + # | |
429 | + # def relations_to_include | |
430 | + # [:image, :domains, :preferred_domain, :environment] | |
431 | + # end | |
432 | + | |
88 | 433 | end | ... | ... |
... | ... | @@ -0,0 +1,21 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class EmailArticlePluginTest < ActiveSupport::TestCase | |
4 | + | |
5 | + def setup | |
6 | + @plugin = EmailArticlePlugin.new | |
7 | + end | |
8 | + | |
9 | + should 'be a noosfero plugin' do | |
10 | + assert_kind_of Noosfero::Plugin, @plugin | |
11 | + end | |
12 | + | |
13 | + should 'have name' do | |
14 | + assert_equal 'Email Article to Community Members Plugin', EmailArticlePlugin.plugin_name | |
15 | + end | |
16 | + | |
17 | + should 'have description' do | |
18 | + assert_equal _("A plugin that emails an article to the members of the community"), EmailArticlePlugin.plugin_description | |
19 | + end | |
20 | + | |
21 | +end | ... | ... |
... | ... | @@ -0,0 +1,133 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class EmailArticlePluginArticleMailerTest < ActiveSupport::TestCase | |
4 | + | |
5 | + def setup | |
6 | + @plugin = EmailArticlePlugin.new | |
7 | + ActionMailer::Base.delivery_method = :test | |
8 | + ActionMailer::Base.perform_deliveries = true | |
9 | + ActionMailer::Base.deliveries = [] | |
10 | + @community = fast_create(Community) | |
11 | + @person = create_user('john').person | |
12 | + member_1 = create_user('user_one').person | |
13 | + member_2 = create_user('user_two').person | |
14 | + @community.add_member(member_1) | |
15 | + @community.add_member(member_2) | |
16 | + end | |
17 | + | |
18 | + attr_reader :community, :person | |
19 | + | |
20 | + should 'deliver mailing to each member after create' do | |
21 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
22 | + community.reload | |
23 | + process_delayed_job_queue | |
24 | + assert_equal 2, ActionMailer::Base.deliveries.count | |
25 | + end | |
26 | + | |
27 | + should 'require source_id' do | |
28 | + mailing = OrganizationMailing.new | |
29 | + mailing.valid? | |
30 | + assert mailing.errors[:source_id.to_s].present? | |
31 | + | |
32 | + mailing.source_id = community.id | |
33 | + mailing.valid? | |
34 | + assert !mailing.errors[:source_id.to_s].present? | |
35 | + end | |
36 | + | |
37 | + should 'return community name' do | |
38 | + mailing = build(OrganizationMailing, :source => community) | |
39 | + assert_equal community.name, mailing.source.name | |
40 | + end | |
41 | + | |
42 | + should 'return community with source_id' do | |
43 | + mailing = build(OrganizationMailing, :source => community) | |
44 | + assert_equal community, mailing.source | |
45 | + end | |
46 | + | |
47 | + should 'return person with person_id' do | |
48 | + mailing = build(OrganizationMailing, :source => community, :person => person) | |
49 | + assert_equal person, mailing.person | |
50 | + end | |
51 | + | |
52 | + should 'display name and email on generate_from' do | |
53 | + mailing = build(OrganizationMailing, :source => community, :person => person) | |
54 | + assert_equal "#{person.name} <#{community.environment.noreply_email}>", mailing.generate_from | |
55 | + end | |
56 | + | |
57 | + should 'generate subject' do | |
58 | + mailing = build(OrganizationMailing, :source => community, :subject => 'Hello :)') | |
59 | + assert_equal "[#{community.name}] #{mailing.subject}", mailing.generate_subject | |
60 | + end | |
61 | + | |
62 | + should 'return signature message' do | |
63 | + mailing = build(OrganizationMailing, :source => community) | |
64 | + assert_equal "Sent by community #{community.name}.", mailing.signature_message | |
65 | + end | |
66 | + | |
67 | + should 'return url for organization on url' do | |
68 | + mailing = build(OrganizationMailing, :source => community) | |
69 | + assert_equal "#{community.environment.top_url}/#{community.name.to_slug}/", mailing.url | |
70 | + end | |
71 | + | |
72 | + should 'deliver mailing to each member after create' do | |
73 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
74 | + community.reload | |
75 | + process_delayed_job_queue | |
76 | + assert_equal 2, ActionMailer::Base.deliveries.count | |
77 | + end | |
78 | + | |
79 | + should 'deliver mailing when there are many mailings created' do | |
80 | + 50.times { create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) } | |
81 | + community.reload | |
82 | + process_delayed_job_queue | |
83 | + assert_equal 50*community.members_count, ActionMailer::Base.deliveries.count | |
84 | + end | |
85 | + | |
86 | + should 'create mailing sent to each recipient after delivering mailing' do | |
87 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
88 | + community.reload | |
89 | + assert_difference 'MailingSent.count', 2 do | |
90 | + process_delayed_job_queue | |
91 | + end | |
92 | + end | |
93 | + | |
94 | + should 'change locale according to the mailing locale' do | |
95 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :locale => 'pt', :person => person) | |
96 | + Noosfero.expects(:with_locale).with('pt') | |
97 | + process_delayed_job_queue | |
98 | + end | |
99 | + | |
100 | + should 'have community by source_id' do | |
101 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
102 | + assert_equal community, Mailing.find(mailing.id).source | |
103 | + end | |
104 | + | |
105 | + should 'return recipient' do | |
106 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
107 | + assert_equal [Person['user_one'], Person['user_two']], mailing.recipients | |
108 | + end | |
109 | + | |
110 | + should 'return recipients according to limit' do | |
111 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
112 | + assert_equal [Person['user_one']], mailing.recipients(0, 1) | |
113 | + end | |
114 | + | |
115 | + should 'return true if already sent mailing to a recipient' do | |
116 | + member = Person['user_one'] | |
117 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
118 | + process_delayed_job_queue | |
119 | + assert mailing.mailing_sents.find_by_person_id(member.id) | |
120 | + end | |
121 | + | |
122 | + should 'return false if did not sent mailing to a recipient' do | |
123 | + recipient = fast_create(Person) | |
124 | + mailing = create(OrganizationMailing, :source => community, :subject => 'Hello', :body => 'We have some news', :person => person) | |
125 | + process_delayed_job_queue | |
126 | + | |
127 | + assert !mailing.mailing_sents.find_by_person_id(recipient.id) | |
128 | + end | |
129 | + | |
130 | + protected | |
131 | + include NoosferoTestHelper | |
132 | + | |
133 | +end | ... | ... |