code.gitea.io/gitea@v1.21.7/tests/integration/api_packages_pypi_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "bytes" 8 "fmt" 9 "io" 10 "mime/multipart" 11 "net/http" 12 "regexp" 13 "strings" 14 "testing" 15 16 "code.gitea.io/gitea/models/db" 17 "code.gitea.io/gitea/models/packages" 18 "code.gitea.io/gitea/models/unittest" 19 user_model "code.gitea.io/gitea/models/user" 20 "code.gitea.io/gitea/modules/packages/pypi" 21 "code.gitea.io/gitea/tests" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestPackagePyPI(t *testing.T) { 27 defer tests.PrepareTestEnv(t)() 28 29 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 30 31 packageName := "test-package" 32 packageVersion := "1!1.0.1+r1234" 33 packageAuthor := "KN4CK3R" 34 packageDescription := "Test Description" 35 36 content := "test" 37 hashSHA256 := "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" 38 39 root := fmt.Sprintf("/api/packages/%s/pypi", user.Name) 40 41 uploadFile := func(t *testing.T, filename, content string, expectedStatus int) { 42 body := &bytes.Buffer{} 43 writer := multipart.NewWriter(body) 44 part, _ := writer.CreateFormFile("content", filename) 45 _, _ = io.Copy(part, strings.NewReader(content)) 46 47 writer.WriteField("name", packageName) 48 writer.WriteField("version", packageVersion) 49 writer.WriteField("author", packageAuthor) 50 writer.WriteField("summary", packageDescription) 51 writer.WriteField("description", packageDescription) 52 writer.WriteField("sha256_digest", hashSHA256) 53 writer.WriteField("requires_python", "3.6") 54 55 _ = writer.Close() 56 57 req := NewRequestWithBody(t, "POST", root, body) 58 req.Header.Add("Content-Type", writer.FormDataContentType()) 59 req = AddBasicAuthHeader(req, user.Name) 60 MakeRequest(t, req, expectedStatus) 61 } 62 63 t.Run("Upload", func(t *testing.T) { 64 defer tests.PrintCurrentTest(t)() 65 66 filename := "test.whl" 67 uploadFile(t, filename, content, http.StatusCreated) 68 69 pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypePyPI) 70 assert.NoError(t, err) 71 assert.Len(t, pvs, 1) 72 73 pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0]) 74 assert.NoError(t, err) 75 assert.Nil(t, pd.SemVer) 76 assert.IsType(t, &pypi.Metadata{}, pd.Metadata) 77 assert.Equal(t, packageName, pd.Package.Name) 78 assert.Equal(t, packageVersion, pd.Version.Version) 79 80 pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID) 81 assert.NoError(t, err) 82 assert.Len(t, pfs, 1) 83 assert.Equal(t, filename, pfs[0].Name) 84 assert.True(t, pfs[0].IsLead) 85 86 pb, err := packages.GetBlobByID(db.DefaultContext, pfs[0].BlobID) 87 assert.NoError(t, err) 88 assert.Equal(t, int64(4), pb.Size) 89 }) 90 91 t.Run("UploadAddFile", func(t *testing.T) { 92 defer tests.PrintCurrentTest(t)() 93 94 filename := "test.tar.gz" 95 uploadFile(t, filename, content, http.StatusCreated) 96 97 pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypePyPI) 98 assert.NoError(t, err) 99 assert.Len(t, pvs, 1) 100 101 pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0]) 102 assert.NoError(t, err) 103 assert.Nil(t, pd.SemVer) 104 assert.IsType(t, &pypi.Metadata{}, pd.Metadata) 105 assert.Equal(t, packageName, pd.Package.Name) 106 assert.Equal(t, packageVersion, pd.Version.Version) 107 108 pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID) 109 assert.NoError(t, err) 110 assert.Len(t, pfs, 2) 111 112 pf, err := packages.GetFileForVersionByName(db.DefaultContext, pvs[0].ID, filename, packages.EmptyFileKey) 113 assert.NoError(t, err) 114 assert.Equal(t, filename, pf.Name) 115 assert.True(t, pf.IsLead) 116 117 pb, err := packages.GetBlobByID(db.DefaultContext, pf.BlobID) 118 assert.NoError(t, err) 119 assert.Equal(t, int64(4), pb.Size) 120 }) 121 122 t.Run("UploadHashMismatch", func(t *testing.T) { 123 defer tests.PrintCurrentTest(t)() 124 125 filename := "test2.whl" 126 uploadFile(t, filename, "dummy", http.StatusBadRequest) 127 }) 128 129 t.Run("UploadExists", func(t *testing.T) { 130 defer tests.PrintCurrentTest(t)() 131 132 uploadFile(t, "test.whl", content, http.StatusBadRequest) 133 uploadFile(t, "test.tar.gz", content, http.StatusBadRequest) 134 }) 135 136 t.Run("Download", func(t *testing.T) { 137 defer tests.PrintCurrentTest(t)() 138 139 downloadFile := func(filename string) { 140 req := NewRequest(t, "GET", fmt.Sprintf("%s/files/%s/%s/%s", root, packageName, packageVersion, filename)) 141 req = AddBasicAuthHeader(req, user.Name) 142 resp := MakeRequest(t, req, http.StatusOK) 143 144 assert.Equal(t, []byte(content), resp.Body.Bytes()) 145 } 146 147 downloadFile("test.whl") 148 downloadFile("test.tar.gz") 149 150 pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypePyPI) 151 assert.NoError(t, err) 152 assert.Len(t, pvs, 1) 153 assert.Equal(t, int64(2), pvs[0].DownloadCount) 154 }) 155 156 t.Run("PackageMetadata", func(t *testing.T) { 157 defer tests.PrintCurrentTest(t)() 158 159 req := NewRequest(t, "GET", fmt.Sprintf("%s/simple/%s", root, packageName)) 160 req = AddBasicAuthHeader(req, user.Name) 161 resp := MakeRequest(t, req, http.StatusOK) 162 163 htmlDoc := NewHTMLParser(t, resp.Body) 164 nodes := htmlDoc.doc.Find("a").Nodes 165 assert.Len(t, nodes, 2) 166 167 hrefMatcher := regexp.MustCompile(fmt.Sprintf(`%s/files/%s/%s/test\..+#sha256-%s`, root, regexp.QuoteMeta(packageName), regexp.QuoteMeta(packageVersion), hashSHA256)) 168 169 for _, a := range nodes { 170 for _, att := range a.Attr { 171 switch att.Key { 172 case "href": 173 assert.Regexp(t, hrefMatcher, att.Val) 174 case "data-requires-python": 175 assert.Equal(t, "3.6", att.Val) 176 default: 177 t.Fail() 178 } 179 } 180 } 181 }) 182 }