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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "testing"
  10. api "code.gitea.io/sdk/gitea"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. type APITestContext struct {
  14. Reponame string
  15. Session *TestSession
  16. Token string
  17. Username string
  18. ExpectedCode int
  19. }
  20. func NewAPITestContext(t *testing.T, username, reponame string) APITestContext {
  21. session := loginUser(t, username)
  22. token := getTokenForLoggedInUser(t, session)
  23. return APITestContext{
  24. Session: session,
  25. Token: token,
  26. Username: username,
  27. Reponame: reponame,
  28. }
  29. }
  30. func (ctx APITestContext) GitPath() string {
  31. return fmt.Sprintf("%s/%s.git", ctx.Username, ctx.Reponame)
  32. }
  33. func doAPICreateRepository(ctx APITestContext, empty bool, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
  34. return func(t *testing.T) {
  35. createRepoOption := &api.CreateRepoOption{
  36. AutoInit: !empty,
  37. Description: "Temporary repo",
  38. Name: ctx.Reponame,
  39. Private: true,
  40. Gitignores: "",
  41. License: "WTFPL",
  42. Readme: "Default",
  43. }
  44. req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+ctx.Token, createRepoOption)
  45. if ctx.ExpectedCode != 0 {
  46. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  47. return
  48. }
  49. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  50. var repository api.Repository
  51. DecodeJSON(t, resp, &repository)
  52. if len(callback) > 0 {
  53. callback[0](t, repository)
  54. }
  55. }
  56. }
  57. func doAPIGetRepository(ctx APITestContext, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
  58. return func(t *testing.T) {
  59. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  60. req := NewRequest(t, "GET", urlStr)
  61. if ctx.ExpectedCode != 0 {
  62. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  63. return
  64. }
  65. resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
  66. var repository api.Repository
  67. DecodeJSON(t, resp, &repository)
  68. if len(callback) > 0 {
  69. callback[0](t, repository)
  70. }
  71. }
  72. }
  73. func doAPIDeleteRepository(ctx APITestContext) func(*testing.T) {
  74. return func(t *testing.T) {
  75. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  76. req := NewRequest(t, "DELETE", urlStr)
  77. if ctx.ExpectedCode != 0 {
  78. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  79. return
  80. }
  81. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  82. }
  83. }
  84. func doAPICreateUserKey(ctx APITestContext, keyname, keyFile string, callback ...func(*testing.T, api.PublicKey)) func(*testing.T) {
  85. return func(t *testing.T) {
  86. urlStr := fmt.Sprintf("/api/v1/user/keys?token=%s", ctx.Token)
  87. dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
  88. assert.NoError(t, err)
  89. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateKeyOption{
  90. Title: keyname,
  91. Key: string(dataPubKey),
  92. })
  93. if ctx.ExpectedCode != 0 {
  94. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  95. return
  96. }
  97. resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
  98. var publicKey api.PublicKey
  99. DecodeJSON(t, resp, &publicKey)
  100. if len(callback) > 0 {
  101. callback[0](t, publicKey)
  102. }
  103. }
  104. }
  105. func doAPIDeleteUserKey(ctx APITestContext, keyID int64) func(*testing.T) {
  106. return func(t *testing.T) {
  107. urlStr := fmt.Sprintf("/api/v1/user/keys/%d?token=%s", keyID, ctx.Token)
  108. req := NewRequest(t, "DELETE", urlStr)
  109. if ctx.ExpectedCode != 0 {
  110. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  111. return
  112. }
  113. ctx.Session.MakeRequest(t, req, http.StatusNoContent)
  114. }
  115. }
  116. func doAPICreateDeployKey(ctx APITestContext, keyname, keyFile string, readOnly bool) func(*testing.T) {
  117. return func(t *testing.T) {
  118. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
  119. dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
  120. assert.NoError(t, err)
  121. req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{
  122. Title: keyname,
  123. Key: string(dataPubKey),
  124. ReadOnly: readOnly,
  125. })
  126. if ctx.ExpectedCode != 0 {
  127. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  128. return
  129. }
  130. ctx.Session.MakeRequest(t, req, http.StatusCreated)
  131. }
  132. }