Skip to content

Commit efe681e

Browse files
committed
added unit test
Signed-off-by: Shashank <[email protected]>
1 parent 0a90c1e commit efe681e

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

pkg/configs/db/db.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ type DB interface {
5959
Close() error
6060
}
6161

62+
func SetUserPassword(u *url.URL, passwordFilePath string) (*url.URL, error) {
63+
if u.User == nil {
64+
return nil, fmt.Errorf("--database.password-file requires username in --database.uri")
65+
}
66+
67+
passwordBytes, err := os.ReadFile(passwordFilePath)
68+
if err != nil {
69+
return nil, fmt.Errorf("Could not read database password file: %v", err)
70+
}
71+
72+
password := strings.TrimSpace(string(passwordBytes))
73+
u.User = url.UserPassword(u.User.Username(), password)
74+
return u, nil
75+
}
76+
6277
// New creates a new database.
6378
func New(cfg Config) (DB, error) {
6479
if cfg.Mock != nil {
@@ -71,15 +86,11 @@ func New(cfg Config) (DB, error) {
7186
}
7287

7388
if len(cfg.PasswordFile) != 0 {
74-
if u.User == nil {
75-
return nil, fmt.Errorf("--database.password-file requires username in --database.uri")
76-
}
77-
passwordBytes, err := os.ReadFile(cfg.PasswordFile)
89+
updatedUrl, err := SetUserPassword(u, cfg.PasswordFile)
7890
if err != nil {
79-
return nil, fmt.Errorf("Could not read database password file: %v", err)
91+
return nil, err
8092
}
81-
password := strings.TrimSpace(string(passwordBytes))
82-
u.User = url.UserPassword(u.User.Username(), password)
93+
u = updatedUrl
8394
}
8495

8596
var d DB

pkg/configs/db/dbtest/unit.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
package dbtest
55

66
import (
7+
"io/ioutil"
8+
"net/url"
9+
"os"
710
"testing"
811

912
"github.com/stretchr/testify/require"
@@ -26,3 +29,48 @@ func Setup(t *testing.T) db.DB {
2629
func Cleanup(t *testing.T, database db.DB) {
2730
require.NoError(t, database.Close())
2831
}
32+
33+
func TestUserPasswordFromPasswordFile(t *testing.T) {
34+
35+
tempFile, _ := ioutil.TempFile("", "testpassword")
36+
defer tempFile.Close()
37+
defer os.Remove(tempFile.Name())
38+
39+
_, writeErr := tempFile.WriteString(" testpassword ")
40+
if writeErr != nil {
41+
t.Fatalf("Error writing to temporary file: %v", writeErr)
42+
}
43+
44+
testCases := []struct {
45+
testName string
46+
url string
47+
expectedUsername string
48+
expectedPassword string
49+
}{
50+
{
51+
testName: "set username and password correctly from url and password file",
52+
url: "testscheme://[email protected]",
53+
expectedUsername: "testuser",
54+
expectedPassword: "testpassword",
55+
},
56+
}
57+
58+
for _, tc := range testCases {
59+
t.Run(tc.testName, func(t *testing.T) {
60+
u, _ := url.Parse(tc.url)
61+
62+
updatedUrl, err := db.SetUserPassword(u, tempFile.Name())
63+
if err != nil {
64+
t.Errorf("Unexpected error: %v", err)
65+
}
66+
67+
if u.User == nil || updatedUrl.User.Username() != tc.expectedUsername {
68+
t.Errorf("Username not set as expected. Got: %v, Expected: %v", u.User, tc.expectedUsername)
69+
}
70+
71+
if trimmedPassword, _ := updatedUrl.User.Password(); trimmedPassword != tc.expectedPassword {
72+
t.Errorf("Password not set as expected. Got: %v, Expected: %v", trimmedPassword, tc.expectedPassword)
73+
}
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)