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
[64157] Handle multiple tokens
Oliver Günther4dc60db27cb2
diff --git a/app/components/admin/enterprise_tokens/create_dialog_component.html.erb b/app/components/admin/enterprise_tokens/create_dialog_component.html.erbnew file mode 100644index 00000000000..b77af480078--- /dev/null+++ b/app/components/admin/enterprise_tokens/create_dialog_component.html.erb@@ -0,0 +1,39 @@+<%=+ render(+ Primer::Alpha::Dialog.new(+ id: "new-enterprise-token-dialog",+ title: I18n.t("admin.enterprise.create_dialog.title"),+ size: :medium_portrait,+ data: { "keep-open-on-submit": true }+ )+ ) do |dialog|+ dialog.with_header(variant: :large)+ dialog.with_body do+ render(+ Admin::EnterpriseTokens::FormComponent.new(@token)+ )+ end++ dialog.with_footer do+ component_collection do |modal_footer|+ modal_footer.with_component(+ Primer::Beta::Button.new(+ data: { "close-dialog-id": "new-enterprise-token-dialog" }+ )+ ) do+ I18n.t(:button_cancel)+ end++ modal_footer.with_component(+ Primer::Beta::Button.new(+ scheme: :primary,+ form: "enterprise-token-form",+ type: :submit+ )+ ) do+ I18n.t(:button_create)+ end+ end+ end+ end+%>diff --git a/app/components/admin/enterprise_tokens/create_dialog_component.rb b/app/components/admin/enterprise_tokens/create_dialog_component.rbnew file mode 100644index 00000000000..f3c618312b3--- /dev/null+++ b/app/components/admin/enterprise_tokens/create_dialog_component.rb@@ -0,0 +1,44 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Admin::EnterpriseTokens+ class CreateDialogComponent < ApplicationComponent+ include ApplicationHelper+ include OpenProject::FormTagHelper+ include OpTurbo::Streamable+ include OpPrimer::ComponentHelpers++ def initialize(token)+ super++ @token = token+ end+ end+enddiff --git a/app/components/admin/enterprise_tokens/delete_dialog_component.html.erb b/app/components/admin/enterprise_tokens/delete_dialog_component.html.erbnew file mode 100644index 00000000000..52279d062a9--- /dev/null+++ b/app/components/admin/enterprise_tokens/delete_dialog_component.html.erb@@ -0,0 +1,49 @@+<%#-- copyright+OpenProject is an open source project management software.+Copyright (C) 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.++++#%>++<%=+ render(+ Primer::OpenProject::DangerDialog.new(+ id:,+ title:,+ confirm_button_text:,+ cancel_button_text:,+ form_arguments: {+ action: enterprise_token_path(@token),+ method: :delete,+ data: { turbo: true }+ }+ )+ ) do |dialog|+ dialog.with_confirmation_message do |message|+ message.with_heading(tag: :h2) { heading }+ message.with_description_content(confirmation_message)+ end+ end+%>diff --git a/app/components/admin/enterprise_tokens/delete_dialog_component.rb b/app/components/admin/enterprise_tokens/delete_dialog_component.rbnew file mode 100644index 00000000000..584ad526a0d--- /dev/null+++ b/app/components/admin/enterprise_tokens/delete_dialog_component.rb@@ -0,0 +1,66 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Admin::EnterpriseTokens+ class DeleteDialogComponent < ApplicationComponent+ include ApplicationHelper+ include OpTurbo::Streamable++ def initialize(token)+ super++ @token = token+ end++ private++ def id = "delete-enterprise-token-dialog"++ def title+ I18n.t("admin.enterprise.delete_dialog.title")+ end++ def heading+ I18n.t("admin.enterprise.delete_dialog.heading")+ end++ def confirmation_message+ I18n.t("admin.enterprise.delete_dialog.confirmation")+ end++ def confirm_button_text+ I18n.t("button_delete")+ end++ def cancel_button_text+ I18n.t("button_cancel")+ end+ end+enddiff --git a/app/components/admin/enterprise_tokens/form_component.html.erb b/app/components/admin/enterprise_tokens/form_component.html.erbnew file mode 100644index 00000000000..3e0fe5330a0--- /dev/null+++ b/app/components/admin/enterprise_tokens/form_component.html.erb@@ -0,0 +1,25 @@+<%=+ component_wrapper do+ primer_form_with(+ scope: :enterprise_token,+ model: @token,+ method: :post,+ url: enterprise_tokens_path,+ html: {+ id: "enterprise-token-form"+ }+ ) do |f|+ flex_layout(mb: 3) do |modal_body|+ if @token.errors[:base].present?+ modal_body.with_row do+ render(Primer::Alpha::Banner.new(mb: 3, icon: :stop, scheme: :danger)) { @token.errors[:base].join("\n") }+ end+ end++ modal_body.with_row(mb: 3) do+ render(EnterpriseTokens::TokenForm.new(f))+ end+ end+ end+ end+%>diff --git a/app/components/admin/enterprise_tokens/form_component.rb b/app/components/admin/enterprise_tokens/form_component.rbnew file mode 100644index 00000000000..54283810500--- /dev/null+++ b/app/components/admin/enterprise_tokens/form_component.rb@@ -0,0 +1,43 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Admin::EnterpriseTokens+ class FormComponent < ApplicationComponent+ include ApplicationHelper+ include OpTurbo::Streamable+ include OpPrimer::ComponentHelpers++ def initialize(token)+ super++ @token = token+ end+ end+enddiff --git a/app/components/admin/enterprise_tokens/row_component.rb b/app/components/admin/enterprise_tokens/row_component.rbnew file mode 100644index 00000000000..6577a98e926--- /dev/null+++ b/app/components/admin/enterprise_tokens/row_component.rb@@ -0,0 +1,94 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Admin::EnterpriseTokens+ class RowComponent < ::OpPrimer::BorderBoxRowComponent+ alias :token :model+ delegate :subscriber, :domain, to: :token++ def token+ model+ end++ def email+ token.mail+ end++ def plan+ helpers.enterprise_token_plan_name(token)+ end++ def active_users+ if token.restrictions.nil? || token.restrictions[:active_user_count].blank?+ I18n.t("js.admin.enterprise.upsell.unlimited")+ else+ token.restrictions[:active_user_count]+ end+ end++ def dates+ [+ helpers.format_date(token.starts_at),+ token.will_expire? ? helpers.format_date(token.expires_at) : I18n.t("js.admin.enterprise.upsell.unlimited")+ ].join(" – ")+ end++ def button_links+ [+ action_menu+ ]+ end++ def action_menu+ render(Primer::Alpha::ActionMenu.new) do |menu|+ menu.with_show_button(icon: "kebab-horizontal",+ "aria-label": t(:label_more),+ scheme: :invisible,+ data: {+ "test-selector": "more-button"+ })++ delete_action(menu)+ end+ end++ def delete_action(menu)+ menu.with_item(label: I18n.t(:button_delete),+ scheme: :danger,+ href: destroy_dialog_enterprise_token_path(token),+ tag: :a,+ content_arguments: {+ data: { controller: "async-dialog" }+ }) do |item|+ item.with_leading_visual_icon(icon: :trash)+ end+ end+ end+enddiff --git a/app/components/admin/enterprise_tokens/table_component.rb b/app/components/admin/enterprise_tokens/table_component.rbnew file mode 100644index 00000000000..c9e0c2d06ef--- /dev/null+++ b/app/components/admin/enterprise_tokens/table_component.rb@@ -0,0 +1,68 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Admin::EnterpriseTokens+ class TableComponent < ::OpPrimer::BorderBoxTableComponent+ columns :plan, :subscriber, :active_users, :email, :domain, :dates++ mobile_columns :plan, :subscriber, :active_users, :dates++ mobile_labels :project_name++ main_column :plan++ def sortable?+ false+ end++ def paginated?+ false+ end++ def has_actions?+ true+ end++ def mobile_title+ EnterpriseToken.model_name.plural+ end++ def headers+ @headers ||= [+ [:plan, { caption: EnterpriseToken.human_attribute_name(:plan) }],+ [:subscriber, { caption: EnterpriseToken.human_attribute_name(:subscriber) }],+ [:active_users, { caption: EnterpriseToken.human_attribute_name(:active_user_count_restriction) }],+ [:email, { caption: EnterpriseToken.human_attribute_name(:email) }],+ [:domain, { caption: EnterpriseToken.human_attribute_name(:domain) }],+ [:dates, { caption: I18n.t(:label_dates) }],+ ].compact+ end+ end+enddiff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprise_tokens_controller.rbsimilarity index 66%rename from app/controllers/enterprises_controller.rbrename to app/controllers/enterprise_tokens_controller.rbindex fe0d3dc483b..588d30c641b 100644--- a/app/controllers/enterprises_controller.rb+++ b/app/controllers/enterprise_tokens_controller.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -25,27 +27,35 @@## See COPYRIGHT and LICENSE files for more details.#++-class EnterprisesController < ApplicationController+class EnterpriseTokensController < ApplicationController+ include OpTurbo::DialogStreamHelper+ include OpTurbo::ComponentStream+layout "admin"menu_item :enterprisebefore_action :require_admin- before_action :check_user_limit, only: [:show]- before_action :check_domain, only: [:show]+ before_action :check_user_limit, only: [:index]+ before_action :find_token, only: %i[destroy destroy_dialog]++ def index+ # TODO: delete next line. @current_token is used in the old angular thingy.+ # It should not be needed anymore then.+ @current_token = EnterpriseToken.active_tokens.first+ end- def show- @current_token = EnterpriseToken.current- @token = @current_token || EnterpriseToken.new+ def new+ respond_with_dialog Admin::EnterpriseTokens::CreateDialogComponent.new(EnterpriseToken.new)enddef create # rubocop:disable Metrics/AbcSize- @token = EnterpriseToken.current || EnterpriseToken.new+ @token = EnterpriseToken.newsaved_encoded_token = @token.encoded_token@token.encoded_token = params[:enterprise_token][:encoded_token]if @token.saveflash[:notice] = t(:notice_successful_update)respond_to do |format|- format.html { redirect_to action: :show }+ format.html { redirect_to action: :index, status: :see_other }format.json { head :no_content }endelse@@ -55,24 +65,30 @@ class EnterprisesController < ApplicationController@current_token = @token || EnterpriseToken.newendrespond_to do |format|- format.html { render action: :show, status: :unprocessable_entity }+ format.html { render action: :index, status: :unprocessable_entity }format.json { render json: { description: @token.errors.full_messages.join(", ") }, status: :bad_request }+ format.turbo_stream do+ component = Admin::EnterpriseTokens::FormComponent.new(@token)+ update_via_turbo_stream(component: component)+ render turbo_stream: turbo_streams, status: :unprocessable_entity+ endendendend+ def destroy_dialog+ respond_with_dialog Admin::EnterpriseTokens::DeleteDialogComponent.new(@token)+ end+def destroy- token = EnterpriseToken.current- if token- token.destroy+ if @token.destroyflash[:notice] = t(:notice_successful_delete)-- delete_trial_key-- redirect_to action: :showelse- render_404+ flash[:error] = t(:error_failed_to_delete_entry)end++ delete_trial_key+ redirect_to action: :indexenddef save_trial_key@@ -85,6 +101,10 @@ class EnterprisesController < ApplicationControllerprivate+ def find_token+ EnterpriseToken.find(params[:id])+ end+def check_user_limitif OpenProject::Enterprise.user_limit_reached?flash.now[:warning] = I18n.t(@@ -94,14 +114,4 @@ class EnterprisesController < ApplicationController)endend-- def check_domain- if OpenProject::Enterprise.token.try(:invalid_domain?)- flash.now[:error] = I18n.t(- "error_enterprise_token_invalid_domain",- expected: Setting.host_name,- actual: OpenProject::Enterprise.token.domain- )- end- endenddiff --git a/app/forms/enterprise_tokens/token_form.rb b/app/forms/enterprise_tokens/token_form.rbnew file mode 100644index 00000000000..36e650bb3a4--- /dev/null+++ b/app/forms/enterprise_tokens/token_form.rb@@ -0,0 +1,42 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 EnterpriseTokens+ class TokenForm < ApplicationForm+ form do |f|+ f.text_area(+ name: :encoded_token,+ label: I18n.t("admin.enterprise.create_dialog.type_token_text"),+ placeholder: I18n.t("admin.enterprise.create_dialog.token_placeholder"),+ rows: 10,+ style: "resize: none"+ )+ end+ end+enddiff --git a/app/helpers/enterprise_helper.rb b/app/helpers/enterprise_helper.rbindex 6a429ce6330..ffc04cb7eb7 100644--- a/app/helpers/enterprise_helper.rb+++ b/app/helpers/enterprise_helper.rb@@ -31,9 +31,8 @@module EnterpriseHelperdef enterprise_angular_trial_inputstrial_key = Token::EnterpriseTrialKey.find_by(user_id: User.system.id)- token = EnterpriseToken.current- if token.present? || trial_key.blank?+ if EnterpriseToken.active? || trial_key.blank?enterprise_angular_static_inputselseenterprise_angular_static_inputs.merge(diff --git a/app/helpers/security_badge_helper.rb b/app/helpers/security_badge_helper.rbindex 780d1e4a1e0..1f4424b8edc 100644--- a/app/helpers/security_badge_helper.rb+++ b/app/helpers/security_badge_helper.rb@@ -35,7 +35,7 @@ module SecurityBadgeHelperversion: OpenProject::VERSION.to_semver,db: ActiveRecord::Base.connection.adapter_name.downcase,lang: User.current.try(:language),- ee: EnterpriseToken.current.present?+ ee: EnterpriseToken.active?}.merge(args.symbolize_keys)uri.query = info.to_queryuri.to_sdiff --git a/app/models/enterprise_token.rb b/app/models/enterprise_token.rbindex e5559fb4a5f..76bd8694d67 100644--- a/app/models/enterprise_token.rb+++ b/app/models/enterprise_token.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -27,40 +29,68 @@#++class EnterpriseToken < ApplicationRecordclass << self- def current- RequestStore.fetch(:current_ee_token) do- set_current_token+ def all_tokens+ all.sort_by(&:sort_key)+ end++ def active_tokens+ RequestStore.fetch(:current_ee_tokens) do+ set_active_tokensendend+ def active_non_trial_tokens+ active_tokens.reject(&:trial?)+ end++ def active_trial_tokens+ active_tokens.select(&:trial?)+ end+def table_exists?connection.data_source_exists? table_nameenddef allows_to?(feature)- Authorization::EnterpriseService.new(current).call(feature).result+ active_tokens.any? { |token| Authorization::EnterpriseService.new(token).call(feature).result }enddef active?- current && !current.expired?+ active_tokens.any?enddef available_features- EnterpriseToken.current&.available_features || []+ active_tokens.map(&:available_features).flatten.uniq+ end++ def non_trialling_features+ active_non_trial_tokens.map(&:available_features).flatten.uniqenddef trialling_features- available_features.select { |feature| trialling?(feature) }+ available_features - non_trialling_featuresenddef trialling?(feature)- allows_to?(feature) && EnterpriseToken.current.trial?+ trialling_features.include?(feature)enddef hide_banners?OpenProject::Configuration.ee_hide_banners?end+ def user_limit+ non_trial_user_limit.presence || trial_user_limit+ end++ def non_trial_user_limit+ active_non_trial_tokens.map { |token| Hash(token.restrictions)[:active_user_count] }.max+ end++ def trial_user_limit+ active_trial_tokens.map { |token| Hash(token.restrictions)[:active_user_count] }.max+ end+def banner_type_for(feature:)if !active?:no_token@@ -69,21 +99,27 @@ class EnterpriseToken < ApplicationRecordendend- def set_current_token- token = EnterpriseToken.order(Arel.sql("created_at DESC")).first+ def set_active_tokens+ EnterpriseToken+ .order(Arel.sql("created_at DESC"))+ .to_a+ .reject(&:expired?)+ end- if token&.token_object- token- end+ def clear_current_tokens_cache+ RequestStore.delete :current_ee_tokensendend+ FAR_FUTURE_DATE = Date.new(9999, 1, 1)+ private_constant :FAR_FUTURE_DATE+validates :encoded_token, presence: truevalidate :valid_token_objectvalidate :valid_domain- before_save :unset_current_token- before_destroy :unset_current_token+ before_save :clear_current_tokens_cache+ before_destroy :clear_current_tokens_cachedelegate :will_expire?,:subscriber,@@ -112,10 +148,7 @@ class EnterpriseToken < ApplicationRecordAuthorization::EnterpriseService.new(self).call(action).resultend- def unset_current_token- # Clear current cache- RequestStore.delete :current_ee_token- end+ delegate :clear_current_tokens_cache, to: :EnterpriseTokendef expired?(reprieve: true)token_object.expired?(reprieve:) || invalid_domain?@@ -129,6 +162,10 @@ class EnterpriseToken < ApplicationRecord!token_object.valid_domain?(Setting.host_name)end+ def sort_key+ [expires_at || FAR_FUTURE_DATE, starts_at || FAR_FUTURE_DATE]+ end+privatedef load_token!diff --git a/app/views/enterprises/_current.html.erb b/app/views/enterprise_tokens/_current.html.erbsimilarity index 96%rename from app/views/enterprises/_current.html.erbrename to app/views/enterprise_tokens/_current.html.erbindex 2bf626dc0c1..92680f74f30 100644--- a/app/views/enterprises/_current.html.erb+++ b/app/views/enterprise_tokens/_current.html.erb@@ -13,7 +13,7 @@</opce-enterprise-active-saved-trial><%= form_tag(- enterprise_path,+ enterprise_token_path(@current_token),method: :delete,data: augmented_confirmation_dialog(danger_zone: true,diff --git a/app/views/enterprises/_form.html.erb b/app/views/enterprise_tokens/_form.html.erbsimilarity index 85%rename from app/views/enterprises/_form.html.erbrename to app/views/enterprise_tokens/_form.html.erbindex 6998e736711..12fe979200d 100644--- a/app/views/enterprises/_form.html.erb+++ b/app/views/enterprise_tokens/_form.html.erb@@ -1,7 +1,7 @@<% title = @current_token.present? ? t('admin.enterprise.replace_token') : t('admin.enterprise.add_token') %><%= augmented_collapsible_section initiallyExpanded: !@current_token.present?,title: title do %>- <%= labelled_tabular_form_for @token,+ <%= labelled_tabular_form_for EnterpriseToken.new,url: { action: :create },html: { class: 'token-form form -vertical' },method: :post do |f| %>@@ -18,6 +18,6 @@<div class="form--space"></div><%= styled_button_tag t(:button_save), id: 'token-submit-button', class: '-primary -with-icon icon-checkmark' %>- <%= link_to t(:button_cancel), { action: :show }, class: 'button -with-icon icon-cancel' %>+ <%= link_to t(:button_cancel), { action: :index }, class: 'button -with-icon icon-cancel' %><% end %><% end %>diff --git a/app/views/enterprises/_info.html.erb b/app/views/enterprise_tokens/_info.html.erbsimilarity index 100%rename from app/views/enterprises/_info.html.erbrename to app/views/enterprise_tokens/_info.html.erbdiff --git a/app/views/enterprise_tokens/index.html.erb b/app/views/enterprise_tokens/index.html.erbnew file mode 100644index 00000000000..07140ac4f3e--- /dev/null+++ b/app/views/enterprise_tokens/index.html.erb@@ -0,0 +1,38 @@+<% html_title t(:label_administration), t("admin.enterprise.upgrade_to_ee") %>++<%=+ render(Primer::OpenProject::PageHeader.new) do |header|+ header.with_title { t(:label_enterprise_edition) }+ header.with_breadcrumbs(+ [{ href: admin_index_path, text: t(:label_administration) },+ t(:label_enterprise_edition)]+ )++ button_title = t("admin.enterprise.create_dialog.title")+ header.with_action_button(+ tag: :a,+ scheme: :primary,+ mobile_label: button_title,+ mobile_icon: :plus,+ size: :medium,+ href: new_enterprise_token_path,+ aria: { label: button_title },+ data: {+ turbo_stream: true+ },+ title: button_title+ ) do |button|+ button.with_leading_visual_icon(icon: :plus)+ button_title+ end+ end+%>++<%= error_messages_for "token" if @token %>++<% if @current_token.present? %>+ <%= render Admin::EnterpriseTokens::TableComponent.new(rows: EnterpriseToken.all_tokens) %>+ <%= render partial: "current" %>+<% else %>+ <%= render partial: "info" %>+<% end %>diff --git a/app/views/enterprises/show.html.erb b/app/views/enterprise_tokens/show.html.erbsimilarity index 91%rename from app/views/enterprises/show.html.erbrename to app/views/enterprise_tokens/show.html.erbindex a4f373a33b1..aedf31f755b 100644--- a/app/views/enterprises/show.html.erb+++ b/app/views/enterprise_tokens/show.html.erb@@ -10,7 +10,7 @@end%>-<%= error_messages_for "token" %>+<%= error_messages_for "token" if @token %><% if @current_token.present? %><%= render partial: "current" %>diff --git a/config/initializers/menus.rb b/config/initializers/menus.rbindex 497b954f6cf..17520582cd2 100644--- a/config/initializers/menus.rb+++ b/config/initializers/menus.rb@@ -560,7 +560,7 @@ Redmine::MenuManager.map :admin_menu do |menu|icon: "meter"menu.push :enterprise,- { controller: "/enterprises", action: :show },+ { controller: "/enterprise_tokens", action: :index },caption: :label_enterprise_edition,icon: "op-enterprise-addons",if: proc { User.current.admin? && OpenProject::Configuration.ee_manager_visible? }diff --git a/config/locales/en.yml b/config/locales/en.ymlindex e5c4f80ca85..ec4acfaa528 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -88,6 +88,14 @@ en:main-menu-bg-color: "Left side menu's background color."theme_warning: Changing the theme will overwrite you custom style. The design will then be lost. Are you sure you want to continue?enterprise:+ delete_dialog:+ title: "Delete enterprise token"+ heading: "Delete this enterprise token?"+ confirmation: "Are you sure you want to delete this Enterprise edition support token?"+ create_dialog:+ title: "Add Enterprise token"+ type_token_text: "Type support token text"+ token_placeholder: "Paste your Enterprise edition support token here"upgrade_to_ee: "Upgrade to the Enterprise edition"add_token: "Upload an Enterprise edition support token"delete_token_modal:@@ -986,6 +994,7 @@ en:domain: "Domain"starts_at: "Valid since"subscriber: "Subscriber"+ plan: "Plan"encoded_token: "Enterprise support token"active_user_count_restriction: "Maximum active users"grids/grid:@@ -1594,6 +1603,9 @@ en:custom_action: "Custom action"custom_field: "Custom field""doorkeeper/application": "OAuth application"+ enterprise_token:+ one: "Enterprise token"+ other: "Enterprise tokens"forum: "Forum"global_role: "Global role"group: "Group"@@ -2685,6 +2697,7 @@ en:label_dashboard: "Dashboard"label_database_version: "PostgreSQL version"label_date: "Date"+ label_dates: "Dates"label_date_and_time: "Date and time"label_date_format: "Date format"label_date_from: "From"diff --git a/config/routes.rb b/config/routes.rbindex 4698a4846de..8c44afb5e3d 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -477,10 +477,15 @@ Rails.application.routes.draw doscope "admin" doresource :announcements, only: %i[edit update]constraints(Constraints::Enterprise) do- resource :enterprise, only: %i[show create destroy]- scope controller: "enterprises" do- post "enterprise/save_trial_key" => "enterprises#save_trial_key"- delete "enterprise/delete_trial_key" => "enterprises#delete_trial_key"+ resources :enterprise_tokens, only: %i[index show new create destroy] do+ member do+ get :destroy_dialog+ end++ collection do+ post :save_trial_key+ delete :delete_trial_key+ endendenddiff --git a/lib/api/v3/configuration/configuration_representer.rb b/lib/api/v3/configuration/configuration_representer.rbindex ce33491e3cc..027dec8fc66 100644--- a/lib/api/v3/configuration/configuration_representer.rb+++ b/lib/api/v3/configuration/configuration_representer.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbHdiff --git a/lib/open_project/enterprise.rb b/lib/open_project/enterprise.rbindex dd0ec0b050e..d9e34957abd 100644--- a/lib/open_project/enterprise.rb+++ b/lib/open_project/enterprise.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -29,20 +31,14 @@module OpenProjectmodule Enterpriseclass << self- def token- EnterpriseToken.current.presence- end-def upgrade_url"#{Setting.protocol}://#{Setting.host_name}#{upgrade_path}"end- def upgrade_path- url_helpers.enterprise_path- end+ delegate :user_limit, to: EnterpriseToken- def user_limit- Hash(token.restrictions)[:active_user_count] if token+ def upgrade_path+ url_helpers.enterprise_tokens_pathenddef active_user_countdiff --git a/spec/controllers/custom_styles_controller_spec.rb b/spec/controllers/custom_styles_controller_spec.rbindex f6438b071e3..dac72bf7936 100644--- a/spec/controllers/custom_styles_controller_spec.rb+++ b/spec/controllers/custom_styles_controller_spec.rb@@ -50,7 +50,7 @@ RSpec.describe CustomStylesController docontext "when no active token exists" dobefore do- allow(EnterpriseToken).to receive(:current).and_return(nil)+ allow(EnterpriseToken).to receive(:active_tokens).and_return([])endit "redirects to #upsell" dodiff --git a/spec/models/enterprise_token_spec.rb b/spec/models/enterprise_token_spec.rbindex c2e121627bf..f47dbc984a0 100644--- a/spec/models/enterprise_token_spec.rb+++ b/spec/models/enterprise_token_spec.rb@@ -31,26 +31,41 @@require "spec_helper"RSpec.describe EnterpriseToken do- let(:object) { OpenProject::Token.new domain: Setting.host_name }- let(:ee_hide_banners) { true }+ before do+ described_class.clear_current_tokens_cache- subject { described_class.new(encoded_token: "foo") }+ # Calls are mocked in mock_token_object for enterprise tokens created by+ # tests. This line is to call normal implementation when not mocked.+ allow(OpenProject::Token).to receive(:import).and_call_original+ end- before do- RequestStore.delete :current_ee_token- allow(OpenProject::Configuration).to receive(:ee_hide_banners?).and_return(ee_hide_banners)+ def create_enterprise_token(encoded_token_name, **attributes)+ mock_token_object(encoded_token_name, **attributes)+ enterprise_token = described_class.new(encoded_token: encoded_token_name)+ enterprise_token.save!(validate: false)+ enterprise_token+ end++ def mock_token_object(encoded_token_name, **attributes)+ token = OpenProject::Token.new(domain: Setting.host_name,+ expires_at: 1.year.from_now,+ **attributes)+ allow(OpenProject::Token)+ .to receive(:import).with(encoded_token_name)+ .and_return(token)+ tokenenddescribe ".active?" do- before do- allow(described_class).to receive(:current).and_return(subject)- allow(described_class.current).to receive(:token_object).and_return(object)- subject.save!(validate: false)+ context "without any tokens" do+ it "returns false" do+ expect(described_class.active?).to be(false)+ endendcontext "with a non expired token" dobefore do- allow(object).to receive(:expired?).and_return(false)+ create_enterprise_token("an_non_expired_token", expires_at: 1.year.from_now)endit "returns true" do@@ -60,27 +75,39 @@ RSpec.describe EnterpriseToken docontext "with an expired token" dobefore do- allow(object).to receive(:expired?).and_return(true)+ create_enterprise_token(subject.encoded_token, expires_at: Date.yesterday)endit "returns false" doexpect(described_class.active?).to be(false)endend++ context "with two tokens: one expired and one active" do+ before do+ # expired token+ create_enterprise_token("an_expired_token", expires_at: Date.yesterday)++ # active token+ create_enterprise_token("an_active_token", expires_at: 1.year.from_now)+ end++ it "returns true" do+ expect(described_class.active?).to be(true)+ end+ endenddescribe ".hide_banners?" do- context "when ee manager is visible" do- let(:ee_hide_banners) { true }-+ context "when ee_hide_banners is true",+ with_config: { ee_hide_banners: true } doit "returns true" doexpect(described_class).to be_hide_bannersendend- context "when ee manager is not visible" do- let(:ee_hide_banners) { false }-+ context "when ee_hide_banners is false",+ with_config: { ee_hide_banners: false } doit "returns false" doexpect(described_class).not_to be_hide_bannersend@@ -124,41 +151,40 @@ RSpec.describe EnterpriseToken doendend- describe "existing token" do- before do- allow_any_instance_of(described_class).to receive(:token_object).and_return(object) # rubocop:disable RSpec/AnyInstance- subject.save!(validate: false)- end-+ context "with an existing token" docontext "when inner token is active" do- it "has an active token" do- allow(object).to receive(:expired?).and_return(false)+ subject! do+ mock_token_object(+ "an_active_token_object",+ subscriber: "foo",+ mail: "[email protected]",+ starts_at: Date.current,+ issued_at: Date.current,+ expires_at: nil,+ restrictions: { foo: :bar }+ )+ described_class.create!(encoded_token: "an_active_token_object")+ end++ it "is returned by .active_tokens" doexpect(described_class.count).to eq(1)- expect(described_class.current).to eq(subject)- expect(described_class.current.encoded_token).to eq("foo")+ expect(described_class.active_tokens).to eq([subject])+ active_token = described_class.active_tokens.first+ expect(active_token.encoded_token).to eq("an_active_token_object")- # Deleting it updates the current token- described_class.current.destroy!+ # Deleting it updates the active tokens list+ active_token.destroy!expect(described_class.count).to eq(0)- expect(described_class.current).to be_nil+ expect(described_class.active_tokens).to be_emptyend- it "delegates to the token object" do- allow(object).to receive_messages(- subscriber: "foo",- mail: "bar",- starts_at: Time.zone.today,- issued_at: Time.zone.today,- expires_at: "never",- restrictions: { foo: :bar }- )-+ it "delegates calls to the inner token object" doexpect(subject.subscriber).to eq("foo")- expect(subject.mail).to eq("bar")- expect(subject.starts_at).to eq(Time.zone.today)- expect(subject.issued_at).to eq(Time.zone.today)- expect(subject.expires_at).to eq("never")+ expect(subject.mail).to eq("[email protected]")+ expect(subject.starts_at).to eq(Date.current)+ expect(subject.issued_at).to eq(Date.current)+ expect(subject.expires_at).to be_nilexpect(subject.restrictions).to eq(foo: :bar)end@@ -188,42 +214,200 @@ RSpec.describe EnterpriseToken doendend- context "when inner token is expired" do- before do- allow(object).to receive(:expired?).and_return(true)+ context "when updated with an invalid token" do+ subject! { create_enterprise_token("an_active_token_object", expires_at: 1.year.from_now) }++ it "fails validations" do+ expect { subject.encoded_token = "bar" }+ .to change(subject, :valid?).from(true).to(false)end+ end+ end++ describe ".all_tokens" do+ it "returns all tokens, ordered from oldest expiration date to latest (non expiring ones are last)" do+ create_enterprise_token("a_token_expired_recently", expires_at: Date.yesterday)+ create_enterprise_token("a_token_expiring_soon", expires_at: Date.tomorrow)+ create_enterprise_token("a_token_without_an_expiration_date", expires_at: nil)+ create_enterprise_token("a_token_expired_since_one_year", expires_at: Date.current - 1.year)+ create_enterprise_token("a_token_expiring_in_one_year", expires_at: Date.current + 1.year)++ expect(described_class.all_tokens.map(&:encoded_token))+ .to eq(%w[+ a_token_expired_since_one_year+ a_token_expired_recently+ a_token_expiring_soon+ a_token_expiring_in_one_year+ a_token_without_an_expiration_date+ ])+ end- it "has an expired token" do- expect(described_class.current).to eq(subject)- expect(described_class).not_to be_active+ it "sorts by token start date if multiple tokens have the same expiration date" do+ create_enterprise_token("a_token_started_one_week_ago", starts_at: Date.current - 1.week, expires_at: Date.current + 1.year)+ create_enterprise_token("a_token_starting_in_one_week", starts_at: Date.current + 1.week, expires_at: Date.current + 1.year)+ create_enterprise_token("a_token_started_one_year_ago", starts_at: Date.current - 1.year, expires_at: Date.current + 1.year)+ create_enterprise_token("a_non_expiring_token_starting_in_one_month", starts_at: Date.current + 1.month, expires_at: nil)+ create_enterprise_token("a_non_expiring_token_started_today", starts_at: Date.current, expires_at: nil)+ create_enterprise_token("a_non_expiring_token_started_one_month_ago", starts_at: Date.current - 1.month, expires_at: nil)++ expect(described_class.all_tokens.map(&:encoded_token))+ .to eq(%w[+ a_token_started_one_year_ago+ a_token_started_one_week_ago+ a_token_starting_in_one_week+ a_non_expiring_token_started_one_month_ago+ a_non_expiring_token_started_today+ a_non_expiring_token_starting_in_one_month+ ])+ end+ end++ describe ".active_tokens" do+ context "with no tokens" do+ it "returns an empty array" do+ expect(described_class.active_tokens).to be_emptyendend- context "when updating it with an invalid token" do- it "fails validations" do- subject.encoded_token = "bar"- expect(subject.save).to be_falsey+ context "with an active token" do+ let!(:active_token) { create_enterprise_token("an_active_token", expires_at: 1.year.from_now) }++ it "returns the active token" do+ expect(described_class.active_tokens).to eq([active_token])+ end+ end++ context "with a trial token" do+ let!(:trial_token) { create_enterprise_token("a_trial_token", trial: true, expires_at: 1.year.from_now) }++ it "returns the trial token" do+ expect(described_class.active_tokens).to eq([trial_token])+ end+ end++ context "with expired and invalid tokens" do+ let!(:expired_token) { create_enterprise_token("an_expired_token", expires_at: Date.yesterday) }+ let!(:expired_trial_token) { create_enterprise_token("an_expired_trial_token", trial: true, expires_at: Date.yesterday) }+ let!(:invalid_token) { create_enterprise_token("an_invalid_token_with_wrong_domain", domain: "wrong.domain") }++ it "returns an empty array" do+ expect(described_class.active_tokens).to be_emptyendendend- describe "no token" do- it do- expect(described_class.current).to be_nil- expect(described_class).not_to be_active+ describe ".available_features" do+ context "with no tokens" do+ it "returns an empty array" do+ expect(described_class.available_features).to be_empty+ end+ end++ context "with an active token" do+ let!(:active_token) { create_enterprise_token("an_active_token", plan: :basic, expires_at: 1.year.from_now) }++ it "returns the features for the plan of the token" do+ expect(described_class.available_features).to eq(OpenProject::Token::FEATURES_PER_PLAN[:basic])+ end+ end++ context "with a trial token" do+ let!(:trial_token) { create_enterprise_token("a_trial_token", plan: :basic, trial: true, expires_at: 1.year.from_now) }++ it "returns the features for the plan of the token" do+ expect(described_class.available_features).to eq(OpenProject::Token::FEATURES_PER_PLAN[:basic])+ end+ end++ context "with expired and invalid tokens" do+ let!(:expired_token) { create_enterprise_token("an_expired_token", plan: :basic, expires_at: Date.yesterday) }+ let!(:expired_trial_token) do+ create_enterprise_token("an_expired_trial_token", plan: :basic, trial: true, expires_at: Date.yesterday)+ end+ let!(:invalid_token) { create_enterprise_token("an_invalid_token_with_wrong_domain", plan: :basic, domain: "wrong.domain") }++ it "returns an empty array" do+ expect(described_class.available_features).to be_empty+ endendend- describe "invalid token" do- it "appears as if no token is shown" do- expect(described_class.current).to be_nil- expect(described_class).not_to be_active+ describe ".trialling?(feature)" do+ # basic < professional < premium < corporate+ let(:basic_plan_feature) { OpenProject::Token::BASIC_PLAN_FEATURES.first }+ let(:professional_plan_feature) { OpenProject::Token::PROFESSIONAL_PLAN_FEATURES.first }+ let(:premium_plan_feature) { OpenProject::Token::PREMIUM_PLAN_FEATURES.first }+ let(:corporate_plan_feature) { OpenProject::Token::CORPORATE_PLAN_FEATURES.first }++ context "with no tokens" do+ it "returns false" do+ expect(described_class.trialling?(basic_plan_feature)).to be false+ expect(described_class.trialling?(premium_plan_feature)).to be false+ end+ end++ context "with only an active non-trial token" do+ let!(:token) { create_enterprise_token(plan: :basic) }++ it "returns false" do+ expect(described_class.trialling?(basic_plan_feature)).to be false+ expect(described_class.trialling?(premium_plan_feature)).to be false+ end+ end++ context "with only a trial token" do+ let!(:trial_token) { create_enterprise_token(plan: :basic, trial: true) }++ it "returns true for features included in the token plan" do+ expect(described_class.trialling?(basic_plan_feature)).to be true+ expect(described_class.trialling?(premium_plan_feature)).to be false+ end+ end++ context "with a basic non-trial token and a premium trial token" do+ let!(:token) { create_enterprise_token("a_token_for_basic_plan", plan: :basic) }+ let!(:trial_token) { create_enterprise_token("a_trial_token_for_premium_plan", plan: :premium, trial: true) }++ it "returns false for features included in non-trial plan and true for features only included in trial plan" do+ expect(described_class.trialling?(basic_plan_feature)).to be false+ expect(described_class.trialling?(professional_plan_feature)).to be true+ expect(described_class.trialling?(premium_plan_feature)).to be true+ expect(described_class.trialling?(corporate_plan_feature)).to be false+ end+ end++ context "with a premium non-trial token and a basic trial token" do+ let!(:token) { create_enterprise_token("a_token_for_premium_plan", plan: :premium) }+ let!(:trial_token) { create_enterprise_token("a_trial_token_for_basic_plan", plan: :basic, trial: true) }++ it "returns false for any features as trial basic features are included in non-trial premium plan" do+ expect(described_class.trialling?(basic_plan_feature)).to be false+ expect(described_class.trialling?(professional_plan_feature)).to be false+ expect(described_class.trialling?(premium_plan_feature)).to be false+ expect(described_class.trialling?(corporate_plan_feature)).to be false+ end+ end++ context "with expired and invalid tokens" do+ let!(:expired_trial_token) do+ create_enterprise_token("an_expired_trial_token", plan: :premium, trial: true, expires_at: Date.yesterday)+ end+ let!(:invalid_trial_token) do+ create_enterprise_token("an_invalid_trial_token_with_wrong_domain", plan: :basic, trial: true, domain: "wrong.domain")+ end++ it "returns an empty array" do+ expect(described_class.trialling?(basic_plan_feature)).to be false+ expect(described_class.trialling?(professional_plan_feature)).to be false+ expect(described_class.trialling?(premium_plan_feature)).to be false+ expect(described_class.trialling?(corporate_plan_feature)).to be false+ endendend- describe "Configuration file has `ee_hide_banners` set to false" do- it "does not show banners promoting EE" do- allow(OpenProject::Configuration).to receive(:ee_hide_banners?).and_return(false)+ context "when Configuration file has `ee_hide_banners` set to false",+ with_config: { ee_hide_banners: false } do+ it "shows banners promoting Enterprise plans" doexpect(described_class).not_to be_hide_bannersendenddiff --git a/spec/requests/admin/token_domain_spec.rb b/spec/requests/admin/enterprise_tokens_spec.rbsimilarity index 84%rename from spec/requests/admin/token_domain_spec.rbrename to spec/requests/admin/enterprise_tokens_spec.rbindex 3cb24dc0bdd..b3d894e3ade 100644--- a/spec/requests/admin/token_domain_spec.rb+++ b/spec/requests/admin/enterprise_tokens_spec.rb@@ -35,7 +35,6 @@ RSpec.describe "Enterprise Edition token domain",let(:current_user) { create(:admin) }let(:valid_token) { Rails.root.join("spec/fixtures/ee_tokens/v2_1_user_test_host.token").read }let(:invalid_token) { Rails.root.join("spec/fixtures/ee_tokens/v2_1_user_localhost_3001.token").read }- let(:ee_token) { valid_token }before dologin_as(current_user)@@ -43,7 +42,7 @@ RSpec.describe "Enterprise Edition token domain",shared_context "when uploading a token" dobefore do- post "/admin/enterprise",+ post "/admin/enterprise_tokens",params: {"enterprise_token[encoded_token]": ee_token}@@ -52,10 +51,12 @@ RSpec.describe "Enterprise Edition token domain",describe "initial upload" docontext "with correct domain" do+ let(:ee_token) { valid_token }+it_behaves_like "when uploading a token" doit "saves the token" doexpect(response).to be_redirect- expect(EnterpriseToken.current).to be_present+ expect(EnterpriseToken.all_tokens).to be_presentendendend@@ -71,7 +72,7 @@ RSpec.describe "Enterprise Edition token domain",endend- context "with an active token" do+ context "with an existing inactive invalid token" dolet!(:token) doobj = EnterpriseToken.new(encoded_token: invalid_token, created_at: 5.days.ago, updated_at: 5.days.ago)obj.save!(validate: false)@@ -80,10 +81,13 @@ RSpec.describe "Enterprise Edition token domain",describe "replacing the token" docontext "with correct domain" do+ let(:ee_token) { valid_token }+it_behaves_like "when uploading a token" do- it "updates the token" do+ it "adds the token" doexpect(response).to be_redirect- expect(EnterpriseToken.current.updated_at.to_date).to eq(Time.zone.today)+ expect(EnterpriseToken.all_tokens.count).to eq(2)+ expect(EnterpriseToken.active_tokens.count).to eq(1)endendend@@ -92,9 +96,13 @@ RSpec.describe "Enterprise Edition token domain",it_behaves_like "when uploading a token" dolet(:ee_token) { invalid_token }- it "shows an invalid domain error", :aggregate_failures do+ it "shows an invalid domain error" doexpect(page).to have_text "Domain is invalid."end++ it "does not save it" do+ expect(EnterpriseToken.all_tokens.count).to eq(1)+ endendendenddiff --git a/spec/routing/enterprise_routing_spec.rb b/spec/routing/enterprise_routing_spec.rbindex 0531e5901a0..d7de97ece34 100644--- a/spec/routing/enterprise_routing_spec.rb+++ b/spec/routing/enterprise_routing_spec.rb@@ -30,23 +30,23 @@require "spec_helper"-RSpec.describe EnterprisesController do+RSpec.describe EnterpriseTokensController docontext "when `ee_manager_visible`" do- it "connects GET /admin/enterprise to enterprises#show" do- allow(OpenProject::Configuration).to receive(:ee_manager_visible?).and_return(true)- expect(get("/admin/enterprise")).to route_to(controller: "enterprises",- action: "show")+ it "GET /admin/enterprise_tokens routes to enterprise_tokens#index",+ with_config: { ee_manager_visible: true } do+ expect(get("/admin/enterprise_tokens")).to route_to(controller: "enterprise_tokens",+ action: "index")endendcontext "when NOT `ee_manager_visible`" do- it "GET /admin/enterprise should not route to enterprise#show" do- # With such a configuration and in case a token is present, the might be a- # good reason not to reveal the enterpise token to the admin.- # Think of cloud solutions for instance.- allow(OpenProject::Configuration).to receive(:ee_manager_visible?).and_return(false)- expect(get("/admin/enterprise")).not_to route_to(controller: "enterprises",- action: "show")+ it "GET /admin/enterprise_tokens should not route to enterprise_tokens#index",+ with_config: { ee_manager_visible: false } do+ # With such a configuration and in case a token is present, there might be+ # a good reason not to reveal the enterprise token to the admin. Think of+ # cloud solutions for instance.+ expect(get("/admin/enterprise_tokens")).not_to route_to(controller: "enterprise_tokens",+ action: "index")endendenddiff --git a/spec/support/shared/with_ee.rb b/spec/support/shared/with_ee.rbindex 4c2689238f0..a436f58c3db 100644--- a/spec/support/shared/with_ee.rb+++ b/spec/support/shared/with_ee.rb@@ -50,7 +50,7 @@ RSpec.configure do |config|if allowed.present? || example.metadata[:with_ee_trial]allowed = aggregate_parent_array(example, allowed.to_set)- token_double = instance_double(EnterpriseToken)+ enterprise_token_double = instance_double(EnterpriseToken)token_object_double = instance_double(OpenProject::Token)allow(EnterpriseToken).to receive(:allows_to?).and_call_originalallow(token_object_double).to receive(:has_feature?).and_return(false)@@ -60,8 +60,8 @@ RSpec.configure do |config|end# Also signal available features- allow(EnterpriseToken).to receive(:current).and_return(token_double)- allow(token_double)+ allow(EnterpriseToken).to receive(:active_tokens).and_return([enterprise_token_double])+ allow(enterprise_token_double).to receive_messages(token_object: token_object_double,available_features: allowed.to_a,expired?: false,diff --git a/spec/views/layouts/base.html.erb_spec.rb b/spec/views/layouts/base.html.erb_spec.rbindex d00c22f02fc..b3cd26a4760 100644--- a/spec/views/layouts/base.html.erb_spec.rb+++ b/spec/views/layouts/base.html.erb_spec.rb@@ -211,11 +211,8 @@ RSpec.describe "layouts/base" doendend- context "EE is active and styles are not present" do+ context "when an Enterprise token is active and styles are not present", with_ee: %i[define_custom_style] dobefore do- allow(EnterpriseToken).to receive(:current).and_return(a_token)- allow(a_token).to receive(:expired?).and_return(false)- allow(a_token).to receive(:allows_to?).with(:define_custom_style).and_return(true)allow(CustomStyle).to receive(:current).and_return(nil)render@@ -226,12 +223,8 @@ RSpec.describe "layouts/base" doendend- context "EE does not allow custom styles" do+ context "when an Enterprise token is active but does not allow custom styles", with_ee: %i[] dobefore do- allow(EnterpriseToken).to receive(:current).and_return(a_token)- allow(a_token).to receive(:expired?).and_return(false)- allow(a_token).to receive(:allows_to?).with(:define_custom_style).and_return(false)-renderend@@ -240,10 +233,8 @@ RSpec.describe "layouts/base" doendend- context "no EE present" do+ context "when there are no Enterprise tokens" dobefore do- allow(EnterpriseToken).to receive(:current).and_return(nil)-renderend