Commit 55f83385023adf0fc70a54922209388a223dd05f

Authored by randx
2 parents 1e1c5b7a 65989141

Merge branch 'separate_user_and_issue_observer_from_mail_observer' of https://gi…

…thub.com/robbkidd/gitlabhq into robbkidd-separate_user_and_issue_observer_from_mail_observer
Gemfile
... ... @@ -77,4 +77,5 @@ group :test do
77 77 gem "simplecov", :require => false
78 78 gem "shoulda-matchers"
79 79 gem 'email_spec'
  80 + gem 'resque_spec'
80 81 end
... ...
Gemfile.lock
... ... @@ -257,6 +257,9 @@ GEM
257 257 resque_mailer (2.0.3)
258 258 actionmailer (>= 3.0.0)
259 259 resque (>= 1.2.3)
  260 + resque_spec (0.11.0)
  261 + resque (>= 1.19.0)
  262 + rspec (>= 2.5.0)
260 263 rspec (2.10.0)
261 264 rspec-core (~> 2.10.0)
262 265 rspec-expectations (~> 2.10.0)
... ... @@ -385,6 +388,7 @@ DEPENDENCIES
385 388 redcarpet (~> 2.1.1)
386 389 resque (~> 1.20.0)
387 390 resque_mailer
  391 + resque_spec
388 392 rspec-rails
389 393 sass-rails (= 3.2.5)
390 394 seed-fu
... ...
app/models/issue.rb
... ... @@ -27,7 +27,7 @@ class Issue < ActiveRecord::Base
27 27 validates :title,
28 28 :presence => true,
29 29 :length => { :within => 0..255 }
30   -
  30 +
31 31 validates :description,
32 32 :length => { :within => 0..2000 }
33 33  
... ... @@ -55,6 +55,18 @@ class Issue < ActiveRecord::Base
55 55 def new?
56 56 today? && created_at == updated_at
57 57 end
  58 +
  59 + def is_being_reassigned?
  60 + assignee_id_changed?
  61 + end
  62 +
  63 + def is_being_closed?
  64 + closed_changed? && closed
  65 + end
  66 +
  67 + def is_being_reopened?
  68 + closed_changed? && !closed
  69 + end
58 70 end
59 71 # == Schema Information
60 72 #
... ...
app/models/issue_observer.rb 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +class IssueObserver < ActiveRecord::Observer
  2 + cattr_accessor :current_user
  3 +
  4 + def after_create(issue)
  5 + Notify.new_issue_email(issue.id).deliver if issue.assignee != current_user
  6 + end
  7 +
  8 + def after_update(issue)
  9 + send_reassigned_email(issue) if issue.is_being_reassigned?
  10 + Note.create_status_change_note(issue, current_user, 'closed') if issue.is_being_closed?
  11 + Note.create_status_change_note(issue, current_user, 'reopened') if issue.is_being_reopened?
  12 + end
  13 +
  14 + protected
  15 +
  16 + def send_reassigned_email(issue)
  17 + recipient_ids = [issue.assignee_id, issue.assignee_id_was].keep_if {|id| id != current_user.id }
  18 +
  19 + recipient_ids.each do |recipient_id|
  20 + Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver
  21 + end
  22 + end
  23 +end
... ...
app/models/mailer_observer.rb
1 1 class MailerObserver < ActiveRecord::Observer
2   - observe :issue, :user, :note, :merge_request
  2 + observe :note, :merge_request
3 3 cattr_accessor :current_user
4 4  
5 5 def after_create(model)
6   - new_issue(model) if model.kind_of?(Issue)
7   - new_user(model) if model.kind_of?(User)
8 6 new_note(model) if model.kind_of?(Note)
9 7 new_merge_request(model) if model.kind_of?(MergeRequest)
10 8 end
11 9  
12 10 def after_update(model)
13 11 changed_merge_request(model) if model.kind_of?(MergeRequest)
14   - changed_issue(model) if model.kind_of?(Issue)
15 12 end
16 13  
17 14 protected
18 15  
19   - def new_issue(issue)
20   - if issue.assignee != current_user
21   - Notify.new_issue_email(issue.id).deliver
22   - end
23   - end
24   -
25   - def new_user(user)
26   - Notify.new_user_email(user.id, user.password).deliver
27   - end
28   -
29 16 def new_note(note)
30 17 if note.notify
31 18 # Notify whole team except author of note
... ... @@ -65,12 +52,8 @@ class MailerObserver &lt; ActiveRecord::Observer
65 52 status_notify_and_comment merge_request, :reassigned_merge_request_email
66 53 end
67 54  
68   - def changed_issue(issue)
69   - status_notify_and_comment issue, :reassigned_issue_email
70   - end
71   -
72 55 # This method used for Issues & Merge Requests
73   - #
  56 + #
