parent
5fa08c5b70
commit
c84980b257
@ -0,0 +1,56 @@ |
||||
#-- 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. |
||||
#++ |
||||
|
||||
module OAuthClients |
||||
class CreateContract < ::ModelContract |
||||
include ActiveModel::Validations |
||||
include ActiveModel::Validations |
||||
|
||||
attribute :client_id, writable: true |
||||
validates :client_id, presence: true, length: { maximum: 255 } |
||||
|
||||
attribute :client_secret, writable: true |
||||
validates :client_secret, presence: true, length: { maximum: 255 } |
||||
|
||||
attribute :integration_type, writable: true |
||||
validates :integration_type, presence: true |
||||
|
||||
attribute :integration_id, writable: true |
||||
validates :integration_id, presence: true |
||||
|
||||
validate :validate_user_allowed |
||||
|
||||
private |
||||
|
||||
def validate_user_allowed |
||||
unless user.admin? && user.active? |
||||
errors.add :base, :error_unauthorized |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,34 @@ |
||||
#-- 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. |
||||
#++ |
||||
|
||||
# See also: base_contract.rb for comments |
||||
module OAuthClients |
||||
class DeleteContract < ::DeleteContract |
||||
delete_permission :admin |
||||
end |
||||
end |
@ -0,0 +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 OAuthClient < ApplicationRecord |
||||
belongs_to :integration, polymorphic: true |
||||
end |
@ -0,0 +1,39 @@ |
||||
#-- 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. |
||||
#++ |
||||
|
||||
# The logic for creating storage was extracted from the controller and put into |
||||
# a service: https://dev.to/joker666/ruby-on-rails-pattern-service-objects-b19 |
||||
# Purpose: create and persist a Storages::Storage record |
||||
# Used by: Storages::Admin::StoragesController#create, could also be used by the |
||||
# API in the future. |
||||
# Reference: https://www.openproject.org/docs/development/concepts/contracted-services/ |
||||
# The comments here are also valid for the other *_service.rb files |
||||
module OAuthClients |
||||
class CreateService < ::BaseServices::Create |
||||
end |
||||
end |
@ -0,0 +1,33 @@ |
||||
#-- 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. |
||||
#++ |
||||
|
||||
# See also: create_service.rb for comments |
||||
module OAuthClients |
||||
class DeleteService < ::BaseServices::Delete |
||||
end |
||||
end |
@ -0,0 +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. |
||||
#++ |
||||
|
||||
# See also: create_service.rb for comments |
||||
class OAuthClients::SetAttributesService < ::BaseServices::SetAttributes |
||||
end |
@ -0,0 +1,40 @@ |
||||
#-- 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 CreateOAuthClient < ActiveRecord::Migration[6.1] |
||||
def change |
||||
create_table :oauth_clients do |t| |
||||
t.string :client_id, null: false |
||||
t.string :client_secret, null: false |
||||
t.references :integration, |
||||
polymorphic: true, index: { unique: true }, null: false |
||||
|
||||
t.timestamps |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,109 @@ |
||||
#-- 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 Storages::Admin::OAuthClientsController < ApplicationController |
||||
# See https://guides.rubyonrails.org/layouts_and_rendering.html for reference on layout |
||||
layout 'admin' |
||||
|
||||
# Before executing any action below: Make sure the current user is an admin |
||||
# and set the @<controller_name> variable to the object referenced in the URL. |
||||
before_action :require_admin |
||||
|
||||
before_action :find_storage |
||||
before_action :delete_current_oauth_client, only: %i[create] |
||||
|
||||
# menu_item is defined in the Redmine::MenuManager::MenuController |
||||
# module, included from ApplicationController. |
||||
# The menu item is defined in the engine.rb |
||||
menu_item :storages_admin_settings |
||||
|
||||
# Show the admin page to create a new OAuthClient object. |
||||
def new |
||||
@oauth_client = ::OAuthClients::SetAttributesService.new(user: User.current, |
||||
model: OAuthClient.new, |
||||
contract_class: EmptyContract) |
||||
.call |
||||
.result |
||||
render '/storages/admin/storages/new_oauth_client' |
||||
end |
||||
|
||||
# Actually create a OAuthClient object. |
||||
# Use service pattern to create a new OAuthClient |
||||
# See also: https://www.openproject.org/docs/development/concepts/contracted-services/ |
||||
# Called by: Global app/config/routes.rb to serve Web page |
||||
def create |
||||
service_result = ::OAuthClients::CreateService.new(user: User.current) |
||||
.call(permitted_oauth_client_params.merge(integration: @storage)) |
||||
@oauth_client = service_result.result |
||||
if service_result.success? |
||||
flash[:notice] = I18n.t(:notice_successful_create) |
||||
# admin_settings_storage_path is automagically created by Ruby routes. |
||||
redirect_to admin_settings_storage_path(@storage) |
||||
else |
||||
@errors = service_result.errors |
||||
render '/storages/admin/storages/new_oauth_client' |
||||
end |
||||
end |
||||
|
||||
# Used by: admin layout |
||||
# Breadcrumbs is something like OpenProject > Admin > Storages. |
||||
# This returns the name of the last part (Storages admin page) |
||||
def default_breadcrumb |
||||
ActionController::Base.helpers.link_to(t('storages.label_oauth_client_details'), admin_settings_storage_oauth_client_path) |
||||
end |
||||
|
||||
# See: default_breadcrumb above |
||||
# Defines whether to show breadcrumbs on the page or not. |
||||
def show_local_breadcrumb |
||||
true |
||||
end |
||||
|
||||
private |
||||
|
||||
def ensure_storages_module_active |
||||
return if OpenProject::FeatureDecisions.storages_module_active? |
||||
|
||||
raise ActionController::RoutingError, 'Not Found' |
||||
end |
||||
|
||||
# Called by create and update above in order to check if the |
||||
# update parameters are correctly set. |
||||
def permitted_oauth_client_params |
||||
params |
||||
.require(:oauth_client) |
||||
.permit('client_id', 'client_secret') |
||||
end |
||||
|
||||
def find_storage |
||||
@storage = ::Storages::Storage.find(params[:storage_id]) |
||||
end |
||||
|
||||
def delete_current_oauth_client |
||||
::OAuthClients::DeleteService.new(user: User.current, model: @storage.oauth_client).call if @storage.oauth_client |
||||
end |
||||
end |
@ -0,0 +1,38 @@ |
||||
<!-- Standard Ruby view, please see the controller for comments --> |
||||
<% html_title t(:label_administration), t("project_module_storages"), @storage.name, "#{t("storages.provider_types.#{@storage.provider_type}")} #{t("storages.label_oauth_client_details")}" %> |
||||
<% local_assigns[:additional_breadcrumb] = "#{t("storages.provider_types.#{@storage.provider_type}")} #{t("storages.label_oauth_client_details")}" %> |
||||
<%= toolbar title: "#{t("storages.provider_types.#{@storage.provider_type}")} #{t("storages.label_oauth_client_details")}" %> |
||||
|
||||
<%= labelled_tabular_form_for @oauth_client, url: admin_settings_storage_oauth_client_path do |f| -%> |
||||
<div class="form--field -required"> |
||||
<%= f.text_field :client_id, |
||||
label: t('storages.label_oauth_client_id'), |
||||
required: true, |
||||
size: 40, |
||||
container_class: '-wide' %> |
||||
<span class="form--field-instructions"> |
||||
<%= t("storages.instructions.#{@storage.provider_type}.oauth_client_id") %> |
||||
<%= link_to "#{t("storages.provider_types.#{@storage.provider_type}")} / #{t("storages.instructions.#{@storage.provider_type}.administration")} / #{t("storages.instructions.#{@storage.provider_type}.oauth2_clients")}", |
||||
URI::join(@storage.host, "settings/admin/security#oauth2").to_s, |
||||
target: "blank" %> |
||||
</span> |
||||
</div> |
||||
<div class="form--field -required"> |
||||
<%= f.text_field :client_secret, |
||||
label: t('storages.label_oauth_client_secret'), |
||||
required: true, |
||||
size: 40, |
||||
container_class: '-wide' %> |
||||
<span class="form--field-instructions"> |
||||
<%= t("storages.instructions.#{@storage.provider_type}.oauth_client_secret") %> |
||||
<%= link_to "#{t("storages.provider_types.#{@storage.provider_type}")} / #{t("storages.instructions.#{@storage.provider_type}.administration")} / #{t("storages.instructions.#{@storage.provider_type}.oauth2_clients")}", |
||||
URI::join(@storage.host, "settings/admin/security#oauth2").to_s, |
||||
target: "blank" %> |
||||
</span> |
||||
</div> |
||||
<% if @storage.oauth_client %> |
||||
<%= styled_button_tag t(:button_replace), class: "-highlight -with-icon icon-checkmark" %> |
||||
<% else %> |
||||
<%= styled_button_tag t(:button_save), class: "-highlight -with-icon icon-checkmark" %> |
||||
<% end %> |
||||
<% end %> |
@ -0,0 +1,87 @@ |
||||
#-- 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 |
||||
require 'contracts/shared/model_contract_shared_context' |
||||
|
||||
describe ::OAuthClients::CreateContract do |
||||
include_context 'ModelContract shared context' |
||||
|
||||
let(:current_user) { create(:admin) } |
||||
let(:client_id) { "1234567889" } |
||||
let(:client_secret) { "asdfasdfasdf" } |
||||
let(:integration) { build_stubbed :storage } |
||||
let(:oauth_client) do |
||||
build(:oauth_client, client_id:, client_secret:, integration:) |
||||
end |
||||
|
||||
let(:contract) { described_class.new(oauth_client, current_user) } |
||||
|
||||
it_behaves_like 'contract is valid for active admins and invalid for regular users' |
||||
|
||||
describe 'validations' do |
||||
context 'when all attributes are valid' do |
||||
include_examples 'contract is valid' |
||||
end |
||||
|
||||
%i[client_id client_secret].each do |attribute_name| |
||||
context 'when client_id is invalid' do |
||||
context 'as it is too long' do |
||||
let(attribute_name) { 'X' * 257 } |
||||
|
||||
include_examples 'contract is invalid', attribute_name => :too_long |
||||
end |
||||
|
||||
context 'as it is empty' do |
||||
let(attribute_name) { '' } |
||||
|
||||
include_examples 'contract is invalid', attribute_name => :blank |
||||
end |
||||
|
||||
context 'as it is nil' do |
||||
let(attribute_name) { nil } |
||||
|
||||
include_examples 'contract is invalid', attribute_name => :blank |
||||
end |
||||
end |
||||
end |
||||
|
||||
context 'with integration (polymorphic attribute) linked' do |
||||
let(:integration) { create :storage } |
||||
|
||||
include_examples 'contract is valid' |
||||
end |
||||
|
||||
context 'without integration (polymorphic attribute)' do |
||||
let(:integration) { nil } |
||||
|
||||
include_examples 'contract is invalid', { integration_id: :blank, integration_type: :blank } |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,43 @@ |
||||
#-- 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 |
||||
require 'contracts/shared/model_contract_shared_context' |
||||
|
||||
# This DeleteContract spec just tests if the user is _allowed_ |
||||
# to execute the operation. |
||||
describe ::OAuthClients::DeleteContract do |
||||
include_context 'ModelContract shared context' |
||||
|
||||
let(:oauth_client) { create :oauth_client } |
||||
let(:contract) { described_class.new(oauth_client, current_user) } |
||||
|
||||
# Generic checks that the contract is valid for valid admin, but invalid otherwise |
||||
it_behaves_like 'contract is valid for active admins and invalid for regular users' |
||||
end |
@ -0,0 +1,35 @@ |
||||
#-- 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. |
||||
#++ |
||||
|
||||
FactoryBot.define do |
||||
factory :oauth_client, class: '::OAuthClient' do |
||||
sequence(:client_id) { |n| "1234567890-#{n}" } |
||||
sequence(:client_secret) { |n| "2345678901-#{n}" } |
||||
integration factory: :storage |
||||
end |
||||
end |
@ -0,0 +1,36 @@ |
||||
#-- 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 'services/base_services/behaves_like_create_service' |
||||
|
||||
describe ::OAuthClients::CreateService, type: :model do |
||||
it_behaves_like 'BaseServices create service' do |
||||
let(:factory) { :oauth_client } |
||||
end |
||||
end |
@ -0,0 +1,36 @@ |
||||
#-- 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 'services/base_services/behaves_like_delete_service' |
||||
|
||||
describe ::OAuthClients::DeleteService, type: :model do |
||||
it_behaves_like 'BaseServices delete service' do |
||||
let(:factory) { :oauth_client } |
||||
end |
||||
end |
@ -0,0 +1,111 @@ |
||||
#-- 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 ::OAuthClients::SetAttributesService, type: :model do |
||||
let(:current_user) { build_stubbed(:admin) } |
||||
|
||||
let(:contract_instance) do |
||||
contract = instance_double(::OAuthClients::CreateContract, 'contract_instance') |
||||
allow(contract) |
||||
.to receive(:validate) |
||||
.and_return(contract_valid) |
||||
allow(contract) |
||||
.to receive(:errors) |
||||
.and_return(contract_errors) |
||||
contract |
||||
end |
||||
|
||||
let(:contract_errors) { instance_double(ActiveModel::Errors, 'contract_errors') } |
||||
let(:contract_valid) { true } |
||||
let(:model_valid) { true } |
||||
|
||||
let(:instance) do |
||||
described_class.new(user: current_user, model: model_instance, contract_class:, contract_options: {}) |
||||
end |
||||
let(:model_instance) { ::OAuthClient.new } |
||||
let(:contract_class) do |
||||
allow(::OAuthClients::CreateContract) |
||||
.to receive(:new) |
||||
.and_return(contract_instance) |
||||
|
||||
::OAuthClients::CreateContract |
||||
end |
||||
|
||||
let(:params) { {} } |
||||
|
||||
before do |
||||
allow(model_instance) |
||||
.to receive(:valid?) |
||||
.and_return(model_valid) |
||||
end |
||||
|
||||
subject { instance.call(params) } |
||||
|
||||
it 'returns the instance as the result' do |
||||
expect(subject.result) |
||||
.to eql model_instance |
||||
end |
||||
|
||||
it 'is a success' do |
||||
expect(subject) |
||||
.to be_success |
||||
end |
||||
|
||||
context 'with params' do |
||||
let(:params) do |
||||
{ |
||||
client_id: '0123456789-client_id', |
||||
client_secret: '1234567890-client_secret' |
||||
} |
||||
end |
||||
|
||||
before do |
||||
subject |
||||
end |
||||
|
||||
it 'assigns the params' do |
||||
expect(model_instance.client_id).to eq '0123456789-client_id' |
||||
expect(model_instance.client_secret).to eq '1234567890-client_secret' |
||||
end |
||||
end |
||||
|
||||
context 'with an invalid contract' do |
||||
let(:contract_valid) { false } |
||||
|
||||
it 'returns failure' do |
||||
expect(subject).not_to be_success |
||||
end |
||||
|
||||
it "returns the contract's errors" do |
||||
expect(subject.errors) |
||||
.to eql(contract_errors) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue