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 "my" feature spec for api tokens

Andreas Pfohlfe0b946da04c
diff --git a/app/components/my/access_token/new_access_token_component.html.erb b/app/components/my/access_token/new_access_token_component.html.erb
index 0104f24f92f..e7eaae379dc 100644
--- a/app/components/my/access_token/new_access_token_component.html.erb
+++ b/app/components/my/access_token/new_access_token_component.html.erb
@@ -36,7 +36,7 @@ See COPYRIGHT and LICENSE files for more details.
size: :large
)
) do |dialog|
- dialog.with_show_button(scheme: :primary, mt: 3) do |button|
+ dialog.with_show_button(scheme: :primary, mt: 3, test_selector: "api-token-add") do |button|
button.with_leading_visual_icon(icon: :plus)
I18n.t("my.access_token.new_access_token_dialog_show_button_text")
end
diff --git a/app/components/my/access_token/new_access_token_form_component.html.erb b/app/components/my/access_token/new_access_token_form_component.html.erb
index d889ca9d157..90c080753f0 100644
--- a/app/components/my/access_token/new_access_token_form_component.html.erb
+++ b/app/components/my/access_token/new_access_token_form_component.html.erb
@@ -64,7 +64,7 @@ See COPYRIGHT and LICENSE files for more details.
I18n.t("button_cancel")
end
- footer.with_component(Primer::ButtonComponent.new(scheme: :primary, type: :submit)) do
+ footer.with_component(Primer::ButtonComponent.new(scheme: :primary, type: :submit, test_selector: "create-api-token-button")) do
I18n.t("my.access_token.new_access_token_dialog_submit_button_text")
end
end
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb
index 6a6f6c87d74..527891665ef 100644
--- a/app/controllers/my_controller.rb
+++ b/app/controllers/my_controller.rb
@@ -158,7 +158,7 @@ class MyController < ApplicationController
redirect_to action: "access_token"
end
- # Create a new API key
+ # rubocop:disable Metrics/AbcSize
def generate_api_key
result = APITokens::CreateService.new(user: current_user).call(token_name: params[:token_api][:token_name])
@@ -181,22 +181,28 @@ class MyController < ApplicationController
respond_with_turbo_streams
end
end
+ # rubocop:enable Metrics/AbcSize
+ # rubocop:disable Metrics/AbcSize
def revoke_api_key
result = APITokens::DeleteService.new(user: current_user, model: @api_token).call
+ # rubocop:disable Rails/ActionControllerFlashBeforeRender
result.on_success do
- flash.now[:info] = t("my.access_token.notice_api_token_revoked")
+ flash[:info] = t("my.access_token.notice_api_token_revoked")
end
result.on_failure do
Rails.logger.error "Failed to revoke api token ##{current_user.id}: #{e}"
- flash.now[:error] = t("my.access_token.failed_to_revoke_token", error: e.message)
+ flash[:error] = t("my.access_token.failed_to_revoke_token", error: e.message)
end
+ # rubocop:enable Rails/ActionControllerFlashBeforeRender
redirect_to action: "access_token"
end
+ # rubocop:enable Metrics/AbcSize
+
def revoke_ical_token
message = ical_destroy_info_message
@ical_token.destroy
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 820e337ee02..455e0403490 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -432,11 +432,11 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
token_name_blank: "Please provide an API token name"
token_name_in_use: "This API token name is already in use, please select a different one"
new_access_token_dialog_title: "Create new API token"
- new_access_token_dialog_show_button_text: "API Token"
+ new_access_token_dialog_show_button_text: "API token"
new_access_token_dialog_text_field_placeholder_text: "My API token"
new_access_token_dialog_text_field_label: "Name"
new_access_token_dialog_submit_button_text: "Create"
- new_access_token_dialog_text: "This token will allow third-party applications to communicate with your instance. To generate a new API token please introduce a token name."
+ new_access_token_dialog_text: "This token will allow third-party applications to communicate with your instance. To differentiate the new API token, please give it a name."
new_access_token_dialog_attention_text: "Treat API tokens like passwords. Anyone with this link will have access to information from this instance, share it only with trusted users."
failed_to_reset_token: "Failed to reset access token: %{error}"
failed_to_create_token: "Failed to create access token: %{error}"
diff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rb
index af11f256d7c..2f39f345cf6 100644
--- a/spec/controllers/my_controller_spec.rb
+++ b/spec/controllers/my_controller_spec.rb
@@ -353,18 +353,18 @@ RSpec.describe MyController do
it "revoke specific ical tokens" do
expect(user.ical_tokens).to contain_exactly(
- ical_token_for_query, another_ical_token_for_query, ical_token_for_another_query
- )
+ ical_token_for_query, another_ical_token_for_query, ical_token_for_another_query
+ )
delete :revoke_ical_token, params: { id: another_ical_token_for_query.id }
expect(user.ical_tokens.reload).to contain_exactly(
- ical_token_for_query, ical_token_for_another_query
- )
+ ical_token_for_query, ical_token_for_another_query
+ )
expect(user.ical_tokens.reload).not_to contain_exactly(
- ical_token_for_another_query
- )
+ ical_token_for_another_query
+ )
expect(flash[:info]).to be_present
expect(flash[:error]).not_to be_present
diff --git a/spec/features/users/my_spec.rb b/spec/features/users/my_spec.rb
index 53ff55f7994..0e309461ed2 100644
--- a/spec/features/users/my_spec.rb
+++ b/spec/features/users/my_spec.rb
@@ -152,14 +152,20 @@ RSpec.describe "my", :js, :with_cuprite do
find_test_selector("api-token-add").click
end
+ expect(page).to have_test_selector("new-access-token-dialog")
+
+ # create API token
+ fill_in "token_api[token_name]", with: "Testing Token"
+ find_test_selector("create-api-token-button").click
+
expect(page).to have_content "A new API token has been generated. Your access token is"
User.current.reload
visit my_access_token_path
- # only one API token can be created
+ # multiple API tokens can be created
within "#api-token-section" do
- expect(page).not_to have_test_selector("api-token-add", text: "API token")
+ expect(page).to have_test_selector("api-token-add", text: "API token")
end
# revoke API token