project_test.go 13 KB

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