project_test.go 12 KB

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