Skip to content
代码片段 群组 项目
未验证 提交 a7e2f777 编辑于 作者: David Fernandez's avatar David Fernandez 提交者: GitLab
浏览文件

Update maven upstream credentials handling

If the url is changed, the credentials are now reset to empty values.
Also, allow passing username and password values to the
initialized method.
上级 439ce447
No related branches found
No related tags found
无相关合并请求
......@@ -28,17 +28,33 @@ class Upstream < ApplicationRecord
validates :url, :username, :password, length: { maximum: 255 }
after_initialize :read_credentials
after_validation :reset_credentials, if: -> { persisted? && url_changed? }
before_save :write_credentials
private
def read_credentials
self.credentials ||= {}
# if credentials are blank we might have a username + password from initializer. Don't reset them.
return if credentials.blank?
self.username, self.password = (credentials || {}).values_at('username', 'password')
clear_username_change
clear_password_change
end
def write_credentials
self.credentials = (credentials || {}).merge('username' => username, 'password' => password)
end
def reset_credentials
return if username_changed? && password_changed?
self.username = nil
self.password = nil
self.credentials = {}
end
end
end
end
......
......@@ -88,6 +88,32 @@
end
end
end
context 'when url is updated' do
where(:new_url, :new_user, :new_pwd, :expected_user, :expected_pwd, :expected_credentials) do
'http://original_url.test' | 'test' | 'test' | 'test' | 'test' | { 'username' => 'test', 'password' => 'test' }
'http://update_url.test' | 'test' | 'test' | 'test' | 'test' | { 'username' => 'test', 'password' => 'test' }
'http://update_url.test' | :none | :none | nil | nil | { 'username' => nil, 'password' => nil }
'http://update_url.test' | 'test' | :none | nil | nil | { 'username' => nil, 'password' => nil }
'http://update_url.test' | :none | 'test' | nil | nil | { 'username' => nil, 'password' => nil }
end
with_them do
before do
upstream.update!(url: 'http://original_url.test', username: 'original_user', password: 'original_pwd')
end
it 'resets the username and the password when necessary' do
new_attributes = { url: new_url, username: new_user, password: new_pwd }.select { |_, v| v != :none }
upstream.update!(new_attributes)
expect(upstream.reload.url).to eq(new_url)
expect(upstream.username).to eq(expected_user)
expect(upstream.password).to eq(expected_pwd)
expect(upstream.credentials).to eq(expected_credentials)
end
end
end
end
end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册