74 57 # It create a comment for Issue or MR if someone close/reopen.
75 58 # It also notify via email if assignee was changed
76 59 #
... ...
app/models/note.rb
... ... @@ -42,6 +42,14 @@ class Note &lt; ActiveRecord::Base
42 42  
43 43 mount_uploader :attachment, AttachmentUploader
44 44  
  45 + def self.create_status_change_note(noteable, author, status)
  46 + create({ :noteable => noteable,
  47 + :project => noteable.project,
  48 + :author => author,
  49 + :note => "_Status changed to #{status}_" },
  50 + :without_protection => true)
  51 + end
  52 +
45 53 def notify
46 54 @notify ||= false
47 55 end
... ...
app/models/user_observer.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class UserObserver < ActiveRecord::Observer
  2 + def after_create(user)
  3 + Notify.new_user_email(user.id, user.password).deliver
  4 + end
  5 +end
... ...
config/application.rb
... ... @@ -23,7 +23,7 @@ module Gitlab
23 23 # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24 24  
25 25 # Activate observers that should always be running.
26   - config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer
  26 + config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer, :user_observer
27 27  
28 28 # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29 29 # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
... ...
config/initializers/resque_mailer.rb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +Resque::Mailer.excluded_environments = []
... ...
spec/models/activity_observer_spec.rb
... ... @@ -9,9 +9,11 @@ describe ActivityObserver do
9 9 end
10 10  
11 11 describe "Merge Request created" do
12   - before do
13   - @merge_request = Factory :merge_request, :project => project
14   - @event = Event.last
  12 + before do
  13 + MergeRequest.observers.enable :activity_observer do
  14 + @merge_request = Factory :merge_request, :project => project
  15 + @event = Event.last
  16 + end
15 17 end
16 18  
17 19 it_should_be_valid_event
... ... @@ -20,9 +22,11 @@ describe ActivityObserver do
20 22 end
21 23  
22 24 describe "Issue created" do
23   - before do
24   - @issue = Factory :issue, :project => project
25   - @event = Event.last
  25 + before do
  26 + Issue.observers.enable :activity_observer do
  27 + @issue = Factory :issue, :project => project
  28 + @event = Event.last
  29 + end
