Skip to content

Commit

Permalink
Prioritize project_urls over home_page and refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kemzeb committed Jan 3, 2025
1 parent f4ff059 commit 653fc4c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
30 changes: 16 additions & 14 deletions routers/api/packages/pypi/pypi.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,23 @@ func UploadPackageFile(ctx *context.Context) {
// Ensure ctx.Req.Form exists.
_ = ctx.Req.ParseForm()

// TODO: Home-page is a deprecated metadata field. Remove this form lookup once it's no longer apart of the spec.
homepageURL := ctx.Req.FormValue("home_page")
if len(homepageURL) == 0 {
projectURLs := ctx.Req.Form["project_urls"]
for _, purl := range projectURLs {
label, url, found := strings.Cut(purl, ",")
if !found {
continue
}
if normalizeLabel(label) != "homepage" {
continue
}
homepageURL = strings.TrimSpace(url)
break
var homepageURL string
projectURLs := ctx.Req.Form["project_urls"]
for _, purl := range projectURLs {
label, url, found := strings.Cut(purl, ",")
if !found {
continue
}
if normalizeLabel(label) != "homepage" {
continue
}
homepageURL = strings.TrimSpace(url)
break
}

if len(homepageURL) == 0 {
// TODO: Home-page is a deprecated metadata field. Remove this branch once it's no longer apart of the spec.
homepageURL = ctx.Req.FormValue("home_page")
}

if !validation.IsValidURL(homepageURL) {
Expand Down
24 changes: 12 additions & 12 deletions tests/integration/api_packages_pypi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestPackagePyPI(t *testing.T) {

root := fmt.Sprintf("/api/packages/%s/pypi", user.Name)

createBasicMultipartFile := func(packageName, filename, content string) (body *bytes.Buffer, writer *multipart.Writer) {
createBasicMultipartFile := func(filename, packageName, content string) (body *bytes.Buffer, writer *multipart.Writer, closer func() error) {
body = &bytes.Buffer{}
writer = multipart.NewWriter(body)
part, _ := writer.CreateFormFile("content", filename)
Expand All @@ -53,25 +53,25 @@ func TestPackagePyPI(t *testing.T) {
writer.WriteField("sha256_digest", hashSHA256)
writer.WriteField("requires_python", "3.6")

return
return body, writer, writer.Close
}

upload := func(t *testing.T, body *bytes.Buffer, contentType string, expectedStatus int) {
uploadHelper := func(t *testing.T, body *bytes.Buffer, contentType string, expectedStatus int) {
req := NewRequestWithBody(t, "POST", root, body).
SetHeader("Content-Type", contentType).
AddBasicAuth(user.Name)
MakeRequest(t, req, expectedStatus)
}

uploadFile := func(t *testing.T, filename, content string, expectedStatus int) {
body, writer := createBasicMultipartFile(packageName, filename, content)
body, writer, closeFunc := createBasicMultipartFile(filename, packageName, content)

writer.WriteField("project_urls", "DOCUMENTATION , https://readthedocs.org")
writer.WriteField("project_urls", fmt.Sprintf("Home-page, %s", projectURL))

_ = writer.Close()
_ = closeFunc()

upload(t, body, writer.FormDataContentType(), expectedStatus)
uploadHelper(t, body, writer.FormDataContentType(), expectedStatus)
}

t.Run("Upload", func(t *testing.T) {
Expand Down Expand Up @@ -152,13 +152,13 @@ func TestPackagePyPI(t *testing.T) {
defer tests.PrintCurrentTest(t)()

pkgName := "homepage-package"
body, writer := createBasicMultipartFile(pkgName, "test.whl", content)
body, writer, closeFunc := createBasicMultipartFile("test.whl", pkgName, content)

writer.WriteField("home_page", projectURL)

_ = writer.Close()
_ = closeFunc()

upload(t, body, writer.FormDataContentType(), http.StatusCreated)
uploadHelper(t, body, writer.FormDataContentType(), http.StatusCreated)

pvs, err := packages.GetVersionsByPackageName(db.DefaultContext, user.ID, packages.TypePyPI, pkgName)
assert.NoError(t, err)
Expand All @@ -174,11 +174,11 @@ func TestPackagePyPI(t *testing.T) {
defer tests.PrintCurrentTest(t)()

pkgName := "no-project-url-or-homepage-package"
body, writer := createBasicMultipartFile(pkgName, "test.whl", content)
body, writer, closeFunc := createBasicMultipartFile("test.whl", pkgName, content)

_ = writer.Close()
_ = closeFunc()

upload(t, body, writer.FormDataContentType(), http.StatusCreated)
uploadHelper(t, body, writer.FormDataContentType(), http.StatusCreated)

pvs, err := packages.GetVersionsByPackageName(db.DefaultContext, user.ID, packages.TypePyPI, pkgName)
assert.NoError(t, err)
Expand Down

0 comments on commit 653fc4c

Please sign in to comment.