template_handler_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 templateTest struct {
  12. initializers []func(tester *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 *templateTest, tester *tester, t *testing.T)
  21. }
  22. func testTemplatesRequests(t *testing.T, tests []*templateTest, 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 listTemplatesTests = []*templateTest{
  57. // &templateTest{
  58. // initializers: []func(tester *tester){
  59. // initUserDefault,
  60. // },
  61. // msg: "List templates",
  62. // method: "GET",
  63. // endpoint: "/api/templates",
  64. // body: "",
  65. // expStatus: http.StatusOK,
  66. // expBody: `[{"name":"Docker","description":"Template to deploy any Docker container on Porter.","icon":"https://cdn4.iconfinder.com/data/icons/logos-and-brands/512/97_Docker_logo_logos-512.png"},{"name":"redis","description":"DEPRECATED Open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.","icon":"https://bitnami.com/assets/stacks/redis/img/redis-stack-220x234.png"}]`,
  67. // useCookie: true,
  68. // validators: []func(c *templateTest, tester *tester, t *testing.T){
  69. // templatesListValidator,
  70. // },
  71. // },
  72. // }
  73. // func TestHandleListTemplates(t *testing.T) {
  74. // testTemplatesRequests(t, listTemplatesTests, true)
  75. // }
  76. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  77. func templatesListValidator(c *templateTest, tester *tester, t *testing.T) {
  78. gotBody := make([]*models.PorterChartList, 0)
  79. expBody := make([]*models.PorterChartList, 0)
  80. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  81. json.Unmarshal([]byte(c.expBody), &expBody)
  82. if diff := deep.Equal(gotBody, expBody); diff != nil {
  83. t.Errorf("handler returned wrong body:\n")
  84. t.Error(diff)
  85. }
  86. }
  87. func templateBodyValidator(c *templateTest, tester *tester, t *testing.T) {
  88. gotBody := models.PorterChartRead{}
  89. expBody := models.PorterChartRead{}
  90. bytes := tester.rr.Body.Bytes()
  91. t.Errorf(string(bytes))
  92. json.Unmarshal(bytes, &gotBody)
  93. json.Unmarshal([]byte(c.expBody), &expBody)
  94. if diff := deep.Equal(gotBody, expBody); diff != nil {
  95. t.Errorf("handler returned wrong body:\n")
  96. t.Error(diff)
  97. }
  98. }