26 30 end
27 31  
28 32 it_should_be_valid_event
... ...
spec/models/issue_observer_spec.rb 0 → 100644
... ... @@ -0,0 +1,144 @@
  1 +require 'spec_helper'
  2 +
  3 +describe IssueObserver do
  4 + let(:some_user) { double(:user, :id => 1) }
  5 + let(:assignee) { double(:user, :id => 2) }
  6 + let(:issue) { double(:issue, :id => 42, :assignee => assignee) }
  7 +
  8 + before(:each) { subject.stub(:current_user).and_return(some_user) }
  9 +
  10 + subject { IssueObserver.instance }
  11 +
  12 + describe '#after_create' do
  13 +
  14 + it 'is called when an issue is created' do
  15 + subject.should_receive(:after_create)
  16 +
  17 + Issue.observers.enable :issue_observer do
  18 + Factory.create(:issue, :project => Factory.create(:project))
  19 + end
  20 + end
  21 +
  22 + it 'sends an email to the assignee' do
  23 + Notify.should_receive(:new_issue_email).with(issue.id).
  24 + and_return(double(:deliver => true))
  25 +
  26 + subject.after_create(issue)
  27 + end
  28 +
  29 + it 'does not send an email to the assignee if assignee created the issue' do
  30 + subject.stub(:current_user).and_return(assignee)
  31 + Notify.should_not_receive(:new_issue_email)
  32 +
  33 + subject.after_create(issue)
  34 + end
  35 + end
  36 +
  37 + context '#after_update' do
  38 + before(:each) do
  39 + issue.stub(:is_being_reassigned?).and_return(false)
  40 + issue.stub(:is_being_closed?).and_return(false)
  41 + issue.stub(:is_being_reopened?).and_return(false)
  42 + end
  43 +
  44 + it 'is called when an issue is changed' do
  45 + changed = Factory.create(:issue, :project => Factory.create(:project))
  46 + subject.should_receive(:after_update)
  47 +
  48 + Issue.observers.enable :issue_observer do
  49 + changed.description = 'I changed'
  50 + changed.save
  51 + end
  52 + end
  53 +
  54 + context 'a reassigned email' do
  55 + it 'is sent if the issue is being reassigned' do
  56 + issue.should_receive(:is_being_reassigned?).and_return(true)
  57 + subject.should_receive(:send_reassigned_email).with(issue)
  58 +
  59 + subject.after_update(issue)
  60 + end
  61 +
  62 + it 'is not sent if the issue is not being reassigned' do
  63 + issue.should_receive(:is_being_reassigned?).and_return(false)
  64 + subject.should_not_receive(:send_reassigned_email)
  65 +
  66 + subject.after_update(issue)
  67 + end
  68 + end
  69 +
  70 + context 'a status "closed" note' do
  71 + it 'is created if the issue is being closed' do
  72 + issue.should_receive(:is_being_closed?).and_return(true)
  73 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'closed')
  74 +
  75 + subject.after_update(issue)
  76 + end
  77 +
  78 + it 'is not created if the issue is not being closed' do
  79 + issue.should_receive(:is_being_closed?).and_return(false)
  80 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'closed')
  81 +
  82 + subject.after_update(issue)
  83 + end
  84 + end
  85 +
  86 + context 'a status "reopened" note' do
  87 + it 'is created if the issue is being reopened' do
  88 + issue.should_receive(:is_being_reopened?).and_return(true)
  89 + Note.should_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  90 +
  91 + subject.after_update(issue)
  92 + end
  93 +
  94 + it 'is not created if the issue is not being reopened' do
  95 + issue.should_receive(:is_being_reopened?).and_return(false)
  96 + Note.should_not_receive(:create_status_change_note).with(issue, some_user, 'reopened')
  97 +
  98 + subject.after_update(issue)
  99 + end
  100 + end
  101 + end
  102 +
  103 + describe '#send_reassigned_email' do
  104 + let(:previous_assignee) { double(:user, :id => 3) }
  105 +
  106 + before(:each) do
  107 + issue.stub(:assignee_id).and_return(assignee.id)
  108 + issue.stub(:assignee_id_was).and_return(previous_assignee.id)
  109 + end
  110 +
  111 + def it_sends_a_reassigned_email_to(recipient)
  112 + Notify.should_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id).
  113 + and_return(double(:deliver => true))
  114 + end
  115 +
  116 + def it_does_not_send_a_reassigned_email_to(recipient)
  117 + Notify.should_not_receive(:reassigned_issue_email).with(recipient, issue.id, previous_assignee.id)
  118 + end
  119 +
  120 + it 'sends a reassigned email to the previous and current assignees' do
  121 + it_sends_a_reassigned_email_to assignee.id
  122 + it_sends_a_reassigned_email_to previous_assignee.id
  123 +
  124 + subject.send(:send_reassigned_email, issue)
  125 + end
  126 +
  127 + context 'does not send an email to the user who made the reassignment' do
  128 + it 'if the user is the assignee' do
  129 + subject.stub(:current_user).and_return(assignee)
  130 + it_sends_a_reassigned_email_to previous_assignee.id
  131 + it_does_not_send_a_reassigned_email_to assignee.id
  132 +
  133 + subject.send(:send_reassigned_email, issue)
  134 + end
  135 + it 'if the user is the previous assignee' do
  136 + subject.stub(:current_user).and_return(previous_assignee)
  137 + it_sends_a_reassigned_email_to assignee.id
  138 + it_does_not_send_a_reassigned_email_to previous_assignee.id
  139 +
  140 + subject.send(:send_reassigned_email, issue)
  141 + end
  142 + end
  143 + end
  144 +end
