project_handler_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package api_test
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "github.com/porter-dev/porter/internal/forms"
  10. "github.com/porter-dev/porter/internal/models"
  11. )
  12. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  13. type projTest 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 *projTest, tester *tester, t *testing.T)
  23. }
  24. func testProjRequests(t *testing.T, tests []*projTest, 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 createProjectTests = []*projTest{
  59. &projTest{
  60. initializers: []func(t *tester){
  61. initUserDefault,
  62. },
  63. msg: "Create project",
  64. method: "POST",
  65. endpoint: "/api/projects",
  66. body: `{
  67. "name": "project-test"
  68. }`,
  69. expStatus: http.StatusCreated,
  70. expBody: ``,
  71. useCookie: true,
  72. validators: []func(c *projTest, tester *tester, t *testing.T){
  73. projectBasicBodyValidator,
  74. },
  75. },
  76. }
  77. func TestHandleCreateProject(t *testing.T) {
  78. testProjRequests(t, createProjectTests, true)
  79. }
  80. var readProjectTests = []*projTest{
  81. &projTest{
  82. initializers: []func(t *tester){
  83. initUserDefault,
  84. initProject,
  85. },
  86. msg: "Read project",
  87. method: "GET",
  88. endpoint: "/api/projects/1",
  89. body: ``,
  90. expStatus: http.StatusOK,
  91. expBody: `{"id":1,"name":"project-test","roles":[{"id":0,"kind":"admin","user_id":1,"project_id":1}]}`,
  92. useCookie: true,
  93. validators: []func(c *projTest, tester *tester, t *testing.T){
  94. projectModelBodyValidator,
  95. },
  96. },
  97. }
  98. func TestHandleReadProject(t *testing.T) {
  99. testProjRequests(t, readProjectTests, true)
  100. }
  101. var createProjectSACandidatesTests = []*projTest{
  102. &projTest{
  103. initializers: []func(t *tester){
  104. initUserDefault,
  105. initProject,
  106. },
  107. msg: "Create project SA candidate w/ no actions -- should create SA by default",
  108. method: "POST",
  109. endpoint: "/api/projects/1/candidates",
  110. body: `{"kubeconfig":"` + OIDCAuthWithDataForJSON + `"}`,
  111. expStatus: http.StatusCreated,
  112. expBody: `[{"id":1,"actions":[],"project_id":1,"kind":"connector","cluster_name":"cluster-test","cluster_endpoint":"https://localhost","auth_mechanism":"oidc"}]`,
  113. useCookie: true,
  114. validators: []func(c *projTest, tester *tester, t *testing.T){
  115. projectSACandidateBodyValidator,
  116. // check that ServiceAccount was created by default
  117. func(c *projTest, tester *tester, t *testing.T) {
  118. serviceAccounts, err := tester.repo.ServiceAccount.ListServiceAccountsByProjectID(1)
  119. if err != nil {
  120. t.Fatalf("%v\n", err)
  121. }
  122. if len(serviceAccounts) != 1 {
  123. t.Fatal("Expected service account to be created by default, but does not exist\n")
  124. }
  125. sa := serviceAccounts[0]
  126. decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  127. if len(sa.Clusters) != 1 {
  128. t.Fatalf("cluster not written\n")
  129. }
  130. if sa.Clusters[0].ServiceAccountID != 1 {
  131. t.Errorf("service account ID of joined cluster is not 1")
  132. }
  133. if sa.AuthMechanism != models.OIDC {
  134. t.Errorf("service account auth mechanism is not %s\n", models.OIDC)
  135. }
  136. if string(sa.OIDCCertificateAuthorityData) != string(decodedStr) {
  137. t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  138. string(sa.OIDCCertificateAuthorityData), string(decodedStr))
  139. }
  140. if sa.OIDCClientID != "porter-api" {
  141. t.Errorf("service account oidc client id is not %s\n", "porter-api")
  142. }
  143. if sa.OIDCIDToken != "token" {
  144. t.Errorf("service account oidc id token is not %s\n", "token")
  145. }
  146. },
  147. },
  148. },
  149. &projTest{
  150. initializers: []func(t *tester){
  151. initUserDefault,
  152. initProject,
  153. },
  154. msg: "Create project SA candidate",
  155. method: "POST",
  156. endpoint: "/api/projects/1/candidates",
  157. body: `{"kubeconfig":"` + OIDCAuthWithoutDataForJSON + `"}`,
  158. expStatus: http.StatusCreated,
  159. expBody: `[{"id":1,"actions":[{"name":"upload-oidc-idp-issuer-ca-data","docs":"https://github.com/porter-dev/porter","resolved":false,"fields":"oidc_idp_issuer_ca_data"}],"project_id":1,"kind":"connector","cluster_name":"cluster-test","cluster_endpoint":"https://localhost","auth_mechanism":"oidc"}]`,
  160. useCookie: true,
  161. validators: []func(c *projTest, tester *tester, t *testing.T){
  162. projectSACandidateBodyValidator,
  163. },
  164. },
  165. }
  166. func TestHandleCreateProjectSACandidate(t *testing.T) {
  167. testProjRequests(t, createProjectSACandidatesTests, true)
  168. }
  169. var listProjectSACandidatesTests = []*projTest{
  170. &projTest{
  171. initializers: []func(t *tester){
  172. initUserDefault,
  173. initProject,
  174. initProjectSACandidate,
  175. },
  176. msg: "List project SA candidates",
  177. method: "GET",
  178. endpoint: "/api/projects/1/candidates",
  179. body: ``,
  180. expStatus: http.StatusOK,
  181. expBody: `[{"id":1,"actions":[{"name":"upload-oidc-idp-issuer-ca-data","docs":"https://github.com/porter-dev/porter","resolved":false,"fields":"oidc_idp_issuer_ca_data"}],"project_id":1,"kind":"connector","cluster_name":"cluster-test","cluster_endpoint":"https://localhost","auth_mechanism":"oidc"}]`,
  182. useCookie: true,
  183. validators: []func(c *projTest, tester *tester, t *testing.T){
  184. projectSACandidateBodyValidator,
  185. },
  186. },
  187. }
  188. func TestHandleListProjectSACandidates(t *testing.T) {
  189. testProjRequests(t, listProjectSACandidatesTests, true)
  190. }
  191. var resolveProjectSACandidatesTests = []*projTest{
  192. &projTest{
  193. initializers: []func(t *tester){
  194. initUserDefault,
  195. initProject,
  196. initProjectSACandidate,
  197. },
  198. msg: "Resolve project SA candidate",
  199. method: "POST",
  200. endpoint: "/api/projects/1/candidates/1/resolve",
  201. body: `[{"name": "upload-oidc-idp-issuer-ca-data", "oidc_idp_issuer_ca_data": "LS0tLS1CRUdJTiBDRVJ="}]`,
  202. expStatus: http.StatusCreated,
  203. expBody: `{"id":1,"project_id":1,"kind":"connector","clusters":[{"service_account_id":1,"server":"https://localhost"}],"auth_mechanism":"oidc"}`,
  204. useCookie: true,
  205. validators: []func(c *projTest, tester *tester, t *testing.T){
  206. projectSABodyValidator,
  207. },
  208. },
  209. }
  210. func TestHandleResolveProjectSACandidate(t *testing.T) {
  211. testProjRequests(t, resolveProjectSACandidatesTests, true)
  212. }
  213. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  214. func initProject(tester *tester) {
  215. user, _ := tester.repo.User.ReadUserByEmail("belanger@getporter.dev")
  216. // handle write to the database
  217. projModel, _ := tester.repo.Project.CreateProject(&models.Project{
  218. Name: "project-test",
  219. })
  220. // create a new Role with the user as the owner
  221. tester.repo.Project.CreateProjectRole(projModel, &models.Role{
  222. UserID: user.ID,
  223. ProjectID: projModel.ID,
  224. Kind: models.RoleAdmin,
  225. })
  226. }
  227. func initProjectSACandidate(tester *tester) {
  228. proj, _ := tester.repo.Project.ReadProject(1)
  229. form := &forms.CreateServiceAccountCandidatesForm{
  230. ProjectID: uint(proj.ID),
  231. Kubeconfig: OIDCAuthWithoutData,
  232. }
  233. // convert the form to a ServiceAccountCandidate
  234. saCandidates, _ := form.ToServiceAccountCandidates()
  235. for _, saCandidate := range saCandidates {
  236. tester.repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  237. }
  238. }
  239. func projectBasicBodyValidator(c *projTest, tester *tester, t *testing.T) {
  240. if body := tester.rr.Body.String(); strings.TrimSpace(body) != strings.TrimSpace(c.expBody) {
  241. t.Errorf("%s, handler returned wrong body: got %v want %v",
  242. c.msg, body, c.expBody)
  243. }
  244. }
  245. func projectModelBodyValidator(c *projTest, tester *tester, t *testing.T) {
  246. gotBody := &models.ProjectExternal{}
  247. expBody := &models.ProjectExternal{}
  248. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  249. json.Unmarshal([]byte(c.expBody), expBody)
  250. if !reflect.DeepEqual(gotBody, expBody) {
  251. t.Errorf("%s, handler returned wrong body: got %v want %v",
  252. c.msg, gotBody, expBody)
  253. }
  254. }
  255. func projectSACandidateBodyValidator(c *projTest, tester *tester, t *testing.T) {
  256. gotBody := make([]*models.ServiceAccountCandidateExternal, 0)
  257. expBody := make([]*models.ServiceAccountCandidateExternal, 0)
  258. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  259. json.Unmarshal([]byte(c.expBody), &expBody)
  260. if !reflect.DeepEqual(gotBody, expBody) {
  261. t.Errorf("%s, handler returned wrong body: got %v want %v",
  262. c.msg, gotBody, expBody)
  263. }
  264. }
  265. func projectSABodyValidator(c *projTest, tester *tester, t *testing.T) {
  266. gotBody := &models.ServiceAccountExternal{}
  267. expBody := &models.ServiceAccountExternal{}
  268. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  269. json.Unmarshal([]byte(c.expBody), expBody)
  270. if !reflect.DeepEqual(gotBody, expBody) {
  271. t.Errorf("%s, handler returned wrong body: got %v want %v",
  272. c.msg, gotBody, expBody)
  273. }
  274. }
  275. const OIDCAuthWithDataForJSON string = `apiVersion: v1\nclusters:\n- cluster:\n server: https://localhost\n certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\ncurrent-context: context-test\nkind: Config\npreferences: {}\nusers:\n- name: test-admin\n user:\n auth-provider:\n config:\n client-id: porter-api\n id-token: token\n idp-issuer-url: https://localhost\n idp-certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=\n name: oidc`
  276. const OIDCAuthWithoutDataForJSON string = `apiVersion: v1\nclusters:\n- cluster:\n server: https://localhost\n certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\ncurrent-context: context-test\nkind: Config\npreferences: {}\nusers:\n- name: test-admin\n user:\n auth-provider:\n config:\n client-id: porter-api\n id-token: token\n idp-issuer-url: https://localhost\n idp-certificate-authority: /fake/path/to/ca.pem\n name: oidc`
  277. const OIDCAuthWithoutData string = `
  278. apiVersion: v1
  279. clusters:
  280. - cluster:
  281. server: https://localhost
  282. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  283. name: cluster-test
  284. contexts:
  285. - context:
  286. cluster: cluster-test
  287. user: test-admin
  288. name: context-test
  289. current-context: context-test
  290. kind: Config
  291. preferences: {}
  292. users:
  293. - name: test-admin
  294. user:
  295. auth-provider:
  296. config:
  297. client-id: porter-api
  298. id-token: token
  299. idp-issuer-url: https://localhost
  300. idp-certificate-authority: /fake/path/to/ca.pem
  301. name: oidc
  302. `