本站源代码
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.

119 lines
3.1KB

  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, 2, 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, 1, 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. }
上海开阖软件有限公司 沪ICP备12045867号-1