code.gitea.io/gitea@v1.21.7/tests/integration/api_packages_composer_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  	"archive/zip"
     8  	"bytes"
     9  	"fmt"
    10  	"net/http"
    11  	neturl "net/url"
    12  	"testing"
    13  
    14  	"code.gitea.io/gitea/models/db"
    15  	"code.gitea.io/gitea/models/packages"
    16  	"code.gitea.io/gitea/models/unittest"
    17  	user_model "code.gitea.io/gitea/models/user"
    18  	composer_module "code.gitea.io/gitea/modules/packages/composer"
    19  	"code.gitea.io/gitea/modules/setting"
    20  	"code.gitea.io/gitea/routers/api/packages/composer"
    21  	"code.gitea.io/gitea/tests"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestPackageComposer(t *testing.T) {
    27  	defer tests.PrepareTestEnv(t)()
    28  
    29  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    30  
    31  	vendorName := "gitea"
    32  	projectName := "composer-package"
    33  	packageName := vendorName + "/" + projectName
    34  	packageVersion := "1.0.3"
    35  	packageDescription := "Package Description"
    36  	packageType := "composer-plugin"
    37  	packageAuthor := "Gitea Authors"
    38  	packageLicense := "MIT"
    39  
    40  	var buf bytes.Buffer
    41  	archive := zip.NewWriter(&buf)
    42  	w, _ := archive.Create("composer.json")
    43  	w.Write([]byte(`{
    44  		"name": "` + packageName + `",
    45  		"description": "` + packageDescription + `",
    46  		"type": "` + packageType + `",
    47  		"license": "` + packageLicense + `",
    48  		"authors": [
    49  			{
    50  				"name": "` + packageAuthor + `"
    51  			}
    52  		]
    53  	}`))
    54  	archive.Close()
    55  	content := buf.Bytes()
    56  
    57  	url := fmt.Sprintf("%sapi/packages/%s/composer", setting.AppURL, user.Name)
    58  
    59  	t.Run("ServiceIndex", func(t *testing.T) {
    60  		defer tests.PrintCurrentTest(t)()
    61  
    62  		req := NewRequest(t, "GET", fmt.Sprintf("%s/packages.json", url))
    63  		req = AddBasicAuthHeader(req, user.Name)
    64  		resp := MakeRequest(t, req, http.StatusOK)
    65  
    66  		var result composer.ServiceIndexResponse
    67  		DecodeJSON(t, resp, &result)
    68  
    69  		assert.Equal(t, url+"/search.json?q=%query%&type=%type%", result.SearchTemplate)
    70  		assert.Equal(t, url+"/p2/%package%.json", result.MetadataTemplate)
    71  		assert.Equal(t, url+"/list.json", result.PackageList)
    72  	})
    73  
    74  	t.Run("Upload", func(t *testing.T) {
    75  		t.Run("MissingVersion", func(t *testing.T) {
    76  			defer tests.PrintCurrentTest(t)()
    77  
    78  			req := NewRequestWithBody(t, "PUT", url, bytes.NewReader(content))
    79  			req = AddBasicAuthHeader(req, user.Name)
    80  			MakeRequest(t, req, http.StatusBadRequest)
    81  		})
    82  
    83  		t.Run("Valid", func(t *testing.T) {
    84  			defer tests.PrintCurrentTest(t)()
    85  
    86  			uploadURL := url + "?version=" + packageVersion
    87  
    88  			req := NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader(content))
    89  			req = AddBasicAuthHeader(req, user.Name)
    90  			MakeRequest(t, req, http.StatusCreated)
    91  
    92  			pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeComposer)
    93  			assert.NoError(t, err)
    94  			assert.Len(t, pvs, 1)
    95  
    96  			pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0])
    97  			assert.NoError(t, err)
    98  			assert.NotNil(t, pd.SemVer)
    99  			assert.IsType(t, &composer_module.Metadata{}, pd.Metadata)
   100  			assert.Equal(t, packageName, pd.Package.Name)
   101  			assert.Equal(t, packageVersion, pd.Version.Version)
   102  
   103  			pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID)
   104  			assert.NoError(t, err)
   105  			assert.Len(t, pfs, 1)
   106  			assert.Equal(t, fmt.Sprintf("%s-%s.%s.zip", vendorName, projectName, packageVersion), pfs[0].Name)
   107  			assert.True(t, pfs[0].IsLead)
   108  
   109  			pb, err := packages.GetBlobByID(db.DefaultContext, pfs[0].BlobID)
   110  			assert.NoError(t, err)
   111  			assert.Equal(t, int64(len(content)), pb.Size)
   112  
   113  			req = NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader(content))
   114  			req = AddBasicAuthHeader(req, user.Name)
   115  			MakeRequest(t, req, http.StatusBadRequest)
   116  		})
   117  	})
   118  
   119  	t.Run("Download", func(t *testing.T) {
   120  		defer tests.PrintCurrentTest(t)()
   121  
   122  		pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeComposer)
   123  		assert.NoError(t, err)
   124  		assert.Len(t, pvs, 1)
   125  		assert.Equal(t, int64(0), pvs[0].DownloadCount)
   126  
   127  		pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID)
   128  		assert.NoError(t, err)
   129  		assert.Len(t, pfs, 1)
   130  
   131  		req := NewRequest(t, "GET", fmt.Sprintf("%s/files/%s/%s/%s", url, neturl.PathEscape(packageName), neturl.PathEscape(pvs[0].LowerVersion), neturl.PathEscape(pfs[0].LowerName)))
   132  		req = AddBasicAuthHeader(req, user.Name)
   133  		resp := MakeRequest(t, req, http.StatusOK)
   134  
   135  		assert.Equal(t, content, resp.Body.Bytes())
   136  
   137  		pvs, err = packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeComposer)
   138  		assert.NoError(t, err)
   139  		assert.Len(t, pvs, 1)
   140  		assert.Equal(t, int64(1), pvs[0].DownloadCount)
   141  	})
   142  
   143  	t.Run("SearchService", func(t *testing.T) {
   144  		defer tests.PrintCurrentTest(t)()
   145  
   146  		cases := []struct {
   147  			Query           string
   148  			Type            string
   149  			Page            int
   150  			PerPage         int
   151  			ExpectedTotal   int64
   152  			ExpectedResults int
   153  		}{
   154  			{"", "", 0, 0, 1, 1},
   155  			{"", "", 1, 1, 1, 1},
   156  			{"test", "", 1, 0, 0, 0},
   157  			{"gitea", "", 1, 1, 1, 1},
   158  			{"gitea", "", 2, 1, 1, 0},
   159  			{"", packageType, 1, 1, 1, 1},
   160  			{"gitea", packageType, 1, 1, 1, 1},
   161  			{"gitea", "dummy", 1, 1, 0, 0},
   162  		}
   163  
   164  		for i, c := range cases {
   165  			req := NewRequest(t, "GET", fmt.Sprintf("%s/search.json?q=%s&type=%s&page=%d&per_page=%d", url, c.Query, c.Type, c.Page, c.PerPage))
   166  			req = AddBasicAuthHeader(req, user.Name)
   167  			resp := MakeRequest(t, req, http.StatusOK)
   168  
   169  			var result composer.SearchResultResponse
   170  			DecodeJSON(t, resp, &result)
   171  
   172  			assert.Equal(t, c.ExpectedTotal, result.Total, "case %d: unexpected total hits", i)
   173  			assert.Len(t, result.Results, c.ExpectedResults, "case %d: unexpected result count", i)
   174  		}
   175  	})
   176  
   177  	t.Run("EnumeratePackages", func(t *testing.T) {
   178  		defer tests.PrintCurrentTest(t)()
   179  
   180  		req := NewRequest(t, "GET", url+"/list.json")
   181  		req = AddBasicAuthHeader(req, user.Name)
   182  		resp := MakeRequest(t, req, http.StatusOK)
   183  
   184  		var result map[string][]string
   185  		DecodeJSON(t, resp, &result)
   186  
   187  		assert.Contains(t, result, "packageNames")
   188  		names := result["packageNames"]
   189  		assert.Len(t, names, 1)
   190  		assert.Equal(t, packageName, names[0])
   191  	})
   192  
   193  	t.Run("PackageMetadata", func(t *testing.T) {
   194  		defer tests.PrintCurrentTest(t)()
   195  
   196  		req := NewRequest(t, "GET", fmt.Sprintf("%s/p2/%s/%s.json", url, vendorName, projectName))
   197  		req = AddBasicAuthHeader(req, user.Name)
   198  		resp := MakeRequest(t, req, http.StatusOK)
   199  
   200  		var result composer.PackageMetadataResponse
   201  		DecodeJSON(t, resp, &result)
   202  
   203  		assert.Contains(t, result.Packages, packageName)
   204  		pkgs := result.Packages[packageName]
   205  		assert.Len(t, pkgs, 1)
   206  		assert.Equal(t, packageName, pkgs[0].Name)
   207  		assert.Equal(t, packageVersion, pkgs[0].Version)
   208  		assert.Equal(t, packageType, pkgs[0].Type)
   209  		assert.Equal(t, packageDescription, pkgs[0].Description)
   210  		assert.Len(t, pkgs[0].Authors, 1)
   211  		assert.Equal(t, packageAuthor, pkgs[0].Authors[0].Name)
   212  		assert.Equal(t, "zip", pkgs[0].Dist.Type)
   213  		assert.Equal(t, "7b40bfd6da811b2b78deec1e944f156dbb2c747b", pkgs[0].Dist.Checksum)
   214  	})
   215  }