integration_handler_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "github.com/go-test/deep"
  8. ints "github.com/porter-dev/porter/internal/models/integrations"
  9. )
  10. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  11. type publicIntTest 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 *publicIntTest, tester *tester, t *testing.T)
  21. }
  22. func testPublicIntegrationRequests(t *testing.T, tests []*publicIntTest, 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 listClusterIntegrationsTests = []*publicIntTest{
  57. &publicIntTest{
  58. initializers: []func(t *tester){
  59. initUserDefault,
  60. },
  61. msg: "List cluster integrations",
  62. method: "GET",
  63. endpoint: "/api/integrations/cluster",
  64. body: ``,
  65. expStatus: http.StatusOK,
  66. expBody: `[{"auth_mechanism":"gcp","category":"cluster","service":"gke"},{"auth_mechanism":"aws","category":"cluster","service":"eks"},{"auth_mechanism":"kube","category":"cluster","service":"kube"}]`,
  67. useCookie: true,
  68. validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  69. publicIntBodyValidator,
  70. },
  71. },
  72. }
  73. func TestHandleListClusterIntegrations(t *testing.T) {
  74. testPublicIntegrationRequests(t, listClusterIntegrationsTests, true)
  75. }
  76. var listRegistryIntegrationsTests = []*publicIntTest{
  77. &publicIntTest{
  78. initializers: []func(t *tester){
  79. initUserDefault,
  80. },
  81. msg: "List registry integrations",
  82. method: "GET",
  83. endpoint: "/api/integrations/registry",
  84. body: ``,
  85. expStatus: http.StatusOK,
  86. expBody: `[{"auth_mechanism":"gcp","category":"registry","service":"gcr"},{"auth_mechanism":"aws","category":"registry","service":"ecr"},{"auth_mechanism":"oauth","category":"registry","service":"docker"}]`,
  87. useCookie: true,
  88. validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  89. publicIntBodyValidator,
  90. },
  91. },
  92. }
  93. func TestHandleListRegistryIntegrations(t *testing.T) {
  94. testPublicIntegrationRequests(t, listRegistryIntegrationsTests, true)
  95. }
  96. var listRepoIntegrationsTests = []*publicIntTest{
  97. &publicIntTest{
  98. initializers: []func(t *tester){
  99. initUserDefault,
  100. },
  101. msg: "List repo integrations",
  102. method: "GET",
  103. endpoint: "/api/integrations/repo",
  104. body: ``,
  105. expStatus: http.StatusOK,
  106. expBody: `[{"auth_mechanism":"oauth","category":"repo","service":"github"}]`,
  107. useCookie: true,
  108. validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  109. publicIntBodyValidator,
  110. },
  111. },
  112. }
  113. func TestHandleListRepoIntegrations(t *testing.T) {
  114. testPublicIntegrationRequests(t, listRepoIntegrationsTests, true)
  115. }
  116. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  117. func publicIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  118. gotBody := make([]*ints.PorterIntegration, 0)
  119. expBody := make([]*ints.PorterIntegration, 0)
  120. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  121. json.Unmarshal([]byte(c.expBody), &expBody)
  122. if diff := deep.Equal(gotBody, expBody); diff != nil {
  123. t.Errorf("handler returned wrong body:\n")
  124. t.Error(diff)
  125. }
  126. }