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_helper_for_declarative_test.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. "context"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "testing"
  12. "time"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/auth"
  15. "code.gitea.io/gitea/modules/queue"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. type APITestContext struct {
  20. Reponame string
  21. Session *TestSession
  22. Token string
  23. Username string
  24. ExpectedCode int
  25. }
  26. func NewAPITestContext(t *testing.T, username, reponame string) APITestContext {
  27. session := loginUser(t, username)
  28. token := getTokenForLoggedInUser(t, session)
  29. return APITestContext{
  30. Session: session,
  31. Token: token,
  32. Username: username,
  33. Reponame: reponame,
  34. }
  35. }
  36. func (ctx APITestContext) GitPath() string {
  37. return fmt.Sprintf("%s/%s.git", ctx.Username, ctx.Reponame)
  38. }
  39. func doAPICreateRepository(ctx APITestContext, empty bool, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
  40. return func(t *testing.T) {
  41. createRepoOption := &api.CreateRepoOption{
  42. AutoInit: !empty,
  43. Description: "Temporary repo",
  44. Name: ctx.Reponame,
  45. Private: true,
  46. Template: true,
  47. Gitignores: "",
  48. License: "WTFPL",
  49. Readme: "Default",
  50. }
  51. req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+ctx.Token, createRepoOption)
  52. if ctx.ExpectedCode != 0 {
  53. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  54. return
  55. }
  56. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  57. var repository api.Repository
  58. DecodeJSON(t, resp, &repository)
  59. if len(callback) > 0 {
  60. callback[0](t, repository)
  61. }
  62. }
  63. }
  64. func doAPIAddCollaborator(ctx APITestContext, username string, mode models.AccessMode) func(*testing.T) {
  65. return func(t *testing.T) {
  66. permission := "read"
  67. if mode == models.AccessModeAdmin {
  68. permission = "admin"
  69. } else if mode > models.AccessModeRead {
  70. permission = "write"
  71. }
  72. addCollaboratorOption := &api.AddCollaboratorOption{
  73. Permission: &permission,
  74. }
  75. req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/collaborators/%s?token=%s", ctx.Username, ctx.Reponame, username, ctx.Token), addCollaboratorOption)
  76. if ctx.ExpectedCode != 0 {
  77. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  78. return
  79. }
  80. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  81. }
  82. }
  83. func doAPIForkRepository(ctx APITestContext, username string, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
  84. return func(t *testing.T) {
  85. createForkOption := &api.CreateForkOption{}
  86. req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/forks?token=%s", username, ctx.Reponame, ctx.Token), createForkOption)
  87. if ctx.ExpectedCode != 0 {
  88. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  89. return
  90. }
  91. resp := ctx.Session.MakeRequest(t, req, http.StatusAccepted)
  92. var repository api.Repository
  93. DecodeJSON(t, resp, &repository)
  94. if len(callback) > 0 {
  95. callback[0](t, repository)
  96. }
  97. }
  98. }
  99. func doAPIGetRepository(ctx APITestContext, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
  100. return func(t *testing.T) {
  101. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  102. req := NewRequest(t, "GET", urlStr)
  103. if ctx.ExpectedCode != 0 {
  104. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  105. return
  106. }
  107. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  108. var repository api.Repository
  109. DecodeJSON(t, resp, &repository)
  110. if len(callback) > 0 {
  111. callback[0](t, repository)
  112. }
  113. }
  114. }
  115. func doAPIDeleteRepository(ctx APITestContext) func(*testing.T) {
  116. return func(t *testing.T) {
  117. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  118. req := NewRequest(t, "DELETE", urlStr)
  119. if ctx.ExpectedCode != 0 {
  120. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  121. return
  122. }
  123. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  124. }
  125. }
  126. func doAPICreateUserKey(ctx APITestContext, keyname, keyFile string, callback ...func(*testing.T, api.PublicKey)) func(*testing.T) {
  127. return func(t *testing.T) {
  128. urlStr := fmt.Sprintf("/api/v1/user/keys?token=%s", ctx.Token)
  129. dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
  130. assert.NoError(t, err)
  131. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateKeyOption{
  132. Title: keyname,
  133. Key: string(dataPubKey),
  134. })
  135. if ctx.ExpectedCode != 0 {
  136. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  137. return
  138. }
  139. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  140. var publicKey api.PublicKey
  141. DecodeJSON(t, resp, &publicKey)
  142. if len(callback) > 0 {
  143. callback[0](t, publicKey)
  144. }
  145. }
  146. }
  147. func doAPIDeleteUserKey(ctx APITestContext, keyID int64) func(*testing.T) {
  148. return func(t *testing.T) {
  149. urlStr := fmt.Sprintf("/api/v1/user/keys/%d?token=%s", keyID, ctx.Token)
  150. req := NewRequest(t, "DELETE", urlStr)
  151. if ctx.ExpectedCode != 0 {
  152. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  153. return
  154. }
  155. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  156. }
  157. }
  158. func doAPICreateDeployKey(ctx APITestContext, keyname, keyFile string, readOnly bool) func(*testing.T) {
  159. return func(t *testing.T) {
  160. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  161. dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
  162. assert.NoError(t, err)
  163. req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{
  164. Title: keyname,
  165. Key: string(dataPubKey),
  166. ReadOnly: readOnly,
  167. })
  168. if ctx.ExpectedCode != 0 {
  169. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  170. return
  171. }
  172. ctx.Session.MakeRequest(t, req, http.StatusCreated)
  173. }
  174. }
  175. func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBranch string) func(*testing.T) (api.PullRequest, error) {
  176. return func(t *testing.T) (api.PullRequest, error) {
  177. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s",
  178. owner, repo, ctx.Token)
  179. req := NewRequestWithJSON(t, http.MethodPost, urlStr, &api.CreatePullRequestOption{
  180. Head: headBranch,
  181. Base: baseBranch,
  182. Title: fmt.Sprintf("create a pr from %s to %s", headBranch, baseBranch),
  183. })
  184. expected := 201
  185. if ctx.ExpectedCode != 0 {
  186. expected = ctx.ExpectedCode
  187. }
  188. resp := ctx.Session.MakeRequest(t, req, expected)
  189. decoder := json.NewDecoder(resp.Body)
  190. pr := api.PullRequest{}
  191. err := decoder.Decode(&pr)
  192. return pr, err
  193. }
  194. }
  195. func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) {
  196. return func(t *testing.T) {
  197. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge?token=%s",
  198. owner, repo, index, ctx.Token)
  199. req := NewRequestWithJSON(t, http.MethodPost, urlStr, &auth.MergePullRequestForm{
  200. MergeMessageField: "doAPIMergePullRequest Merge",
  201. Do: string(models.MergeStyleMerge),
  202. })
  203. resp := ctx.Session.MakeRequest(t, req, NoExpectedStatus)
  204. if resp.Code == http.StatusMethodNotAllowed {
  205. err := api.APIError{}
  206. DecodeJSON(t, resp, &err)
  207. assert.EqualValues(t, "Please try again later", err.Message)
  208. queue.GetManager().FlushAll(context.Background(), 5*time.Second)
  209. resp = ctx.Session.MakeRequest(t, req, NoExpectedStatus)
  210. }
  211. expected := ctx.ExpectedCode
  212. if expected == 0 {
  213. expected = 200
  214. }
  215. if !assert.EqualValues(t, expected, resp.Code,
  216. "Request: %s %s", req.Method, req.URL.String()) {
  217. logUnexpectedResponse(t, resp)
  218. }
  219. }
  220. }
  221. func doAPIGetBranch(ctx APITestContext, branch string, callback ...func(*testing.T, api.Branch)) func(*testing.T) {
  222. return func(t *testing.T) {
  223. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/branches/%s?token=%s", ctx.Username, ctx.Reponame, branch, ctx.Token)
  224. if ctx.ExpectedCode != 0 {
  225. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  226. return
  227. }
  228. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  229. var branch api.Branch
  230. DecodeJSON(t, resp, &branch)
  231. if len(callback) > 0 {
  232. callback[0](t, branch)
  233. }
  234. }
  235. }
  236. func doAPICreateFile(ctx APITestContext, treepath string, options *api.CreateFileOptions, callback ...func(*testing.T, api.FileResponse)) func(*testing.T) {
  237. return func(t *testing.T) {
  238. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", ctx.Username, ctx.Reponame, treepath, ctx.Token)
  239. req := NewRequestWithJSON(t, "POST", url, &options)
  240. if ctx.ExpectedCode != 0 {
  241. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  242. return
  243. }
  244. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  245. var contents api.FileResponse
  246. DecodeJSON(t, resp, &contents)
  247. if len(callback) > 0 {
  248. callback[0](t, contents)
  249. }
  250. }
  251. }
  252. func doAPICreateOrganization(ctx APITestContext, options *api.CreateOrgOption, callback ...func(*testing.T, api.Organization)) func(t *testing.T) {
  253. return func(t *testing.T) {
  254. url := fmt.Sprintf("/api/v1/orgs?token=%s", ctx.Token)
  255. req := NewRequestWithJSON(t, "POST", url, &options)
  256. if ctx.ExpectedCode != 0 {
  257. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  258. return
  259. }
  260. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  261. var contents api.Organization
  262. DecodeJSON(t, resp, &contents)
  263. if len(callback) > 0 {
  264. callback[0](t, contents)
  265. }
  266. }
  267. }
  268. func doAPICreateOrganizationRepository(ctx APITestContext, orgName string, options *api.CreateRepoOption, callback ...func(*testing.T, api.Repository)) func(t *testing.T) {
  269. return func(t *testing.T) {
  270. url := fmt.Sprintf("/api/v1/orgs/%s/repos?token=%s", orgName, ctx.Token)
  271. req := NewRequestWithJSON(t, "POST", url, &options)
  272. if ctx.ExpectedCode != 0 {
  273. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  274. return
  275. }
  276. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  277. var contents api.Repository
  278. DecodeJSON(t, resp, &contents)
  279. if len(callback) > 0 {
  280. callback[0](t, contents)
  281. }
  282. }
  283. }
  284. func doAPICreateOrganizationTeam(ctx APITestContext, orgName string, options *api.CreateTeamOption, callback ...func(*testing.T, api.Team)) func(t *testing.T) {
  285. return func(t *testing.T) {
  286. url := fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", orgName, ctx.Token)
  287. req := NewRequestWithJSON(t, "POST", url, &options)
  288. if ctx.ExpectedCode != 0 {
  289. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  290. return
  291. }
  292. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  293. var contents api.Team
  294. DecodeJSON(t, resp, &contents)
  295. if len(callback) > 0 {
  296. callback[0](t, contents)
  297. }
  298. }
  299. }
  300. func doAPIAddUserToOrganizationTeam(ctx APITestContext, teamID int64, username string) func(t *testing.T) {
  301. return func(t *testing.T) {
  302. url := fmt.Sprintf("/api/v1/teams/%d/members/%s?token=%s", teamID, username, ctx.Token)
  303. req := NewRequest(t, "PUT", url)
  304. if ctx.ExpectedCode != 0 {
  305. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  306. return
  307. }
  308. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  309. }
  310. }
  311. func doAPIAddRepoToOrganizationTeam(ctx APITestContext, teamID int64, orgName, repoName string) func(t *testing.T) {
  312. return func(t *testing.T) {
  313. url := fmt.Sprintf("/api/v1/teams/%d/repos/%s/%s?token=%s", teamID, orgName, repoName, ctx.Token)
  314. req := NewRequest(t, "PUT", url)
  315. if ctx.ExpectedCode != 0 {
  316. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  317. return
  318. }
  319. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  320. }
  321. }