commit
108160ed88
@ -0,0 +1,87 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
class JournalNotificationMailer |
||||
class << self |
||||
def distinguish_journals(journal, send_notification) |
||||
if send_notification |
||||
if journal.journable_type == 'WorkPackage' && journal.initial? |
||||
handle_work_package_create(journal.journable) |
||||
elsif journal.journable_type == 'WorkPackage' |
||||
handle_work_package_update(journal) |
||||
end |
||||
end |
||||
end |
||||
|
||||
def handle_work_package_create(work_package) |
||||
if Setting.notified_events.include?('work_package_added') |
||||
notification_receivers(work_package).uniq.each do |user| |
||||
job = DeliverWorkPackageCreatedJob.new(user.id, work_package.id, User.current.id) |
||||
|
||||
Delayed::Job.enqueue job |
||||
end |
||||
end |
||||
end |
||||
|
||||
def handle_work_package_update(journal) |
||||
if send_update_notification?(journal) |
||||
work_package = journal.journable |
||||
notification_receivers(work_package).uniq.each do |user| |
||||
job = DeliverWorkPackageUpdatedJob.new(user.id, journal.id, User.current.id) |
||||
Delayed::Job.enqueue job |
||||
end |
||||
end |
||||
end |
||||
|
||||
def send_update_notification?(journal) |
||||
Setting.notified_events.include?('work_package_updated') || |
||||
notify_for_notes?(journal) || |
||||
notify_for_status?(journal) || |
||||
notify_for_priority(journal) |
||||
end |
||||
|
||||
def notify_for_notes?(journal) |
||||
Setting.notified_events.include?('work_package_note_added') && journal.notes.present? |
||||
end |
||||
|
||||
def notify_for_status?(journal) |
||||
Setting.notified_events.include?('status_updated') && |
||||
journal.changed_data.has_key?(:status_id) |
||||
end |
||||
|
||||
def notify_for_priority(journal) |
||||
Setting.notified_events.include?('work_package_priority_updated') && |
||||
journal.changed_data.has_key?(:priority_id) |
||||
end |
||||
|
||||
def notification_receivers(work_package) |
||||
work_package.recipients + work_package.watcher_recipients |
||||
end |
||||
end |
||||
end |
@ -1,67 +0,0 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
class JournalObserver < ActiveRecord::Observer |
||||
attr_accessor :send_notification |
||||
|
||||
def after_create(journal) |
||||
if journal.journable_type == 'WorkPackage' and !journal.initial? and send_notification |
||||
after_create_issue_journal(journal) |
||||
end |
||||
clear_notification |
||||
end |
||||
|
||||
def after_create_issue_journal(journal) |
||||
if Setting.notified_events.include?('work_package_updated') || |
||||
(Setting.notified_events.include?('work_package_note_added') && journal.notes.present?) || |
||||
(Setting.notified_events.include?('status_updated') && journal.changed_data.has_key?(:status_id)) || |
||||
(Setting.notified_events.include?('work_package_priority_updated') && journal.changed_data.has_key?(:priority_id)) |
||||
issue = journal.journable |
||||
recipients = issue.recipients + issue.watcher_recipients |
||||
users = User.find_all_by_mails(recipients.uniq) |
||||
users.each do |user| |
||||
job = DeliverWorkPackageUpdatedJob.new(user.id, journal.id, User.current.id) |
||||
Delayed::Job.enqueue job |
||||
end |
||||
end |
||||
end |
||||
|
||||
# Wrap send_notification so it defaults to true, when it's nil |
||||
def send_notification |
||||
return true if @send_notification.nil? |
||||
@send_notification |
||||
end |
||||
|
||||
private |
||||
|
||||
# Need to clear the notification setting after each usage otherwise it might be cached |
||||
def clear_notification |
||||
@send_notification = true |
||||
end |
||||
end |
@ -0,0 +1,77 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Repositories |
||||
class RevisionRepresenter < ::API::Decorators::Single |
||||
include API::V3::Utilities |
||||
|
||||
self_link path: :revision, |
||||
title_getter: -> (*) { nil } |
||||
|
||||
link :project do |
||||
{ |
||||
href: api_v3_paths.project(represented.project.id), |
||||
title: represented.project.name |
||||
} |
||||
end |
||||
|
||||
link :author do |
||||
{ |
||||
href: api_v3_paths.user(represented.user.id), |
||||
title: represented.user.name |
||||
} unless represented.user.nil? |
||||
end |
||||
|
||||
property :id |
||||
property :identifier |
||||
property :author, as: :authorName |
||||
property :message, |
||||
exec_context: :decorator, |
||||
getter: -> (*) { |
||||
::API::Decorators::Formattable.new(represented.comments, |
||||
object: represented, |
||||
format: 'plain') |
||||
}, |
||||
render_nil: true |
||||
|
||||
property :created_at, |
||||
exec_context: :decorator, |
||||
getter: -> (*) { |
||||
datetime_formatter.format_datetime(represented.committed_on) |
||||
} |
||||
|
||||
def _type |
||||
'Revision' |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,61 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
module API |
||||
module V3 |
||||
module Repositories |
||||
class RevisionsAPI < ::API::OpenProjectAPI |
||||
resources :revisions do |
||||
params do |
||||
requires :id, desc: 'Revision id' |
||||
end |
||||
route_param :id do |
||||
helpers do |
||||
attr_reader :revision |
||||
|
||||
def revision_representer |
||||
RevisionRepresenter.new(revision) |
||||
end |
||||
end |
||||
|
||||
before do |
||||
@revision = Changeset.find(params[:id]) |
||||
|
||||
authorize(:view_changesets, context: revision.project) do |
||||
raise API::Errors::NotFound.new |
||||
end |
||||
end |
||||
|
||||
get do |
||||
revision_representer |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,52 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'api/v3/repositories/revisions_collection_representer' |
||||
|
||||
module API |
||||
module V3 |
||||
module Repositories |
||||
class RevisionsByWorkPackageAPI < ::API::OpenProjectAPI |
||||
resources :revisions do |
||||
before do |
||||
authorize(:view_changesets, context: work_package.project) |
||||
end |
||||
|
||||
get do |
||||
self_path = api_v3_paths.work_package_revisions(work_package.id) |
||||
|
||||
revisions = work_package.changesets |
||||
RevisionsCollectionRepresenter.new(revisions, |
||||
revisions.count, |
||||
self_path) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -1,133 +0,0 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
require 'legacy_spec_helper' |
||||
|
||||
describe JournalObserver, type: :model do |
||||
before do |
||||
@type = FactoryGirl.create :type_with_workflow |
||||
@project = FactoryGirl.create :project, |
||||
types: [@type] |
||||
@workflow = @type.workflows.first |
||||
@user = FactoryGirl.create :user, |
||||
mail_notification: 'all', |
||||
member_in_project: @project |
||||
@issue = FactoryGirl.create :work_package, |
||||
project: @project, |
||||
author: @user, |
||||
type: @type, |
||||
status: @workflow.old_status |
||||
|
||||
@user.members.first.roles << @workflow.role |
||||
@user.reload |
||||
|
||||
allow(User).to receive(:current).and_return(@user) |
||||
|
||||
ActionMailer::Base.deliveries.clear |
||||
end |
||||
|
||||
context "#after_create for 'work_package_updated'" do |
||||
it 'should send a notification when configured as a notification' do |
||||
Setting.notified_events = ['work_package_updated'] |
||||
assert_difference('ActionMailer::Base.deliveries.size', +1) do |
||||
@issue.add_journal(@user) |
||||
@issue.subject = 'A change to the issue' |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
|
||||
it 'should not send a notification with not configured' do |
||||
Setting.notified_events = [] |
||||
assert_no_difference('ActionMailer::Base.deliveries.size') do |
||||
@issue.add_journal(@user) |
||||
@issue.subject = 'A change to the issue' |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "#after_create for 'work_package_note_added'" do |
||||
it 'should send a notification when configured as a notification' do |
||||
@issue.recreate_initial_journal! |
||||
|
||||
Setting.notified_events = ['work_package_note_added'] |
||||
assert_difference('ActionMailer::Base.deliveries.size', +1) do |
||||
@issue.add_journal(@user, 'This update has a note') |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
|
||||
it 'should not send a notification with not configured' do |
||||
Setting.notified_events = [] |
||||
assert_no_difference('ActionMailer::Base.deliveries.size') do |
||||
@issue.add_journal(@user, 'This update has a note') |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "#after_create for 'status_updated'" do |
||||
it 'should send a notification when configured as a notification' do |
||||
Setting.notified_events = ['status_updated'] |
||||
assert_difference('ActionMailer::Base.deliveries.size', +1) do |
||||
@issue.add_journal(@user) |
||||
@issue.status = @workflow.new_status |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
|
||||
it 'should not send a notification with not configured' do |
||||
Setting.notified_events = [] |
||||
assert_no_difference('ActionMailer::Base.deliveries.size') do |
||||
@issue.add_journal(@user) |
||||
@issue.status = @workflow.new_status |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "#after_create for 'work_package_priority_updated'" do |
||||
it 'should send a notification when configured as a notification' do |
||||
Setting.notified_events = ['work_package_priority_updated'] |
||||
assert_difference('ActionMailer::Base.deliveries.size', +1) do |
||||
@issue.add_journal(@user) |
||||
@issue.priority = IssuePriority.generate! |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
|
||||
it 'should not send a notification with not configured' do |
||||
Setting.notified_events = [] |
||||
assert_no_difference('ActionMailer::Base.deliveries.size') do |
||||
@issue.add_journal(@user) |
||||
@issue.priority = IssuePriority.generate! |
||||
assert @issue.save(validate: false) |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,127 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::Repositories::RevisionRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:representer) { described_class.new(revision) } |
||||
|
||||
let(:project) { FactoryGirl.build :project } |
||||
let(:repository) { FactoryGirl.build :repository_subversion, project: project } |
||||
let(:revision) { |
||||
FactoryGirl.build(:changeset, |
||||
id: 42, |
||||
revision: '1234', |
||||
repository: repository, |
||||
comments: commit_message, |
||||
committer: 'foo bar <foo@example.org>', |
||||
committed_on: DateTime.now, |
||||
) |
||||
} |
||||
|
||||
let(:commit_message) { 'Some commit message' } |
||||
|
||||
context 'generation' do |
||||
subject(:generated) { representer.to_json } |
||||
|
||||
it { is_expected.to be_json_eql('Revision'.to_json).at_path('_type') } |
||||
|
||||
describe 'revision' do |
||||
it { is_expected.to have_json_path('id') } |
||||
|
||||
it_behaves_like 'API V3 formattable', 'message' do |
||||
let(:format) { 'plain' } |
||||
let(:raw) { revision.comments } |
||||
let(:html) { '<p>' + revision.comments + '</p>' } |
||||
end |
||||
|
||||
describe 'identifier' do |
||||
it { is_expected.to have_json_path('identifier') } |
||||
it { is_expected.to be_json_eql('1234'.to_json).at_path('identifier') } |
||||
end |
||||
|
||||
describe 'createdAt' do |
||||
it_behaves_like 'has UTC ISO 8601 date and time' do |
||||
let(:date) { revision.committed_on } |
||||
let(:json_path) { 'createdAt' } |
||||
end |
||||
end |
||||
|
||||
describe 'authorName' do |
||||
it { is_expected.to have_json_path('authorName') } |
||||
it { is_expected.to be_json_eql('foo bar '.to_json).at_path('authorName') } |
||||
end |
||||
end |
||||
|
||||
context 'with referencing commit message' do |
||||
let(:work_package) { FactoryGirl.build_stubbed(:work_package, project: project) } |
||||
let(:commit_message) { "Totally references ##{work_package.id}" } |
||||
let(:html_reference) { |
||||
id = work_package.id |
||||
|
||||
str = "Totally references <a href=\"/work_packages/#{id}\"" |
||||
str << " class=\"issue work_package status-1 priority-1 parent\"" |
||||
str << " title=\"#{work_package.subject} (#{work_package.status})\">" |
||||
str << "##{id}</a>" |
||||
} |
||||
|
||||
before do |
||||
allow(WorkPackage).to receive(:find_by_id).and_return(work_package) |
||||
end |
||||
|
||||
it_behaves_like 'API V3 formattable', 'message' do |
||||
let(:format) { 'plain' } |
||||
let(:raw) { revision.comments } |
||||
let(:html) { '<p>' + html_reference + '</p>' } |
||||
end |
||||
end |
||||
|
||||
describe 'author' do |
||||
context 'with no linked user' do |
||||
it_behaves_like 'has no link' do |
||||
let(:link) { 'author' } |
||||
end |
||||
end |
||||
|
||||
context 'with linked user as author' do |
||||
let(:user) { FactoryGirl.build(:user) } |
||||
before do |
||||
allow(revision).to receive(:user).and_return(user) |
||||
end |
||||
|
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'author' } |
||||
let(:href) { api_v3_paths.user(user.id) } |
||||
let(:title) { user.name } |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,156 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
require 'spec_helper' |
||||
|
||||
describe JournalNotificationMailer do |
||||
let(:project) { FactoryGirl.create(:project_with_types) } |
||||
let(:user) do |
||||
FactoryGirl.build(:user, |
||||
mail_notification: 'all', |
||||
member_in_project: project) |
||||
end |
||||
let(:work_package) { |
||||
FactoryGirl.create(:work_package, |
||||
project: project, |
||||
author: user, |
||||
type: project.types.first) |
||||
} |
||||
let(:notifications) { [] } |
||||
|
||||
before do |
||||
allow(User).to receive(:current).and_return(user) |
||||
allow(Setting).to receive(:notified_events).and_return(notifications) |
||||
|
||||
ActionMailer::Base.deliveries.clear |
||||
end |
||||
|
||||
shared_examples_for 'handles deliveries' do |notification_setting| |
||||
context 'setting enabled' do |
||||
let(:notifications) { [notification_setting] } |
||||
|
||||
it 'sends a notification' do |
||||
expect(ActionMailer::Base.deliveries.size).to eq(1) |
||||
end |
||||
|
||||
context 'insufficient work package changes' do |
||||
let!(:another_work_package) { |
||||
FactoryGirl.create(:work_package, |
||||
project: project, |
||||
author: user, |
||||
type: project.types.first) |
||||
} |
||||
before do |
||||
ActionMailer::Base.deliveries.clear |
||||
another_work_package.add_journal(user) |
||||
another_work_package.description = 'needs more changes' |
||||
another_work_package.save!(validate: false) |
||||
end |
||||
|
||||
it 'sends no notification' do |
||||
expect(ActionMailer::Base.deliveries.size).to eq(0) |
||||
end |
||||
end |
||||
end |
||||
|
||||
it 'sends no notification' do |
||||
expect(ActionMailer::Base.deliveries.size).to eq(0) |
||||
end |
||||
end |
||||
|
||||
describe 'journal creation' do |
||||
context 'work_package_created' do |
||||
before do |
||||
FactoryGirl.create(:work_package, project: project) |
||||
end |
||||
|
||||
it_behaves_like 'handles deliveries', 'work_package_added' |
||||
end |
||||
|
||||
context 'work_package_updated' do |
||||
before do |
||||
work_package.add_journal(user) |
||||
work_package.subject = 'A change to the issue' |
||||
work_package.save!(validate: false) |
||||
end |
||||
|
||||
context 'setting enabled' do |
||||
let(:notifications) { ['work_package_updated'] } |
||||
|
||||
it 'sends a notification' do |
||||
expect(ActionMailer::Base.deliveries.size).to eq(1) |
||||
end |
||||
end |
||||
|
||||
it 'sends no notification' do |
||||
expect(ActionMailer::Base.deliveries.size).to eq(0) |
||||
end |
||||
end |
||||
|
||||
context 'work_package_note_added' do |
||||
before do |
||||
work_package.add_journal(user, 'This update has a note') |
||||
work_package.save!(validate: false) |
||||
work_package.recreate_initial_journal! |
||||
end |
||||
|
||||
it_behaves_like 'handles deliveries', 'work_package_note_added' |
||||
end |
||||
|
||||
context 'status_updated' do |
||||
before do |
||||
work_package.add_journal(user) |
||||
work_package.status = FactoryGirl.build(:status) |
||||
work_package.save!(validate: false) |
||||
end |
||||
|
||||
it_behaves_like 'handles deliveries', 'status_updated' |
||||
end |
||||
|
||||
context 'work_package_priority_updated' do |
||||
before do |
||||
work_package.add_journal(user) |
||||
work_package.priority = IssuePriority.generate! |
||||
work_package.save!(validate: false) |
||||
end |
||||
|
||||
it_behaves_like 'handles deliveries', 'work_package_priority_updated' |
||||
end |
||||
|
||||
context 'send_notification disabled' do |
||||
before do |
||||
allow(JournalManager).to receive(:send_notification).and_return(false) |
||||
FactoryGirl.create(:work_package, project: project) # Provoke notification |
||||
end |
||||
|
||||
it 'sends no notification' do |
||||
expect(ActionMailer::Base.deliveries.size).to eq(0) |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,98 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
require 'rack/test' |
||||
|
||||
describe 'API v3 Revisions by work package resource', type: :request do |
||||
include Rack::Test::Methods |
||||
include API::V3::Utilities::PathHelper |
||||
include FileHelpers |
||||
|
||||
let(:current_user) { |
||||
FactoryGirl.create(:user, |
||||
member_in_project: project, |
||||
member_through_role: role) |
||||
} |
||||
let(:project) { FactoryGirl.create(:project, is_public: false) } |
||||
let(:role) { FactoryGirl.create(:role, permissions: permissions) } |
||||
let(:permissions) { [:view_work_packages, :view_changesets] } |
||||
let(:repository) { FactoryGirl.create(:repository_subversion, project: project) } |
||||
let(:work_package) { FactoryGirl.create(:work_package, author: current_user, project: project) } |
||||
let(:revisions) { [] } |
||||
|
||||
subject(:response) { last_response } |
||||
|
||||
before do |
||||
allow(User).to receive(:current).and_return current_user |
||||
end |
||||
|
||||
describe '#get' do |
||||
let(:get_path) { api_v3_paths.work_package_revisions work_package.id } |
||||
|
||||
before do |
||||
revisions.each do |rev| rev.save! end |
||||
get get_path |
||||
end |
||||
|
||||
it 'should respond with 200' do |
||||
expect(subject.status).to eq(200) |
||||
end |
||||
|
||||
it_behaves_like 'API V3 collection response', 0, 0, 'Revision' |
||||
|
||||
|
||||
context 'with existing revisions' do |
||||
let(:revisions) { |
||||
FactoryGirl.build_list(:changeset, |
||||
5, |
||||
comments: "This commit references ##{work_package.id}", |
||||
repository: repository |
||||
) |
||||
} |
||||
|
||||
it_behaves_like 'API V3 collection response', 5, 5, 'Revision' |
||||
end |
||||
|
||||
context 'user unauthorized to view work package' do |
||||
let(:current_user) { FactoryGirl.create(:user) } |
||||
|
||||
it 'should respond with 404' do |
||||
expect(subject.status).to eq(404) |
||||
end |
||||
end |
||||
|
||||
context 'user unauthorized to view revisions' do |
||||
let(:permissions) { [:view_work_packages] } |
||||
|
||||
it 'should respond with 403' do |
||||
expect(subject.status).to eq(403) |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,97 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2013 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
require 'rack/test' |
||||
|
||||
describe 'API v3 Revisions resource', type: :request do |
||||
include Rack::Test::Methods |
||||
include Capybara::RSpecMatchers |
||||
include API::V3::Utilities::PathHelper |
||||
|
||||
let(:revision) { |
||||
FactoryGirl.create(:changeset, |
||||
repository: repository, |
||||
comments: 'Some commit message', |
||||
committer: 'foo bar <foo@example.org>' |
||||
) |
||||
} |
||||
let(:repository) { |
||||
FactoryGirl.create(:repository_subversion, project: project) |
||||
} |
||||
let(:project) { |
||||
FactoryGirl.create(:project, identifier: 'test_project', is_public: false) |
||||
} |
||||
let(:role) { |
||||
FactoryGirl.create(:role, |
||||
permissions: [:view_changesets]) |
||||
} |
||||
let(:current_user) { |
||||
FactoryGirl.create(:user, member_in_project: project, member_through_role: role) |
||||
} |
||||
|
||||
let(:unauthorized_user) { FactoryGirl.create(:user) } |
||||
|
||||
describe '#get' do |
||||
let(:get_path) { api_v3_paths.revision revision.id } |
||||
|
||||
context 'when acting as a user with permission to view revisions' do |
||||
before(:each) do |
||||
allow(User).to receive(:current).and_return current_user |
||||
get get_path |
||||
end |
||||
|
||||
it 'should respond with 200' do |
||||
expect(last_response.status).to eq(200) |
||||
end |
||||
|
||||
describe 'response body' do |
||||
subject(:response) { last_response.body } |
||||
|
||||
it 'should respond with revision in HAL+JSON format' do |
||||
is_expected.to be_json_eql(revision.id.to_json).at_path('id') |
||||
end |
||||
end |
||||
|
||||
context 'requesting nonexistent revision' do |
||||
let(:get_path) { api_v3_paths.revision 909090 } |
||||
|
||||
it_behaves_like 'not found' |
||||
end |
||||
end |
||||
|
||||
context 'when acting as an user without permission to view work package' do |
||||
before(:each) do |
||||
allow(User).to receive(:current).and_return unauthorized_user |
||||
get get_path |
||||
end |
||||
|
||||
it_behaves_like 'not found' |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue