Skip to content

Commit 92776d6

Browse files
committed
add validation test for empty credentials
1 parent 0bc91f3 commit 92776d6

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

cmd/prompt/prompt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package prompt
22

33
import (
44
"bufio"
5+
"errors"
56
"fmt"
67
"io"
78
"os"
@@ -77,6 +78,10 @@ func NewPromptForCredentials(in io.Reader, out, errOut io.Writer) func(repositor
7778

7879
result = oci.Credentials{Username: u, Password: p}
7980
}
81+
82+
if strings.TrimSpace(result.Username) == "" || strings.TrimSpace(result.Password) == "" {
83+
return oci.Credentials{}, errors.New("username or password cannot be empty")
84+
}
8085

8186
return result, nil
8287
}

cmd/prompt/prompt_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ func Test_NewPromptForCredentials(t *testing.T) {
7979
}
8080
})
8181
}
82+
t.Run("empty username or password returns error", func(t *testing.T) {
83+
in := strings.NewReader("\r\n\r\n")
84+
out := io.Discard
85+
errOut := io.Discard
86+
87+
credPrompt := NewPromptForCredentials(in, out, errOut)
88+
_, err := credPrompt("example.com")
89+
if err == nil {
90+
t.Fatal("expected error for empty username or password, got nil")
91+
}
92+
})
93+
8294
}

0 commit comments

Comments
 (0)