code.gitea.io/gitea@v1.21.7/tests/integration/api_packages_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  	"net/http"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  
    14  	auth_model "code.gitea.io/gitea/models/auth"
    15  	"code.gitea.io/gitea/models/db"
    16  	packages_model "code.gitea.io/gitea/models/packages"
    17  	container_model "code.gitea.io/gitea/models/packages/container"
    18  	"code.gitea.io/gitea/models/unittest"
    19  	user_model "code.gitea.io/gitea/models/user"
    20  	"code.gitea.io/gitea/modules/setting"
    21  	api "code.gitea.io/gitea/modules/structs"
    22  	"code.gitea.io/gitea/modules/util"
    23  	packages_service "code.gitea.io/gitea/services/packages"
    24  	packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
    25  	"code.gitea.io/gitea/tests"
    26  
    27  	"github.com/minio/sha256-simd"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestPackageAPI(t *testing.T) {
    32  	defer tests.PrepareTestEnv(t)()
    33  
    34  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
    35  	session := loginUser(t, user.Name)
    36  	tokenReadPackage := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadPackage)
    37  	tokenDeletePackage := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWritePackage)
    38  
    39  	packageName := "test-package"
    40  	packageVersion := "1.0.3"
    41  	filename := "file.bin"
    42  
    43  	url := fmt.Sprintf("/api/packages/%s/generic/%s/%s/%s", user.Name, packageName, packageVersion, filename)
    44  	req := NewRequestWithBody(t, "PUT", url, bytes.NewReader([]byte{}))
    45  	AddBasicAuthHeader(req, user.Name)
    46  	MakeRequest(t, req, http.StatusCreated)
    47  
    48  	t.Run("ListPackages", func(t *testing.T) {
    49  		defer tests.PrintCurrentTest(t)()
    50  
    51  		req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s?token=%s", user.Name, tokenReadPackage))
    52  		resp := MakeRequest(t, req, http.StatusOK)
    53  
    54  		var apiPackages []*api.Package
    55  		DecodeJSON(t, resp, &apiPackages)
    56  
    57  		assert.Len(t, apiPackages, 1)
    58  		assert.Equal(t, string(packages_model.TypeGeneric), apiPackages[0].Type)
    59  		assert.Equal(t, packageName, apiPackages[0].Name)
    60  		assert.Equal(t, packageVersion, apiPackages[0].Version)
    61  		assert.NotNil(t, apiPackages[0].Creator)
    62  		assert.Equal(t, user.Name, apiPackages[0].Creator.UserName)
    63  	})
    64  
    65  	t.Run("GetPackage", func(t *testing.T) {
    66  		defer tests.PrintCurrentTest(t)()
    67  
    68  		req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s/dummy/%s/%s?token=%s", user.Name, packageName, packageVersion, tokenReadPackage))
    69  		MakeRequest(t, req, http.StatusNotFound)
    70  
    71  		req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s/generic/%s/%s?token=%s", user.Name, packageName, packageVersion, tokenReadPackage))
    72  		resp := MakeRequest(t, req, http.StatusOK)
    73  
    74  		var p *api.Package
    75  		DecodeJSON(t, resp, &p)
    76  
    77  		assert.Equal(t, string(packages_model.TypeGeneric), p.Type)
    78  		assert.Equal(t, packageName, p.Name)
    79  		assert.Equal(t, packageVersion, p.Version)
    80  		assert.NotNil(t, p.Creator)
    81  		assert.Equal(t, user.Name, p.Creator.UserName)
    82  
    83  		t.Run("RepositoryLink", func(t *testing.T) {
    84  			defer tests.PrintCurrentTest(t)()
    85  
    86  			p, err := packages_model.GetPackageByName(db.DefaultContext, user.ID, packages_model.TypeGeneric, packageName)
    87  			assert.NoError(t, err)
    88  
    89  			// no repository link
    90  			req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s/generic/%s/%s?token=%s", user.Name, packageName, packageVersion, tokenReadPackage))
    91  			resp := MakeRequest(t, req, http.StatusOK)
    92  
    93  			var ap1 *api.Package
    94  			DecodeJSON(t, resp, &ap1)
    95  			assert.Nil(t, ap1.Repository)
    96  
    97  			// link to public repository
    98  			assert.NoError(t, packages_model.SetRepositoryLink(db.DefaultContext, p.ID, 1))
    99  
   100  			req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s/generic/%s/%s?token=%s", user.Name, packageName, packageVersion, tokenReadPackage))
   101  			resp = MakeRequest(t, req, http.StatusOK)
   102  
   103  			var ap2 *api.Package
   104  			DecodeJSON(t, resp, &ap2)
   105  			assert.NotNil(t, ap2.Repository)
   106  			assert.EqualValues(t, 1, ap2.Repository.ID)
   107  
   108  			// link to private repository
   109  			assert.NoError(t, packages_model.SetRepositoryLink(db.DefaultContext, p.ID, 2))
   110  
   111  			req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s/generic/%s/%s?token=%s", user.Name, packageName, packageVersion, tokenReadPackage))
   112  			resp = MakeRequest(t, req, http.StatusOK)
   113  
   114  			var ap3 *api.Package
   115  			DecodeJSON(t, resp, &ap3)
   116  			assert.Nil(t, ap3.Repository)
   117  
   118  			assert.NoError(t, packages_model.UnlinkRepositoryFromAllPackages(db.DefaultContext, 2))
   119  		})
   120  	})
   121  
   122  	t.Run("ListPackageFiles", func(t *testing.T) {
   123  		defer tests.PrintCurrentTest(t)()
   124  
   125  		req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s/dummy/%s/%s/files?token=%s", user.Name, packageName, packageVersion, tokenReadPackage))
   126  		MakeRequest(t, req, http.StatusNotFound)
   127  
   128  		req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s/generic/%s/%s/files?token=%s", user.Name, packageName, packageVersion, tokenReadPackage))
   129  		resp := MakeRequest(t, req, http.StatusOK)
   130  
   131  		var files []*api.PackageFile
   132  		DecodeJSON(t, resp, &files)
   133  
   134  		assert.Len(t, files, 1)
   135  		assert.Equal(t, int64(0), files[0].Size)
   136  		assert.Equal(t, filename, files[0].Name)
   137  		assert.Equal(t, "d41d8cd98f00b204e9800998ecf8427e", files[0].HashMD5)
   138  		assert.Equal(t, "da39a3ee5e6b4b0d3255bfef95601890afd80709", files[0].HashSHA1)
   139  		assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", files[0].HashSHA256)
   140  		assert.Equal(t, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", files[0].HashSHA512)
   141  	})
   142  
   143  	t.Run("DeletePackage", func(t *testing.T) {
   144  		defer tests.PrintCurrentTest(t)()
   145  
   146  		req := NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/packages/%s/dummy/%s/%s?token=%s", user.Name, packageName, packageVersion, tokenDeletePackage))
   147  		MakeRequest(t, req, http.StatusNotFound)
   148  
   149  		req = NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/packages/%s/generic/%s/%s?token=%s", user.Name, packageName, packageVersion, tokenDeletePackage))
   150  		MakeRequest(t, req, http.StatusNoContent)
   151  	})
   152  }
   153  
   154  func TestPackageAccess(t *testing.T) {
   155  	defer tests.PrepareTestEnv(t)()
   156  
   157  	admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
   158  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
   159  	inactive := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 9})
   160  	limitedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 33})
   161  	privateUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 31})
   162  	privateOrgMember := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 23}) // user has package write access
   163  	limitedOrgMember := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 36}) // user has package write access
   164  	publicOrgMember := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 25})  // user has package read access
   165  	privateOrgNoMember := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 35})
   166  	limitedOrgNoMember := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 22})
   167  	publicOrgNoMember := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 17})
   168  
   169  	uploadPackage := func(doer, owner *user_model.User, filename string, expectedStatus int) {
   170  		url := fmt.Sprintf("/api/packages/%s/generic/test-package/1.0/%s.bin", owner.Name, filename)
   171  		req := NewRequestWithBody(t, "PUT", url, bytes.NewReader([]byte{1}))
   172  		if doer != nil {
   173  			AddBasicAuthHeader(req, doer.Name)
   174  		}
   175  		MakeRequest(t, req, expectedStatus)
   176  	}
   177  
   178  	downloadPackage := func(doer, owner *user_model.User, expectedStatus int) {
   179  		url := fmt.Sprintf("/api/packages/%s/generic/test-package/1.0/admin.bin", owner.Name)
   180  		req := NewRequest(t, "GET", url)
   181  		if doer != nil {
   182  			AddBasicAuthHeader(req, doer.Name)
   183  		}
   184  		MakeRequest(t, req, expectedStatus)
   185  	}
   186  
   187  	type Target struct {
   188  		Owner          *user_model.User
   189  		ExpectedStatus int
   190  	}
   191  
   192  	t.Run("Upload", func(t *testing.T) {
   193  		defer tests.PrintCurrentTest(t)()
   194  
   195  		cases := []struct {
   196  			Doer     *user_model.User
   197  			Filename string
   198  			Targets  []Target
   199  		}{
   200  			{ // Admins can upload to every owner
   201  				Doer:     admin,
   202  				Filename: "admin",
   203  				Targets: []Target{
   204  					{admin, http.StatusCreated},
   205  					{inactive, http.StatusCreated},
   206  					{user, http.StatusCreated},
   207  					{limitedUser, http.StatusCreated},
   208  					{privateUser, http.StatusCreated},
   209  					{privateOrgMember, http.StatusCreated},
   210  					{limitedOrgMember, http.StatusCreated},
   211  					{publicOrgMember, http.StatusCreated},
   212  					{privateOrgNoMember, http.StatusCreated},
   213  					{limitedOrgNoMember, http.StatusCreated},
   214  					{publicOrgNoMember, http.StatusCreated},
   215  				},
   216  			},
   217  			{ // Without credentials no upload should be possible
   218  				Doer:     nil,
   219  				Filename: "nil",
   220  				Targets: []Target{
   221  					{admin, http.StatusUnauthorized},
   222  					{inactive, http.StatusUnauthorized},
   223  					{user, http.StatusUnauthorized},
   224  					{limitedUser, http.StatusUnauthorized},
   225  					{privateUser, http.StatusUnauthorized},
   226  					{privateOrgMember, http.StatusUnauthorized},
   227  					{limitedOrgMember, http.StatusUnauthorized},
   228  					{publicOrgMember, http.StatusUnauthorized},
   229  					{privateOrgNoMember, http.StatusUnauthorized},
   230  					{limitedOrgNoMember, http.StatusUnauthorized},
   231  					{publicOrgNoMember, http.StatusUnauthorized},
   232  				},
   233  			},
   234  			{ // Inactive users can't upload anywhere
   235  				Doer:     inactive,
   236  				Filename: "inactive",
   237  				Targets: []Target{
   238  					{admin, http.StatusUnauthorized},
   239  					{inactive, http.StatusUnauthorized},
   240  					{user, http.StatusUnauthorized},
   241  					{limitedUser, http.StatusUnauthorized},
   242  					{privateUser, http.StatusUnauthorized},
   243  					{privateOrgMember, http.StatusUnauthorized},
   244  					{limitedOrgMember, http.StatusUnauthorized},
   245  					{publicOrgMember, http.StatusUnauthorized},
   246  					{privateOrgNoMember, http.StatusUnauthorized},
   247  					{limitedOrgNoMember, http.StatusUnauthorized},
   248  					{publicOrgNoMember, http.StatusUnauthorized},
   249  				},
   250  			},
   251  			{ // Normal users can upload to self and orgs in which they are members and have package write access
   252  				Doer:     user,
   253  				Filename: "user",
   254  				Targets: []Target{
   255  					{admin, http.StatusUnauthorized},
   256  					{inactive, http.StatusUnauthorized},
   257  					{user, http.StatusCreated},
   258  					{limitedUser, http.StatusUnauthorized},
   259  					{privateUser, http.StatusUnauthorized},
   260  					{privateOrgMember, http.StatusCreated},
   261  					{limitedOrgMember, http.StatusCreated},
   262  					{publicOrgMember, http.StatusUnauthorized},
   263  					{privateOrgNoMember, http.StatusUnauthorized},
   264  					{limitedOrgNoMember, http.StatusUnauthorized},
   265  					{publicOrgNoMember, http.StatusUnauthorized},
   266  				},
   267  			},
   268  		}
   269  
   270  		for _, c := range cases {
   271  			for _, t := range c.Targets {
   272  				uploadPackage(c.Doer, t.Owner, c.Filename, t.ExpectedStatus)
   273  			}
   274  		}
   275  	})
   276  
   277  	t.Run("Download", func(t *testing.T) {
   278  		defer tests.PrintCurrentTest(t)()
   279  
   280  		cases := []struct {
   281  			Doer     *user_model.User
   282  			Filename string
   283  			Targets  []Target
   284  		}{
   285  			{ // Admins can access everything
   286  				Doer: admin,
   287  				Targets: []Target{
   288  					{admin, http.StatusOK},
   289  					{inactive, http.StatusOK},
   290  					{user, http.StatusOK},
   291  					{limitedUser, http.StatusOK},
   292  					{privateUser, http.StatusOK},
   293  					{privateOrgMember, http.StatusOK},
   294  					{limitedOrgMember, http.StatusOK},
   295  					{publicOrgMember, http.StatusOK},
   296  					{privateOrgNoMember, http.StatusOK},
   297  					{limitedOrgNoMember, http.StatusOK},
   298  					{publicOrgNoMember, http.StatusOK},
   299  				},
   300  			},
   301  			{ // Without credentials only public owners are accessible
   302  				Doer: nil,
   303  				Targets: []Target{
   304  					{admin, http.StatusOK},
   305  					{inactive, http.StatusOK},
   306  					{user, http.StatusOK},
   307  					{limitedUser, http.StatusUnauthorized},
   308  					{privateUser, http.StatusUnauthorized},
   309  					{privateOrgMember, http.StatusUnauthorized},
   310  					{limitedOrgMember, http.StatusUnauthorized},
   311  					{publicOrgMember, http.StatusOK},
   312  					{privateOrgNoMember, http.StatusUnauthorized},
   313  					{limitedOrgNoMember, http.StatusUnauthorized},
   314  					{publicOrgNoMember, http.StatusOK},
   315  				},
   316  			},
   317  			{ // Inactive users have no access
   318  				Doer: inactive,
   319  				Targets: []Target{
   320  					{admin, http.StatusUnauthorized},
   321  					{inactive, http.StatusUnauthorized},
   322  					{user, http.StatusUnauthorized},
   323  					{limitedUser, http.StatusUnauthorized},
   324  					{privateUser, http.StatusUnauthorized},
   325  					{privateOrgMember, http.StatusUnauthorized},
   326  					{limitedOrgMember, http.StatusUnauthorized},
   327  					{publicOrgMember, http.StatusUnauthorized},
   328  					{privateOrgNoMember, http.StatusUnauthorized},
   329  					{limitedOrgNoMember, http.StatusUnauthorized},
   330  					{publicOrgNoMember, http.StatusUnauthorized},
   331  				},
   332  			},
   333  			{ // Normal users can access self, public or limited users/orgs and private orgs in which they are members
   334  				Doer: user,
   335  				Targets: []Target{
   336  					{admin, http.StatusOK},
   337  					{inactive, http.StatusOK},
   338  					{user, http.StatusOK},
   339  					{limitedUser, http.StatusOK},
   340  					{privateUser, http.StatusUnauthorized},
   341  					{privateOrgMember, http.StatusOK},
   342  					{limitedOrgMember, http.StatusOK},
   343  					{publicOrgMember, http.StatusOK},
   344  					{privateOrgNoMember, http.StatusUnauthorized},
   345  					{limitedOrgNoMember, http.StatusOK},
   346  					{publicOrgNoMember, http.StatusOK},
   347  				},
   348  			},
   349  		}
   350  
   351  		for _, c := range cases {
   352  			for _, target := range c.Targets {
   353  				downloadPackage(c.Doer, target.Owner, target.ExpectedStatus)
   354  			}
   355  		}
   356  	})
   357  
   358  	t.Run("API", func(t *testing.T) {
   359  		defer tests.PrintCurrentTest(t)()
   360  
   361  		session := loginUser(t, user.Name)
   362  		tokenReadPackage := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadPackage)
   363  
   364  		for _, target := range []Target{
   365  			{admin, http.StatusOK},
   366  			{inactive, http.StatusOK},
   367  			{user, http.StatusOK},
   368  			{limitedUser, http.StatusOK},
   369  			{privateUser, http.StatusForbidden},
   370  			{privateOrgMember, http.StatusOK},
   371  			{limitedOrgMember, http.StatusOK},
   372  			{publicOrgMember, http.StatusOK},
   373  			{privateOrgNoMember, http.StatusForbidden},
   374  			{limitedOrgNoMember, http.StatusOK},
   375  			{publicOrgNoMember, http.StatusOK},
   376  		} {
   377  			req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/packages/%s?token=%s", target.Owner.Name, tokenReadPackage))
   378  			MakeRequest(t, req, target.ExpectedStatus)
   379  		}
   380  	})
   381  }
   382  
   383  func TestPackageQuota(t *testing.T) {
   384  	defer tests.PrepareTestEnv(t)()
   385  
   386  	limitTotalOwnerCount, limitTotalOwnerSize := setting.Packages.LimitTotalOwnerCount, setting.Packages.LimitTotalOwnerSize
   387  
   388  	// Exceeded quota result in StatusForbidden for normal users but admins are always allowed to upload.
   389  	admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
   390  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 10})
   391  
   392  	t.Run("Common", func(t *testing.T) {
   393  		defer tests.PrintCurrentTest(t)()
   394  
   395  		limitSizeGeneric := setting.Packages.LimitSizeGeneric
   396  
   397  		uploadPackage := func(doer *user_model.User, version string, expectedStatus int) {
   398  			url := fmt.Sprintf("/api/packages/%s/generic/test-package/%s/file.bin", user.Name, version)
   399  			req := NewRequestWithBody(t, "PUT", url, bytes.NewReader([]byte{1}))
   400  			AddBasicAuthHeader(req, doer.Name)
   401  			MakeRequest(t, req, expectedStatus)
   402  		}
   403  
   404  		setting.Packages.LimitTotalOwnerCount = 0
   405  		uploadPackage(user, "1.0", http.StatusForbidden)
   406  		uploadPackage(admin, "1.0", http.StatusCreated)
   407  		setting.Packages.LimitTotalOwnerCount = limitTotalOwnerCount
   408  
   409  		setting.Packages.LimitTotalOwnerSize = 0
   410  		uploadPackage(user, "1.1", http.StatusForbidden)
   411  		uploadPackage(admin, "1.1", http.StatusCreated)
   412  		setting.Packages.LimitTotalOwnerSize = limitTotalOwnerSize
   413  
   414  		setting.Packages.LimitSizeGeneric = 0
   415  		uploadPackage(user, "1.2", http.StatusForbidden)
   416  		uploadPackage(admin, "1.2", http.StatusCreated)
   417  		setting.Packages.LimitSizeGeneric = limitSizeGeneric
   418  	})
   419  
   420  	t.Run("Container", func(t *testing.T) {
   421  		defer tests.PrintCurrentTest(t)()
   422  
   423  		limitSizeContainer := setting.Packages.LimitSizeContainer
   424  
   425  		uploadBlob := func(doer *user_model.User, data string, expectedStatus int) {
   426  			url := fmt.Sprintf("/v2/%s/quota-test/blobs/uploads?digest=sha256:%x", user.Name, sha256.Sum256([]byte(data)))
   427  			req := NewRequestWithBody(t, "POST", url, strings.NewReader(data))
   428  			AddBasicAuthHeader(req, doer.Name)
   429  			MakeRequest(t, req, expectedStatus)
   430  		}
   431  
   432  		setting.Packages.LimitTotalOwnerSize = 0
   433  		uploadBlob(user, "2", http.StatusForbidden)
   434  		uploadBlob(admin, "2", http.StatusCreated)
   435  		setting.Packages.LimitTotalOwnerSize = limitTotalOwnerSize
   436  
   437  		setting.Packages.LimitSizeContainer = 0
   438  		uploadBlob(user, "3", http.StatusForbidden)
   439  		uploadBlob(admin, "3", http.StatusCreated)
   440  		setting.Packages.LimitSizeContainer = limitSizeContainer
   441  	})
   442  }
   443  
   444  func TestPackageCleanup(t *testing.T) {
   445  	defer tests.PrepareTestEnv(t)()
   446  
   447  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
   448  
   449  	duration, _ := time.ParseDuration("-1h")
   450  
   451  	t.Run("Common", func(t *testing.T) {
   452  		defer tests.PrintCurrentTest(t)()
   453  
   454  		// Upload and delete a generic package and upload a container blob
   455  		data, _ := util.CryptoRandomBytes(5)
   456  		url := fmt.Sprintf("/api/packages/%s/generic/cleanup-test/1.1.1/file.bin", user.Name)
   457  		req := NewRequestWithBody(t, "PUT", url, bytes.NewReader(data))
   458  		AddBasicAuthHeader(req, user.Name)
   459  		MakeRequest(t, req, http.StatusCreated)
   460  
   461  		req = NewRequest(t, "DELETE", url)
   462  		AddBasicAuthHeader(req, user.Name)
   463  		MakeRequest(t, req, http.StatusNoContent)
   464  
   465  		data, _ = util.CryptoRandomBytes(5)
   466  		url = fmt.Sprintf("/v2/%s/cleanup-test/blobs/uploads?digest=sha256:%x", user.Name, sha256.Sum256(data))
   467  		req = NewRequestWithBody(t, "POST", url, bytes.NewReader(data))
   468  		AddBasicAuthHeader(req, user.Name)
   469  		MakeRequest(t, req, http.StatusCreated)
   470  
   471  		pbs, err := packages_model.FindExpiredUnreferencedBlobs(db.DefaultContext, duration)
   472  		assert.NoError(t, err)
   473  		assert.NotEmpty(t, pbs)
   474  
   475  		_, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, "cleanup-test", container_model.UploadVersion)
   476  		assert.NoError(t, err)
   477  
   478  		err = packages_cleanup_service.CleanupTask(db.DefaultContext, duration)
   479  		assert.NoError(t, err)
   480  
   481  		pbs, err = packages_model.FindExpiredUnreferencedBlobs(db.DefaultContext, duration)
   482  		assert.NoError(t, err)
   483  		assert.Empty(t, pbs)
   484  
   485  		_, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, "cleanup-test", container_model.UploadVersion)
   486  		assert.ErrorIs(t, err, packages_model.ErrPackageNotExist)
   487  	})
   488  
   489  	t.Run("CleanupRules", func(t *testing.T) {
   490  		defer tests.PrintCurrentTest(t)()
   491  
   492  		type version struct {
   493  			Version     string
   494  			ShouldExist bool
   495  			Created     int64
   496  		}
   497  
   498  		cases := []struct {
   499  			Name     string
   500  			Versions []version
   501  			Rule     *packages_model.PackageCleanupRule
   502  		}{
   503  			{
   504  				Name: "Disabled",
   505  				Versions: []version{
   506  					{Version: "keep", ShouldExist: true},
   507  				},
   508  				Rule: &packages_model.PackageCleanupRule{
   509  					Enabled: false,
   510  				},
   511  			},
   512  			{
   513  				Name: "KeepCount",
   514  				Versions: []version{
   515  					{Version: "keep", ShouldExist: true},
   516  					{Version: "v1.0", ShouldExist: true},
   517  					{Version: "test-3", ShouldExist: false, Created: 1},
   518  					{Version: "test-4", ShouldExist: false, Created: 1},
   519  				},
   520  				Rule: &packages_model.PackageCleanupRule{
   521  					Enabled:   true,
   522  					KeepCount: 2,
   523  				},
   524  			},
   525  			{
   526  				Name: "KeepPattern",
   527  				Versions: []version{
   528  					{Version: "keep", ShouldExist: true},
   529  					{Version: "v1.0", ShouldExist: false},
   530  				},
   531  				Rule: &packages_model.PackageCleanupRule{
   532  					Enabled:     true,
   533  					KeepPattern: "k.+p",
   534  				},
   535  			},
   536  			{
   537  				Name: "RemoveDays",
   538  				Versions: []version{
   539  					{Version: "keep", ShouldExist: true},
   540  					{Version: "v1.0", ShouldExist: false, Created: 1},
   541  				},
   542  				Rule: &packages_model.PackageCleanupRule{
   543  					Enabled:    true,
   544  					RemoveDays: 60,
   545  				},
   546  			},
   547  			{
   548  				Name: "RemovePattern",
   549  				Versions: []version{
   550  					{Version: "test", ShouldExist: true},
   551  					{Version: "test-3", ShouldExist: false},
   552  					{Version: "test-4", ShouldExist: false},
   553  				},
   554  				Rule: &packages_model.PackageCleanupRule{
   555  					Enabled:       true,
   556  					RemovePattern: `t[e]+st-\d+`,
   557  				},
   558  			},
   559  			{
   560  				Name: "MatchFullName",
   561  				Versions: []version{
   562  					{Version: "keep", ShouldExist: true},
   563  					{Version: "test", ShouldExist: false},
   564  				},
   565  				Rule: &packages_model.PackageCleanupRule{
   566  					Enabled:       true,
   567  					RemovePattern: `package/test|different/keep`,
   568  					MatchFullName: true,
   569  				},
   570  			},
   571  			{
   572  				Name: "Mixed",
   573  				Versions: []version{
   574  					{Version: "keep", ShouldExist: true, Created: time.Now().Add(time.Duration(10000)).Unix()},
   575  					{Version: "dummy", ShouldExist: true, Created: 1},
   576  					{Version: "test-3", ShouldExist: true},
   577  					{Version: "test-4", ShouldExist: false, Created: 1},
   578  				},
   579  				Rule: &packages_model.PackageCleanupRule{
   580  					Enabled:       true,
   581  					KeepCount:     1,
   582  					KeepPattern:   `dummy`,
   583  					RemoveDays:    7,
   584  					RemovePattern: `t[e]+st-\d+`,
   585  				},
   586  			},
   587  		}
   588  
   589  		for _, c := range cases {
   590  			t.Run(c.Name, func(t *testing.T) {
   591  				defer tests.PrintCurrentTest(t)()
   592  
   593  				for _, v := range c.Versions {
   594  					url := fmt.Sprintf("/api/packages/%s/generic/package/%s/file.bin", user.Name, v.Version)
   595  					req := NewRequestWithBody(t, "PUT", url, bytes.NewReader([]byte{1}))
   596  					AddBasicAuthHeader(req, user.Name)
   597  					MakeRequest(t, req, http.StatusCreated)
   598  
   599  					if v.Created != 0 {
   600  						pv, err := packages_model.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeGeneric, "package", v.Version)
   601  						assert.NoError(t, err)
   602  						_, err = db.GetEngine(db.DefaultContext).Exec("UPDATE package_version SET created_unix = ? WHERE id = ?", v.Created, pv.ID)
   603  						assert.NoError(t, err)
   604  					}
   605  				}
   606  
   607  				c.Rule.OwnerID = user.ID
   608  				c.Rule.Type = packages_model.TypeGeneric
   609  
   610  				pcr, err := packages_model.InsertCleanupRule(db.DefaultContext, c.Rule)
   611  				assert.NoError(t, err)
   612  
   613  				err = packages_cleanup_service.CleanupTask(db.DefaultContext, duration)
   614  				assert.NoError(t, err)
   615  
   616  				for _, v := range c.Versions {
   617  					pv, err := packages_model.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeGeneric, "package", v.Version)
   618  					if v.ShouldExist {
   619  						assert.NoError(t, err)
   620  						err = packages_service.DeletePackageVersionAndReferences(db.DefaultContext, pv)
   621  						assert.NoError(t, err)
   622  					} else {
   623  						assert.ErrorIs(t, err, packages_model.ErrPackageNotExist)
   624  					}
   625  				}
   626  
   627  				assert.NoError(t, packages_model.DeleteCleanupRuleByID(db.DefaultContext, pcr.ID))
   628  			})
   629  		}
   630  	})
   631  }