You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.4 KiB

Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestUploadAttachment(t *testing.T) {
  12. assert.NoError(t, PrepareTestDatabase())
  13. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  14. var fPath = "./attachment_test.go"
  15. f, err := os.Open(fPath)
  16. assert.NoError(t, err)
  17. defer f.Close()
  18. var buf = make([]byte, 1024)
  19. n, err := f.Read(buf)
  20. assert.NoError(t, err)
  21. buf = buf[:n]
  22. attach, err := NewAttachment(&Attachment{
  23. UploaderID: user.ID,
  24. Name: filepath.Base(fPath),
  25. }, buf, f)
  26. assert.NoError(t, err)
  27. attachment, err := GetAttachmentByUUID(attach.UUID)
  28. assert.NoError(t, err)
  29. assert.EqualValues(t, user.ID, attachment.UploaderID)
  30. assert.Equal(t, int64(0), attachment.DownloadCount)
  31. }
  32. func TestIncreaseDownloadCount(t *testing.T) {
  33. assert.NoError(t, PrepareTestDatabase())
  34. attachment, err := GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
  35. assert.NoError(t, err)
  36. assert.Equal(t, int64(0), attachment.DownloadCount)
  37. // increase download count
  38. err = attachment.IncreaseDownloadCount()
  39. assert.NoError(t, err)
  40. attachment, err = GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
  41. assert.NoError(t, err)
  42. assert.Equal(t, int64(1), attachment.DownloadCount)
  43. }
  44. func TestGetByCommentOrIssueID(t *testing.T) {
  45. assert.NoError(t, PrepareTestDatabase())
  46. // count of attachments from issue ID
  47. attachments, err := GetAttachmentsByIssueID(1)
  48. assert.NoError(t, err)
  49. assert.Equal(t, 1, len(attachments))
  50. attachments, err = GetAttachmentsByCommentID(1)
  51. assert.NoError(t, err)
  52. assert.Equal(t, 2, len(attachments))
  53. }
  54. func TestDeleteAttachments(t *testing.T) {
  55. assert.NoError(t, PrepareTestDatabase())
  56. count, err := DeleteAttachmentsByIssue(4, false)
  57. assert.NoError(t, err)
  58. assert.Equal(t, 2, count)
  59. count, err = DeleteAttachmentsByComment(2, false)
  60. assert.NoError(t, err)
  61. assert.Equal(t, 2, count)
  62. err = DeleteAttachment(&Attachment{ID: 8}, false)
  63. assert.NoError(t, err)
  64. attachment, err := GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18")
  65. assert.Error(t, err)
  66. assert.True(t, IsErrAttachmentNotExist(err))
  67. assert.Nil(t, attachment)
  68. }
  69. func TestGetAttachmentByID(t *testing.T) {
  70. assert.NoError(t, PrepareTestDatabase())
  71. attach, err := GetAttachmentByID(1)
  72. assert.NoError(t, err)
  73. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
  74. }
  75. func TestAttachment_DownloadURL(t *testing.T) {
  76. attach := &Attachment{
  77. UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
  78. ID: 1,
  79. }
  80. assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL())
  81. }
  82. func TestUpdateAttachment(t *testing.T) {
  83. assert.NoError(t, PrepareTestDatabase())
  84. attach, err := GetAttachmentByID(1)
  85. assert.NoError(t, err)
  86. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
  87. attach.Name = "new_name"
  88. assert.NoError(t, UpdateAttachment(attach))
  89. AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"})
  90. }
  91. func TestGetAttachmentsByUUIDs(t *testing.T) {
  92. assert.NoError(t, PrepareTestDatabase())
  93. attachList, err := GetAttachmentsByUUIDs([]string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
  94. assert.NoError(t, err)
  95. assert.Equal(t, 2, len(attachList))
  96. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID)
  97. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", attachList[1].UUID)
  98. assert.Equal(t, int64(1), attachList[0].IssueID)
  99. assert.Equal(t, int64(5), attachList[1].IssueID)
  100. }
  101. func TestLinkedRepository(t *testing.T) {
  102. assert.NoError(t, PrepareTestDatabase())
  103. testCases := []struct {
  104. name string
  105. attachID int64
  106. expectedRepo *Repository
  107. expectedUnitType UnitType
  108. }{
  109. {"LinkedIssue", 1, &Repository{ID: 1}, UnitTypeIssues},
  110. {"LinkedComment", 3, &Repository{ID: 1}, UnitTypePullRequests},
  111. {"LinkedRelease", 9, &Repository{ID: 1}, UnitTypeReleases},
  112. {"Notlinked", 10, nil, -1},
  113. }
  114. for _, tc := range testCases {
  115. t.Run(tc.name, func(t *testing.T) {
  116. attach, err := GetAttachmentByID(tc.attachID)
  117. assert.NoError(t, err)
  118. repo, unitType, err := attach.LinkedRepository()
  119. assert.NoError(t, err)
  120. if tc.expectedRepo != nil {
  121. assert.Equal(t, tc.expectedRepo.ID, repo.ID)
  122. }
  123. assert.Equal(t, tc.expectedUnitType, unitType)
  124. })
  125. }
  126. }