Skip to content
Snippets Groups Projects
Unverified Commit f7aab2a8 authored by Stan Hu's avatar Stan Hu Committed by Smriti Garg
Browse files

Add ability to remove expiration dates entirely

Now that we've decided that admins can allow tokens to have
no expiration date, bring back the option to remove them.
parent fc0dfe50
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -82,7 +82,8 @@ def prompt_action
menu.enum "."
 
menu.choice "Extend expiration date", 1
menu.choice "Quit", 2
menu.choice "Remove expiration date", 2
menu.choice "Quit", 3
end
 
case user_choice
Loading
Loading
@@ -90,6 +91,9 @@ def prompt_action
extend_expiration_date
true
when 2
remove_expiration_date
true
when 3
false
end
end
Loading
Loading
@@ -97,7 +101,7 @@ def prompt_action
def extend_expiration_date
old_date = prompt_expiration_date_selection
 
return unless old_date.is_a?(Date)
return unless old_date
 
prompt = TTY::Prompt.new
 
Loading
Loading
@@ -125,6 +129,24 @@ def extend_expiration_date
puts "Invalid date, aborting..."
end
 
def remove_expiration_date
old_date = prompt_expiration_date_selection
return unless old_date
prompt = TTY::Prompt.new
puts ""
puts "WARNING: This will remove the expiration for tokens that expire on #{old_date}."
confirmed = prompt.yes?("This will affect #{token_count(old_date)} tokens. Are you sure?", default: false)
if confirmed
update_tokens_with_expiration(old_date, nil)
else
puts "Aborting!"
end
end
def update_tokens_with_expiration(old_date, new_date)
total = 0
# rubocop:disable CodeReuse/ActiveRecord -- Rake task specifically for fixing an issue
Loading
Loading
Loading
Loading
@@ -56,47 +56,36 @@
end
 
describe '.extend_expiration_date' do
context 'with no max personal access token lifetime set' do
it 'extends the expiration date for selected tokens' do
new_date = expires_at + 1.day
default_date = expires_at + 365.days
prompt = instance_double(TTY::Prompt)
expect(task).to receive(:prompt_expiration_date_selection).and_return(expires_at)
expect(TTY::Prompt).to receive(:new).and_return(prompt)
expect(prompt).to receive(:ask).with(anything, default: default_date).and_return(new_date.to_s)
expect(prompt).to receive(:yes?).and_return(true)
expect(task).to receive(:update_tokens_with_expiration).with(expires_at, new_date).and_call_original
expect { task.send(:extend_expiration_date) }.to output(/Updated 2 tokens!/).to_stdout
expect(personal_access_token1.reload.expires_at).to eq(new_date)
expect(personal_access_token2.reload.expires_at).to eq(new_date)
end
end
it 'extends the expiration date for selected tokens' do
new_date = expires_at + 1.day
prompt = instance_double(TTY::Prompt)
allow(task).to receive(:prompt_expiration_date_selection).and_return(expires_at)
allow(TTY::Prompt).to receive(:new).and_return(prompt)
allow(prompt).to receive(:ask).and_return(new_date.to_s)
allow(prompt).to receive(:yes?).and_return(true)
expect(task).to receive(:update_tokens_with_expiration).with(expires_at, new_date).and_call_original
 
context 'with max personal access token token lifetime set' do
before do
stub_application_setting(max_personal_access_token_lifetime: 30)
end
expect { task.send(:extend_expiration_date) }.to output(/Updated 2 tokens!/).to_stdout
 
it 'asks with the max_personal_access_token_lifetime default' do
new_date = expires_at + 29.days
default_date = expires_at + 30.days
prompt = instance_double(TTY::Prompt)
expect(personal_access_token1.reload.expires_at).to eq(new_date)
expect(personal_access_token2.reload.expires_at).to eq(new_date)
end
end
 
expect(task).to receive(:prompt_expiration_date_selection).and_return(expires_at)
expect(TTY::Prompt).to receive(:new).and_return(prompt)
expect(prompt).to receive(:ask).with(anything, default: default_date).and_return(new_date.to_s)
describe '.remove_expiration_date' do
it 'removes the expiration date for selected tokens' do
prompt = instance_double(TTY::Prompt)
 
expect(prompt).to receive(:yes?).and_return(true)
expect(task).to receive(:update_tokens_with_expiration).with(expires_at, new_date).and_call_original
allow(task).to receive(:prompt_expiration_date_selection).and_return(expires_at)
allow(TTY::Prompt).to receive(:new).and_return(prompt)
allow(prompt).to receive(:yes?).and_return(true)
expect(task).to receive(:update_tokens_with_expiration).with(expires_at, nil).and_call_original
 
expect { task.send(:extend_expiration_date) }.to output(/Updated 2 tokens!/).to_stdout
expect { task.send(:remove_expiration_date) }.to output(/Updated 2 tokens!/).to_stdout
 
expect(personal_access_token1.reload.expires_at).to eq(new_date)
expect(personal_access_token2.reload.expires_at).to eq(new_date)
end
expect(personal_access_token1.reload.expires_at).to be_nil
expect(personal_access_token2.reload.expires_at).to be_nil
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment