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.

api_issue_attachment_test.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2021 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 integration
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "mime/multipart"
  10. "net/http"
  11. "testing"
  12. auth_model "code.gitea.io/gitea/models/auth"
  13. issues_model "code.gitea.io/gitea/models/issues"
  14. repo_model "code.gitea.io/gitea/models/repo"
  15. "code.gitea.io/gitea/models/unittest"
  16. user_model "code.gitea.io/gitea/models/user"
  17. api "code.gitea.io/gitea/modules/structs"
  18. "code.gitea.io/gitea/tests"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. func TestAPIGetIssueAttachment(t *testing.T) {
  22. defer tests.PrepareTestEnv(t)()
  23. attachment := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 1})
  24. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: attachment.RepoID})
  25. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: attachment.IssueID})
  26. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  27. session := loginUser(t, repoOwner.Name)
  28. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadIssue)
  29. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets/%d?token=%s",
  30. repoOwner.Name, repo.Name, issue.Index, attachment.ID, token)
  31. req := NewRequest(t, "GET", urlStr)
  32. resp := session.MakeRequest(t, req, http.StatusOK)
  33. apiAttachment := new(api.Attachment)
  34. DecodeJSON(t, resp, &apiAttachment)
  35. unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID})
  36. }
  37. func TestAPIListIssueAttachments(t *testing.T) {
  38. defer tests.PrepareTestEnv(t)()
  39. attachment := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 1})
  40. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: attachment.RepoID})
  41. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: attachment.IssueID})
  42. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  43. session := loginUser(t, repoOwner.Name)
  44. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadIssue)
  45. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets?token=%s",
  46. repoOwner.Name, repo.Name, issue.Index, token)
  47. req := NewRequest(t, "GET", urlStr)
  48. resp := session.MakeRequest(t, req, http.StatusOK)
  49. apiAttachment := new([]api.Attachment)
  50. DecodeJSON(t, resp, &apiAttachment)
  51. unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: (*apiAttachment)[0].ID, IssueID: issue.ID})
  52. }
  53. func TestAPICreateIssueAttachment(t *testing.T) {
  54. defer tests.PrepareTestEnv(t)()
  55. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  56. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
  57. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  58. session := loginUser(t, repoOwner.Name)
  59. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  60. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets?token=%s",
  61. repoOwner.Name, repo.Name, issue.Index, token)
  62. filename := "image.png"
  63. buff := generateImg()
  64. body := &bytes.Buffer{}
  65. // Setup multi-part
  66. writer := multipart.NewWriter(body)
  67. part, err := writer.CreateFormFile("attachment", filename)
  68. assert.NoError(t, err)
  69. _, err = io.Copy(part, &buff)
  70. assert.NoError(t, err)
  71. err = writer.Close()
  72. assert.NoError(t, err)
  73. req := NewRequestWithBody(t, "POST", urlStr, body)
  74. req.Header.Add("Content-Type", writer.FormDataContentType())
  75. resp := session.MakeRequest(t, req, http.StatusCreated)
  76. apiAttachment := new(api.Attachment)
  77. DecodeJSON(t, resp, &apiAttachment)
  78. unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID})
  79. }
  80. func TestAPIEditIssueAttachment(t *testing.T) {
  81. defer tests.PrepareTestEnv(t)()
  82. const newAttachmentName = "newAttachmentName"
  83. attachment := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 1})
  84. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: attachment.RepoID})
  85. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: attachment.IssueID})
  86. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  87. session := loginUser(t, repoOwner.Name)
  88. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  89. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets/%d?token=%s",
  90. repoOwner.Name, repo.Name, issue.Index, attachment.ID, token)
  91. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  92. "name": newAttachmentName,
  93. })
  94. resp := session.MakeRequest(t, req, http.StatusCreated)
  95. apiAttachment := new(api.Attachment)
  96. DecodeJSON(t, resp, &apiAttachment)
  97. unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, IssueID: issue.ID, Name: apiAttachment.Name})
  98. }
  99. func TestAPIDeleteIssueAttachment(t *testing.T) {
  100. defer tests.PrepareTestEnv(t)()
  101. attachment := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 1})
  102. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: attachment.RepoID})
  103. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: attachment.IssueID})
  104. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  105. session := loginUser(t, repoOwner.Name)
  106. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  107. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/assets/%d?token=%s",
  108. repoOwner.Name, repo.Name, issue.Index, attachment.ID, token)
  109. req := NewRequest(t, "DELETE", urlStr)
  110. session.MakeRequest(t, req, http.StatusNoContent)
  111. unittest.AssertNotExistsBean(t, &repo_model.Attachment{ID: attachment.ID, IssueID: issue.ID})
  112. }