Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

use specific error for github authorization error #493

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cmd/cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,19 @@ func testCreateProjectFromTemplate(t *testing.T) {
assert.Equal(t, "{\"status\":\"success\",\"projectPath\":\"./testDir\",\"result\":{\"language\":\"unknown\",\"projectType\":\"docker\"}}\n", string(out))
})
t.Run("fail case: create GHE project using good username but bad password"+
"\ncwctl project create --url <secureTemplateRepo> --path <testDir> --username <goodUsername> --password <badPassword>", func(t *testing.T) {
"\ncwctl --json project create --url <secureTemplateRepo> --path <testDir> --username <goodUsername> --password <badPassword>", func(t *testing.T) {
os.RemoveAll(testDir)
defer os.RemoveAll(testDir)

cmd := exec.Command(cwctl, "project", "create",
cmd := exec.Command(cwctl, "--json", "project", "create",
"--url="+test.GHERepoURL,
"--path="+testDir,
"--username="+test.GHEUsername,
"--password=badpassword",
)
out, err := cmd.CombinedOutput()
assert.NotNil(t, err)
assert.Contains(t, string(out), "invalid_git_credentials")
assert.Contains(t, string(out), "401 Unauthorized")
})
}
Expand Down Expand Up @@ -390,7 +391,7 @@ func testSuccessfulAddAndRemoveTemplateRepos(t *testing.T) {
})
t.Run("fail case: add GHE template repo and create one of its projects using bad password, overriding good stored GHE creds"+
"\ncwctl templates repos add --url <GHEDevfile> --username --password"+
"\ncwctl project create --url <GHETemplateRepo> --username <goodUsername> --password <badPassword>"+
"\ncwctl --json project create --url <GHETemplateRepo> --username <goodUsername> --password <badPassword>"+
"\ncwctl templates repos remove --url", func(t *testing.T) {
if !test.UsingOwnGHECredentials {
t.Skip("skipping this test because you haven't set GitHub credentials needed for this test")
Expand All @@ -408,14 +409,15 @@ func testSuccessfulAddAndRemoveTemplateRepos(t *testing.T) {
assert.Nil(t, err)
assert.Contains(t, string(out), test.GHEDevfileURL)

createCmd := exec.Command(cwctl, "project", "create",
createCmd := exec.Command(cwctl, "--json", "project", "create",
"--url="+test.GHERepoURL,
"--path="+testDir,
"--username="+test.GHEUsername,
"--password=badpassword",
)
createOut, createErr := createCmd.CombinedOutput()
assert.NotNil(t, createErr)
assert.Contains(t, string(createOut), "invalid_git_credentials")
assert.Contains(t, string(createOut), "401 Unauthorized")

removeCmd := exec.Command(cwctl, "templates", "repos", "remove",
Expand All @@ -427,7 +429,7 @@ func testSuccessfulAddAndRemoveTemplateRepos(t *testing.T) {
})
t.Run("fail case: add GHE template repo and create one of its projects using bad personalAccessToken, overriding good stored GHE creds"+
"\ncwctl templates repos add --url <GHEDevfile> --personalAccessToken <goodToken>"+
"\ncwctl project create --url <GHETemplateRepo> --personalAccessToken <badToken>"+
"\ncwctl --json project create --url <GHETemplateRepo> --personalAccessToken <badToken>"+
"\ncwctl templates repos remove --url", func(t *testing.T) {
if !test.UsingOwnGHECredentials {
t.Skip("skipping this test because you haven't set GitHub credentials needed for this test")
Expand All @@ -444,13 +446,14 @@ func testSuccessfulAddAndRemoveTemplateRepos(t *testing.T) {
assert.Nil(t, err)
assert.Contains(t, string(out), test.GHEDevfileURL)

createCmd := exec.Command(cwctl, "project", "create",
createCmd := exec.Command(cwctl, "--json", "project", "create",
"--url="+test.GHERepoURL,
"--path="+testDir,
"--personalAccessToken=badtoken",
)
createOut, createErr := createCmd.CombinedOutput()
assert.NotNil(t, createErr)
assert.Contains(t, string(createOut), "invalid_git_credentials")
assert.Contains(t, string(createOut), "401 Unauthorized")

removeCmd := exec.Command(cwctl, "templates", "repos", "remove",
Expand Down
7 changes: 6 additions & 1 deletion pkg/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func DownloadTemplate(destination, url string, gitCredentials *utils.GitCredenti

err := utils.DownloadFromURLThenExtract(url, destination, gitCredentials)
if err != nil {
return nil, &ProjectError{errOpCreateProject, err, err.Error()}
errOp := errOpCreateProject
// if 401 error, use invalid credentials error code
if strings.Contains(err.Error(), "401 Unauthorized") {
errOp = errOpInvalidCredentials
}
return nil, &ProjectError{errOp, err, err.Error()}
}

err = utils.ReplaceInFiles(destination, "[PROJ_NAME_PLACEHOLDER]", projectName)
Expand Down
2 changes: 2 additions & 0 deletions pkg/project/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestDownloadTemplate(t *testing.T) {
out, err := DownloadTemplate(dest, url, gitCredentials)

assert.Nil(t, out)
assert.Equal(t, errOpInvalidCredentials, err.Op)
assert.Equal(t, "unexpected status code: 401 Unauthorized", err.Desc)
})
t.Run("fail case: download GHE template using bad personalAccessToken)", func(t *testing.T) {
Expand All @@ -114,6 +115,7 @@ func TestDownloadTemplate(t *testing.T) {
out, err := DownloadTemplate(dest, url, gitCredentials)

assert.Nil(t, out)
assert.Equal(t, errOpInvalidCredentials, err.Op)
assert.Equal(t, "unexpected status code: 401 Unauthorized", err.Desc)
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/project/project_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
errOpSync = "proj_sync"
errOpSyncRef = "proj_sync_ref"
errOpWriteCwSettings = "proj_write_cw_settings"
errOpInvalidCredentials = "invalid_git_credentials"
)

const (
Expand Down