Add attachment by activity comment API spec

Support contaner aliasing needed from "journal" to "activity"
This commit is contained in:
Kabiru Mwenja
2025-04-11 11:09:17 +03:00
parent bf0c21346d
commit cf950cc75f
6 changed files with 111 additions and 4 deletions
@@ -45,6 +45,17 @@ module API
# sense in different contexts.
class PropertyNameConverter
class << self
def from_ar_name_with_aliases(attribute, ar_name_aliases = {})
normalized_attribute = attribute.to_s
normalized_aliases = ar_name_aliases.transform_keys(&:to_s)
if normalized_aliases.key?(normalized_attribute)
normalized_attribute = normalized_aliases[normalized_attribute]
end
from_ar_name(normalized_attribute)
end
# Converts the attribute name as referred to by ActiveRecord to a corresponding API-conform
# attribute name:
# * camelCasing the attribute name
@@ -129,7 +129,9 @@ module API
end
def v3_container_name
::API::Utilities::PropertyNameConverter.from_ar_name(represented.container.class.name.underscore).underscore
ar_name = represented.container.class.name.underscore
::API::Utilities::PropertyNameConverter.from_ar_name_with_aliases(ar_name, "journal" => "activity").underscore
end
def container_title_attribute
@@ -141,7 +141,9 @@ module API
end
def v3_container_name
::API::Utilities::PropertyNameConverter.from_ar_name(represented.container.class.name.underscore).underscore
ar_name = represented.container.class.name.underscore
::API::Utilities::PropertyNameConverter.from_ar_name_with_aliases(ar_name, "journal" => "activity").underscore
end
def container_title_attribute
@@ -29,6 +29,26 @@
require "spec_helper"
RSpec.describe API::Utilities::PropertyNameConverter do
describe "#from_ar_name_with_aliases" do
let(:attribute_name) { :an_attribute }
context "without any attribute aliases" do
it "returns the ar name" do
result = described_class.from_ar_name_with_aliases(attribute_name)
expect(result).to eql("anAttribute")
end
end
context "with attribute aliases" do
let(:attribute_aliases) { { "an_attribute" => "anAttributeAlias" } }
it "returns the alias" do
result = described_class.from_ar_name_with_aliases(attribute_name, attribute_aliases)
expect(result).to eql("anAttributeAlias")
end
end
end
describe "#from_ar_name" do
let(:attribute_name) { :an_attribute }
@@ -0,0 +1,72 @@
# 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"
require_relative "attachment_resource_shared_examples"
RSpec.describe "Activity comment attachments" do
it_behaves_like "an APIv3 attachment resource" do
let(:work_package) do
create(:work_package, author: current_user, project:)
end
let(:activity) do
work_package.add_journal(user: current_user, notes: "A comment")
work_package.save(validate: false)
work_package.journals.last
end
let(:attachment_type) { :activity }
let(:create_permission) { :add_work_package_notes }
let(:read_permission) { :view_work_packages }
let(:update_permission) { :edit_own_work_package_notes }
end
context "with a restricted journal" do
it_behaves_like "an APIv3 attachment resource" do
let(:work_package) do
create(:work_package, author: current_user, project:)
end
let(:activity) do
work_package.add_journal(user: current_user, notes: "Need to know!", restricted: true)
work_package.save(validate: false)
work_package.journals.last
end
let(:attachment_type) { :activity }
let(:create_permission) { %i[add_work_package_notes add_comments_with_restricted_visibility] }
let(:read_permission) { %i[view_work_packages view_comments_with_restricted_visibility] }
let(:update_permission) { %i[edit_own_work_package_notes edit_own_comments_with_restricted_visibility] }
end
end
end
@@ -547,8 +547,8 @@ RSpec.shared_examples "an APIv3 attachment resource", content_type: :json, type:
context "by container", if: include_by_container do
it_behaves_like "it supports direct uploads" do
let(:request_path) { "/api/v3/#{attachment_type}s/#{container.id}/attachments/prepare" }
let(:container_href) { "/api/v3/#{attachment_type}s/#{container.id}" }
let(:request_path) { "/api/v3/#{attachment_type.to_s.pluralize}/#{container.id}/attachments/prepare" }
let(:container_href) { "/api/v3/#{attachment_type.to_s.pluralize}/#{container.id}" }
end
subject(:response) { last_response }