project_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package api_test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. )
  8. func initProject(name string, client *api.Client, t *testing.T) *api.CreateProjectResponse {
  9. t.Helper()
  10. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  11. Name: name,
  12. })
  13. if err != nil {
  14. t.Fatalf("%v\n", err)
  15. }
  16. return resp
  17. }
  18. func initProjectCandidate(
  19. projectID uint,
  20. kubeconfig string,
  21. client *api.Client,
  22. t *testing.T,
  23. ) *models.ServiceAccountCandidateExternal {
  24. t.Helper()
  25. resp, err := client.CreateProjectCandidates(
  26. context.Background(),
  27. projectID,
  28. &api.CreateProjectCandidatesRequest{
  29. Kubeconfig: kubeconfig,
  30. },
  31. )
  32. if err != nil {
  33. t.Fatalf("%v\n", err)
  34. }
  35. return resp[0]
  36. }
  37. func initProjectSA(
  38. projectID uint,
  39. candidateID uint,
  40. client *api.Client,
  41. t *testing.T,
  42. ) *api.CreateProjectServiceAccountResponse {
  43. t.Helper()
  44. resp, err := client.CreateProjectServiceAccount(
  45. context.Background(),
  46. projectID,
  47. candidateID,
  48. api.CreateProjectServiceAccountRequest{
  49. &models.ServiceAccountAllActions{
  50. Name: models.OIDCIssuerDataAction,
  51. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  52. },
  53. },
  54. )
  55. if err != nil {
  56. t.Fatalf("%v\n", err)
  57. }
  58. return resp
  59. }
  60. func TestCreateProject(t *testing.T) {
  61. email := "create_project_test@example.com"
  62. client := api.NewClient(baseURL, "cookie_create_project_test.json")
  63. user := initUser(email, client, t)
  64. client.Login(context.Background(), &api.LoginRequest{
  65. Email: user.Email,
  66. Password: "hello1234",
  67. })
  68. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  69. Name: "project-test",
  70. })
  71. if err != nil {
  72. t.Fatalf("%v\n", err)
  73. }
  74. // make sure user is admin and project name is correct
  75. if resp.Name != "project-test" {
  76. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  77. }
  78. if len(resp.Roles) != 1 {
  79. t.Fatalf("project role length is not 1")
  80. }
  81. if resp.Roles[0].Kind != models.RoleAdmin {
  82. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  83. }
  84. if resp.Roles[0].UserID != user.ID {
  85. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  86. }
  87. }
  88. func TestGetProject(t *testing.T) {
  89. email := "get_project_test@example.com"
  90. client := api.NewClient(baseURL, "cookie_get_project_test.json")
  91. user := initUser(email, client, t)
  92. client.Login(context.Background(), &api.LoginRequest{
  93. Email: user.Email,
  94. Password: "hello1234",
  95. })
  96. project := initProject("project-test", client, t)
  97. resp, err := client.GetProject(context.Background(), project.ID)
  98. if err != nil {
  99. t.Fatalf("%v\n", err)
  100. }
  101. // make sure user is admin and project name is correct
  102. if resp.Name != "project-test" {
  103. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  104. }
  105. if len(resp.Roles) != 1 {
  106. t.Fatalf("project role length is not 1")
  107. }
  108. if resp.Roles[0].Kind != models.RoleAdmin {
  109. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  110. }
  111. if resp.Roles[0].UserID != user.ID {
  112. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  113. }
  114. }
  115. func TestCreateProjectCandidates(t *testing.T) {
  116. email := "create_project_candidates_test@example.com"
  117. client := api.NewClient(baseURL, "cookie_create_project_candidates_test.json")
  118. user := initUser(email, client, t)
  119. client.Login(context.Background(), &api.LoginRequest{
  120. Email: user.Email,
  121. Password: "hello1234",
  122. })
  123. project := initProject("project-test", client, t)
  124. resp, err := client.CreateProjectCandidates(
  125. context.Background(),
  126. project.ID,
  127. &api.CreateProjectCandidatesRequest{
  128. Kubeconfig: OIDCAuthWithoutData,
  129. },
  130. )
  131. if err != nil {
  132. t.Fatalf("%v\n", err)
  133. }
  134. // make sure length is 1
  135. if len(resp) != 1 {
  136. t.Fatalf("candidates length is not 1\n")
  137. }
  138. // make sure auth mechanism is OIDC, project id is correct, and cluster info is correct
  139. if resp[0].AuthMechanism != models.OIDC {
  140. t.Errorf("oidc auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp[0].AuthMechanism)
  141. }
  142. if resp[0].ProjectID != project.ID {
  143. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp[0].ProjectID)
  144. }
  145. if resp[0].ClusterName != "cluster-test" {
  146. t.Errorf("cluster name incorrect: expected %s, got %s\n", "cluster-test", resp[0].ClusterName)
  147. }
  148. if resp[0].ClusterEndpoint != "https://localhost" {
  149. t.Errorf("cluster endpoint incorrect: expected %s, got %s\n", "https://localhost", resp[0].ClusterEndpoint)
  150. }
  151. // make sure correct actions need to be performed
  152. if len(resp[0].Actions) != 1 {
  153. t.Fatalf("actions length is not 1\n")
  154. }
  155. if resp[0].Actions[0].Name != models.OIDCIssuerDataAction {
  156. t.Errorf("action name incorrect: expected %s, got %s\n", models.OIDCIssuerDataAction, resp[0].Actions[0].Name)
  157. }
  158. if resp[0].Actions[0].Filename != "/fake/path/to/ca.pem" {
  159. t.Errorf("action filename incorrect: expected %s, got %s\n", "/fake/path/to/ca.pem", resp[0].Actions[0].Filename)
  160. }
  161. }
  162. func TestGetProjectCandidates(t *testing.T) {
  163. email := "get_project_candidates_test@example.com"
  164. client := api.NewClient(baseURL, "cookie_get_project_candidates_test.json")
  165. user := initUser(email, client, t)
  166. client.Login(context.Background(), &api.LoginRequest{
  167. Email: user.Email,
  168. Password: "hello1234",
  169. })
  170. project := initProject("project-test", client, t)
  171. initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  172. resp, err := client.GetProjectCandidates(context.Background(), project.ID)
  173. if err != nil {
  174. t.Fatalf("%v\n", err)
  175. }
  176. // make sure length is 1
  177. if len(resp) != 1 {
  178. t.Fatalf("candidates length is not 1\n")
  179. }
  180. // make sure auth mechanism is OIDC, project id is correct, and cluster info is correct
  181. if resp[0].AuthMechanism != models.OIDC {
  182. t.Errorf("oidc auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp[0].AuthMechanism)
  183. }
  184. if resp[0].ProjectID != project.ID {
  185. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp[0].ProjectID)
  186. }
  187. if resp[0].ClusterName != "cluster-test" {
  188. t.Errorf("cluster name incorrect: expected %s, got %s\n", "cluster-test", resp[0].ClusterName)
  189. }
  190. if resp[0].ClusterEndpoint != "https://localhost" {
  191. t.Errorf("cluster endpoint incorrect: expected %s, got %s\n", "https://localhost", resp[0].ClusterEndpoint)
  192. }
  193. // make sure correct actions need to be performed
  194. if len(resp[0].Actions) != 1 {
  195. t.Fatalf("actions length is not 1\n")
  196. }
  197. if resp[0].Actions[0].Name != models.OIDCIssuerDataAction {
  198. t.Errorf("action name incorrect: expected %s, got %s\n", models.OIDCIssuerDataAction, resp[0].Actions[0].Name)
  199. }
  200. if resp[0].Actions[0].Filename != "/fake/path/to/ca.pem" {
  201. t.Errorf("action filename incorrect: expected %s, got %s\n", "/fake/path/to/ca.pem", resp[0].Actions[0].Filename)
  202. }
  203. }
  204. func TestCreateProjectServiceAccount(t *testing.T) {
  205. email := "create_project_sa_test@example.com"
  206. client := api.NewClient(baseURL, "cookie_create_project_sa_test.json")
  207. user := initUser(email, client, t)
  208. client.Login(context.Background(), &api.LoginRequest{
  209. Email: user.Email,
  210. Password: "hello1234",
  211. })
  212. project := initProject("project-test", client, t)
  213. saCandidate := initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  214. resp, err := client.CreateProjectServiceAccount(
  215. context.Background(),
  216. project.ID,
  217. saCandidate.ID,
  218. api.CreateProjectServiceAccountRequest{
  219. &models.ServiceAccountAllActions{
  220. Name: models.OIDCIssuerDataAction,
  221. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  222. },
  223. },
  224. )
  225. if err != nil {
  226. t.Fatalf("%v\n", err)
  227. }
  228. // ensure project id and metadata is correct
  229. if resp.ProjectID != project.ID {
  230. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp.ProjectID)
  231. }
  232. if resp.Kind != "connector" {
  233. t.Errorf("service account kind incorrect: expected %s, got %s\n", "connector", resp.Kind)
  234. }
  235. if resp.AuthMechanism != models.OIDC {
  236. t.Errorf("service account auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp.AuthMechanism)
  237. }
  238. // verify clusters
  239. if len(resp.Clusters) != 1 {
  240. t.Fatalf("length of clusters is not 1")
  241. }
  242. if resp.Clusters[0].ServiceAccountID != resp.ID {
  243. t.Errorf("cluster's sa id is incorrect: expected %d, got %d\n", resp.ID, resp.Clusters[0].ServiceAccountID)
  244. }
  245. if resp.Clusters[0].Name != "cluster-test" {
  246. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "cluster-test", resp.Clusters[0].Name)
  247. }
  248. if resp.Clusters[0].Server != "https://localhost" {
  249. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "https://localhost", resp.Clusters[0].Server)
  250. }
  251. }
  252. func TestListProjectClusters(t *testing.T) {
  253. email := "list_project_clusters_test@example.com"
  254. client := api.NewClient(baseURL, "cookie_list_project_clusters_test.json")
  255. user := initUser(email, client, t)
  256. client.Login(context.Background(), &api.LoginRequest{
  257. Email: user.Email,
  258. Password: "hello1234",
  259. })
  260. project := initProject("project-test", client, t)
  261. saCandidate := initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  262. sa := initProjectSA(project.ID, saCandidate.ID, client, t)
  263. resp, err := client.ListProjectClusters(
  264. context.Background(),
  265. project.ID,
  266. )
  267. if err != nil {
  268. t.Fatalf("%v\n", err)
  269. }
  270. // verify clusters
  271. if len(resp) != 1 {
  272. t.Fatalf("length of clusters is not 1")
  273. }
  274. if resp[0].ServiceAccountID != sa.ID {
  275. t.Errorf("cluster's sa id is incorrect: expected %d, got %d\n", sa.ID, resp[0].ServiceAccountID)
  276. }
  277. if resp[0].Name != "cluster-test" {
  278. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "cluster-test", resp[0].Name)
  279. }
  280. if resp[0].Server != "https://localhost" {
  281. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "https://localhost", resp[0].Server)
  282. }
  283. }
  284. func TestDeleteProject(t *testing.T) {
  285. email := "delete_project_test@example.com"
  286. client := api.NewClient(baseURL, "cookie_delete_project_test.json")
  287. user := initUser(email, client, t)
  288. client.Login(context.Background(), &api.LoginRequest{
  289. Email: user.Email,
  290. Password: "hello1234",
  291. })
  292. project := initProject("project-test", client, t)
  293. resp, err := client.DeleteProject(context.Background(), project.ID)
  294. if err != nil {
  295. t.Fatalf("%v\n", err)
  296. }
  297. // make sure user is admin and project name is correct
  298. if resp.Name != "project-test" {
  299. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  300. }
  301. if len(resp.Roles) != 1 {
  302. t.Fatalf("project role length is not 1")
  303. }
  304. if resp.Roles[0].Kind != models.RoleAdmin {
  305. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  306. }
  307. if resp.Roles[0].UserID != user.ID {
  308. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  309. }
  310. // make sure that project can no longer be found
  311. _, err = client.GetProject(context.Background(), project.ID)
  312. if err == nil {
  313. t.Fatalf("no error returned\n")
  314. }
  315. }
  316. const OIDCAuthWithoutData string = `
  317. apiVersion: v1
  318. clusters:
  319. - cluster:
  320. server: https://localhost
  321. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  322. name: cluster-test
  323. contexts:
  324. - context:
  325. cluster: cluster-test
  326. user: test-admin
  327. name: context-test
  328. current-context: context-test
  329. kind: Config
  330. preferences: {}
  331. users:
  332. - name: test-admin
  333. user:
  334. auth-provider:
  335. config:
  336. client-id: porter-api
  337. id-token: token
  338. idp-issuer-url: https://localhost
  339. idp-certificate-authority: /fake/path/to/ca.pem
  340. name: oidc
  341. `