integration_handler_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. var createGCPIntegrationTests = []*publicIntTest{
  117. &publicIntTest{
  118. initializers: []func(t *tester){
  119. initUserDefault,
  120. initProject,
  121. },
  122. msg: "Create GCP Integration",
  123. method: "POST",
  124. endpoint: "/api/projects/1/integrations/gcp",
  125. body: `{
  126. "gcp_key_data": "yoooo"
  127. }`,
  128. expStatus: http.StatusCreated,
  129. expBody: `{"id":1,"user_id":1,"project_id":1,"gcp-project-id":"","gcp-user-email":""}`,
  130. useCookie: true,
  131. validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  132. gcpIntBodyValidator,
  133. },
  134. },
  135. }
  136. func TestHandleCreateGCPIntegration(t *testing.T) {
  137. testPublicIntegrationRequests(t, createGCPIntegrationTests, true)
  138. }
  139. var createAWSIntegrationTests = []*publicIntTest{
  140. &publicIntTest{
  141. initializers: []func(t *tester){
  142. initUserDefault,
  143. initProject,
  144. },
  145. msg: "Create AWS Integration",
  146. method: "POST",
  147. endpoint: "/api/projects/1/integrations/aws",
  148. body: `{
  149. "aws_cluster_id": "cluster-id-0",
  150. "aws_access_key_id": "accesskey",
  151. "aws_secret_access_key": "secretkey"
  152. }`,
  153. expStatus: http.StatusCreated,
  154. expBody: `{"id":1,"user_id":1,"project_id":1,"aws-entity-id":"","aws-caller-id":""}`,
  155. useCookie: true,
  156. validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  157. awsIntBodyValidator,
  158. },
  159. },
  160. }
  161. func TestHandleCreateAWSIntegration(t *testing.T) {
  162. testPublicIntegrationRequests(t, createGCPIntegrationTests, true)
  163. }
  164. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  165. func publicIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  166. gotBody := make([]*ints.PorterIntegration, 0)
  167. expBody := make([]*ints.PorterIntegration, 0)
  168. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  169. json.Unmarshal([]byte(c.expBody), &expBody)
  170. if diff := deep.Equal(gotBody, expBody); diff != nil {
  171. t.Errorf("handler returned wrong body:\n")
  172. t.Error(diff)
  173. }
  174. }
  175. func gcpIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  176. gotBody := &ints.GCPIntegration{}
  177. expBody := &ints.GCPIntegration{}
  178. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  179. json.Unmarshal([]byte(c.expBody), &expBody)
  180. if diff := deep.Equal(gotBody, expBody); diff != nil {
  181. t.Errorf("handler returned wrong body:\n")
  182. t.Error(diff)
  183. }
  184. }
  185. func awsIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  186. gotBody := &ints.AWSIntegration{}
  187. expBody := &ints.AWSIntegration{}
  188. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  189. json.Unmarshal([]byte(c.expBody), &expBody)
  190. if diff := deep.Equal(gotBody, expBody); diff != nil {
  191. t.Errorf("handler returned wrong body:\n")
  192. t.Error(diff)
  193. }
  194. }