github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/run/shared/artifacts_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"testing"
     8  
     9  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    10  	"github.com/ungtb10d/cli/v2/pkg/httpmock"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestDownloadWorkflowArtifactsPageinates(t *testing.T) {
    15  	testRepoOwner := "OWNER"
    16  	testRepoName := "REPO"
    17  	testRunId := "1234567890"
    18  
    19  	reg := &httpmock.Registry{}
    20  	defer reg.Verify(t)
    21  
    22  	firstReq := httpmock.QueryMatcher(
    23  		"GET",
    24  		fmt.Sprintf("repos/%s/%s/actions/runs/%s/artifacts", testRepoOwner, testRepoName, testRunId),
    25  		url.Values{"per_page": []string{"100"}},
    26  	)
    27  
    28  	firstArtifact := Artifact{
    29  		Name:        "artifact-0",
    30  		Size:        2,
    31  		DownloadURL: fmt.Sprintf("https://api.github.com/repos/%s/%s/actions/artifacts/987654320/zip", testRepoOwner, testRepoName),
    32  		Expired:     true,
    33  	}
    34  	firstRes := httpmock.JSONResponse(artifactsPayload{Artifacts: []Artifact{firstArtifact}})
    35  	testLinkUri := fmt.Sprintf("repositories/123456789/actions/runs/%s/artifacts", testRunId)
    36  	testLinkUrl := fmt.Sprintf("https://api.github.com/%s", testLinkUri)
    37  	firstRes = httpmock.WithHeader(
    38  		firstRes,
    39  		"Link",
    40  		fmt.Sprintf(`<%s?per_page=100&page=2>; rel="next", <%s?per_page=100&page=2>; rel="last"`, testLinkUrl, testLinkUrl),
    41  	)
    42  
    43  	secondReq := httpmock.QueryMatcher(
    44  		"GET",
    45  		testLinkUri,
    46  		url.Values{"per_page": []string{"100"}, "page": []string{"2"}},
    47  	)
    48  
    49  	secondArtifact := Artifact{
    50  		Name:        "artifact-1",
    51  		Size:        2,
    52  		DownloadURL: fmt.Sprintf("https://api.github.com/repos/%s/%s/actions/artifacts/987654321/zip", testRepoOwner, testRepoName),
    53  		Expired:     false,
    54  	}
    55  	secondRes := httpmock.JSONResponse(artifactsPayload{Artifacts: []Artifact{secondArtifact}})
    56  
    57  	reg.Register(firstReq, firstRes)
    58  	reg.Register(secondReq, secondRes)
    59  
    60  	httpClient := &http.Client{Transport: reg}
    61  	repo := ghrepo.New(testRepoOwner, testRepoName)
    62  
    63  	result, err := ListArtifacts(httpClient, repo, testRunId)
    64  	assert.NoError(t, err)
    65  	assert.Equal(t, []Artifact{firstArtifact, secondArtifact}, result)
    66  }