diff --git a/pkg/cmd/repo/autolink/delete/http_test.go b/pkg/cmd/repo/autolink/delete/http_test.go index a67e08acc..928e561c7 100644 --- a/pkg/cmd/repo/autolink/delete/http_test.go +++ b/pkg/cmd/repo/autolink/delete/http_test.go @@ -1 +1,69 @@ package delete + +import ( + "fmt" + "net/http" + "testing" + + "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/httpmock" + "github.com/stretchr/testify/require" +) + +func TestAutolinkDeleter_Delete(t *testing.T) { + repo := ghrepo.New("OWNER", "REPO") + + tests := []struct { + name string + id string + stubStatus int + stubRespJSON string + + expectErr bool + expectedErrMsg string + }{ + { + name: "204 successful delete", + id: "123", + stubStatus: http.StatusNoContent, + }, + { + name: "404 repo or autolink not found", + id: "123", + stubStatus: http.StatusNotFound, + stubRespJSON: `{ + "message": "Not Found", + "documentation_url": "https://docs.github.com/rest/repos/autolinks#get-an-autolink-reference-of-a-repository", + "status": "404" + }`, + expectErr: true, + expectedErrMsg: "error deleting autolink: HTTP 404: Perhaps you are missing admin rights to the repository? (https://api.github.com/repos/OWNER/REPO/autolinks/123)", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + reg.Register( + httpmock.REST( + http.MethodDelete, + fmt.Sprintf("repos/%s/%s/autolinks/%s", repo.RepoOwner(), repo.RepoName(), tt.id), + ), + httpmock.StatusStringResponse(tt.stubStatus, tt.stubRespJSON), + ) + defer reg.Verify(t) + + autolinkDeleter := &AutolinkDeleter{ + HTTPClient: &http.Client{Transport: reg}, + } + + err := autolinkDeleter.Delete(repo, tt.id) + + if tt.expectErr { + require.EqualError(t, err, tt.expectedErrMsg) + } else { + require.NoError(t, err) + } + }) + } +} diff --git a/pkg/cmd/repo/autolink/view/http_test.go b/pkg/cmd/repo/autolink/view/http_test.go index 5bfe9369f..c47418404 100644 --- a/pkg/cmd/repo/autolink/view/http_test.go +++ b/pkg/cmd/repo/autolink/view/http_test.go @@ -28,7 +28,7 @@ func TestAutolinkViewer_View(t *testing.T) { { name: "200 successful alphanumeric view", id: "123", - stubStatus: 200, + stubStatus: http.StatusOK, stubRespJSON: `{ "id": 123, "key_prefix": "TICKET-", @@ -45,7 +45,7 @@ func TestAutolinkViewer_View(t *testing.T) { { name: "200 successful numeric view", id: "123", - stubStatus: 200, + stubStatus: http.StatusOK, stubRespJSON: `{ "id": 123, "key_prefix": "TICKET-", @@ -62,7 +62,7 @@ func TestAutolinkViewer_View(t *testing.T) { { name: "404 repo or autolink not found", id: "123", - stubStatus: 404, + stubStatus: http.StatusNotFound, stubRespJSON: `{ "message": "Not Found", "documentation_url": "https://docs.github.com/rest/repos/autolinks#get-an-autolink-reference-of-a-repository", @@ -85,11 +85,11 @@ func TestAutolinkViewer_View(t *testing.T) { ) defer reg.Verify(t) - autolinkCreator := &AutolinkViewer{ + autolinkViewer := &AutolinkViewer{ HTTPClient: &http.Client{Transport: reg}, } - autolink, err := autolinkCreator.View(repo, tt.id) + autolink, err := autolinkViewer.View(repo, tt.id) if tt.expectErr { require.EqualError(t, err, tt.expectedErrMsg)