Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 24b5929

Browse files
committed
Fix hole in determining which password to store in credential record
1 parent d312466 commit 24b5929

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

lib/wpxf/db/credentials.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ module Wpxf::Db::Credentials
77
# @param password [String] the password.
88
# @param type [String] the type of string stored in the password field.
99
# @return [Models::Credential] the newly created {Models::Credential}.
10-
def store_credentials(username, password = nil, type = 'plain')
10+
def store_credentials(username, password = '', type = 'plain')
1111
credential = Models::Credential.first(
1212
host: target_host,
1313
port: target_port,
1414
username: username,
15-
password: nil,
1615
type: type,
1716
workspace: active_workspace
1817
)
@@ -21,10 +20,18 @@ def store_credentials(username, password = nil, type = 'plain')
2120
credential.host = target_host
2221
credential.port = target_port
2322
credential.username = username
24-
credential.password = password
23+
credential.password = _determine_password_to_store(credential, password)
2524
credential.type = type
2625
credential.workspace = active_workspace
2726

2827
credential.save
2928
end
29+
30+
private
31+
32+
def _determine_password_to_store(model, new_password)
33+
new_password = '' if new_password.nil?
34+
return model.password if new_password == '' && !model.password.nil?
35+
new_password
36+
end
3037
end

spec/lib/wpxf/db/credentials_spec.rb

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,59 @@
3939
expect(credential.type).to eql 'plain'
4040
end
4141

42+
it 'should store `nil` passwords as an empty string' do
43+
subject.store_credentials 'foo', nil
44+
credential = Models::Credential.first
45+
expect(credential.password).to_not be_nil
46+
expect(credential.password).to eql ''
47+
end
48+
4249
context 'if a record with the same host + username + type already exists' do
4350
context 'and the record is in the same workspace' do
4451
context 'and has no password' do
4552
it 'should overwrite the previous credential' do
46-
subject.store_credentials 'foo', nil, 'test'
53+
subject.store_credentials 'foo', '', 'test'
4754
subject.store_credentials 'foo', 'bar', 'test'
4855
expect(Models::Credential.count).to eql 1
4956

5057
credential = Models::Credential.first
5158
expect(credential.password).to eql 'bar'
52-
Models::Credential.truncate
53-
54-
subject.store_credentials 'foo', 'bar', 'unique'
55-
subject.store_credentials 'foo', 'foo', 'unique2'
56-
expect(Models::Credential.count).to eql 2
5759
end
5860
end
5961

6062
context 'and has a password' do
61-
it 'should add a new entry' do
62-
Models::Credential.create(
63-
host: subject.target_host,
64-
port: subject.target_port,
65-
username: 'foo',
66-
password: 'bar',
67-
type: 'plain',
68-
workspace: Models::Workspace.first(name: 'default')
69-
)
63+
context 'if the password is the same' do
64+
it 'should overwrite the previous credential' do
65+
subject.store_credentials 'foo', 'bar', 'test'
66+
subject.store_credentials 'foo', 'bar', 'test'
67+
expect(Models::Credential.count).to eql 1
68+
end
69+
end
7070

71-
expect(Models::Credential.count).to eql 1
72-
subject.store_credentials 'foo', 'foo'
73-
expect(Models::Credential.count).to eql 2
71+
context 'if the new password is blank' do
72+
it 'should not overwrite the existing pasword' do
73+
subject.store_credentials 'foo', 'bar', 'test'
74+
subject.store_credentials 'foo', '', 'test'
75+
expect(Models::Credential.count).to eql 1
76+
expect(Models::Credential.first.password).to eql 'bar'
77+
end
78+
end
79+
80+
context 'if the password is different' do
81+
it 'should add a new entry' do
82+
Models::Credential.create(
83+
host: subject.target_host,
84+
port: subject.target_port,
85+
username: 'foo',
86+
password: 'bar',
87+
type: 'plain',
88+
workspace: Models::Workspace.first(name: 'default')
89+
)
90+
91+
expect(Models::Credential.count).to eql 1
92+
subject.store_credentials 'foo', 'foo'
93+
expect(Models::Credential.count).to eql 2
94+
end
7495
end
7596
end
7697
end

0 commit comments

Comments
 (0)