From f039fc8333f53023708d6911a996302716c8bbb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 1 Nov 2021 17:02:58 +0100 Subject: [PATCH] Add representation formats to project API --- .../schemas/list_projects_model.yml | 9 +++++ .../project_collection_representer.rb | 4 +- .../spec/models/work_package_export_spec.rb | 4 +- .../project_collection_representer_spec.rb | 40 +++++++++++++++++-- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/docs/api/apiv3/components/schemas/list_projects_model.yml b/docs/api/apiv3/components/schemas/list_projects_model.yml index 63e58adacd..7976d44a6c 100644 --- a/docs/api/apiv3/components/schemas/list_projects_model.yml +++ b/docs/api/apiv3/components/schemas/list_projects_model.yml @@ -5,6 +5,15 @@ example: _links: self: href: "/api/v3/projects" + representations: + - href: "/projects.csv?filters=%5B%7B%22nameAndIdentifier%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%22open%22%5D%7D%7D%5D&offset=1&pageSize=50", + identifier: "csv", + type: "text/csv", + title: "CSV" + - href: "/projects.xls?filters=%5B%7B%22nameAndIdentifier%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%22open%22%5D%7D%7D%5D&offset=1&pageSize=50", + identifier: "xls", + type: "application/vnd.ms-excel", + title: "XLS" _type: Collection total: 2 count: 2 diff --git a/lib/api/v3/projects/project_collection_representer.rb b/lib/api/v3/projects/project_collection_representer.rb index 3c90aaac6c..130ee2c3a7 100644 --- a/lib/api/v3/projects/project_collection_representer.rb +++ b/lib/api/v3/projects/project_collection_representer.rb @@ -36,11 +36,11 @@ require 'roar/json/hal' module API module V3 module Projects - class ProjectCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection + class ProjectCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection self.to_eager_load = ::API::V3::Projects::ProjectRepresenter.to_eager_load self.checked_permissions = ::API::V3::Projects::ProjectRepresenter.checked_permissions - def initialize(models, **options) + def initialize(represented, self_link:, current_user:, query: {}, page: nil, per_page: nil, groups: nil) super @represented = ::API::V3::Projects::ProjectEagerLoadingWrapper.wrap(represented) diff --git a/modules/backlogs/spec/models/work_package_export_spec.rb b/modules/backlogs/spec/models/work_package_export_spec.rb index e29334d621..5b0a34e7c7 100644 --- a/modules/backlogs/spec/models/work_package_export_spec.rb +++ b/modules/backlogs/spec/models/work_package_export_spec.rb @@ -28,7 +28,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe WorkPackage::Exporter::PDF, type: :model do +describe WorkPackage::PDFExport::WorkPackageToPdf, type: :model do let(:project) { FactoryBot.create :project } let(:query) { Query.new_default(name: '_', project: project) } subject { described_class.new query } @@ -39,7 +39,7 @@ describe WorkPackage::Exporter::PDF, type: :model do describe 'backlogs column' do it 'should contain the story_points column in valid export column names' do - backlog_column = subject.valid_export_columns.detect { |c| c.name == :story_points } + backlog_column = subject.columns.detect { |c| c.name == :story_points } expect(backlog_column).to be_present end end diff --git a/spec/lib/api/v3/projects/project_collection_representer_spec.rb b/spec/lib/api/v3/projects/project_collection_representer_spec.rb index 1fc2baa441..1983096606 100644 --- a/spec/lib/api/v3/projects/project_collection_representer_spec.rb +++ b/spec/lib/api/v3/projects/project_collection_representer_spec.rb @@ -29,17 +29,49 @@ require 'spec_helper' describe ::API::V3::Projects::ProjectCollectionRepresenter do - let(:self_link) { '/api/v3/versions/1/projects' } - let(:projects) { FactoryBot.build_list(:project, 3) } + shared_let(:projects) { FactoryBot.create_list(:project, 3) } + + let(:self_base_link) { '/api/v3/projects' } let(:current_user) { FactoryBot.build(:user) } let(:representer) do - described_class.new(projects, self_link: self_link, current_user: current_user) + described_class.new Project.all, + self_link: self_base_link, + current_user: current_user end + let(:total) { 3 } + let(:page) { 1 } + let(:page_size) { 30 } + let(:actual_count) { 3 } + let(:collection_inner_type) { 'Project' } + + subject { representer.to_json } context 'generation' do subject(:collection) { representer.to_json } - it_behaves_like 'unpaginated APIv3 collection', 3, 'versions/1/projects', 'Project' + it_behaves_like 'offset-paginated APIv3 collection', 3, 'projects', 'Project' + end + + describe 'representation formats' do + it_behaves_like 'has a link collection' do + let(:link) { 'representations' } + let(:hrefs) do + [ + { + 'href' => '/projects.csv?offset=1&pageSize=30', + 'identifier' => 'csv', + 'type' => 'text/csv', + 'title' => 'CSV' + }, + { + 'href' => '/projects.xls?offset=1&pageSize=30', + 'identifier' => 'xls', + 'type' => 'application/vnd.ms-excel', + 'title' => 'XLS' + } + ] + end + end end describe '.checked_permissions' do