Skip to content

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

[48619] Added specs for api token services and contracts

Andreas Pfohlecaaf43be7b9
diff --git a/app/contracts/api_tokens/create_contract.rb b/app/contracts/api_tokens/create_contract.rb
index 48cc10c4c4a..e95a33640df 100644
--- a/app/contracts/api_tokens/create_contract.rb
+++ b/app/contracts/api_tokens/create_contract.rb
@@ -33,13 +33,17 @@ module APITokens
attribute :token_name
validates :token_name, presence: { message: I18n.t("my.access_token.errors.token_name_blank") }
- validate :token_name_is_unique
+ validate :token_name_is_unique, unless: :token_name_is_blank?
private
+ def token_name_is_blank?
+ token_name.blank?
+ end
+
def token_name_is_unique
if Token::API.where(user: model.user).any? { |t| t.token_name == model.token_name }
- errors.add(:token_name, I18n.t("my.access_token.errors.token_name_in_use"))
+ errors.add(:token_name, :taken, message: I18n.t("my.access_token.errors.token_name_in_use"))
end
end
end
diff --git a/spec/contracts/api_tokens/create_contract_spec.rb b/spec/contracts/api_tokens/create_contract_spec.rb
new file mode 100644
index 00000000000..7e15aabc850
--- /dev/null
+++ b/spec/contracts/api_tokens/create_contract_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 "shared_contract_examples"
+
+RSpec.describe APITokens::CreateContract do
+ it_behaves_like "api token contract" do
+ subject(:contract) { described_class.new(token, current_user) }
+ end
+end
diff --git a/spec/contracts/api_tokens/shared_contract_examples.rb b/spec/contracts/api_tokens/shared_contract_examples.rb
new file mode 100644
index 00000000000..e82376b3611
--- /dev/null
+++ b/spec/contracts/api_tokens/shared_contract_examples.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.shared_examples_for "api token contract" do
+ let(:current_user) { build_stubbed(:admin) }
+ let(:token_name) { "my token name" }
+ let(:token) { create(:api_token, token_name:) }
+
+ def expect_valid(valid, symbols = {})
+ expect(contract.validate).to eq(valid)
+
+ symbols.each do |key, arr|
+ expect(contract.errors.symbols_for(key)).to match_array arr
+ end
+ end
+
+ shared_examples "is valid" do
+ it "is valid" do
+ expect_valid(true)
+ end
+ end
+
+ describe "validation" do
+ it_behaves_like "is valid"
+
+ context "if the token_name is nil" do
+ let(:token_name) { nil }
+
+ it "is invalid" do
+ expect_valid(false, token_name: %i(blank))
+ end
+ end
+
+ context "if the token_name is taken for current user" do
+ before do
+ create(:api_token, token_name:)
+ end
+
+ it "is invalid" do
+ expect_valid(false, token_name: %i(taken))
+ end
+ end
+ end
+end
diff --git a/spec/factories/token_factory.rb b/spec/factories/token_factory.rb
index 51fd7e0d765..c8f194feae5 100644
--- a/spec/factories/token_factory.rb
+++ b/spec/factories/token_factory.rb
@@ -35,14 +35,7 @@ FactoryBot.define do
factory :api_token, class: "::Token::API" do
user
-
- transient do
- name { "Default Token Name" }
- end
-
- after(:build) do |token, evaluator|
- token.data = { name: evaluator.name }
- end
+ token_name { "my token name" }
end
factory :rss_token, class: "::Token::RSS" do
diff --git a/spec/services/api_tokens/create_service_spec.rb b/spec/services/api_tokens/create_service_spec.rb
new file mode 100644
index 00000000000..94c6ec3e25d
--- /dev/null
+++ b/spec/services/api_tokens/create_service_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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"
+
+RSpec.describe APITokens::CreateService, type: :model do
+ it_behaves_like "BaseServices create service" do
+ let(:model_class) { Token::API }
+ end
+end
diff --git a/spec/services/api_tokens/delete_service_spec.rb b/spec/services/api_tokens/delete_service_spec.rb
new file mode 100644
index 00000000000..425b9c51762
--- /dev/null
+++ b/spec/services/api_tokens/delete_service_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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"
+
+RSpec.describe APITokens::DeleteService, type: :model do
+ it_behaves_like "BaseServices delete service"
+end