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

api_helper_for_declarative_test.go 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2019 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 integrations
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. type APITestContext struct {
  17. Reponame string
  18. Session *TestSession
  19. Token string
  20. Username string
  21. ExpectedCode int
  22. }
  23. func NewAPITestContext(t *testing.T, username, reponame string) APITestContext {
  24. session := loginUser(t, username)
  25. token := getTokenForLoggedInUser(t, session)
  26. return APITestContext{
  27. Session: session,
  28. Token: token,
  29. Username: username,
  30. Reponame: reponame,
  31. }
  32. }
  33. func (ctx APITestContext) GitPath() string {
  34. return fmt.Sprintf("%s/%s.git", ctx.Username, ctx.Reponame)
  35. }
  36. func doAPICreateRepository(ctx APITestContext, empty bool, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
  37. return func(t *testing.T) {
  38. createRepoOption := &api.CreateRepoOption{
  39. AutoInit: !empty,
  40. Description: "Temporary repo",
  41. Name: ctx.Reponame,
  42. Private: true,
  43. Gitignores: "",
  44. License: "WTFPL",
  45. Readme: "Default",
  46. }
  47. req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+ctx.Token, createRepoOption)
  48. if ctx.ExpectedCode != 0 {
  49. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  50. return
  51. }
  52. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  53. var repository api.Repository
  54. DecodeJSON(t, resp, &repository)
  55. if len(callback) > 0 {
  56. callback[0](t, repository)
  57. }
  58. }
  59. }
  60. func doAPIGetRepository(ctx APITestContext, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
  61. return func(t *testing.T) {
  62. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  63. req := NewRequest(t, "GET", urlStr)
  64. if ctx.ExpectedCode != 0 {
  65. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  66. return
  67. }
  68. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  69. var repository api.Repository
  70. DecodeJSON(t, resp, &repository)
  71. if len(callback) > 0 {
  72. callback[0](t, repository)
  73. }
  74. }
  75. }
  76. func doAPIDeleteRepository(ctx APITestContext) func(*testing.T) {
  77. return func(t *testing.T) {
  78. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  79. req := NewRequest(t, "DELETE", urlStr)
  80. if ctx.ExpectedCode != 0 {
  81. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  82. return
  83. }
  84. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  85. }
  86. }
  87. func doAPICreateUserKey(ctx APITestContext, keyname, keyFile string, callback ...func(*testing.T, api.PublicKey)) func(*testing.T) {
  88. return func(t *testing.T) {
  89. urlStr := fmt.Sprintf("/api/v1/user/keys?token=%s", ctx.Token)
  90. dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
  91. assert.NoError(t, err)
  92. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateKeyOption{
  93. Title: keyname,
  94. Key: string(dataPubKey),
  95. })
  96. if ctx.ExpectedCode != 0 {
  97. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  98. return
  99. }
  100. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  101. var publicKey api.PublicKey
  102. DecodeJSON(t, resp, &publicKey)
  103. if len(callback) > 0 {
  104. callback[0](t, publicKey)
  105. }
  106. }
  107. }
  108. func doAPIDeleteUserKey(ctx APITestContext, keyID int64) func(*testing.T) {
  109. return func(t *testing.T) {
  110. urlStr := fmt.Sprintf("/api/v1/user/keys/%d?token=%s", keyID, ctx.Token)
  111. req := NewRequest(t, "DELETE", urlStr)
  112. if ctx.ExpectedCode != 0 {
  113. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  114. return
  115. }
  116. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  117. }
  118. }
  119. func doAPICreateDeployKey(ctx APITestContext, keyname, keyFile string, readOnly bool) func(*testing.T) {
  120. return func(t *testing.T) {
  121. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  122. dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
  123. assert.NoError(t, err)
  124. req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{
  125. Title: keyname,
  126. Key: string(dataPubKey),
  127. ReadOnly: readOnly,
  128. })
  129. if ctx.ExpectedCode != 0 {
  130. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  131. return
  132. }
  133. ctx.Session.MakeRequest(t, req, http.StatusCreated)
  134. }
  135. }
  136. func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBranch string) func(*testing.T) (api.PullRequest, error) {
  137. return func(t *testing.T) (api.PullRequest, error) {
  138. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s",
  139. owner, repo, ctx.Token)
  140. req := NewRequestWithJSON(t, http.MethodPost, urlStr, &api.CreatePullRequestOption{
  141. Head: headBranch,
  142. Base: baseBranch,
  143. Title: fmt.Sprintf("create a pr from %s to %s", headBranch, baseBranch),
  144. })
  145. expected := 201
  146. if ctx.ExpectedCode != 0 {
  147. expected = ctx.ExpectedCode
  148. }
  149. resp := ctx.Session.MakeRequest(t, req, expected)
  150. decoder := json.NewDecoder(resp.Body)
  151. pr := api.PullRequest{}
  152. err := decoder.Decode(&pr)
  153. return pr, err
  154. }
  155. }
  156. func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) {
  157. return func(t *testing.T) {
  158. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge?token=%s",
  159. owner, repo, index, ctx.Token)
  160. req := NewRequestWithJSON(t, http.MethodPost, urlStr, &auth.MergePullRequestForm{
  161. MergeMessageField: "doAPIMergePullRequest Merge",
  162. Do: string(models.MergeStyleMerge),
  163. })
  164. if ctx.ExpectedCode != 0 {
  165. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  166. return
  167. }
  168. ctx.Session.MakeRequest(t, req, 200)
  169. }
  170. }