... ...
spec/models/issue_spec.rb
... ... @@ -20,10 +20,60 @@ describe Issue do
20 20 it { Issue.should respond_to :opened }
21 21 end
22 22  
23   - it { Factory.create(:issue,
24   - :author => Factory(:user),
25   - :assignee => Factory(:user),
26   - :project => Factory.create(:project)).should be_valid }
  23 + subject { Factory.create(:issue,
  24 + :author => Factory(:user),
  25 + :assignee => Factory(:user),
  26 + :project => Factory.create(:project)) }
  27 + it { should be_valid }
  28 +
  29 + describe '#is_being_reassigned?' do
  30 + it 'returns true if the issue assignee has changed' do
  31 + subject.assignee = Factory(:user)
  32 + subject.is_being_reassigned?.should be_true
  33 + end
  34 + it 'returns false if the issue assignee has not changed' do
  35 + subject.is_being_reassigned?.should be_false
  36 + end
  37 + end
  38 +
  39 + describe '#is_being_closed?' do
  40 + it 'returns true if the closed attribute has changed and is now true' do
  41 + subject.closed = true
  42 + subject.is_being_closed?.should be_true
  43 + end
  44 + it 'returns false if the closed attribute has changed and is now false' do
  45 + issue = Factory.create(:issue,
  46 + :closed => true,
  47 + :author => Factory(:user),
  48 + :assignee => Factory(:user),
  49 + :project => Factory.create(:project))
  50 + issue.closed = false
  51 + issue.is_being_closed?.should be_false
  52 + end
  53 + it 'returns false if the closed attribute has not changed' do
  54 + subject.is_being_closed?.should be_false
  55 + end
  56 + end
  57 +
  58 +
  59 + describe '#is_being_reopened?' do
  60 + it 'returns true if the closed attribute has changed and is now false' do
  61 + issue = Factory.create(:issue,
  62 + :closed => true,
  63 + :author => Factory(:user),
  64 + :assignee => Factory(:user),
  65 + :project => Factory.create(:project))
  66 + issue.closed = false
  67 + issue.is_being_reopened?.should be_true
  68 + end
  69 + it 'returns false if the closed attribute has changed and is now true' do
  70 + subject.closed = true
  71 + subject.is_being_reopened?.should be_false
  72 + end
  73 + it 'returns false if the closed attribute has not changed' do
  74 + subject.is_being_reopened?.should be_false
  75 + end
  76 + end
27 77  
28 78 describe "plus 1" do
29 79 let(:project) { Factory(:project) }
... ... @@ -56,6 +106,7 @@ describe Issue do
56 106 subject.upvotes.should == 2
57 107 end
58 108 end
  109 +
