Skip to content

Commit 653fc4c

Browse files
committed
Prioritize project_urls over home_page and refactor tests
1 parent f4ff059 commit 653fc4c

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

routers/api/packages/pypi/pypi.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,23 @@ func UploadPackageFile(ctx *context.Context) {
143143
// Ensure ctx.Req.Form exists.
144144
_ = ctx.Req.ParseForm()
145145

146-
// TODO: Home-page is a deprecated metadata field. Remove this form lookup once it's no longer apart of the spec.
147-
homepageURL := ctx.Req.FormValue("home_page")
148-
if len(homepageURL) == 0 {
149-
projectURLs := ctx.Req.Form["project_urls"]
150-
for _, purl := range projectURLs {
151-
label, url, found := strings.Cut(purl, ",")
152-
if !found {
153-
continue
154-
}
155-
if normalizeLabel(label) != "homepage" {
156-
continue
157-
}
158-
homepageURL = strings.TrimSpace(url)
159-
break
146+
var homepageURL string
147+
projectURLs := ctx.Req.Form["project_urls"]
148+
for _, purl := range projectURLs {
149+
label, url, found := strings.Cut(purl, ",")
150+
if !found {
151+
continue
152+
}
153+
if normalizeLabel(label) != "homepage" {
154+
continue
160155
}
156+
homepageURL = strings.TrimSpace(url)
157+
break
158+
}
159+
160+
if len(homepageURL) == 0 {
161+
// TODO: Home-page is a deprecated metadata field. Remove this branch once it's no longer apart of the spec.
162+
homepageURL = ctx.Req.FormValue("home_page")
161163
}
162164

163165
if !validation.IsValidURL(homepageURL) {

tests/integration/api_packages_pypi_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestPackagePyPI(t *testing.T) {
3939

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

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

56-
return
56+
return body, writer, writer.Close
5757
}
5858

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

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

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

72-
_ = writer.Close()
72+
_ = closeFunc()
7373

74-
upload(t, body, writer.FormDataContentType(), expectedStatus)
74+
uploadHelper(t, body, writer.FormDataContentType(), expectedStatus)
7575
}
7676

7777
t.Run("Upload", func(t *testing.T) {
@@ -152,13 +152,13 @@ func TestPackagePyPI(t *testing.T) {
152152
defer tests.PrintCurrentTest(t)()
153153

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

157157
writer.WriteField("home_page", projectURL)
158158

159-
_ = writer.Close()
159+
_ = closeFunc()
160160

161-
upload(t, body, writer.FormDataContentType(), http.StatusCreated)
161+
uploadHelper(t, body, writer.FormDataContentType(), http.StatusCreated)
162162

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

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

179-
_ = writer.Close()
179+
_ = closeFunc()
180180

181-
upload(t, body, writer.FormDataContentType(), http.StatusCreated)
181+
uploadHelper(t, body, writer.FormDataContentType(), http.StatusCreated)
182182

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

0 commit comments

Comments
 (0)