template_handler_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "github.com/porter-dev/porter/internal/kubernetes"
  8. )
  9. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  10. type templatesTest struct {
  11. initializers []func(tester *tester)
  12. msg string
  13. method string
  14. endpoint string
  15. body string
  16. expStatus int
  17. expBody string
  18. useCookie bool
  19. validators []func(c *templatesTest, tester *tester, t *testing.T)
  20. }
  21. func testTemplatesRequests(t *testing.T, tests []*templatesTest, canQuery bool) {
  22. for _, c := range tests {
  23. // create a new tester
  24. tester := newTester(canQuery)
  25. // if there's an initializer, call it
  26. for _, init := range c.initializers {
  27. init(tester)
  28. }
  29. req, err := http.NewRequest(
  30. c.method,
  31. c.endpoint,
  32. strings.NewReader(c.body),
  33. )
  34. tester.req = req
  35. if c.useCookie {
  36. req.AddCookie(tester.cookie)
  37. }
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. tester.execute()
  42. rr := tester.rr
  43. // first, check that the status matches
  44. if status := rr.Code; status != c.expStatus {
  45. t.Errorf("%s, handler returned wrong status code: got %v want %v",
  46. c.msg, status, c.expStatus)
  47. }
  48. // if there's a validator, call it
  49. for _, validate := range c.validators {
  50. validate(c, tester, t)
  51. }
  52. }
  53. }
  54. // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  55. var listTemplatesTests = []*templatesTest{
  56. &templatesTest{
  57. initializers: []func(tester *tester){
  58. initDefaultTemplates,
  59. },
  60. msg: "List templates",
  61. method: "GET",
  62. endpoint: "/api/templates",
  63. body: "",
  64. expStatus: http.StatusOK,
  65. expBody: "unimplemented",
  66. useCookie: true,
  67. validators: []func(c *templatesTest, tester *tester, t *testing.T){
  68. templatesListValidator,
  69. },
  70. },
  71. }
  72. func TestHandleListTemplates(t *testing.T) {
  73. testTemplatesRequests(t, listTemplatesTests, true)
  74. }
  75. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  76. func initDefaultTemplates(tester *tester) {
  77. initUserDefault(tester)
  78. agent := kubernetes.GetAgentTesting(defaultObjects...)
  79. // overwrite the test agent with new resources
  80. tester.app.TestAgents.K8sAgent = agent
  81. }
  82. func templatesListValidator(c *templatesTest, tester *tester, t *testing.T) {
  83. var gotBody map[string]interface{}
  84. var expBody map[string]interface{}
  85. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  86. json.Unmarshal([]byte(c.expBody), &expBody)
  87. if string(tester.rr.Body.Bytes()) != c.expBody {
  88. t.Errorf("Mismatch")
  89. }
  90. }