registry_handler_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package api_test
  2. // import (
  3. // "encoding/json"
  4. // "net/http"
  5. // "net/http/httptest"
  6. // "strings"
  7. // "testing"
  8. // "github.com/go-test/deep"
  9. // "github.com/porter-dev/porter/internal/kubernetes"
  10. // "github.com/porter-dev/porter/internal/models"
  11. // )
  12. // // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  13. // type regTest struct {
  14. // initializers []func(t *tester)
  15. // msg string
  16. // method string
  17. // endpoint string
  18. // body string
  19. // expStatus int
  20. // expBody string
  21. // useCookie bool
  22. // validators []func(c *regTest, tester *tester, t *testing.T)
  23. // }
  24. // type imagesTest struct {
  25. // initializers []func(tester *tester)
  26. // msg string
  27. // method string
  28. // endpoint string
  29. // body string
  30. // expStatus int
  31. // expBody string
  32. // useCookie bool
  33. // validators []func(c *imagesTest, tester *tester, t *testing.T)
  34. // }
  35. // func testRegistryRequests(t *testing.T, tests []*regTest, canQuery bool) {
  36. // for _, c := range tests {
  37. // // create a new tester
  38. // tester := newTester(canQuery)
  39. // // if there's an initializer, call it
  40. // for _, init := range c.initializers {
  41. // init(tester)
  42. // }
  43. // req, err := http.NewRequest(
  44. // c.method,
  45. // c.endpoint,
  46. // strings.NewReader(c.body),
  47. // )
  48. // tester.req = req
  49. // if c.useCookie {
  50. // req.AddCookie(tester.cookie)
  51. // }
  52. // if err != nil {
  53. // t.Fatal(err)
  54. // }
  55. // tester.execute()
  56. // rr := tester.rr
  57. // // first, check that the status matches
  58. // if status := rr.Code; status != c.expStatus {
  59. // t.Errorf("%s, handler returned wrong status code: got %v want %v",
  60. // c.msg, status, c.expStatus)
  61. // }
  62. // // if there's a validator, call it
  63. // for _, validate := range c.validators {
  64. // validate(c, tester, t)
  65. // }
  66. // }
  67. // }
  68. // func testImagesRequests(t *testing.T, tests []*imagesTest, canQuery bool) {
  69. // for _, c := range tests {
  70. // // create a new tester
  71. // tester := newTester(canQuery)
  72. // // if there's an initializer, call it
  73. // for _, init := range c.initializers {
  74. // init(tester)
  75. // }
  76. // req, err := http.NewRequest(
  77. // c.method,
  78. // c.endpoint,
  79. // strings.NewReader(c.body),
  80. // )
  81. // tester.req = req
  82. // if c.useCookie {
  83. // req.AddCookie(tester.cookie)
  84. // }
  85. // if err != nil {
  86. // t.Fatal(err)
  87. // }
  88. // tester.execute()
  89. // rr := tester.rr
  90. // // first, check that the status matches
  91. // if status := rr.Code; status != c.expStatus {
  92. // t.Errorf("%s, handler returned wrong status code: got %v want %v",
  93. // c.msg, status, c.expStatus)
  94. // }
  95. // // if there's a validator, call it
  96. // for _, validate := range c.validators {
  97. // validate(c, tester, t)
  98. // }
  99. // }
  100. // }
  101. // // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  102. // // var createRegistryTests = []*regTest{
  103. // // &regTest{
  104. // // initializers: []func(t *tester){
  105. // // initUserDefault,
  106. // // initProject,
  107. // // initAWSIntegration,
  108. // // },
  109. // // msg: "Create registry",
  110. // // method: "POST",
  111. // // endpoint: "/api/projects/1/registries",
  112. // // body: `{"name":"registry-test","aws_integration_id":1}`,
  113. // // expStatus: http.StatusCreated,
  114. // // expBody: `{"id":1,"name":"registry-test","project_id":1,"service":"ecr"}`,
  115. // // useCookie: true,
  116. // // validators: []func(c *regTest, tester *tester, t *testing.T){
  117. // // regBodyValidator,
  118. // // },
  119. // // },
  120. // // }
  121. // // func TestHandleCreateRegistry(t *testing.T) {
  122. // // testRegistryRequests(t, createRegistryTests, true)
  123. // // }
  124. // var listRegistryTests = []*regTest{
  125. // {
  126. // initializers: []func(t *tester){
  127. // initUserDefault,
  128. // initProject,
  129. // initRegistry,
  130. // },
  131. // msg: "List registries",
  132. // method: "GET",
  133. // endpoint: "/api/projects/1/registries",
  134. // body: ``,
  135. // expStatus: http.StatusOK,
  136. // expBody: `[{"id":1,"name":"registry-test","project_id":1,"service":"ecr"}]`,
  137. // useCookie: true,
  138. // validators: []func(c *regTest, tester *tester, t *testing.T){
  139. // regsBodyValidator,
  140. // },
  141. // },
  142. // }
  143. // func TestHandleListRegistries(t *testing.T) {
  144. // testRegistryRequests(t, listRegistryTests, true)
  145. // }
  146. // var updateRegistryTests = []*regTest{
  147. // {
  148. // initializers: []func(t *tester){
  149. // initUserDefault,
  150. // initProject,
  151. // initRegistry,
  152. // },
  153. // msg: "Update registry name",
  154. // method: "POST",
  155. // endpoint: "/api/projects/1/registries/1",
  156. // body: `{"name":"registry-new-name"}`,
  157. // expStatus: http.StatusOK,
  158. // expBody: `{"id":1,"name":"registry-new-name","project_id":1,"service":"ecr"}`,
  159. // useCookie: true,
  160. // validators: []func(c *regTest, tester *tester, t *testing.T){
  161. // regBodyValidator,
  162. // },
  163. // },
  164. // }
  165. // func TestHandleUpdateRegistry(t *testing.T) {
  166. // testRegistryRequests(t, updateRegistryTests, true)
  167. // }
  168. // var deleteRegTests = []*regTest{
  169. // {
  170. // initializers: []func(t *tester){
  171. // initUserDefault,
  172. // initProject,
  173. // initRegistry,
  174. // },
  175. // msg: "Delete registry",
  176. // method: "DELETE",
  177. // endpoint: "/api/projects/1/registries/1",
  178. // body: ``,
  179. // expStatus: http.StatusOK,
  180. // expBody: ``,
  181. // useCookie: true,
  182. // validators: []func(c *regTest, tester *tester, t *testing.T){
  183. // func(c *regTest, tester *tester, t *testing.T) {
  184. // req, err := http.NewRequest(
  185. // "GET",
  186. // "/api/projects/1/registries",
  187. // strings.NewReader(""),
  188. // )
  189. // req.AddCookie(tester.cookie)
  190. // if err != nil {
  191. // t.Fatal(err)
  192. // }
  193. // rr2 := httptest.NewRecorder()
  194. // tester.router.ServeHTTP(rr2, req)
  195. // if status := rr2.Code; status != 200 {
  196. // t.Errorf("DELETE registry validation, handler returned wrong status code: got %v want %v",
  197. // status, 200)
  198. // }
  199. // gotBody := make([]*models.RegistryExternal, 0)
  200. // expBody := make([]*models.RegistryExternal, 0)
  201. // json.Unmarshal(rr2.Body.Bytes(), &gotBody)
  202. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  203. // t.Errorf("handler returned wrong body:\n")
  204. // t.Error(diff)
  205. // }
  206. // },
  207. // },
  208. // },
  209. // }
  210. // func TestHandleDeleteRegistry(t *testing.T) {
  211. // testRegistryRequests(t, deleteRegTests, true)
  212. // }
  213. // // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  214. // func initRegistry(tester *tester) {
  215. // proj, _ := tester.repo.Project().ReadProject(1)
  216. // reg := &models.Registry{
  217. // Name: "registry-test",
  218. // ProjectID: proj.Model.ID,
  219. // AWSIntegrationID: 1,
  220. // }
  221. // tester.repo.Registry().CreateRegistry(reg)
  222. // }
  223. // func regBodyValidator(c *regTest, tester *tester, t *testing.T) {
  224. // gotBody := &models.RegistryExternal{}
  225. // expBody := &models.RegistryExternal{}
  226. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  227. // json.Unmarshal([]byte(c.expBody), &expBody)
  228. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  229. // t.Errorf("handler returned wrong body:\n")
  230. // t.Error(diff)
  231. // }
  232. // }
  233. // func regsBodyValidator(c *regTest, tester *tester, t *testing.T) {
  234. // gotBody := make([]*models.RegistryExternal, 0)
  235. // expBody := make([]*models.RegistryExternal, 0)
  236. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  237. // json.Unmarshal([]byte(c.expBody), &expBody)
  238. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  239. // t.Errorf("handler returned wrong body:\n")
  240. // t.Error(diff)
  241. // }
  242. // }
  243. // func initDefaultImages(tester *tester) {
  244. // initUserDefault(tester)
  245. // agent := kubernetes.GetAgentTesting(defaultObjects...)
  246. // // overwrite the test agent with new resources
  247. // tester.app.TestAgents.K8sAgent = agent
  248. // }
  249. // func imagesListValidator(c *imagesTest, tester *tester, t *testing.T) {
  250. // var gotBody map[string]interface{}
  251. // var expBody map[string]interface{}
  252. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  253. // json.Unmarshal([]byte(c.expBody), &expBody)
  254. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  255. // t.Errorf("handler returned wrong body:\n")
  256. // t.Error(diff)
  257. // }
  258. // }