Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

added first unit tests

Jonas Jabarif85eacd9d883
diff --git a/modules/calendar/app/services/calendar/create_ical_service.rb b/modules/calendar/app/services/calendar/create_ical_service.rb
index 6237ecbdc22..735cdf7bc19 100644
--- a/modules/calendar/app/services/calendar/create_ical_service.rb
+++ b/modules/calendar/app/services/calendar/create_ical_service.rb
@@ -46,7 +46,7 @@ module Calendar
calendar.prodid = "-//OpenProject GmbH//OpenProject Core Project//EN"
calendar.x_wr_calname = "OpenProject Calendar"
- work_packages&.each do |work_package|
+ work_packages&.each do |work_package|
next if work_package.due_date.nil?
event = create_event(work_package)
@@ -63,13 +63,29 @@ module Calendar
event.attendee = [work_package.assigned_to&.name]
event.organizer = work_package.author&.name
event.summary = work_package.name
- event.dtstart = Icalendar::Values::Date.new(work_package.start_date)
- event.dtend = Icalendar::Values::Date.new(work_package.due_date + 1.day)
+ event.dtstart = Icalendar::Values::Date.new(start_date(work_package))
+ event.dtend = Icalendar::Values::Date.new(due_date(work_package))
event.location = work_package_url(work_package)
event.description = description_value(work_package)
event
end
+
+ def start_date(work_package)
+ if work_package.start_date.present?
+ work_package.start_date
+ else
+ work_package.due_date
+ end
+ end
+
+ def due_date(work_package)
+ if work_package.start_date.present?
+ work_package.due_date
+ else
+ work_package.due_date + 1.day
+ end
+ end
def work_package_url(work_package)
url_for(
diff --git a/modules/calendar/spec/services/create_ical_service_spec.rb b/modules/calendar/spec/services/create_ical_service_spec.rb
new file mode 100644
index 00000000000..c49c3ada564
--- /dev/null
+++ b/modules/calendar/spec/services/create_ical_service_spec.rb
@@ -0,0 +1,117 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe Calendar::CreateIcalService, type: :model do
+
+ let(:project) { create(:project) }
+
+ let(:work_package_without_dates) do
+ create(:work_package, project: project)
+ end
+ let(:work_package_with_due_date) do
+ create(:work_package, project: project,
+ due_date: Date.today+7.days)
+ end
+ let(:work_package_with_start_and_due_date) do
+ create(:work_package, project: project,
+ start_date: Date.tomorrow, due_date: Date.today+7.days)
+ end
+ let(:work_packages) do
+ [
+ work_package_without_dates,
+ work_package_with_due_date,
+ work_package_with_start_and_due_date
+ ]
+ end
+
+
+ let(:instance) do
+ described_class.new()
+ end
+
+ let(:freezed_date_time) { DateTime.now }
+
+ context 'converts work_packages to an ical string' do
+
+ before(:each) do
+ Timecop.freeze(freezed_date_time)
+ end
+
+ subject { instance.call(work_packages: work_packages) }
+
+ it 'which contains all required ical fields in the correct format and order' do
+
+ expected_ical_string = <<-EOICAL.gsub("\r ", "").gsub("\r", "")
+BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//OpenProject GmbH//OpenProject Core Project//EN
+CALSCALE:GREGORIAN
+X-WR-CALNAME:OpenProject Calendar
+BEGIN:VEVENT
+DTSTAMP:#{freezed_date_time.strftime('%Y%m%dT%H%M%S')}Z
+UID:#{work_package_with_due_date.id}@localhost:3000
+DTSTART;VALUE=DATE:#{work_package_with_due_date.due_date.strftime('%Y%m%d')}
+DTEND;VALUE=DATE:#{(work_package_with_due_date.due_date+1.day).strftime('%Y%m%d')}
+DESCRIPTION:Project: #{project.name}\nType: 🟩 None\nStatus: #{work_package_with_due_date.status.name}\nAssignee: \nPriority: 🟢 #{work_package_with_due_date.priority.name}\nWork package description: #{work_package_with_due_date.description}
+LOCATION:http://localhost:3000/work_packages/#{work_package_with_due_date.id}
+ORGANIZER:Bob Bobbit
+SUMMARY:#{work_package_with_due_date.name}
+ATTENDEE:
+END:VEVENT
+BEGIN:VEVENT
+DTSTAMP:#{freezed_date_time.strftime('%Y%m%dT%H%M%S')}Z
+UID:#{work_package_with_start_and_due_date.id}@localhost:3000
+DTSTART;VALUE=DATE:#{work_package_with_start_and_due_date.start_date.strftime('%Y%m%d')}
+DTEND;VALUE=DATE:#{work_package_with_start_and_due_date.due_date.strftime('%Y%m%d')}
+DESCRIPTION:Project: #{project.name}\nType: 🟩 None\nStatus: #{work_package_with_start_and_due_date.status.name}\nAssignee: \nPriority: 🟢 #{work_package_with_start_and_due_date.priority.name}\nWork package description: #{work_package_with_start_and_due_date.description}
+LOCATION:http://localhost:3000/work_packages/#{work_package_with_start_and_due_date.id}
+ORGANIZER:Bob Bobbit
+SUMMARY:#{work_package_with_start_and_due_date.name}
+ATTENDEE:
+END:VEVENT
+END:VCALENDAR
+ EOICAL
+
+ expect(subject.result.gsub("\r\n ", "").gsub("\r", "").gsub("\\n", "\n"))
+ .to eql(expected_ical_string)
+ end
+
+ it 'is a success' do
+ expect(subject)
+ .to be_success
+ end
+
+ after(:each) do
+ Timecop.return
+ end
+
+ end
+
+end
diff --git a/modules/calendar/spec/services/resolve_ical_user_service_spec.rb b/modules/calendar/spec/services/resolve_ical_user_service_spec.rb
new file mode 100644
index 00000000000..ff2be717b3a
--- /dev/null
+++ b/modules/calendar/spec/services/resolve_ical_user_service_spec.rb
@@ -0,0 +1,76 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe Calendar::ResolveIcalUserService, type: :model do
+ let(:user) { create(:user) }
+ let(:valid_api_token_value) { Token::API.create_and_return_value user }
+ let(:invalid_api_token_value) { valid_api_token_value[0..-2] }
+
+ let(:instance) do
+ described_class.new()
+ end
+
+ context 'resolves a user by a given valid ical token value' do
+
+ subject { instance.call(ical_token: valid_api_token_value) }
+
+ it 'resolves the user as result' do
+ expect(subject.result)
+ .to eql user
+ end
+
+ it 'is a success' do
+ expect(subject)
+ .to be_success
+ end
+
+ end
+
+ context 'handles error when given ical token value is invalid' do
+
+ subject { instance.call(ical_token: invalid_api_token_value) }
+
+ it 'raises ActiveRecord::RecordNotFound' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ end
+
+ context 'handles error when no ical token value is given' do
+
+ subject { instance.call(ical_token: nil) }
+
+ it 'raises ActiveRecord::RecordNotFound' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ end
+
+end
diff --git a/modules/calendar/spec/services/resolve_work_packages_service_spec.rb b/modules/calendar/spec/services/resolve_work_packages_service_spec.rb
new file mode 100644
index 00000000000..78e515859b6
--- /dev/null
+++ b/modules/calendar/spec/services/resolve_work_packages_service_spec.rb
@@ -0,0 +1,128 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe Calendar::ResolveWorkPackagesService, type: :model do
+ let(:user_1) do
+ create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+ let(:user_2_without_permission) do
+ create(:user,
+ member_in_project: project,
+ member_with_permissions: [])
+ end
+ let(:user_3_not_member) do
+ create(:user,
+ member_in_project: nil)
+ end
+ let(:permissions) { %i[view_work_packages] }
+ let(:project) { create(:project, work_packages: [work_package_1]) }
+ let(:work_package_1) { create(:work_package) }
+ let(:query) do
+ create(:query,
+ project: project,
+ user: user_1,
+ public: false)
+ end
+
+ let(:instance) do
+ described_class.new()
+ end
+
+ context 'resolves work_packages of a query by a given query id if authenticated' do
+
+ before do
+ login_as(user_1)
+ end
+
+ subject { instance.call(user: user_1, query_id: query.id) }
+
+ it 'returns work_packages of query as result ' do
+ expect(subject.result)
+ .to match_array(query.results.work_packages)
+ end
+
+ it 'is a success' do
+ expect(subject)
+ .to be_success
+ end
+
+ end
+
+ context 'does not resolve work_packages of a query by a given query id if not authenticated' do
+
+ before do
+ login_as(user_2_without_permission)
+ end
+
+ subject { instance.call(user: user_2_without_permission, query_id: query.id) }
+
+ it 'raises ActiveRecord::RecordNotFound' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ end
+
+ context 'does not resolve work_packages of a query by a given query id if not member of project' do
+
+ before do
+ login_as(user_3_not_member)
+ end
+
+ subject { instance.call(user: user_3_not_member, query_id: query.id) }
+
+ it 'raises ActiveRecord::RecordNotFound' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ end
+
+ context 'does not resolve work_packages if query id is' do
+
+ before do
+ login_as(user_1)
+ end
+
+ subject { instance.call(user: user_1, query_id: SecureRandom.hex) }
+
+ it 'invalid and raises ActiveRecord::RecordNotFound' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ subject { instance.call(user: user_1, query_id: nil) }
+
+ it 'nil and raises ActiveRecord::RecordNotFound' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ end
+
+end