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.

89 lines
2.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. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestIncreaseDownloadCount(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. attachment, err := GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
  12. assert.NoError(t, err)
  13. assert.Equal(t, int64(0), attachment.DownloadCount)
  14. // increase download count
  15. err = attachment.IncreaseDownloadCount()
  16. assert.NoError(t, err)
  17. attachment, err = GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
  18. assert.NoError(t, err)
  19. assert.Equal(t, int64(1), attachment.DownloadCount)
  20. }
  21. func TestGetByCommentOrIssueID(t *testing.T) {
  22. assert.NoError(t, PrepareTestDatabase())
  23. // count of attachments from issue ID
  24. attachments, err := GetAttachmentsByIssueID(1)
  25. assert.NoError(t, err)
  26. assert.Equal(t, 2, len(attachments))
  27. attachments, err = GetAttachmentsByCommentID(1)
  28. assert.NoError(t, err)
  29. assert.Equal(t, 2, len(attachments))
  30. }
  31. func TestDeleteAttachments(t *testing.T) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. count, err := DeleteAttachmentsByIssue(4, false)
  34. assert.NoError(t, err)
  35. assert.Equal(t, 1, count)
  36. count, err = DeleteAttachmentsByComment(2, false)
  37. assert.NoError(t, err)
  38. assert.Equal(t, 2, count)
  39. err = DeleteAttachment(&Attachment{ID: 8}, false)
  40. assert.NoError(t, err)
  41. attachment, err := GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a18")
  42. assert.Error(t, err)
  43. assert.True(t, IsErrAttachmentNotExist(err))
  44. assert.Nil(t, attachment)
  45. }
  46. func TestGetAttachmentByID(t *testing.T) {
  47. assert.NoError(t, PrepareTestDatabase())
  48. attach, err := GetAttachmentByID(1)
  49. assert.NoError(t, err)
  50. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
  51. }
  52. func TestAttachment_DownloadURL(t *testing.T) {
  53. attach := &Attachment{
  54. UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
  55. ID: 1,
  56. }
  57. assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL())
  58. }
  59. func TestUpdateAttachment(t *testing.T) {
  60. assert.NoError(t, PrepareTestDatabase())
  61. attach, err := GetAttachmentByID(1)
  62. assert.NoError(t, err)
  63. assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
  64. attach.Name = "new_name"
  65. assert.NoError(t, UpdateAttachment(attach))
  66. AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"})
  67. }