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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add specs for controller
Klaus Zanders33abd7c74e36
diff --git a/app/models/token/hashed_token.rb b/app/models/token/hashed_token.rbindex 6fb5418c47b..c153833bcfd 100644--- a/app/models/token/hashed_token.rb+++ b/app/models/token/hashed_token.rb@@ -61,6 +61,10 @@ module Tokendef find_by_plaintext_value(input)find_by(value: hash_function(input))end++ def find_by_plaintext_value!(input)+ find_by!(value: hash_function(input))+ endend##diff --git a/modules/meeting/app/controllers/meetings/ical_controller.rb b/modules/meeting/app/controllers/meetings/ical_controller.rbindex e8346e5e57f..74e68565a1f 100644--- a/modules/meeting/app/controllers/meetings/ical_controller.rb+++ b/modules/meeting/app/controllers/meetings/ical_controller.rb@@ -32,16 +32,31 @@ module Meetingsskip_before_action :check_if_login_requiredauthorization_checked! :index+ EMPTY_ICS = "BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR"+def index- token = Token::ICalMeeting.find_by_plaintext_value(params[:token]) # rubocop:disable Rails/DynamicFindBy+ token = Token::ICalMeeting.find_by_plaintext_value!(params[:token]) # rubocop:disable Rails/DynamicFindByuser = token.userservice = AllMeetings::ICalService.new(user:)+ s_call = service.call++ ics_body = if s_call.success?+ s_call.result+ else+ Rails.logger.error "Could not generate ICS feed: #{s_call.message}"+ EMPTY_ICS+ endrespond_to do |format|format.ics do- send_data(service.call.result, filename: "mycal.ics", disposition: "inline; filename=mycal.ics", type: "text/calendar")+ send_data(+ ics_body,+ filename: "mycal.ics",+ disposition: "inline; filename=openproject-meetings.ics",+ type: "text/calendar"+ )endendenddiff --git a/modules/meeting/spec/controllers/meetings/ical_controller_spec.rb b/modules/meeting/spec/controllers/meetings/ical_controller_spec.rbnew file mode 100644index 00000000000..8da992ba4ef--- /dev/null+++ b/modules/meeting/spec/controllers/meetings/ical_controller_spec.rb@@ -0,0 +1,118 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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"++RSpec.describe Meetings::ICalController do+ shared_let(:user) { create(:user) }+ shared_let(:project) { create(:project) }+ shared_let(:ical_token) { create(:ical_meeting_token, user:) }+ shared_let(:token) { ical_token.plain_value }++ describe "GET index" do+ context "with valid token" do+ let(:ical_token) { create(:ical_meeting_token, user:) }+ let(:token) { ical_token.plain_value }+ let(:ical_data) { "BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR" }++ before do+ service_double = instance_double(AllMeetings::ICalService)+ allow(AllMeetings::ICalService).to receive(:new)+ .with(user:)+ .and_return(service_double)+ allow(service_double).to receive(:call)+ .and_return(ServiceResult.success(result: ical_data))+ end++ it "returns iCalendar data with correct headers" do+ get :index, params: { token: token }, format: :ics++ expect(response).to have_http_status(:ok)+ expect(response.content_type).to eq("text/calendar")+ expect(response.headers["Content-Disposition"]).to include("inline; filename=openproject-meetings.ics")+ expect(response.body).to eq(ical_data)+ end++ it "calls AllMeetings::ICalService with the correct user" do+ service_double = instance_double(AllMeetings::ICalService)+ allow(AllMeetings::ICalService).to receive(:new)+ .with(user:)+ .and_return(service_double)+ allow(service_double).to receive(:call)+ .and_return(ServiceResult.success(result: ical_data))++ get :index, params: { token: token }, format: :ics++ expect(AllMeetings::ICalService).to have_received(:new).with(user:)+ expect(service_double).to have_received(:call)+ end+ end++ context "with invalid token" do+ let(:invalid_token_value) { "invalid_token_456" }++ it "raises an error when token is not found" do+ get :index, params: { token: invalid_token_value }, format: :ics+ expect(response).to have_http_status(:not_found)+ end+ end++ context "when service call fails" do+ before do+ service_double = instance_double(AllMeetings::ICalService)+ allow(AllMeetings::ICalService).to receive(:new)+ .with(user:)+ .and_return(service_double)+ allow(service_double).to receive(:call)+ .and_return(ServiceResult.failure(message: "Service failed"))+ end++ it "logs an error" do+ expect(Rails.logger).to receive(:error).with("Could not generate ICS feed: Service failed")+ get :index, params: { token: token }, format: :ics+ end++ it "returns an empty ICS file" do+ get :index, params: { token: token }, format: :ics++ expect(response).to have_http_status(:ok)+ expect(response.content_type).to eq("text/calendar")+ expect(response.body).to eq(described_class::EMPTY_ICS)+ end+ end++ context "when requesting non-ics format" do+ it "does not respond to other formats" do+ expect do+ get :index, params: { token: token }, format: :json+ end.to raise_error(ActionController::UnknownFormat)+ end+ end+ end+enddiff --git a/spec/factories/token_factory.rb b/spec/factories/token_factory.rbindex 9aa865b828d..05489917388 100644--- a/spec/factories/token_factory.rb+++ b/spec/factories/token_factory.rb@@ -85,4 +85,9 @@ FactoryBot.define doical_token: token)endend++ factory :ical_meeting_token, class: "::Token::ICalMeeting" do+ user+ token_name { "My Meetings Calendar" }+ endend