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
added specs for ical token management and added translations
Jonas Jabari5736f48cd407
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rbindex c6b504036ab..f58d71a2488 100644--- a/app/controllers/my_controller.rb+++ b/app/controllers/my_controller.rb@@ -131,6 +131,7 @@ class MyController < ApplicationControllerdef revoke_all_ical_tokensToken::Ical.where(user: current_user).destroy_all+ flash[:info] = t('my.access_token.notice_ical_tokens_reverted')rescue StandardError => eRails.logger.error "Failed to revoke all ical tokens for ##{current_user.id}: #{e}"flash[:error] = t('my.access_token.failed_to_reset_token', error: e.message)diff --git a/app/views/my/access_token.html.erb b/app/views/my/access_token.html.erbindex f760fc58184..ac7198b593f 100644--- a/app/views/my/access_token.html.erb+++ b/app/views/my/access_token.html.erb@@ -111,15 +111,15 @@ See COPYRIGHT and LICENSE files for more details.<% end %><% if Token::Ical.where(user: current_user).any? %><tr>- <td>iCal</td>+ <td><%= t(:label_ical_access_key_type) %></td><td><span title="<%= format_time(Token::Ical.where(user: current_user).last.created_at) %>">- <%= format_time(Token::Ical.where(user: current_user).last.created_at.to_s) %> (latest)+ <%= format_time(Token::Ical.where(user: current_user).last.created_at.to_s) %> (<%= t(:label_ical_access_key_latest) %>)</span></td><td><%= I18n.t('my_account.access_tokens.indefinite_expiration') %></td><td>- <%= link_to 'Revoke all',+ <%= link_to t(:label_ical_access_key_revoke_all),{ action: 'revoke_all_ical_tokens' },method: :delete,class: 'icon icon-delete' %>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 5f297afa886..efad73a0f45 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -300,6 +300,7 @@ en:notice_reset_token: "A new %{type} token has been generated. Your access token is:"token_value_warning: "Note: This is the only time you will see this token, make sure to copy it now."no_results_title_text: There are currently no access tokens available.+ notice_ical_tokens_reverted: All iCalendar tokens have been revoked. All previously used iCalendar URLs are now invaild.news:index:@@ -1560,6 +1561,9 @@ en:label_api_access_key: "API access key"label_api_access_key_created_on: "API access key created %{value} ago"label_api_access_key_type: "API"+ label_ical_access_key_type: "iCalendar"+ label_ical_access_key_latest: "latest"+ label_ical_access_key_revoke_all: "Revoke all"label_applied_status: "Applied status"label_archive_project: "Archive project"label_ascending: "Ascending"diff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rbindex 16571b5ad89..dbf5956ca00 100644--- a/spec/controllers/my_controller_spec.rb+++ b/spec/controllers/my_controller_spec.rb@@ -295,5 +295,31 @@ describe MyController doendendend++ describe 'ical' do+ # unlike with the other tokens, creating new ical tokens is not done in this context+ # ical tokens are generated whenever the user requests a new ical url+ # a user can have N ical tokens+ #+ # in this context all ical tokens of a user should be reverted at once+ # this invalidates all previously generated ical urls, which is the intention+ context 'with existing keys' do+ let!(:key_1) { Token::Ical.create user: }+ let!(:key_2) { Token::Ical.create user: }++ it 'revokes all existing keys' do+ expect(Token::Ical.where(user_id: user.id)).to contain_exactly(key_1, key_2)++ post :revoke_all_ical_tokens++ expect(Token::Ical.where(user_id: user.id).count).to eq(0)++ expect(flash[:info]).to be_present+ expect(flash[:error]).not_to be_present++ expect(response).to redirect_to action: :access_token+ end+ end+ endendenddiff --git a/spec/features/users/my_spec.rb b/spec/features/users/my_spec.rbindex f208c6ab96b..a34134193bd 100644--- a/spec/features/users/my_spec.rb+++ b/spec/features/users/my_spec.rb@@ -151,5 +151,50 @@ describe 'my',expect(page).not_to have_content 'Missing RSS access key'end++ it 'in Access Tokens they can see if Ical tokens exists' do+ visit my_access_token_path+ expect(page).not_to have_content 'iCalendar'++ new_ical_token = Token::Ical.create user: user++ visit my_access_token_path+ expect(page).to have_content 'iCalendar'++ within(:xpath, "//tr[contains(.,'iCalendar')]") do+ expect(page).to have_content "#{::I18n.l(new_ical_token.created_at, format: :time)} (latest)"+ end++ Timecop.travel(1.minutes.from_now) do+ another_new_ical_token = Token::Ical.create user: user++ visit my_access_token_path+ expect(page).to have_content 'iCalendar'++ within(:xpath, "//tr[contains(.,'iCalendar')]") do+ expect(page).not_to have_content "#{::I18n.l(new_ical_token.created_at, format: :time)} (latest)"+ expect(page).to have_content "#{::I18n.l(another_new_ical_token.created_at, format: :time)} (latest)"+ end+ end+ end++ it 'in Access Tokens they can revoke all existing Ical tokens' do+ new_ical_token_1 = Token::Ical.create user: user+ new_ical_token_2 = Token::Ical.create user: user++ visit my_access_token_path+ within(:xpath, "//tr[contains(.,'iCalendar')]") do+ expect(page).to have_content "Revoke all"+ end++ find(:xpath, "//tr[contains(.,'iCalendar')]/td/a", text: 'Revoke all').click++ expect(page).to have_content 'All iCalendar tokens have been revoked.'++ expect(page).not_to have_xpath "//tr[contains(.,'iCalendar')]"++ expect(Token::Ical.where(user_id: user.id).count).to eq 0+ end+endenddiff --git a/spec/models/token/ical_token_spec.rb b/spec/models/token/ical_token_spec.rbnew file mode 100644index 00000000000..444e488633f--- /dev/null+++ b/spec/models/token/ical_token_spec.rb@@ -0,0 +1,96 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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'++describe Token::Ical do+ let(:user) { build(:user) }++ subject { described_class.new user: }++ describe 'behaves like Token::HashedToken' do++ it 'inherits from Token::HashedToken' do+ expect(described_class).to be < Token::HashedToken+ end++ # following code is copy pasted from hashed_token_spec+ # in order to make sure the token behaves in the same way in it's basics+ # cheching for inheritance does not safely check if the basic behaviour is the same+ # is there a better way of reusing the specs from hashed_token_spec?+ describe 'token value' do+ it 'is generated on a new instance' do+ expect(subject.value).to be_present+ end++ it 'provides the generated plain value on a new instance' do+ expect(subject.valid_plaintext?(subject.plain_value)).to be true+ end++ it 'hashes the plain value to value' do+ expect(subject.value).not_to eq(subject.plain_value)+ end++ it 'does not keep the value when finding it' do+ subject.save!++ instance = described_class.where(user:).last+ expect(instance.plain_value).to be_nil+ end+ end++ describe '#find_by_plaintext_value' do+ before do+ subject.save!+ end++ it 'finds using the plaintext value' do+ expect(described_class.find_by_plaintext_value(subject.plain_value)).to eq subject+ expect(described_class.find_by_plaintext_value('foobar')).to be_nil+ end+ end++ end++ describe 'in contrast to HashedToken' do++ it 'a user can have N ical tokens' do+ # Every time an ical url is generated, a new ical token will be generated for this url as well+ # the existing ical tokens (and thus urls) should still be valid+ # until the user decides to revert all existing ical tokens (and urls)+ # therefore a user needs to be allowed to have N ical token+ ical_token_1 = described_class.create user: user+ ical_token_2 = described_class.create user: user++ expect(Token::Ical.where(user_id: user.id)).to contain_exactly(+ ical_token_1, ical_token_2+ )+ end++ end+enddiff --git a/spec/routing/my_spec.rb b/spec/routing/my_spec.rbindex 02da9cc0d93..c7043fa1b6e 100644--- a/spec/routing/my_spec.rb+++ b/spec/routing/my_spec.rb@@ -65,4 +65,8 @@ describe 'my routes' doexpect(get('/my/deletion_info')).to route_to(controller: 'users',action: 'deletion_info')end++ it '/my/revoke_all_ical_tokens DELETE routes to my#revoke_all_ical_tokens' do+ expect(delete('/my/revoke_all_ical_tokens')).to route_to('my#revoke_all_ical_tokens')+ endend