registry_handler_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "github.com/go-test/deep"
  8. "github.com/porter-dev/porter/internal/models"
  9. )
  10. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  11. type regTest struct {
  12. initializers []func(t *tester)
  13. msg string
  14. method string
  15. endpoint string
  16. body string
  17. expStatus int
  18. expBody string
  19. useCookie bool
  20. validators []func(c *regTest, tester *tester, t *testing.T)
  21. }
  22. func testRegistryRequests(t *testing.T, tests []*regTest, canQuery bool) {
  23. for _, c := range tests {
  24. // create a new tester
  25. tester := newTester(canQuery)
  26. // if there's an initializer, call it
  27. for _, init := range c.initializers {
  28. init(tester)
  29. }
  30. req, err := http.NewRequest(
  31. c.method,
  32. c.endpoint,
  33. strings.NewReader(c.body),
  34. )
  35. tester.req = req
  36. if c.useCookie {
  37. req.AddCookie(tester.cookie)
  38. }
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. tester.execute()
  43. rr := tester.rr
  44. // first, check that the status matches
  45. if status := rr.Code; status != c.expStatus {
  46. t.Errorf("%s, handler returned wrong status code: got %v want %v",
  47. c.msg, status, c.expStatus)
  48. }
  49. // if there's a validator, call it
  50. for _, validate := range c.validators {
  51. validate(c, tester, t)
  52. }
  53. }
  54. }
  55. // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  56. var createRegistryTests = []*regTest{
  57. &regTest{
  58. initializers: []func(t *tester){
  59. initUserDefault,
  60. initProject,
  61. },
  62. msg: "Create registry",
  63. method: "POST",
  64. endpoint: "/api/projects/1/registries",
  65. body: `{"name":"registry-test","aws_integration_id":1}`,
  66. expStatus: http.StatusCreated,
  67. expBody: `{"id":1,"name":"registry-test","project_id":1}`,
  68. useCookie: true,
  69. validators: []func(c *regTest, tester *tester, t *testing.T){
  70. regBodyValidator,
  71. },
  72. },
  73. }
  74. func TestHandleCreateRegistry(t *testing.T) {
  75. testRegistryRequests(t, createRegistryTests, true)
  76. }
  77. var listRegistryTests = []*regTest{
  78. &regTest{
  79. initializers: []func(t *tester){
  80. initUserDefault,
  81. initProject,
  82. initRegistry,
  83. },
  84. msg: "List registries",
  85. method: "GET",
  86. endpoint: "/api/projects/1/registries",
  87. body: ``,
  88. expStatus: http.StatusOK,
  89. expBody: `[{"id":1,"name":"registry-test","project_id":1}]`,
  90. useCookie: true,
  91. validators: []func(c *regTest, tester *tester, t *testing.T){
  92. regsBodyValidator,
  93. },
  94. },
  95. }
  96. func TestHandleListRegistries(t *testing.T) {
  97. testRegistryRequests(t, listRegistryTests, true)
  98. }
  99. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  100. func initRegistry(tester *tester) {
  101. proj, _ := tester.repo.Project.ReadProject(1)
  102. reg := &models.Registry{
  103. Name: "registry-test",
  104. ProjectID: proj.Model.ID,
  105. AWSIntegrationID: 1,
  106. }
  107. tester.repo.Registry.CreateRegistry(reg)
  108. }
  109. func regBodyValidator(c *regTest, tester *tester, t *testing.T) {
  110. gotBody := &models.Registry{}
  111. expBody := &models.Registry{}
  112. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  113. json.Unmarshal([]byte(c.expBody), &expBody)
  114. if diff := deep.Equal(gotBody, expBody); diff != nil {
  115. t.Errorf("handler returned wrong body:\n")
  116. t.Error(diff)
  117. }
  118. }
  119. func regsBodyValidator(c *regTest, tester *tester, t *testing.T) {
  120. gotBody := make([]*models.Registry, 0)
  121. expBody := make([]*models.Registry, 0)
  122. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  123. json.Unmarshal([]byte(c.expBody), &expBody)
  124. if diff := deep.Equal(gotBody, expBody); diff != nil {
  125. t.Errorf("handler returned wrong body:\n")
  126. t.Error(diff)
  127. }
  128. }