Commit e6ec9645 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'reset-jenkins-password-when-username-is-blank' into 'master'

Reset Jenkins password if username was left blank

So we not only reset password if URL was changed, but also for username. Also, still require username if a new password was set.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/22430

See merge request !775
parents d20d8f8e 7bcfe097
...@@ -5,7 +5,7 @@ class JenkinsService < CiService ...@@ -5,7 +5,7 @@ class JenkinsService < CiService
before_update :reset_password before_update :reset_password
validates :username, validates :username,
presence: true, presence: true,
if: ->(service) { service.activated? && service.password.present? } if: ->(service) { service.activated? && service.password_touched? }
default_value_for :push_events, true default_value_for :push_events, true
default_value_for :merge_requests_events, false default_value_for :merge_requests_events, false
...@@ -15,7 +15,7 @@ class JenkinsService < CiService ...@@ -15,7 +15,7 @@ class JenkinsService < CiService
def reset_password def reset_password
# don't reset the password if a new one is provided # don't reset the password if a new one is provided
if jenkins_url_changed? && !password_touched? if (jenkins_url_changed? || username.blank?) && !password_touched?
self.password = nil self.password = nil
end end
end end
......
...@@ -13,15 +13,16 @@ ...@@ -13,15 +13,16 @@
- else - else
= form.label name, title, class: "control-label" = form.label name, title, class: "control-label"
.col-sm-10 .col-sm-10
- if type == 'text' - case type
- when 'text'
= form.text_field name, class: "form-control", placeholder: placeholder = form.text_field name, class: "form-control", placeholder: placeholder
- elsif type == 'textarea' - when 'textarea'
= form.text_area name, rows: 5, class: "form-control", placeholder: placeholder = form.text_area name, rows: 5, class: "form-control", placeholder: placeholder
- elsif type == 'checkbox' - when 'checkbox'
= form.check_box name = form.check_box name
- elsif type == 'select' - when 'select'
= form.select name, options_for_select(choices, value ? value : default_choice), {}, { class: "form-control" } = form.select name, options_for_select(choices, value ? value : default_choice), {}, { class: "form-control" }
- elsif type == 'password' - when 'password'
= form.password_field name, autocomplete: "new-password", class: 'form-control' = form.password_field name, autocomplete: "new-password", class: 'form-control'
- if help - if help
%span.help-block= help %span.help-block= help
...@@ -5,7 +5,9 @@ describe JenkinsService do ...@@ -5,7 +5,9 @@ describe JenkinsService do
it { is_expected.to belong_to :project } it { is_expected.to belong_to :project }
it { is_expected.to have_one :service_hook } it { is_expected.to have_one :service_hook }
end end
let(:project) { create(:project) } let(:project) { create(:project) }
let(:jenkins_params) do let(:jenkins_params) do
{ {
active: true, active: true,
...@@ -25,19 +27,36 @@ describe JenkinsService do ...@@ -25,19 +27,36 @@ describe JenkinsService do
properties: { properties: {
jenkins_url: 'http://jenkins.example.com/', jenkins_url: 'http://jenkins.example.com/',
password: 'password', password: 'password',
username: nil, username: 'username',
} }
) )
end end
subject { @jenkins_service } subject { @jenkins_service }
context 'when the service is active' do context 'when the service is active' do
let(:active) { true } let(:active) { true }
it { is_expected.to validate_presence_of :username }
context 'when password was not touched' do
before do
allow(subject).to receive(:password_touched?).and_return(false)
end
it { is_expected.not_to validate_presence_of :username }
end
context 'when password was touched' do
before do
allow(subject).to receive(:password_touched?).and_return(true)
end
it { is_expected.to validate_presence_of :username }
end
end end
context 'when the service is inactive' do context 'when the service is inactive' do
let(:active) { false } let(:active) { false }
it { is_expected.not_to validate_presence_of :username } it { is_expected.not_to validate_presence_of :username }
end end
end end
...@@ -111,12 +130,18 @@ describe JenkinsService do ...@@ -111,12 +130,18 @@ describe JenkinsService do
) )
end end
it 'reset password if url changed' do it 'resets password if url changed' do
@jenkins_service.jenkins_url = 'http://jenkins-edited.example.com/' @jenkins_service.jenkins_url = 'http://jenkins-edited.example.com/'
@jenkins_service.save @jenkins_service.save
expect(@jenkins_service.password).to be_nil expect(@jenkins_service.password).to be_nil
end end
it 'resets password if username is blank' do
@jenkins_service.username = ''
@jenkins_service.save
expect(@jenkins_service.password).to be_nil
end
it 'does not reset password if username changed' do it 'does not reset password if username changed' do
@jenkins_service.username = 'some_name' @jenkins_service.username = 'some_name'
@jenkins_service.save @jenkins_service.save
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment