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
[64636] Prevent importing same token twice
Christophe Bliard520992f5e963
diff --git a/app/models/enterprise_token.rb b/app/models/enterprise_token.rbindex 4a37229cc5a..0bb570cdc18 100644--- a/app/models/enterprise_token.rb+++ b/app/models/enterprise_token.rb@@ -122,10 +122,12 @@ class EnterpriseToken < ApplicationRecordFAR_FUTURE_DATE = Date.new(9999, 1, 1)private_constant :FAR_FUTURE_DATE- validates :encoded_token, presence: true+ validates :encoded_token, presence: true,+ uniqueness: { message: I18n.t("activerecord.errors.models.enterprise_token.already_added") }validate :valid_token_objectvalidate :valid_domain+ before_validation :strip_encoded_tokenbefore_save :extract_validity_from_tokenbefore_save :clear_current_tokens_cachebefore_destroy :clear_current_tokens_cache@@ -226,6 +228,10 @@ class EnterpriseToken < ApplicationRecordprivate+ def strip_encoded_token+ self.encoded_token = encoded_token.strip if encoded_token.present?+ end+def load_token!@token_object = OpenProject::Token.import(encoded_token)rescue OpenProject::Token::ImportError => ediff --git a/config/locales/en.yml b/config/locales/en.ymlindex 1300091fcfc..c032fb672d9 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1350,6 +1350,7 @@ en:general_consent: "Please accept the terms and conditions."enterprise_token:unreadable: "can't be read. Are you sure it is a support token?"+ already_added: "This token has already been added."grids/grid:overlaps: "overlap."outside: "is outside of the grid."diff --git a/spec/features/admin/enterprise/enterprise_spec.rb b/spec/features/admin/enterprise/enterprise_spec.rbindex 048843fce33..6e0e8287f40 100644--- a/spec/features/admin/enterprise/enterprise_spec.rb+++ b/spec/features/admin/enterprise/enterprise_spec.rb@@ -34,18 +34,6 @@ RSpec.describe "Enterprise token", :js doinclude Redmine::I18nshared_let(:admin) { create(:admin) }- let(:token_object) do- OpenProject::Token.new.tap do |token|- token.subscriber = "Foobar"- token.mail = "[email protected]"- token.starts_at = Date.current- token.expires_at = nil- token.domain = Setting.host_name- end- end-- let(:textarea) { find_by_id "enterprise_token_encoded_token" }- let(:submit_button) { find_by_id "token-submit-button" }describe "EnterpriseToken management" dobefore do@@ -53,24 +41,40 @@ RSpec.describe "Enterprise token", :js dovisit enterprise_tokens_pathend- it "shows a teaser page and has a button to add a token" do+ it "shows a teaser page and has a button to add a token with a dialog" doexpect(page).to have_link("Start free trial")- # Try adding invalid enterprise token dataexpect(page).to have_button("Add Enterprise token")click_button "Add Enterprise token"expect(page).to have_dialog("Add Enterprise token")- fill_in "Type support token text", with: "foobar"- click_button "Add"+ expect(page).to have_field("Type support token text", type: "textarea")+ end- # The dialog is still open with an error message on token field- expect(page).to have_dialog("Add Enterprise token")- expect(page).to have_field("Type support token text",- validation_error: "Enterprise support token can't be read. Are you sure it is a support token?")+ context "with invalid input" do+ it "shows an error message" do+ click_button "Add Enterprise token"+ fill_in "Type support token text", with: "foobar"+ click_button "Add"++ # The dialog is still open with an error message on token field+ expect(page).to have_dialog("Add Enterprise token")+ validation_error = "Enterprise support token can't be read. Are you sure it is a support token?"+ expect(page).to have_field("Type support token text", validation_error:)+ endendcontext "with valid input" do+ let(:token_object) do+ OpenProject::Token.new.tap do |token|+ token.subscriber = "Foobar"+ token.mail = "[email protected]"+ token.starts_at = Date.current+ token.expires_at = nil+ token.domain = Setting.host_name+ end+ end+before doallow(OpenProject::Token).to receive(:import).and_return(token_object)end@@ -120,6 +124,32 @@ RSpec.describe "Enterprise token", :js doexpect_and_dismiss_flash(message: I18n.t(:notice_successful_delete))expect(EnterpriseToken.all).to be_emptyend++ it "cannot import same token twice" do+ click_button "Add Enterprise token"+ fill_in "Type support token text", with: "foobar"+ click_button "Add"++ expect_and_dismiss_flash(message: I18n.t(:notice_successful_update))++ click_button "Add Enterprise token"+ fill_in "Type support token text", with: "foobar"+ click_button "Add"++ # The dialog is still open with an error message on token field+ expect(page).to have_dialog("Add Enterprise token")+ validation_error = "This token has already been added."+ expect(page).to have_field("Type support token text", validation_error:)++ # Try importing with blank spaces and newlines before and after+ fill_in "Type support token text", with: " \nfoobar \n"+ click_button "Add"++ # The dialog is still open with an error message on token field+ expect(page).to have_dialog("Add Enterprise token")+ validation_error = "This token has already been added."+ expect(page).to have_field("Type support token text", validation_error:)+ endendendend