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] Fixed APITokens::CreateContract spec

Andreas Pfohl43483e19d45e
diff --git a/spec/contracts/api_tokens/create_contract_spec.rb b/spec/contracts/api_tokens/create_contract_spec.rb
index 7e15aabc850..c70d8e005f1 100644
--- a/spec/contracts/api_tokens/create_contract_spec.rb
+++ b/spec/contracts/api_tokens/create_contract_spec.rb
@@ -29,10 +29,45 @@
#++
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) }
+ let(:current_user) { create(:admin) }
+ let(:token_name) { "my token name" }
+ let(:token) { build_stubbed(:api_token, token_name:, user: current_user) }
+
+ subject(:contract) { described_class.new(token, current_user) }
+
+ 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
+
+ describe "validation" do
+ context "when token_name is set" do
+ it "is valid" do
+ expect_valid(true)
+ end
+ end
+
+ 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:, user: current_user)
+ end
+
+ it "is invalid" do
+ expect_valid(false, token_name: %i(taken))
+ end
+ end
end
end
diff --git a/spec/contracts/api_tokens/shared_contract_examples.rb b/spec/contracts/api_tokens/shared_contract_examples.rb
deleted file mode 100644
index e82376b3611..00000000000
--- a/spec/contracts/api_tokens/shared_contract_examples.rb
+++ /dev/null
@@ -1,73 +0,0 @@
-# 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