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
[#44841] added requests specs
Eric Schubert33fadd2da337
diff --git a/app/contracts/oauth_clients/create_contract.rb b/app/contracts/oauth_clients/create_contract.rbindex 7e584f0abef..21a4e29235a 100644--- a/app/contracts/oauth_clients/create_contract.rb+++ b/app/contracts/oauth_clients/create_contract.rb@@ -29,7 +29,6 @@module OAuthClientsclass CreateContract < ::ModelContractinclude ActiveModel::Validations- include ActiveModel::Validationsattribute :client_id, writable: truevalidates :client_id, presence: true, length: { maximum: 255 }diff --git a/app/models/notification.rb b/app/models/notification.rbindex 1c85c8ec1e7..4dc7df059d2 100644--- a/app/models/notification.rb+++ b/app/models/notification.rb@@ -1,3 +1,31 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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.+#+++class Notification < ApplicationRecordREASONS = {mentioned: 0,diff --git a/app/services/oauth_clients/create_service.rb b/app/services/oauth_clients/create_service.rbindex 977443c7dde..bf4805b46be 100644--- a/app/services/oauth_clients/create_service.rb+++ b/app/services/oauth_clients/create_service.rb@@ -35,5 +35,12 @@# The comments here are also valid for the other *_service.rb filesmodule OAuthClientsclass CreateService < ::BaseServices::Create+ protected++ def before_perform(params, _service_result)+ OAuthClient.where(integration: params[:integration]).delete_all++ super+ endendenddiff --git a/docs/api/apiv3/components/schemas/oauth_application_read_model.yml b/docs/api/apiv3/components/schemas/oauth_application_read_model.ymlindex c806d1c2973..e3fe8766c6c 100644--- a/docs/api/apiv3/components/schemas/oauth_application_read_model.yml+++ b/docs/api/apiv3/components/schemas/oauth_application_read_model.yml@@ -4,8 +4,10 @@ type: objectrequired:- id- _type+ - name- clientId- confidential+ - _linkproperties:id:type: integer@@ -13,13 +15,16 @@ properties:_type:type: stringenum:- - OauthClientCredentials+ - OAuthApplication+ name:+ type: string+ description: The name of the OAuth 2 applicationclientId:type: stringdescription: OAuth 2 client idclientSecret:type: string- description: OAuth 2 client secret+ description: OAuth 2 client secret. This is only returned when creating a new OAuth application.confidential:type: booleandescription: true, if OAuth 2 credentials are confidential, false, if no secret is stored@@ -39,15 +44,58 @@ properties:_links:type: objectrequired:+ - self- owner- redirectUriproperties:+ self:+ allOf:+ - $ref: './link.yml'+ - description: |-+ This OAuth application++ **Resource**: OAuthApplicationowner:+ allOf:+ - $ref: './link.yml'+ - description: |-+ The user that created the OAuth application.++ **Resource**: Userintegration:+ allOf:+ - $ref: './link.yml'+ - description: |-+ The resource that integrates this OAuth application into itself. Currently, only `Storage` resources are+ able to create and maintain own OAuth application.++ **Resource**: StorageredirectUri:+ allOf:+ - $ref: './link.yml'+ - description: |-+ The redirect URI of the OAuth application++ **Resource**: N/Aexample:id: 1337_type: OauthClientCredentials+ name: Vader's secure OAuth appclientId: O5h6WObhMg1Z8IcLHRE3_LMh4jJYmmca2V6OTFSv8DAconfidential: true+ createdAt: 2022-12-07T12:56:42Z+ updatedAt: 2022-12-07T12:56:42Z+ scopes:+ - api_v3+ _links:+ self:+ href: '/api/v3/oauth_applications/1337'+ owner:+ href: '/api/v3/users/13'+ title: Darth Vader+ integration:+ href: '/api/v3/storages/42'+ title: Death Star Cloud+ redirectUri:+ href: 'https://death-star.cloud.tools/index.php/apps/integration_openproject/oauth-redirect'diff --git a/docs/api/apiv3/components/schemas/oauth_client_credentials_read_model.yml b/docs/api/apiv3/components/schemas/oauth_client_credentials_read_model.ymlindex 615804de2e0..deff6f9a183 100644--- a/docs/api/apiv3/components/schemas/oauth_client_credentials_read_model.yml+++ b/docs/api/apiv3/components/schemas/oauth_client_credentials_read_model.yml@@ -6,6 +6,7 @@ required:- _type- clientId- confidential+ - _linksproperties:id:type: integer@@ -13,16 +14,53 @@ properties:_type:type: stringenum:- - OauthClientCredentials+ - OAuthClientCredentialsclientId:type: stringdescription: OAuth 2 client idconfidential:type: booleandescription: true, if OAuth 2 credentials are confidential, false, if no secret is stored+ createdAt:+ type: string+ format: date-time+ description: The time the OAuth client credentials were created at+ updatedAt:+ type: string+ format: date-time+ description: The time the OAuth client credentials were last updated+ _links:+ type: object+ required:+ - self+ - integration+ properties:+ self:+ allOf:+ - $ref: './link.yml'+ - description: |-+ This OAuth Client Credentials object++ **Resource**: OAuthClientCredentials+ integration:+ allOf:+ - $ref: './link.yml'+ - description: |-+ The resource that integrates this OAuth client credentials. Currently, only `Storage` resources are+ able to contain OAuth client credentials.++ **Resource**: Storageexample:id: 1337- _type: OauthClientCredentials+ _type: OAuthClientCredentialsclientId: O5h6WObhMg1Z8IcLHRE3_LMh4jJYmmca2V6OTFSv8DAconfidential: true+ createdAt: 2022-12-07T12:56:42Z+ updatedAt: 2022-12-07T12:56:42Z+ _link:+ self:+ href: '/api/v3/oauth_client_credentials/1337'+ integration:+ href: '/api/v3/storages/42'+ title: Death Star Clouddiff --git a/docs/api/apiv3/components/schemas/storage_read_model.yml b/docs/api/apiv3/components/schemas/storage_read_model.ymlindex fb9dfaacf50..72ec31d0ebc 100644--- a/docs/api/apiv3/components/schemas/storage_read_model.yml+++ b/docs/api/apiv3/components/schemas/storage_read_model.yml@@ -32,7 +32,7 @@ properties:- oauthClientCredentialsproperties:oauthApplication:- $ref: './oauth_client_credentials_read_model.yml'+ $ref: './oauth_application_read_model.yml'oauthClientCredentials:$ref: './oauth_client_credentials_read_model.yml'_links:@@ -142,7 +142,7 @@ example:href: https://nextcloud.deathstar.rocks/authorize/title: AuthorizeoauthApplication:- href: /api/v3/oauth_credentials/42+ href: /api/v3/oauth_application/42title: It's no moon (Nextcloud)oauthClientCredentials:- href: /api/v3/oauth_credentials/42+ href: /api/v3/oauth_client_credentials/42diff --git a/docs/api/apiv3/components/schemas/storage_write_model.yml b/docs/api/apiv3/components/schemas/storage_write_model.ymlindex e8a2c004994..0d16c3141ff 100644--- a/docs/api/apiv3/components/schemas/storage_write_model.yml+++ b/docs/api/apiv3/components/schemas/storage_write_model.yml@@ -34,4 +34,4 @@ example:origin:href: https://nextcloud.deathstar.rockstype:- href: urn:openproject-org:api:v3:storages:nextcloud+ href: urn:openproject-org:api:v3:storages:Nextclouddiff --git a/docs/api/apiv3/openapi-spec.yml b/docs/api/apiv3/openapi-spec.ymlindex e711e147f16..c6ddf2cbbc2 100644--- a/docs/api/apiv3/openapi-spec.yml+++ b/docs/api/apiv3/openapi-spec.yml@@ -251,6 +251,8 @@ paths:"$ref": "./paths/notification_read.yml""/api/v3/notifications/{id}/unread_ian":"$ref": "./paths/notification_unread.yml"+ "/api/v3/oauth_applications/{id}":+ "$ref": "./paths/oauth_application.yml""/api/v3/oauth_client_credentials/{id}":"$ref": "./paths/oauth_client_credentials.yml""/api/v3/posts/{id}":diff --git a/docs/api/apiv3/paths/oauth_application.yml b/docs/api/apiv3/paths/oauth_application.ymlnew file mode 100644index 00000000000..ca5de24676e--- /dev/null+++ b/docs/api/apiv3/paths/oauth_application.yml@@ -0,0 +1,51 @@+# /api/v3/oauth_applications/{id}+---+get:+ summary: Get the oauth application.+ operationId: get_oauth_application+ tags:+ - OAuth 2+ description: |-+ Retrieves the OAuth 2 provider application for the given identifier. The secret will not be part of the response,+ instead a `confidential` flag is indicating, whether there is a secret or not.+ parameters:+ - name: id+ description: OAuth application id+ in: path+ required: true+ schema:+ type: integer+ example: 1337+ responses:+ '200':+ description: OK+ content:+ application/hal+json:+ schema:+ $ref: '../components/schemas/oauth_application_read_model.yml'+ '403':+ content:+ application/hal+json:+ schema:+ $ref: '../components/schemas/error_response.yml'+ examples:+ response:+ value:+ _type: Error+ errorIdentifier: urn:openproject-org:api:v3:errors:MissingPermission+ message: You are not authorized to access this resource.+ description: |-+ Returned if the client does not have sufficient permissions.++ **Required permission:** admin+ '404':+ content:+ application/hal+json:+ schema:+ $ref: '../components/schemas/error_response.yml'+ example:+ _type: Error+ errorIdentifier: urn:openproject-org:api:v3:errors:NotFound+ message: The requested resource could not be found.+ description: |-+ Returned if the application does not exist.diff --git a/docs/api/apiv3/paths/oauth_client_credentials.yml b/docs/api/apiv3/paths/oauth_client_credentials.ymlindex 0aa58b5717c..7cdbde3e357 100644--- a/docs/api/apiv3/paths/oauth_client_credentials.yml+++ b/docs/api/apiv3/paths/oauth_client_credentials.yml@@ -1,16 +1,16 @@# /api/v3/oauth_client_credentials/{id}---get:- summary: Get the oauth credentials object.- operationId: get_storage_oauth_client_credentials+ summary: Get the oauth client credentials object.+ operationId: get_oauth_client_credentialstags:- - File links+ - OAuth 2description: |-- Retrieves the OAuth 2 client credentials for the given storage. The secret will not be part of the response,+ Retrieves the OAuth 2 client credentials for the given identifier. The secret will not be part of the response,instead a `confidential` flag is indicating, whether there is a secret or not.parameters:- name: id- description: Storage id+ description: OAuth Client Credentials idin: pathrequired: trueschema:@@ -48,6 +48,4 @@ get:errorIdentifier: urn:openproject-org:api:v3:errors:NotFoundmessage: The requested resource could not be found.description: |-- Returned if the storage does not exist or the client does not have sufficient permissions to see it.-- **Required permission:** view file links+ Returned if the object does not exist.diff --git a/docs/api/apiv3/paths/storage.yml b/docs/api/apiv3/paths/storage.ymlindex 583e78fdd44..752fc552640 100644--- a/docs/api/apiv3/paths/storage.yml+++ b/docs/api/apiv3/paths/storage.yml@@ -37,7 +37,7 @@ get:**Required permission:** view file links-put:+patch:summary: Updates a storage.operationId: update_storagetags:@@ -70,12 +70,10 @@ put:application/hal+json:schema:$ref: '../components/schemas/error_response.yml'- examples:- response:- value:- _type: Error- errorIdentifier: urn:openproject-org:api:v3:errors:MissingPermission- message: You are not authorized to access this resource.+ example:+ _type: Error+ errorIdentifier: urn:openproject-org:api:v3:errors:MissingPermission+ message: You are not authorized to access this resource.description: |-Returned if the client does not have sufficient permissions.diff --git a/docs/api/apiv3/paths/storages.yml b/docs/api/apiv3/paths/storages.ymlindex 08ffa7883b3..658d9420bfc 100644--- a/docs/api/apiv3/paths/storages.yml+++ b/docs/api/apiv3/paths/storages.yml@@ -6,11 +6,15 @@ post:tags:- File linksdescription: |-- Creates a storage resource. When creating a storage, a confidential oauth provider is created. The oauth client id- and secret are returned in the response.+ Creates a storage resource. When creating a storage, a confidential OAuth 2 provider application is created+ automatically. The oauth client id and secret of the created OAuth application are returned in the response.**IMPORTANT:** This is the only time, the oauth client secret is visible to the consumer. After that, the secret ishidden.++ To update the storage with OAuth client credentials, which enable the storage resource to behave as an OAuth 2+ client against an external OAuth 2 provider application, another request must be made to create those, see+ `POST /api/v3/storages/{id}/oauth_client_credentials`.requestBody:content:application/json:@@ -22,4 +26,4 @@ post:content:application/hal+json:schema:- $ref: '../components/schemas/oauth_client_credentials_write_model.yml'+ $ref: '../components/schemas/storage_read_model.yml'diff --git a/docs/api/apiv3/tags/oauth.yml b/docs/api/apiv3/tags/oauth.ymlnew file mode 100644index 00000000000..b1883303e22--- /dev/null+++ b/docs/api/apiv3/tags/oauth.yml@@ -0,0 +1,4 @@+---+description: |-+ TBD+name: OAuth 2diff --git a/lib/api/v3/oauth/oauth_applications_api.rb b/lib/api/v3/oauth/oauth_applications_api.rbindex cc09f327a02..f1c388fa779 100644--- a/lib/api/v3/oauth/oauth_applications_api.rb+++ b/lib/api/v3/oauth/oauth_applications_api.rb@@ -31,6 +31,7 @@ module API::V3::OAuthresources :oauth_applications doroute_param :oauth_application_id, type: Integer, desc: 'OAuth application id' doafter_validation do+ authorize_admin@application = ::Doorkeeper::Application.find(params[:oauth_application_id])enddiff --git a/lib/api/v3/oauth/oauth_applications_representer.rb b/lib/api/v3/oauth/oauth_applications_representer.rbindex 1c21d83dd32..b5ae46676eb 100644--- a/lib/api/v3/oauth/oauth_applications_representer.rb+++ b/lib/api/v3/oauth/oauth_applications_representer.rb@@ -35,6 +35,8 @@ module API::V3::OAuthproperty :id+ property :name+property :uid, as: :clientIdproperty :confidential@@ -61,8 +63,15 @@ module API::V3::OAuthlink :integration donext if represented.integration.blank?+ href = case represented.integration+ when Storages::Storage+ api_v3_paths.storage(represented.integration.id)+ else+ raise ArgumentError, 'Invalid integration type.'+ end+{- href: api_v3_paths.storage(represented.integration.id),+ href:,title: represented.integration.name}enddiff --git a/lib/api/v3/oauth/oauth_client_credentials_api.rb b/lib/api/v3/oauth/oauth_client_credentials_api.rbindex 8faf67b11c2..d0824204724 100644--- a/lib/api/v3/oauth/oauth_client_credentials_api.rb+++ b/lib/api/v3/oauth/oauth_client_credentials_api.rb@@ -31,6 +31,7 @@ module API::V3::OAuthresources :oauth_client_credentials doroute_param :oauth_client_credentials_id, type: Integer, desc: 'OAuth Client Credentials id' doafter_validation do+ authorize_admin@oauth_client = ::OAuthClient.find(params[:oauth_client_credentials_id])enddiff --git a/lib/api/v3/oauth/oauth_client_credentials_representer.rb b/lib/api/v3/oauth/oauth_client_credentials_representer.rbindex 7055c69ebdb..6ef8b85a003 100644--- a/lib/api/v3/oauth/oauth_client_credentials_representer.rb+++ b/lib/api/v3/oauth/oauth_client_credentials_representer.rb@@ -29,6 +29,7 @@module API::V3::OAuthclass OAuthClientCredentialsRepresenter < ::API::Decorators::Singleinclude API::Decorators::LinkedResource+ include API::Decorators::DatePropertyself_link title: false@@ -42,6 +43,19 @@ module API::V3::OAuthproperty :confidential,getter: ->(*) { client_secret.present? }+ date_time_property :created_at++ date_time_property :updated_at++ link :integration do+ next if represented.integration.blank?++ {+ href: api_v3_paths.storage(represented.integration.id),+ title: represented.integration.name+ }+ end+def _type'OAuthClientCredentials'enddiff --git a/modules/storages/app/services/storages/storages/create_service.rb b/modules/storages/app/services/storages/storages/create_service.rbindex 38ac8635e8c..501536edb81 100644--- a/modules/storages/app/services/storages/storages/create_service.rb+++ b/modules/storages/app/services/storages/storages/create_service.rb@@ -45,6 +45,7 @@ module Storages::Storages# using values from storage (particularly :host) as defaultsif storage.provider_type == 'nextcloud'persist_service_result = ::Storages::OAuthApplications::CreateService.new(storage:, user:).call+ storage.oauth_application = persist_service_result.result if persist_service_result.success?service_call.add_dependent!(persist_service_result)enddiff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rbindex 1fa5773a9d6..dc40cd6913b 100644--- a/modules/storages/lib/api/v3/storages/storage_representer.rb+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb@@ -37,6 +37,14 @@ module API::V3::StoragesURN_STORAGE_TYPE_NEXTCLOUD = "#{::API::V3::URN_PREFIX}storages:Nextcloud".freeze+ STORAGE_TYPE_MAP = {+ URN_STORAGE_TYPE_NEXTCLOUD => Storages::Storage::PROVIDER_TYPE_NEXTCLOUD+ }.freeze++ STORAGE_TYPE_URN_MAP = {+ Storages::Storage::PROVIDER_TYPE_NEXTCLOUD => URN_STORAGE_TYPE_NEXTCLOUD+ }.freeze+class StorageRepresenter < ::API::Decorators::Single# LinkedResource module defines helper methods to describe attributesinclude API::Decorators::LinkedResource@@ -62,10 +70,9 @@ module API::V3::Storagesextend ClassMethods- def initialize(model, current_user:, embed_links: nil, created_oauth_application: nil)+ def initialize(model, current_user:, embed_links: nil)@connection_manager =::OAuthClients::ConnectionManager.new(user: current_user, oauth_client: model.oauth_client)- @oauth_application = created_oauth_application || model.oauth_applicationsuper(model, current_user:, embed_links:)end@@ -82,15 +89,15 @@ module API::V3::Storageslink_without_resource :type,getter: ->(*) {- { href: URN_STORAGE_TYPE_NEXTCLOUD, title: 'Nextcloud' }+ type = STORAGE_TYPE_MAP[represented.provider_type] || represented.provider_type++ { href: type, title: 'Nextcloud' }},setter: ->(fragment:, **) {- break if fragment['href'].blank?+ href = fragment['href']+ break if href.blank?- represented.provider_type = case fragment['href']- when URN_STORAGE_TYPE_NEXTCLOUD- Storages::Storage::PROVIDER_TYPE_NEXTCLOUD- end+ represented.provider_type = STORAGE_TYPE_URN_MAP[href] || href}link_without_resource :origin,@@ -129,12 +136,14 @@ module API::V3::Storagesassociated_resource :oauth_application,skip_render: ->(*) { !current_user.admin? },getter: ->(*) {- ::API::V3::OAuth::OAuthApplicationsRepresenter.create(@oauth_application, current_user:)+ ::API::V3::OAuth::OAuthApplicationsRepresenter.create(represented.oauth_application, current_user:)},link: ->(*) {+ next unless current_user.admin?+{- href: "/api/v3/oauth_applications/#{@oauth_application.id}",- title: @oauth_application.name+ href: api_v3_paths.oauth_application(represented.oauth_application.id),+ title: represented.oauth_application.name}}@@ -143,9 +152,11 @@ module API::V3::Storagesskip_render: ->(*) { !current_user.admin? || represented.oauth_client.blank? },representer: ::API::V3::OAuth::OAuthClientCredentialsRepresenter,link: ->(*) {+ next unless current_user.admin?+return { href: nil } if represented.oauth_client.blank?- { href: "/api/v3/oauth_client_credentials/#{represented.oauth_client.id}" }+ { href: api_v3_paths.oauth_client_credentials(represented.oauth_client.id) }}def _typediff --git a/modules/storages/lib/api/v3/storages/storages_api.rb b/modules/storages/lib/api/v3/storages/storages_api.rbindex e31eeeba057..b82bdcf5b09 100644--- a/modules/storages/lib/api/v3/storages/storages_api.rb+++ b/modules/storages/lib/api/v3/storages/storages_api.rb@@ -39,8 +39,8 @@ class API::V3::Storages::StoragesAPI < ::API::OpenProjectAPI# The `:resources` keyword defines the API namespace -> /api/v3/storages/...resources :storages do- post &::API::V3::Storages::CreateEndpoint.new(model: ::Storages::Storage).mount- # post &::API::V3::Utilities::Endpoints::Create.new(model: ::Storages::Storage).mount+ # post &::API::V3::Storages::CreateEndpoint.new(model: ::Storages::Storage).mount+ post &::API::V3::Utilities::Endpoints::Create.new(model: ::Storages::Storage).mount# `route_param` extends the route by a route parameter of the endpoint.# The input parameter value is parsed into the `:storage_id` symbol.diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rbindex b2c1857205a..65f09a40cad 100644--- a/modules/storages/lib/open_project/storages/engine.rb+++ b/modules/storages/lib/open_project/storages/engine.rb@@ -118,19 +118,19 @@ module OpenProject::Storagesendadd_api_path :storage do |storage_id|- "#{root}/storages/#{storage_id}"+ "#{storages}/#{storage_id}"endadd_api_path :storage_files do |storage_id|- "#{root}/storages/#{storage_id}/files"+ "#{storage(storage_id)}/files"endadd_api_path :prepare_upload do |storage_id|- "#{root}/storages/#{storage_id}/files/prepare_upload"+ "#{storage(storage_id)}/files/prepare_upload"endadd_api_path :storage_oauth_client_credentials do |storage_id|- "#{root}/storages/#{storage_id}/oauth_client_credentials"+ "#{storage(storage_id)}/oauth_client_credentials"endadd_api_path :file_links do |work_package_id|@@ -142,11 +142,11 @@ module OpenProject::Storagesendadd_api_path :file_link_download do |file_link_id|- "#{root}/file_links/#{file_link_id}/download"+ "#{file_link(file_link_id)}/download"endadd_api_path :file_link_open do |file_link_id, location = false|- "#{root}/file_links/#{file_link_id}/open#{location ? '?location=true' : ''}"+ "#{file_link(file_link_id)}/open#{location ? '?location=true' : ''}"end# Add api endpoints specific to this modulediff --git a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rbindex 615865f7189..58ab91e2211 100644--- a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb+++ b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb@@ -30,9 +30,10 @@ require 'spec_helper'describe ::API::V3::Storages::StorageRepresenter, 'rendering' dolet(:oauth_application) { build_stubbed(:oauth_application) }- let(:storage) { build_stubbed(:storage, oauth_application:) }+ let(:oauth_client_credentials) { build_stubbed(:oauth_client) }+ let(:storage) { build_stubbed(:storage, oauth_application:, oauth_client: oauth_client_credentials) }let(:user) { build_stubbed(:user) }- let(:representer) { described_class.new(storage, current_user: user) }+ let(:representer) { described_class.new(storage, current_user: user, embed_links: true) }let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }subject(:generated) { representer.to_json }@@ -71,17 +72,72 @@ describe ::API::V3::Storages::StorageRepresenter, 'rendering' doenddescribe 'oauthApplication' do- it_behaves_like 'has a titled link' do+ it_behaves_like 'has no link' dolet(:link) { 'oauthApplication' }- let(:href) { "/api/v3/oauth_applications/#{oauth_application.id}" }- let(:title) { oauth_application.name }+ end++ context 'as admin' do+ let(:user) { build_stubbed(:admin) }++ it_behaves_like 'has a titled link' do+ let(:link) { 'oauthApplication' }+ let(:href) { "/api/v3/oauth_applications/#{oauth_application.id}" }+ let(:title) { oauth_application.name }+ endendenddescribe 'oauthClientCredentials' do- it_behaves_like 'has an untitled link' do+ it_behaves_like 'has no link' dolet(:link) { 'oauthClientCredentials' }- let(:href) { nil }+ end++ context 'as admin' do+ let(:user) { build_stubbed(:admin) }++ it_behaves_like 'has an untitled link' do+ let(:link) { 'oauthClientCredentials' }+ let(:href) { "/api/v3/oauth_client_credentials/#{oauth_client_credentials.id}" }+ end+ end++ context 'as admin without oauth client credentials set' do+ let(:user) { build_stubbed(:admin) }+ let(:oauth_client_credentials) { nil }++ it_behaves_like 'has an untitled link' do+ let(:link) { 'oauthClientCredentials' }+ let(:href) { nil }+ end+ end+ end+ end++ describe '_embedded' do+ describe 'oauthApplication' do+ it { is_expected.not_to have_json_path('_embedded/oauthApplication') }++ context 'as admin' do+ let(:user) { build_stubbed(:admin) }++ it { is_expected.to be_json_eql(oauth_application.id).at_path('_embedded/oauthApplication/id') }+ end+ end++ describe 'oauthClientCredentials' do+ it { is_expected.not_to have_json_path('_embedded/oauthClientCredentials') }++ context 'as admin' do+ let(:user) { build_stubbed(:admin) }++ it { is_expected.to be_json_eql(oauth_client_credentials.id).at_path('_embedded/oauthClientCredentials/id') }+ end++ context 'as admin without oauth client credentials set' do+ let(:user) { build_stubbed(:admin) }+ let(:oauth_client_credentials) { nil }++ it { is_expected.not_to have_json_path('_embedded/oauthClientCredentials') }endendenddiff --git a/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rbnew file mode 100644index 00000000000..5125bc7157a--- /dev/null+++ b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb@@ -0,0 +1,224 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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_module_spec_helper++describe 'API v3 storage files', content_type: :json, webmock: true do+ include API::V3::Utilities::PathHelper+ include StorageServerHelpers++ let(:permissions) { %i(view_work_packages view_file_links) }+ let(:project) { create(:project) }++ let(:current_user) do+ create(:user, member_in_project: project, member_with_permissions: permissions)+ end++ let(:oauth_application) { create(:oauth_application) }+ let(:storage) { create(:storage, creator: current_user, oauth_application:) }+ let(:project_storage) { create(:project_storage, project:, storage:) }++ let(:authorize_url) { 'https://example.com/authorize' }+ let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }++ subject(:last_response) do+ get path+ end++ before do+ allow(connection_manager).to receive(:get_authorization_uri).and_return(authorize_url)+ allow(connection_manager).to receive(:authorization_state).and_return(:connected)+ allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)+ project_storage+ login_as current_user+ end++ describe 'GET /api/v3/storages/:storage_id/files' do+ let(:path) { api_v3_paths.storage_files(storage.id) }++ let(:files) do+ [+ Storages::StorageFile.new(1, 'new_younglings.md', 4096, 'plain/text', DateTime.now, DateTime.now,+ 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/'),+ Storages::StorageFile.new(2, 'holocron_inventory.md', 4096, 'plain/text', DateTime.now, DateTime.now,+ 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/')+ ]+ end++ describe 'with successful response' do+ before do+ storage_requests = instance_double(Storages::Peripherals::StorageRequests)+ files_query = Proc.new do+ ServiceResult.success(result: files)+ end+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.success(result: files_query))+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)+ end++ subject { last_response.body }++ it { is_expected.to be_json_eql(files.length.to_json).at_path('count') }+ it { is_expected.to be_json_eql(files[0].id.to_json).at_path('_embedded/elements/0/id') }+ it { is_expected.to be_json_eql(files[0].name.to_json).at_path('_embedded/elements/0/name') }+ it { is_expected.to be_json_eql(files[1].id.to_json).at_path('_embedded/elements/1/id') }+ it { is_expected.to be_json_eql(files[1].name.to_json).at_path('_embedded/elements/1/name') }+ end++ describe 'with files query creation failed' do+ let(:storage_requests) { instance_double(Storages::Peripherals::StorageRequests) }++ before do+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)+ end++ describe 'due to authorization failure' do+ before do+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :not_authorized))+ end++ it { expect(last_response.status).to be(500) }+ end++ describe 'due to internal error' do+ before do+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :error))+ end++ it { expect(last_response.status).to be(500) }+ end++ describe 'due to not found' do+ before do+ allow(storage_requests).to receive(:files_query).and_return(ServiceResult.failure(result: :not_found))+ end++ it { expect(last_response.status).to be(404) }+ end+ end++ describe 'with query failed' do+ let(:files_query) do+ Struct.new('FilesQuery', :error) do+ def query(_)+ ServiceResult.failure(result: error)+ end+ end.new(error)+ end++ before do+ storage_queries = instance_double(Storages::Peripherals::StorageInteraction::StorageQueries)+ allow(storage_queries).to receive(:files_query).and_return(ServiceResult.success(result: files_query))+ allow(Storages::Peripherals::StorageInteraction::StorageQueries).to receive(:new).and_return(storage_queries)+ end++ describe 'due to authorization failure' do+ let(:error) { :not_authorized }++ it { expect(last_response.status).to be(500) }+ end++ describe 'due to internal error' do+ let(:error) { :error }++ it { expect(last_response.status).to be(500) }+ end++ describe 'due to not found' do+ let(:error) { :not_found }++ it { expect(last_response.status).to be(404) }+ end+ end+ end++ describe 'POST /api/v3/storages/:storage_id/files/prepare_upload', with_flag: { storage_file_upload: true } do+ let(:path) { api_v3_paths.prepare_upload(storage.id) }+ let(:upload_link) { Storages::UploadLink.new('https://example.com/upload/xyz123') }+ let(:body) { { fileName: "ape.png", parent: "/Pictures" }.to_json }++ subject(:last_response) do+ post(path, body)+ end++ describe 'with successful response' do+ before do+ storage_requests = instance_double(Storages::Peripherals::StorageRequests)+ uplaod_link_query = Proc.new { ServiceResult.success(result: upload_link) }+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.success(result: uplaod_link_query))+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)+ end++ subject { last_response.body }++ it { is_expected.to be_json_eql(Storages::UploadLink.name.split('::').last.to_json).at_path('_type') }++ it do+ expect(subject)+ .to(be_json_eql("#{::API::V3::URN_PREFIX}storages:upload_link:no_link_provided".to_json)+ .at_path('_links/self/href'))+ end++ it { is_expected.to be_json_eql(upload_link.destination.to_json).at_path('_links/destination/href') }+ it { is_expected.to be_json_eql("post".to_json).at_path('_links/destination/method') }+ it { is_expected.to be_json_eql("Upload File".to_json).at_path('_links/destination/title') }+ end++ describe 'with files query creation failed' do+ let(:storage_requests) { instance_double(Storages::Peripherals::StorageRequests) }++ before do+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)+ end++ describe 'due to authorization failure' do+ before do+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :not_authorized))+ end++ it { expect(last_response.status).to be(500) }+ end++ describe 'due to internal error' do+ before do+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :error))+ end++ it { expect(last_response.status).to be(500) }+ end++ describe 'due to not found' do+ before do+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :not_found))+ end++ it { expect(last_response.status).to be(404) }+ end+ end+ end+enddiff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rbindex e020594e9a4..d1bd08c114b 100644--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb@@ -108,10 +108,6 @@ describe 'API v3 storages resource', content_type: :json, webmock: true doit_behaves_like 'successful response', 201- it { is_expected.to be_json_eql('Storage'.to_json).at_path('_type') }- it { is_expected.to be_json_eql(name.to_json).at_path('name') }- it { is_expected.to be_json_eql(host.to_json).at_path('_links/origin/href') }- it { is_expected.to be_json_eql(type.to_json).at_path('_links/type/href') }it { is_expected.to have_json_path('_embedded/oauthApplication/clientSecret') }end@@ -142,6 +138,8 @@ describe 'API v3 storages resource', content_type: :json, webmock: true docontext 'if user belongs to a project using the given storage' dolet!(:project_storage) { create(:project_storage, project:, storage:) }+ subject { last_response.body }+it_behaves_like 'successful storage response'context 'if user is missing permission view_file_links' do@@ -284,202 +282,6 @@ describe 'API v3 storages resource', content_type: :json, webmock: true doendend- describe 'GET /api/v3/storages/:storage_id/files' do- let(:path) { api_v3_paths.storage_files(storage.id) }-- let(:files) do- [- Storages::StorageFile.new(1, 'new_younglings.md', 4096, 'plain/text', DateTime.now, DateTime.now,- 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/'),- Storages::StorageFile.new(2, 'holocron_inventory.md', 4096, 'plain/text', DateTime.now, DateTime.now,- 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/')- ]- end-- describe 'with successful response' do- before do- storage_requests = instance_double(Storages::Peripherals::StorageRequests)- files_query = Proc.new do- ServiceResult.success(result: files)- end- allow(storage_requests).to receive(:files_query).and_return(ServiceResult.success(result: files_query))- allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)- end-- subject { last_response.body }-- it { is_expected.to be_json_eql(files.length.to_json).at_path('count') }- it { is_expected.to be_json_eql(files[0].id.to_json).at_path('_embedded/elements/0/id') }- it { is_expected.to be_json_eql(files[0].name.to_json).at_path('_embedded/elements/0/name') }- it { is_expected.to be_json_eql(files[1].id.to_json).at_path('_embedded/elements/1/id') }- it { is_expected.to be_json_eql(files[1].name.to_json).at_path('_embedded/elements/1/name') }- end-- describe 'with files query creation failed' do- let(:storage_requests) { instance_double(Storages::Peripherals::StorageRequests) }-- before do- allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)- end-- describe 'due to authorization failure' do- before do- allow(storage_requests).to receive(:files_query).and_return(- ServiceResult.failure(- result: :not_authorized,- errors: Storages::StorageError.new(code: :not_authorized)- )- )- end-- it { expect(last_response.status).to be(500) }- end-- describe 'due to internal error' do- before do- allow(storage_requests).to receive(:files_query).and_return(- ServiceResult.failure(- result: :error,- errors: Storages::StorageError.new(code: :error)- )- )- end-- it { expect(last_response.status).to be(500) }- end-- describe 'due to not found' do- before do- allow(storage_requests).to receive(:files_query).and_return(- ServiceResult.failure(- result: :not_found,- errors: Storages::StorageError.new(code: :not_found)- )- )- end-- it { expect(last_response.status).to be(404) }- end- end-- describe 'with query failed' do- let(:files_query) do- Struct.new('FilesQuery', :error) do- def query(_)- ServiceResult.failure(- result: error,- errors: Storages::StorageError.new(code: error)- )- end- end.new(error)- end-- before do- storage_queries = instance_double(Storages::Peripherals::StorageInteraction::StorageQueries)- allow(storage_queries).to receive(:files_query).and_return(ServiceResult.success(result: files_query))- allow(Storages::Peripherals::StorageInteraction::StorageQueries).to receive(:new).and_return(storage_queries)- end-- describe 'due to authorization failure' do- let(:error) { :not_authorized }-- it { expect(last_response.status).to be(500) }- end-- describe 'due to internal error' do- let(:error) { :error }-- it { expect(last_response.status).to be(500) }- end-- describe 'due to not found' do- let(:error) { :not_found }-- it { expect(last_response.status).to be(404) }- end- end- end-- describe 'POST /api/v3/storages/:storage_id/files/prepare_upload', with_flag: { storage_file_upload: true } do- let(:path) { api_v3_paths.prepare_upload(storage.id) }- let(:upload_link) { Storages::UploadLink.new('https://example.com/upload/xyz123') }- let(:body) { { fileName: "ape.png", parent: "/Pictures" }.to_json }-- subject(:last_response) do- post(path, body)- end-- describe 'with successful response' do- before do- storage_requests = instance_double(Storages::Peripherals::StorageRequests)- uplaod_link_query = Proc.new { ServiceResult.success(result: upload_link) }- allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.success(result: uplaod_link_query))- allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)- end-- subject { last_response.body }-- it { is_expected.to be_json_eql(Storages::UploadLink.name.split('::').last.to_json).at_path('_type') }-- it do- expect(subject)- .to(be_json_eql("#{::API::V3::URN_PREFIX}storages:upload_link:no_link_provided".to_json)- .at_path('_links/self/href'))- end-- it { is_expected.to be_json_eql(upload_link.destination.to_json).at_path('_links/destination/href') }- it { is_expected.to be_json_eql("post".to_json).at_path('_links/destination/method') }- it { is_expected.to be_json_eql("Upload File".to_json).at_path('_links/destination/title') }- end-- describe 'with files query creation failed' do- let(:storage_requests) { instance_double(Storages::Peripherals::StorageRequests) }-- before do- allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)- end-- describe 'due to authorization failure' do- before do- allow(storage_requests).to receive(:upload_link_query).and_return(- ServiceResult.failure(- result: :not_authorized,- errors: Storages::StorageError.new(code: :not_authorized)- )- )- end-- it { expect(last_response.status).to be(500) }- end-- describe 'due to internal error' do- before do- allow(storage_requests).to receive(:upload_link_query).and_return(- ServiceResult.failure(- result: :error,- errors: Storages::StorageError.new(code: :error)- )- )- end-- it { expect(last_response.status).to be(500) }- end-- describe 'due to not found' do- before do- allow(storage_requests).to receive(:upload_link_query).and_return(- ServiceResult.failure(- result: :not_found,- errors: Storages::StorageError.new(code: :not_found)- )- )- end-- it { expect(last_response.status).to be(404) }- end- end- end-describe 'POST /api/v3/storages/:storage_id/oauth_client_credentials' dolet(:path) { api_v3_paths.storage_oauth_client_credentials(storage.id) }let(:client_id) { 'myl1ttlecl13ntidii' }diff --git a/modules/storages/lib/api/v3/storages/create_endpoint.rb b/spec/requests/oauth/oauth_application_spec.rbsimilarity index 67%rename from modules/storages/lib/api/v3/storages/create_endpoint.rbrename to spec/requests/oauth/oauth_application_spec.rbindex 78952baa714..ea824f46f2a 100644--- a/modules/storages/lib/api/v3/storages/create_endpoint.rb+++ b/spec/requests/oauth/oauth_application_spec.rb@@ -26,16 +26,29 @@# See COPYRIGHT and LICENSE files for more details.#++-class API::V3::Storages::CreateEndpoint < API::Utilities::Endpoints::Create- include ::API::V3::Utilities::Endpoints::V3Deductions- include ::API::V3::Utilities::Endpoints::V3PresentSingle-- def present_success(request, service_call)- API::V3::Storages::StorageRepresenter.create(- service_call.result,- current_user: request.current_user,- embed_links: true,- created_oauth_application: service_call.dependent_results.first.result- )+require 'spec_helper'++describe 'API v3 oauth applications resource', content_type: :json do+ include API::V3::Utilities::PathHelper++ let(:current_user) { create(:admin) }+ let(:oauth_application) { create(:oauth_application) }++ before do+ login_as current_user++ get path+ end++ describe 'GET /api/v3/oauth_applications/:oauth_application_id' do+ let(:path) { api_v3_paths.oauth_application(oauth_application.id) }++ it_behaves_like 'successful response'++ context 'as non-admin' do+ let(:current_user) { create(:user) }++ it_behaves_like 'unauthorized access'+ endendenddiff --git a/spec/requests/oauth_clients/oauth_client_credentials_spec.rb b/spec/requests/oauth_clients/oauth_client_credentials_spec.rbnew file mode 100644index 00000000000..641fbb67fe4--- /dev/null+++ b/spec/requests/oauth_clients/oauth_client_credentials_spec.rb@@ -0,0 +1,54 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 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 'API v3 oauth applications resource', content_type: :json do+ include API::V3::Utilities::PathHelper++ let(:current_user) { create(:admin) }+ let(:oauth_client_credentials) { create(:oauth_client) }++ before do+ login_as current_user++ get path+ end++ describe 'GET /api/v3/oauth_client_credentials/:oauth_client_credentials_id' do+ let(:path) { api_v3_paths.oauth_client_credentials(oauth_client_credentials.id) }++ it_behaves_like 'successful response'++ context 'as non-admin' do+ let(:current_user) { create(:user) }++ it_behaves_like 'unauthorized access'+ end+ end+end