59 110 end
60 111 # == Schema Information
61 112 #
... ...
spec/models/note_spec.rb
... ... @@ -70,6 +70,25 @@ describe Note do
70 70 end
71 71 end
72 72  
  73 + describe '#create_status_change_note' do
  74 + let(:project) { Factory.create(:project) }
  75 + let(:thing) { Factory.create(:issue, :project => project) }
  76 + let(:author) { Factory(:user) }
  77 + let(:status) { 'new_status' }
  78 +
  79 + subject { Note.create_status_change_note(thing, author, status) }
  80 +
  81 + it 'creates and saves a Note' do
  82 + should be_a Note
  83 + subject.id.should_not be_nil
  84 + end
  85 +
  86 + its(:noteable) { should == thing }
  87 + its(:project) { should == thing.project }
  88 + its(:author) { should == author }
  89 + its(:note) { should =~ /Status changed to #{status}/ }
  90 + end
  91 +
73 92 describe :authorization do
74 93 before do
75 94 @p1 = project
... ...
spec/models/project_spec.rb
... ... @@ -72,16 +72,29 @@ describe Project do
72 72 end
73 73  
74 74 describe "last_activity" do
75   - let(:project) { Factory :project }
  75 + let(:project) { Factory :project }
  76 + let(:last_event) { double }
76 77  
77 78 before do
78   - @issue = Factory :issue, :project => project
  79 + project.stub(:events).and_return( [ double, double, last_event ] )
79 80 end
80 81  
81   - it { project.last_activity.should == Event.last }
82   - it { project.last_activity_date.to_s.should == Event.last.created_at.to_s }
  82 + it { project.last_activity.should == last_event }
83 83 end
84 84  
  85 + describe 'last_activity_date' do
  86 + let(:project) { Factory :project }
  87 +
  88 + it 'returns the creation date of the project\'s last event if present' do
  89 + last_event = double(:created_at => 'now')
  90 + project.stub(:events).and_return( [double, double, last_event] )
  91 + project.last_activity_date.should == last_event.created_at
  92 + end
  93 +
  94 + it 'returns the project\'s last update date if it has no events' do
  95 + project.last_activity_date.should == project.updated_at
  96 + end
  97 + end
85 98 describe "fresh commits" do
86 99 let(:project) { Factory :project }
87 100  
... ...
spec/models/user_observer_spec.rb 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +require 'spec_helper'
  2 +
  3 +describe UserObserver do
  4 + subject { UserObserver.instance }
  5 +
  6 + it 'calls #after_create when new users are created' do
  7 + new_user = Factory.new(:user)
  8 + subject.should_receive(:after_create).with(new_user)
  9 +
  10 + User.observers.enable :user_observer do
  11 + new_user.save
  12 + end
  13 + end
  14 +
  15 + context 'when a new user is created' do
  16 + let(:user) { double(:user, id: 42, password: 'P@ssword!') }
  17 + let(:notification) { double :notification }
  18 +
  19 + it 'sends an email' do
  20 + notification.should_receive(:deliver)
  21 + Notify.should_receive(:new_user_email).with(user.id, user.password).and_return(notification)
  22 +
  23 + subject.after_create(user)
  24 + end
  25 + end
  26 +end
... ...
spec/requests/admin/admin_projects_spec.rb
... ... @@ -88,6 +88,7 @@ describe &quot;Admin::Projects&quot; do
88 88 fill_in 'Name', :with => 'NewProject'
89 89 fill_in 'Code', :with => 'NPR'
90 90 fill_in 'Path', :with => 'gitlabhq_1'
  91 + fill_in 'Description', :with => 'New Project Description'
91 92 expect { click_button "Save" }.to change { Project.count }.by(1)
92 93 @project = Project.last
93 94 end
... ...
spec/requests/admin/admin_users_spec.rb
... ... @@ -41,16 +41,23 @@ describe &quot;Admin::Users&quot; do
41 41  
42 42 it "should call send mail" do
43 43 Notify.should_receive(:new_user_email).and_return(stub(:deliver => true))
44   - click_button "Save"
  44 +
  45 + User.observers.enable :user_observer do
  46 + click_button "Save"
  47 + end
45 48 end
46 49  
47 50 it "should send valid email to user with email & password" do
48   - click_button "Save"
49   - user = User.last
50   - email = ActionMailer::Base.deliveries.last
51   - email.subject.should have_content("Account was created")
52   - email.body.should have_content(user.email)
53   - email.body.should have_content(@password)
  51 + User.observers.enable :user_observer do
  52 + with_resque do
  53 + click_button "Save"
  54 + end
  55 + user = User.last
  56 + email = ActionMailer::Base.deliveries.last
  57 + email.subject.should have_content("Account was created")
  58 + email.body.should have_content(user.email)
  59 + email.body.should have_content(@password)
  60 + end
54 61 end
55 62 end
56 63  
... ...
spec/requests/issues_spec.rb
... ... @@ -128,16 +128,22 @@ describe &quot;Issues&quot; do
128 128 end
129 129  
130 130 it "should call send mail" do
131   - Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true))
132   - click_button "Submit new issue"
  131 + Issue.observers.enable :issue_observer do
  132 + Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true))
  133 + click_button "Submit new issue"
  134 + end
133 135 end
134 136  
135 137 it "should send valid email to user" do
136   - click_button "Submit new issue"
137   - issue = Issue.last
138   - email = ActionMailer::Base.deliveries.last
139   - email.subject.should have_content("New Issue was created")
140   - email.body.should have_content(issue.title)
  138 + Issue.observers.enable :issue_observer do
  139 + with_resque do
  140 + click_button "Submit new issue"
  141 + end
  142 + issue = Issue.last
  143 + email = ActionMailer::Base.deliveries.last
  144 + email.subject.should have_content("New Issue was created")
  145 + email.body.should have_content(issue.title)
  146 + end
141 147 end
142 148  
143 149 end
... ...
spec/spec_helper.rb
... ... @@ -52,6 +52,7 @@ RSpec.configure do |config|
52 52 DatabaseCleaner.start
53 53  
54 54 WebMock.disable_net_connect!(allow_localhost: true)
  55 + ActiveRecord::Base.observers.disable :all
55 56 end
56 57  
57 58 config.after do
... ...