helm_repo_handler_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 helmTest 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 *helmTest, tester *tester, t *testing.T)
  21. // }
  22. // func testHelmRepoRequests(t *testing.T, tests []*helmTest, 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 createHelmRepoTests = []*helmTest{
  57. // {
  58. // initializers: []func(t *tester){
  59. // initUserDefault,
  60. // initProject,
  61. // },
  62. // msg: "Create helm repo",
  63. // method: "POST",
  64. // endpoint: "/api/projects/1/helmrepos",
  65. // body: `{"name":"helm-repo-test","basic_integration_id":1,"repo_url":"https://example-repo.com"}`,
  66. // expStatus: http.StatusCreated,
  67. // expBody: `{"id":1,"project_id":1,"name":"helm-repo-test","repo_name":"https://example-repo.com","service":"helm"}`,
  68. // useCookie: true,
  69. // validators: []func(c *helmTest, tester *tester, t *testing.T){
  70. // helmRepoBodyValidator,
  71. // },
  72. // },
  73. // }
  74. // func TestHandleCreateHelmRepo(t *testing.T) {
  75. // testHelmRepoRequests(t, createHelmRepoTests, true)
  76. // }
  77. // var listHelmReposTest = []*helmTest{
  78. // {
  79. // initializers: []func(t *tester){
  80. // initUserDefault,
  81. // initProject,
  82. // initHelmRepo,
  83. // },
  84. // msg: "List helm repos",
  85. // method: "GET",
  86. // endpoint: "/api/projects/1/helmrepos",
  87. // body: ``,
  88. // expStatus: http.StatusOK,
  89. // expBody: `[{"id":1,"project_id":1,"name":"helm-repo-test","repo_name":"https://example-repo.com","service":"helm"}]`,
  90. // useCookie: true,
  91. // validators: []func(c *helmTest, tester *tester, t *testing.T){
  92. // hrsBodyValidator,
  93. // },
  94. // },
  95. // }
  96. // func TestHandleListHelmRepos(t *testing.T) {
  97. // testHelmRepoRequests(t, listHelmReposTest, true)
  98. // }
  99. // // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  100. // func initHelmRepo(tester *tester) {
  101. // proj, _ := tester.repo.Project().ReadProject(1)
  102. // hr := &models.HelmRepo{
  103. // Name: "helm-repo-test",
  104. // ProjectID: proj.Model.ID,
  105. // RepoURL: "https://example-repo.com",
  106. // BasicAuthIntegrationID: 1,
  107. // }
  108. // tester.repo.HelmRepo().CreateHelmRepo(hr)
  109. // }
  110. // func helmRepoBodyValidator(c *helmTest, tester *tester, t *testing.T) {
  111. // gotBody := &models.HelmRepoExternal{}
  112. // expBody := &models.HelmRepoExternal{}
  113. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  114. // json.Unmarshal([]byte(c.expBody), &expBody)
  115. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  116. // t.Errorf("handler returned wrong body:\n")
  117. // t.Error(diff)
  118. // }
  119. // }
  120. // func hrsBodyValidator(c *helmTest, tester *tester, t *testing.T) {
  121. // gotBody := make([]*models.HelmRepoExternal, 0)
  122. // expBody := make([]*models.HelmRepoExternal, 0)
  123. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  124. // json.Unmarshal([]byte(c.expBody), &expBody)
  125. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  126. // t.Errorf("handler returned wrong body:\n")
  127. // t.Error(diff)
  128. // }
  129. // }