integration_handler_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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/forms"
  9. // ints "github.com/porter-dev/porter/internal/models/integrations"
  10. // )
  11. // // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  12. // type publicIntTest struct {
  13. // initializers []func(t *tester)
  14. // msg string
  15. // method string
  16. // endpoint string
  17. // body string
  18. // expStatus int
  19. // expBody string
  20. // useCookie bool
  21. // validators []func(c *publicIntTest, tester *tester, t *testing.T)
  22. // }
  23. // func testPublicIntegrationRequests(t *testing.T, tests []*publicIntTest, canQuery bool) {
  24. // for _, c := range tests {
  25. // // create a new tester
  26. // tester := newTester(canQuery)
  27. // // if there's an initializer, call it
  28. // for _, init := range c.initializers {
  29. // init(tester)
  30. // }
  31. // req, err := http.NewRequest(
  32. // c.method,
  33. // c.endpoint,
  34. // strings.NewReader(c.body),
  35. // )
  36. // tester.req = req
  37. // if c.useCookie {
  38. // req.AddCookie(tester.cookie)
  39. // }
  40. // if err != nil {
  41. // t.Fatal(err)
  42. // }
  43. // tester.execute()
  44. // rr := tester.rr
  45. // // first, check that the status matches
  46. // if status := rr.Code; status != c.expStatus {
  47. // t.Errorf("%s, handler returned wrong status code: got %v want %v",
  48. // c.msg, status, c.expStatus)
  49. // }
  50. // // if there's a validator, call it
  51. // for _, validate := range c.validators {
  52. // validate(c, tester, t)
  53. // }
  54. // }
  55. // }
  56. // // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  57. // var listClusterIntegrationsTests = []*publicIntTest{
  58. // {
  59. // initializers: []func(t *tester){
  60. // initUserDefault,
  61. // },
  62. // msg: "List cluster integrations",
  63. // method: "GET",
  64. // endpoint: "/api/integrations/cluster",
  65. // body: ``,
  66. // expStatus: http.StatusOK,
  67. // expBody: `[{"auth_mechanism":"gcp","category":"cluster","service":"gke"},{"auth_mechanism":"aws","category":"cluster","service":"eks"},{"auth_mechanism":"kube","category":"cluster","service":"kube"}]`,
  68. // useCookie: true,
  69. // validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  70. // publicIntBodyValidator,
  71. // },
  72. // },
  73. // }
  74. // func TestHandleListClusterIntegrations(t *testing.T) {
  75. // testPublicIntegrationRequests(t, listClusterIntegrationsTests, true)
  76. // }
  77. // var listRegistryIntegrationsTests = []*publicIntTest{
  78. // {
  79. // initializers: []func(t *tester){
  80. // initUserDefault,
  81. // },
  82. // msg: "List registry integrations",
  83. // method: "GET",
  84. // endpoint: "/api/integrations/registry",
  85. // body: ``,
  86. // expStatus: http.StatusOK,
  87. // expBody: `[{"auth_mechanism":"gcp","category":"registry","service":"gcr"},{"auth_mechanism":"aws","category":"registry","service":"ecr"},{"auth_mechanism":"oauth","category":"registry","service":"docker"}]`,
  88. // useCookie: true,
  89. // validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  90. // publicIntBodyValidator,
  91. // },
  92. // },
  93. // }
  94. // func TestHandleListRegistryIntegrations(t *testing.T) {
  95. // testPublicIntegrationRequests(t, listRegistryIntegrationsTests, true)
  96. // }
  97. // var listHelmRepoIntegrationsTest = []*publicIntTest{
  98. // {
  99. // initializers: []func(t *tester){
  100. // initUserDefault,
  101. // },
  102. // msg: "List Helm repo integrations",
  103. // method: "GET",
  104. // endpoint: "/api/integrations/helm",
  105. // body: ``,
  106. // expStatus: http.StatusOK,
  107. // expBody: `[{"auth_mechanism":"basic","category":"helm","service":"helm"},{"auth_mechanism":"gcp","category":"helm","service":"gcs"},{"auth_mechanism":"aws","category":"helm","service":"s3"}]`,
  108. // useCookie: true,
  109. // validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  110. // publicIntBodyValidator,
  111. // },
  112. // },
  113. // }
  114. // func TestHandleListHelmRepoIntegrations(t *testing.T) {
  115. // testPublicIntegrationRequests(t, listHelmRepoIntegrationsTest, true)
  116. // }
  117. // var listRepoIntegrationsTests = []*publicIntTest{
  118. // {
  119. // initializers: []func(t *tester){
  120. // initUserDefault,
  121. // },
  122. // msg: "List repo integrations",
  123. // method: "GET",
  124. // endpoint: "/api/integrations/repo",
  125. // body: ``,
  126. // expStatus: http.StatusOK,
  127. // expBody: `[{"auth_mechanism":"oauth","category":"repo","service":"github"}]`,
  128. // useCookie: true,
  129. // validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  130. // publicIntBodyValidator,
  131. // },
  132. // },
  133. // }
  134. // func TestHandleListRepoIntegrations(t *testing.T) {
  135. // testPublicIntegrationRequests(t, listRepoIntegrationsTests, true)
  136. // }
  137. // var createGCPIntegrationTests = []*publicIntTest{
  138. // {
  139. // initializers: []func(t *tester){
  140. // initUserDefault,
  141. // initProject,
  142. // },
  143. // msg: "Create GCP Integration",
  144. // method: "POST",
  145. // endpoint: "/api/projects/1/integrations/gcp",
  146. // body: `{
  147. // "gcp_key_data": "yoooo"
  148. // }`,
  149. // expStatus: http.StatusCreated,
  150. // expBody: `{"id":1,"user_id":1,"project_id":1,"gcp-project-id":"","gcp-user-email":""}`,
  151. // useCookie: true,
  152. // validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  153. // gcpIntBodyValidator,
  154. // },
  155. // },
  156. // }
  157. // func TestHandleCreateGCPIntegration(t *testing.T) {
  158. // testPublicIntegrationRequests(t, createGCPIntegrationTests, true)
  159. // }
  160. // var createAWSIntegrationTests = []*publicIntTest{
  161. // {
  162. // initializers: []func(t *tester){
  163. // initUserDefault,
  164. // initProject,
  165. // },
  166. // msg: "Create AWS Integration",
  167. // method: "POST",
  168. // endpoint: "/api/projects/1/integrations/aws",
  169. // body: `{
  170. // "aws_cluster_id": "cluster-id-0",
  171. // "aws_access_key_id": "accesskey",
  172. // "aws_secret_access_key": "secretkey"
  173. // }`,
  174. // expStatus: http.StatusCreated,
  175. // expBody: `{"id":1,"user_id":1,"project_id":1,"aws-entity-id":"","aws-caller-id":""}`,
  176. // useCookie: true,
  177. // validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  178. // awsIntBodyValidator,
  179. // },
  180. // },
  181. // }
  182. // func TestHandleCreateAWSIntegration(t *testing.T) {
  183. // testPublicIntegrationRequests(t, createGCPIntegrationTests, true)
  184. // }
  185. // var createBasicIntegrationTests = []*publicIntTest{
  186. // {
  187. // initializers: []func(t *tester){
  188. // initUserDefault,
  189. // initProject,
  190. // },
  191. // msg: "Create basic integration",
  192. // method: "POST",
  193. // endpoint: "/api/projects/1/integrations/basic",
  194. // body: `{
  195. // "username": "username",
  196. // "password": "password"
  197. // }`,
  198. // expStatus: http.StatusCreated,
  199. // expBody: `{"id":1,"user_id":1,"project_id":1}`,
  200. // useCookie: true,
  201. // validators: []func(c *publicIntTest, tester *tester, t *testing.T){
  202. // basicIntBodyValidator,
  203. // },
  204. // },
  205. // }
  206. // func TestHandleCreateBasicIntegration(t *testing.T) {
  207. // testPublicIntegrationRequests(t, createBasicIntegrationTests, true)
  208. // }
  209. // // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  210. // func initAWSIntegration(tester *tester) {
  211. // proj, _ := tester.repo.Project().ReadProject(1)
  212. // form := &forms.CreateAWSIntegrationForm{
  213. // ProjectID: proj.ID,
  214. // UserID: 1,
  215. // }
  216. // // convert the form to a ServiceAccountCandidate
  217. // awsInt, _ := form.ToAWSIntegration()
  218. // tester.repo.AWSIntegration().CreateAWSIntegration(awsInt)
  219. // }
  220. // func initBasicIntegration(tester *tester) {
  221. // proj, _ := tester.repo.Project().ReadProject(1)
  222. // basicInt := &ints.BasicIntegration{
  223. // ProjectID: proj.ID,
  224. // UserID: 1,
  225. // }
  226. // tester.repo.BasicIntegration().CreateBasicIntegration(basicInt)
  227. // }
  228. // func publicIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  229. // gotBody := make([]*ints.PorterIntegration, 0)
  230. // expBody := make([]*ints.PorterIntegration, 0)
  231. // bytes := tester.rr.Body.Bytes()
  232. // json.Unmarshal(bytes, &gotBody)
  233. // json.Unmarshal([]byte(c.expBody), &expBody)
  234. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  235. // t.Errorf("handler returned wrong body:\n")
  236. // t.Error(diff)
  237. // }
  238. // }
  239. // func gcpIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  240. // gotBody := &ints.GCPIntegration{}
  241. // expBody := &ints.GCPIntegration{}
  242. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  243. // json.Unmarshal([]byte(c.expBody), &expBody)
  244. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  245. // t.Errorf("handler returned wrong body:\n")
  246. // t.Error(diff)
  247. // }
  248. // }
  249. // func awsIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  250. // gotBody := &ints.AWSIntegration{}
  251. // expBody := &ints.AWSIntegration{}
  252. // json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  253. // json.Unmarshal([]byte(c.expBody), &expBody)
  254. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  255. // t.Errorf("handler returned wrong body:\n")
  256. // t.Error(diff)
  257. // }
  258. // }
  259. // func basicIntBodyValidator(c *publicIntTest, tester *tester, t *testing.T) {
  260. // gotBody := &ints.BasicIntegration{}
  261. // expBody := &ints.BasicIntegration{}
  262. // bytes := tester.rr.Body.Bytes()
  263. // json.Unmarshal(bytes, &gotBody)
  264. // json.Unmarshal([]byte(c.expBody), &expBody)
  265. // if diff := deep.Equal(gotBody, expBody); diff != nil {
  266. // t.Errorf("handler returned wrong body:\n")
  267. // t.Error(diff)
  268. // }
  269. // }