aboutsummaryrefslogtreecommitdiffstats
path: root/tests/integration/api_packages_chef_test.go
blob: 0ee43e17418606fd884570d58b48b11c29d210db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"crypto"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha1"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"fmt"
	"hash"
	"math/big"
	"mime/multipart"
	"net/http"
	"path"
	"strings"
	"testing"
	"time"

	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/models/packages"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	chef_module "code.gitea.io/gitea/modules/packages/chef"
	"code.gitea.io/gitea/modules/setting"
	chef_router "code.gitea.io/gitea/routers/api/packages/chef"
	"code.gitea.io/gitea/tests"

	"github.com/minio/sha256-simd"
	"github.com/stretchr/testify/assert"
)

func TestPackageChef(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

	privPem := `-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAtWp2PZz4TSU5A6ixw41HdbfBuGJwPuTtrsdoUf0DQ0/DJBNP
qOCBAgEu6ZdUqIbWJ5Da+nevjtncy5hENdi6XrXjyzlUxghMuXjE5SeLGpgfQvkq
bTkYaFpMe8PTzNeze3fei8+Eu6mzeb6g1GrqXznuPIc7bNss0w5iX9RiBM9dWPuX
onx9xSEy0LYqJm7yXmshNe1aRwkjG/y5C26BzBFnMKp9YRTua0DO1WqLNhcaRnda
lIFYouDNVTbwxSlYL16bZVoebqzZvLGrPvZJkPuCu6vH9brvOuYo0q8hLVNkBeXc
imRpsDjLhQYzEJjoMTbaiVGnjBky+PWNiofJnwIDAQABAoIBAQCotF1KxLt/ejr/
9ROCh9JJXV3v6tL5GgkSPOv9Oq2bHgSZer/cixJNW+5VWd5nbiSe3K1WuJBw5pbW
Wj4sWORPiRRR+3mjQzqeS/nGJDTOwWJo9K8IrUzOVhLEEYLX/ksxaXJyT8PehFyb
vbNwdhCIB6ZNcXDItTWE+95twWJ5lxAIj2dNwZZni3UkwwjYnCnqFtvHCKOg0NH2
RjQcFYmu3fncNeqLezUSdVyRyXxSCHsUdlYeX/e44StCnXdrmLUHlb2P27ZVdPGh
SW7qTUPpmJKekYiRPOpTLj+ZKXIsANkyWO+7dVtZLBm5bIyAsmp0W/DmK+wRsejj
alFbIsh5AoGBANJr7HSG695wkfn+kvu/V8qHbt+KDv4WjWHjGRsUqvxoHOUNkQmW
vZWdk4gjHYn1l+QHWmoOE3AgyqtCZ4bFILkZPLN/F8Mh3+r4B0Ac4biJJt7XGMNQ
Nv4wsk7TR7CCARsjO7GP1PT60hpjMvYmc1E36gNM7QIZE9jBE+L8eWYtAoGBANy2
JOAWf+QeBlur6o9feH76cEmpQzUUq4Lj9mmnXgIirSsFoBnDb8VA6Ws+ltL9U9H2
vaCoaTyi9twW9zWj+Ywg2mVR5nlSAPfdlTWS1GLUbDotlj5apc/lvnGuNlWzN+I4
Tu64hhgBXqGvRZ0o7HzFodqRAkpVXp6CQCqBM7p7AoGAIgO0K3oL8t87ma/fTra1
mFWgRJ5qogQ/Qo2VZ11F7ptd4GD7CxPE/cSFLsKOadi7fu75XJ994OhMGrcXSR/g
lEtSFqn6y15UdgU2FtUUX+I72FXo+Nmkqh5xFHDu68d4Kkzdv2xCvn81K3LRsByz
E3P4biQnQ+mN3cIIVu79KNkCgYEAm6uctrEn4y2KLn5DInyj8GuTZ2ELFhVOIzPG
SR7TH451tTJyiblezDHMcOfkWUx0IlN1zCr8jtgiZXmNQzg0erFxWKU7ebZtGGYh
J3g4dLx+2Unt/mzRJqFUgbnueOO/Nr+gbJ+ZdLUCmeeVohOLOTXrws0kYGl2Izab
K1+VrKECgYEAxQohoOegA0f4mofisXItbwwqTIX3bLpxBc4woa1sB4kjNrLo4slc
qtWZGVlRxwBvQUg0cYj+xtr5nyBdHLy0qwX/kMq4GqQnvW6NqsbrP3MjCZ8NX/Sj
A2W0jx50Hs/XNw6IZFLYgWVoOzCaD+jYFpHhzUZyQD6/rYhwhHrNQmU=
-----END RSA PRIVATE KEY-----`

	tmp, _ := pem.Decode([]byte(privPem))
	privKey, _ := x509.ParsePKCS1PrivateKey(tmp.Bytes)

	pubPem := `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtWp2PZz4TSU5A6ixw41H
dbfBuGJwPuTtrsdoUf0DQ0/DJBNPqOCBAgEu6ZdUqIbWJ5Da+nevjtncy5hENdi6
XrXjyzlUxghMuXjE5SeLGpgfQvkqbTkYaFpMe8PTzNeze3fei8+Eu6mzeb6g1Grq
XznuPIc7bNss0w5iX9RiBM9dWPuXonx9xSEy0LYqJm7yXmshNe1aRwkjG/y5C26B
zBFnMKp9YRTua0DO1WqLNhcaRndalIFYouDNVTbwxSlYL16bZVoebqzZvLGrPvZJ
kPuCu6vH9brvOuYo0q8hLVNkBeXcimRpsDjLhQYzEJjoMTbaiVGnjBky+PWNiofJ
nwIDAQAB
-----END PUBLIC KEY-----`

	err := user_model.SetUserSetting(user.ID, chef_module.SettingPublicPem, pubPem)
	assert.NoError(t, err)

	t.Run("Authenticate", func(t *testing.T) {
		auth := &chef_router.Auth{}

		t.Run("MissingUser", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "POST", "/dummy")
			u, err := auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.NoError(t, err)
		})

		t.Run("NotExistingUser", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "POST", "/dummy")
			req.Header.Set("X-Ops-Userid", "not-existing-user")
			u, err := auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)
		})

		t.Run("Timestamp", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "POST", "/dummy")
			req.Header.Set("X-Ops-Userid", user.Name)
			u, err := auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)

			req.Header.Set("X-Ops-Timestamp", "2023-01-01T00:00:00Z")
			u, err = auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)
		})

		t.Run("SigningVersion", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "POST", "/dummy")
			req.Header.Set("X-Ops-Userid", user.Name)
			req.Header.Set("X-Ops-Timestamp", time.Now().UTC().Format(time.RFC3339))
			u, err := auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)

			req.Header.Set("X-Ops-Sign", "version=none")
			u, err = auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)

			req.Header.Set("X-Ops-Sign", "version=1.4")
			u, err = auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)

			req.Header.Set("X-Ops-Sign", "version=1.0;algorithm=sha2")
			u, err = auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)

			req.Header.Set("X-Ops-Sign", "version=1.0;algorithm=sha256")
			u, err = auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)
		})

		t.Run("SignedHeaders", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			ts := time.Now().UTC().Format(time.RFC3339)

			req := NewRequest(t, "POST", "/dummy")
			req.Header.Set("X-Ops-Userid", user.Name)
			req.Header.Set("X-Ops-Timestamp", ts)
			req.Header.Set("X-Ops-Sign", "version=1.0;algorithm=sha1")
			req.Header.Set("X-Ops-Content-Hash", "unused")
			req.Header.Set("X-Ops-Authorization-4", "dummy")
			u, err := auth.Verify(req, nil, nil, nil)
			assert.Nil(t, u)
			assert.Error(t, err)

			signRequest := func(t *testing.T, req *http.Request, version string) {
				username := req.Header.Get("X-Ops-Userid")
				if version != "1.0" && version != "1.3" {
					sum := sha1.Sum([]byte(username))
					username = base64.StdEncoding.EncodeToString(sum[:])
				}

				req.Header.Set("X-Ops-Sign", "version="+version)

				var data []byte
				if version == "1.3" {
					data = []byte(fmt.Sprintf(
						"Method:%s\nPath:%s\nX-Ops-Content-Hash:%s\nX-Ops-Sign:version=%s\nX-Ops-Timestamp:%s\nX-Ops-UserId:%s\nX-Ops-Server-API-Version:%s",
						req.Method,
						path.Clean(req.URL.Path),
						req.Header.Get("X-Ops-Content-Hash"),
						version,
						req.Header.Get("X-Ops-Timestamp"),
						username,
						req.Header.Get("X-Ops-Server-Api-Version"),
					))
				} else {
					sum := sha1.Sum([]byte(path.Clean(req.URL.Path)))
					data = []byte(fmt.Sprintf(
						"Method:%s\nHashed Path:%s\nX-Ops-Content-Hash:%s\nX-Ops-Timestamp:%s\nX-Ops-UserId:%s",
						req.Method,
						base64.StdEncoding.EncodeToString(sum[:]),
						req.Header.Get("X-Ops-Content-Hash"),
						req.Header.Get("X-Ops-Timestamp"),
						username,
					))
				}

				for k := range req.Header {
					if strings.HasPrefix(k, "X-Ops-Authorization-") {
						req.Header.Del(k)
					}
				}

				var signature []byte
				if version == "1.3" || version == "1.2" {
					var h hash.Hash
					var ch crypto.Hash
					if version == "1.3" {
						h = sha256.New()
						ch = crypto.SHA256
					} else {
						h = sha1.New()
						ch = crypto.SHA1
					}
					h.Write(data)

					signature, _ = rsa.SignPKCS1v15(rand.Reader, privKey, ch, h.Sum(nil))
				} else {
					c := new(big.Int).SetBytes(data)
					m := new(big.Int).Exp(c, privKey.D, privKey.N)

					signature = m.Bytes()
				}

				enc := base64.StdEncoding.EncodeToString(signature)

				const chunkSize = 60
				chunks := make([]string, 0, (len(enc)-1)/chunkSize+1)
				currentLen := 0
				currentStart := 0
				for i := range enc {
					if currentLen == chunkSize {
						chunks = append(chunks, enc[currentStart:i])
						currentLen = 0
						currentStart = i
					}
					currentLen++
				}
				chunks = append(chunks, enc[currentStart:])

				for i, chunk := range chunks {
					req.Header.Set(fmt.Sprintf("X-Ops-Authorization-%d", i+1), chunk)
				}
			}

			for _, v := range []string{"1.0", "1.1", "1.2", "1.3"} {
				t.Run(v, func(t *testing.T) {
					defer tests.PrintCurrentTest(t)()

					signRequest(t, req, v)
					u, err = auth.Verify(req, nil, nil, nil)
					assert.NotNil(t, u)
					assert.NoError(t, err)
				})
			}
		})
	})

	packageName := "test"
	packageVersion := "1.0.1"
	packageDescription := "Test Description"
	packageAuthor := "KN4CK3R"

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

	uploadPackage := func(t *testing.T, version string, expectedStatus int) {
		var body bytes.Buffer
		mpw := multipart.NewWriter(&body)
		part, _ := mpw.CreateFormFile("tarball", fmt.Sprintf("%s.tar.gz", version))
		zw := gzip.NewWriter(part)
		tw := tar.NewWriter(zw)

		content := `{"name":"` + packageName + `","version":"` + version + `","description":"` + packageDescription + `","maintainer":"` + packageAuthor + `"}`

		hdr := &tar.Header{
			Name: packageName + "/metadata.json",
			Mode: 0o600,
			Size: int64(len(content)),
		}
		tw.WriteHeader(hdr)
		tw.Write([]byte(content))

		tw.Close()
		zw.Close()
		mpw.Close()

		req := NewRequestWithBody(t, "POST", root+"/cookbooks", &body)
		req.Header.Add("Content-Type", mpw.FormDataContentType())
		AddBasicAuthHeader(req, user.Name)
		MakeRequest(t, req, expectedStatus)
	}

	t.Run("Upload", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		req := NewRequestWithBody(t, "POST", root+"/cookbooks", bytes.NewReader([]byte{}))
		MakeRequest(t, req, http.StatusUnauthorized)

		uploadPackage(t, packageVersion, http.StatusCreated)

		pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeChef)
		assert.NoError(t, err)
		assert.Len(t, pvs, 1)

		pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0])
		assert.NoError(t, err)
		assert.NotNil(t, pd.SemVer)
		assert.IsType(t, &chef_module.Metadata{}, pd.Metadata)
		assert.Equal(t, packageName, pd.Package.Name)
		assert.Equal(t, packageVersion, pd.Version.Version)

		pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID)
		assert.NoError(t, err)
		assert.Len(t, pfs, 1)
		assert.Equal(t, fmt.Sprintf("%s.tar.gz", packageVersion), pfs[0].Name)
		assert.True(t, pfs[0].IsLead)

		uploadPackage(t, packageVersion, http.StatusBadRequest)
	})

	t.Run("Download", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		req := NewRequest(t, "GET", fmt.Sprintf("%s/cookbooks/%s/versions/%s/download", root, packageName, packageVersion))
		MakeRequest(t, req, http.StatusOK)
	})

	t.Run("Universe", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		req := NewRequest(t, "GET", root+"/universe")
		resp := MakeRequest(t, req, http.StatusOK)

		type VersionInfo struct {
			LocationType string            `json:"location_type"`
			LocationPath string            `json:"location_path"`
			DownloadURL  string            `json:"download_url"`
			Dependencies map[string]string `json:"dependencies"`
		}

		var result map[string]map[string]*VersionInfo
		DecodeJSON(t, resp, &result)

		assert.Len(t, result, 1)
		assert.Contains(t, result, packageName)

		versions := result[packageName]

		assert.Len(t, versions, 1)
		assert.Contains(t, versions, packageVersion)

		info := versions[packageVersion]

		assert.Equal(t, "opscode", info.LocationType)
		assert.Equal(t, setting.AppURL+root[1:], info.LocationPath)
		assert.Equal(t, fmt.Sprintf("%s%s/cookbooks/%s/versions/%s/download", setting.AppURL, root[1:], packageName, packageVersion), info.DownloadURL)
	})

	t.Run("Search", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		cases := []struct {
			Query           string
			Start           int
			Items           int
			ExpectedTotal   int
			ExpectedResults int
		}{
			{"", 0, 0, 1, 1},
			{"", 0, 10, 1, 1},
			{"gitea", 0, 10, 0, 0},
			{"test", 0, 10, 1, 1},
			{"test", 1, 10, 1, 0},
		}

		type Item struct {
			CookbookName        string `json:"cookbook_name"`
			CookbookMaintainer  string `json:"cookbook_maintainer"`
			CookbookDescription string `json:"cookbook_description"`
			Cookbook            string `json:"cookbook"`
		}

		type Result struct {
			Start int     `json:"start"`
			Total int     `json:"total"`
			Items []*Item `json:"items"`
		}

		for i, c := range cases {
			req := NewRequest(t, "GET", fmt.Sprintf("%s/search?q=%s&start=%d&items=%d", root, c.Query, c.Start, c.Items))
			req = AddBasicAuthHeader(req, user.Name)
			resp := MakeRequest(t, req, http.StatusOK)

			var result Result
			DecodeJSON(t, resp, &result)

			assert.Equal(t, c.ExpectedTotal, result.Total, "case %d: unexpected total hits", i)
			assert.Len(t, result.Items, c.ExpectedResults, "case %d: unexpected result count", i)

			if len(result.Items) == 1 {
				item := result.Items[0]
				assert.Equal(t, packageName, item.CookbookName)
				assert.Equal(t, packageAuthor, item.CookbookMaintainer)
				assert.Equal(t, packageDescription, item.CookbookDescription)
				assert.Equal(t, fmt.Sprintf("%s%s/cookbooks/%s", setting.AppURL, root[1:], packageName), item.Cookbook)
			}
		}
	})

	t.Run("EnumeratePackages", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		cases := []struct {
			Sort            string
			Start           int
			Items           int
			ExpectedTotal   int
			ExpectedResults int
		}{
			{"", 0, 0, 1, 1},
			{"", 0, 10, 1, 1},
			{"RECENTLY_ADDED", 0, 10, 1, 1},
			{"RECENTLY_UPDATED", 0, 10, 1, 1},
			{"", 1, 10, 1, 0},
		}

		type Item struct {
			CookbookName        string `json:"cookbook_name"`
			CookbookMaintainer  string `json:"cookbook_maintainer"`
			CookbookDescription string `json:"cookbook_description"`
			Cookbook            string `json:"cookbook"`
		}

		type Result struct {
			Start int     `json:"start"`
			Total int     `json:"total"`
			Items []*Item `json:"items"`
		}

		for i, c := range cases {
			req := NewRequest(t, "GET", fmt.Sprintf("%s/cookbooks?start=%d&items=%d&sort=%s", root, c.Start, c.Items, c.Sort))
			req = AddBasicAuthHeader(req, user.Name)
			resp := MakeRequest(t, req, http.StatusOK)

			var result Result
			DecodeJSON(t, resp, &result)

			assert.Equal(t, c.ExpectedTotal, result.Total, "case %d: unexpected total hits", i)
			assert.Len(t, result.Items, c.ExpectedResults, "case %d: unexpected result count", i)

			if len(result.Items) == 1 {
				item := result.Items[0]
				assert.Equal(t, packageName, item.CookbookName)
				assert.Equal(t, packageAuthor, item.CookbookMaintainer)
				assert.Equal(t, packageDescription, item.CookbookDescription)
				assert.Equal(t, fmt.Sprintf("%s%s/cookbooks/%s", setting.AppURL, root[1:], packageName), item.Cookbook)
			}
		}
	})

	t.Run("PackageMetadata", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		req := NewRequest(t, "GET", fmt.Sprintf("%s/cookbooks/%s", root, packageName))
		resp := MakeRequest(t, req, http.StatusOK)

		type Result struct {
			Name          string    `json:"name"`
			Maintainer    string    `json:"maintainer"`
			Description   string    `json:"description"`
			Category      string    `json:"category"`
			LatestVersion string    `json:"latest_version"`
			SourceURL     string    `json:"source_url"`
			CreatedAt     time.Time `json:"created_at"`
			UpdatedAt     time.Time `json:"updated_at"`
			Deprecated    bool      `json:"deprecated"`
			Versions      []string  `json:"versions"`
		}

		var result Result
		DecodeJSON(t, resp, &result)

		versionURL := fmt.Sprintf("%s%s/cookbooks/%s/versions/%s", setting.AppURL, root[1:], packageName, packageVersion)

		assert.Equal(t, packageName, result.Name)
		assert.Equal(t, packageAuthor, result.Maintainer)
		assert.Equal(t, packageDescription, result.Description)
		assert.Equal(t, versionURL, result.LatestVersion)
		assert.False(t, result.Deprecated)
		assert.ElementsMatch(t, []string{versionURL}, result.Versions)
	})

	t.Run("PackageVersionMetadata", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		req := NewRequest(t, "GET", fmt.Sprintf("%s/cookbooks/%s/versions/%s", root, packageName, packageVersion))
		resp := MakeRequest(t, req, http.StatusOK)

		type Result struct {
			Version         string            `json:"version"`
			TarballFileSize int64             `json:"tarball_file_size"`
			PublishedAt     time.Time         `json:"published_at"`
			Cookbook        string            `json:"cookbook"`
			File            string            `json:"file"`
			License         string            `json:"license"`
			Dependencies    map[string]string `json:"dependencies"`
		}

		var result Result
		DecodeJSON(t, resp, &result)

		packageURL := fmt.Sprintf("%s%s/cookbooks/%s", setting.AppURL, root[1:], packageName)

		assert.Equal(t, packageVersion, result.Version)
		assert.Equal(t, packageURL, result.Cookbook)
		assert.Equal(t, fmt.Sprintf("%s/versions/%s/download", packageURL, packageVersion), result.File)
	})

	t.Run("Delete", func(t *testing.T) {
		uploadPackage(t, "1.0.2", http.StatusCreated)
		uploadPackage(t, "1.0.3", http.StatusCreated)

		t.Run("Version", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "DELETE", fmt.Sprintf("%s/cookbooks/%s/versions/%s", root, packageName, "1.0.2"))
			MakeRequest(t, req, http.StatusUnauthorized)

			req = NewRequest(t, "DELETE", fmt.Sprintf("%s/cookbooks/%s/versions/%s", root, packageName, "1.0.2"))
			AddBasicAuthHeader(req, user.Name)
			MakeRequest(t, req, http.StatusOK)

			pv, err := packages.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages.TypeChef, packageName, "1.0.2")
			assert.Nil(t, pv)
			assert.Error(t, err)
		})

		t.Run("Package", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "DELETE", fmt.Sprintf("%s/cookbooks/%s", root, packageName))
			MakeRequest(t, req, http.StatusUnauthorized)

			req = NewRequest(t, "DELETE", fmt.Sprintf("%s/cookbooks/%s", root, packageName))
			AddBasicAuthHeader(req, user.Name)
			MakeRequest(t, req, http.StatusOK)

			pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeChef)
			assert.NoError(t, err)
			assert.Empty(t, pvs)
		})
	})
}