integration_handler_test.go 8.4 KB

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