repo_handler_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "testing"
  8. "github.com/porter-dev/porter/internal/kubernetes"
  9. )
  10. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  11. type reposTest 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 *reposTest, tester *tester, t *testing.T)
  21. }
  22. func testReposRequests(t *testing.T, tests []*reposTest, 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 listReposTests = []*reposTest{
  57. &reposTest{
  58. initializers: []func(tester *tester){
  59. initDefaultRepos,
  60. },
  61. msg: "List repos",
  62. method: "GET",
  63. endpoint: "/api/repos/github/porter/master/contents?dir=" + url.QueryEscape("./"),
  64. body: "",
  65. expStatus: http.StatusOK,
  66. expBody: "unimplemented",
  67. useCookie: true,
  68. validators: []func(c *reposTest, tester *tester, t *testing.T){
  69. reposListValidator,
  70. },
  71. },
  72. }
  73. func TestHandleListRepos(t *testing.T) {
  74. testReposRequests(t, listReposTests, true)
  75. }
  76. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  77. func initDefaultRepos(tester *tester) {
  78. initUserDefault(tester)
  79. agent := kubernetes.GetAgentTesting(defaultObjects...)
  80. // overwrite the test agent with new resources
  81. tester.app.TestAgents.K8sAgent = agent
  82. }
  83. func reposListValidator(c *reposTest, tester *tester, t *testing.T) {
  84. var gotBody map[string]interface{}
  85. var expBody map[string]interface{}
  86. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  87. json.Unmarshal([]byte(c.expBody), &expBody)
  88. if string(tester.rr.Body.Bytes()) != c.expBody {
  89. t.Errorf("Mismatch")
  90. }
  91. }