Skip to content

Commit

Permalink
fix: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
sZma5a committed Nov 5, 2023
1 parent 9b1e5b7 commit 4256c8f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
12 changes: 10 additions & 2 deletions pkg/config/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,17 @@ func (g PipedGit) LoadSSHKey() ([]byte, error) {
}

func (g *PipedGit) Validate() error {
if g.ShouldConfigureSSHConfig() && (g.PersonalAccessToken.Validate() != nil) {
patErr := g.PersonalAccessToken.Validate()
patFlag := g.PersonalAccessToken.UserName != "" || g.PersonalAccessToken.UserToken != ""
if g.ShouldConfigureSSHConfig() && patFlag {
return errors.New("cannot configure both sshKeyData or sshKeyFile and personalAccessToken")
}
if patFlag && patErr != nil {
return patErr
}
if g.SSHKeyData != "" && g.SSHKeyFile != "" {
return errors.New("only either sshKeyFile or sshKeyData can be set")
}
return nil
}

Expand Down Expand Up @@ -382,7 +390,7 @@ type PipedGitPersonalAccessToken struct {
}

func (p PipedGitPersonalAccessToken) Validate() error {
if p.UserName != "" && p.UserToken != "" {
if p.UserName == "" || p.UserToken == "" {
return errors.New("both userName and userToken must be set")
}
return nil
Expand Down
60 changes: 56 additions & 4 deletions pkg/config/piped_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,10 +1390,12 @@ func TestFindPlatformProvidersByLabel(t *testing.T) {
func TestPipeGitValidate(t *testing.T) {
t.Parallel()
testcases := []struct {
git PipedGit
err error
name string
git PipedGit
err error
}{
{
name: "Both SSH and PAT are not valid",
git: PipedGit{
SSHKeyData: "sshkey1",
PersonalAccessToken: PipedGitPersonalAccessToken{
Expand All @@ -1404,12 +1406,41 @@ func TestPipeGitValidate(t *testing.T) {
err: errors.New("cannot configure both sshKeyData or sshKeyFile and personalAccessToken"),
},
{
name: "Both SSH and PAT is not valid",
git: PipedGit{
SSHKeyFile: "sshkeyfile",
SSHKeyData: "sshkeydata",
PersonalAccessToken: PipedGitPersonalAccessToken{
UserName: "",
UserToken: "UserToken",
},
},
err: errors.New("cannot configure both sshKeyData or sshKeyFile and personalAccessToken"),
},
{
name: "SSH key data is not empty",
git: PipedGit{
SSHKeyData: "sshkey2",
},
err: nil,
},
{
name: "SSH key file is not empty",
git: PipedGit{
SSHKeyFile: "sshkey2",
},
err: nil,
},
{
name: "Both SSH file and data is not empty",
git: PipedGit{
SSHKeyData: "sshkeydata",
SSHKeyFile: "sshkeyfile",
},
err: errors.New("only either sshKeyFile or sshKeyData can be set"),
},
{
name: "PAT is valid",
git: PipedGit{
PersonalAccessToken: PipedGitPersonalAccessToken{
UserName: "UserName",
Expand All @@ -1419,8 +1450,29 @@ func TestPipeGitValidate(t *testing.T) {
err: nil,
},
{
git: PipedGit{ },
err: nil,
name: "PAT username is empty",
git: PipedGit{
PersonalAccessToken: PipedGitPersonalAccessToken{
UserName: "UserName",
UserToken: "",
},
},
err: errors.New("both userName and userToken must be set"),
},
{
name: "PAT token is empty",
git: PipedGit{
PersonalAccessToken: PipedGitPersonalAccessToken{
UserName: "",
UserToken: "UserToken",
},
},
err: errors.New("both userName and userToken must be set"),
},
{
name: "Git config is empty",
git: PipedGit{},
err: nil,
},
}
for _, tc := range testcases {
Expand Down

0 comments on commit 4256c8f

Please sign in to comment.