Commit cdefad8887ff39db9e79a264dcc624f6c4d53968

Authored by Dmitriy Zaporozhets
2 parents 2c32574a 3c96d1f0

Merge pull request #1305 from AlexDenisov/project_access_notifications

Project access notifications
app/mailers/notify.rb
... ... @@ -76,6 +76,13 @@ class Notify < ActionMailer::Base
76 76 mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title))
77 77 end
78 78  
  79 + def project_access_granted_email(user_project_id)
  80 + @users_project = UsersProject.find user_project_id
  81 + @project = @users_project.project
  82 + mail(to: @users_project.user.email,
  83 + subject: subject("access to project was granted"))
  84 + end
  85 +
79 86 private
80 87  
81 88 # Look up a User by their ID and return their email address
... ...
app/observers/users_project_observer.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class UsersProjectObserver < ActiveRecord::Observer
  2 + def after_create(users_project)
  3 + Notify.project_access_granted_email(users_project.id).deliver
  4 + end
  5 +
  6 + def after_update(users_project)
  7 + Notify.project_access_granted_email(users_project.id).deliver
  8 + end
  9 +end
... ...
app/views/notify/project_access_granted_email.html.haml 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +%td.content{align: "left", style: "font-family: Helvetica, Arial, sans-serif; padding: 20px 0 0;", valign: "top", width: "600"}
  2 + %table{border: "0", cellpadding: "0", cellspacing: "0", style: "color: #717171; font: normal 11px Helvetica, Arial, sans-serif; margin: 0; padding: 0;", width: "600"}
  3 + %tr
  4 + %td{style: "font-size: 1px; line-height: 1px;", width: "21"}
  5 + %td{align: "left", style: "padding: 20px 0 0;"}
  6 + %h2{style: "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
  7 + = "You got granted #{@users_project.project_access_human} access to project"
  8 + %td{style: "font-size: 1px; line-height: 1px;", width: "21"}
  9 + %tr
  10 + %td{style: "font-size: 1px; line-height: 1px;", width: "21"}
  11 + %td{align: "left", style: "padding: 20px 0 0;"}
  12 + %h2{style: "color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
  13 + = link_to_gfm truncate(@project.name, length: 45), project_url(@project), title: @project.name
  14 + %br
... ...
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, :issue_observer, :user_observer, :system_hook_observer
  26 + config.active_record.observers = :mailer_observer, :activity_observer, :project_observer, :key_observer, :issue_observer, :user_observer, :system_hook_observer, :users_project_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.
... ...
spec/factories.rb
... ... @@ -90,3 +90,8 @@ Factory.add(:milestone, Milestone) do |obj|
90 90 obj.title = Faker::Lorem.sentence
91 91 obj.due_date = Date.today + 1.month
92 92 end
  93 +
  94 +Factory.add(:users_project, UsersProject) do |obj|
  95 + obj.user = Factory :user
  96 + obj.project = Factory :project
  97 +end
... ...
spec/mailers/notify_spec.rb
... ... @@ -145,6 +145,26 @@ describe Notify do
145 145 end
146 146 end
147 147  
  148 + describe 'project access changed' do
  149 + let(:project) { Factory.create(:project,
  150 + path: "Fuu",
  151 + code: "Fuu") }
  152 + let(:user) { Factory.create :user }
  153 + let(:users_project) { Factory.create(:users_project,
  154 + project: project,
  155 + user: user) }
  156 + subject { Notify.project_access_granted_email(users_project.id) }
  157 + it 'has the correct subject' do
  158 + should have_subject /access to project was granted/
  159 + end
  160 + it 'contains name of project' do
  161 + should have_body_text /#{project.name}/
  162 + end
  163 + it 'contains new user role' do
  164 + should have_body_text /#{users_project.project_access_human}/
  165 + end
  166 + end
  167 +
148 168 context 'items that are noteable, the email for a note' do
149 169 let(:note_author) { Factory.create(:user, name: 'author_name') }
150 170 let(:note) { Factory.create(:note, project: project, author: note_author) }
... ...
spec/observers/users_project_observer_spec.rb 0 → 100644
... ... @@ -0,0 +1,40 @@
  1 +require 'spec_helper'
  2 +
  3 +describe UsersProjectObserver do
  4 + let(:user) { Factory.create :user }
  5 + let(:project) { Factory.create(:project,
  6 + code: "Fuu",
  7 + path: "Fuu" ) }
  8 + let(:users_project) { Factory.create(:users_project,
  9 + project: project,
  10 + user: user )}
  11 + subject { UsersProjectObserver.instance }
  12 +
  13 + describe "#after_create" do
  14 + it "should called when UsersProject created" do
  15 + subject.should_receive(:after_create)
  16 + UsersProject.observers.enable :users_project_observer do
  17 + Factory.create(:users_project,
  18 + project: project,
  19 + user: user)
  20 + end
  21 + end
  22 + it "should send email to user" do
  23 + Notify.should_receive(:project_access_granted_email).with(users_project.id).and_return(double(deliver: true))
  24 + subject.after_create(users_project)
  25 + end
  26 + end
  27 +
  28 + describe "#after_update" do
  29 + it "should called when UsersProject updated" do
  30 + subject.should_receive(:after_update)
  31 + UsersProject.observers.enable :users_project_observer do
  32 + users_project.update_attribute(:project_access, 40)
  33 + end
  34 + end
  35 + it "should send email to user" do
  36 + Notify.should_receive(:project_access_granted_email).with(users_project.id).and_return(double(deliver: true))
  37 + subject.after_update(users_project)
  38 + end
  39 + end
  40 +end
... ...