project_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 TestCreateProject(t *testing.T) {
  38. email := "create_project_test@example.com"
  39. client := api.NewClient(baseURL, "cookie_create_project_test.json")
  40. user := initUser(email, client, t)
  41. client.Login(context.Background(), &api.LoginRequest{
  42. Email: user.Email,
  43. Password: "hello1234",
  44. })
  45. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  46. Name: "project-test",
  47. })
  48. if err != nil {
  49. t.Fatalf("%v\n", err)
  50. }
  51. // make sure user is admin and project name is correct
  52. if resp.Name != "project-test" {
  53. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  54. }
  55. if len(resp.Roles) != 1 {
  56. t.Fatalf("project role length is not 1")
  57. }
  58. if resp.Roles[0].Kind != models.RoleAdmin {
  59. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  60. }
  61. if resp.Roles[0].UserID != user.ID {
  62. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  63. }
  64. }
  65. func TestGetProject(t *testing.T) {
  66. email := "get_project_test@example.com"
  67. client := api.NewClient(baseURL, "cookie_get_project_test.json")
  68. user := initUser(email, client, t)
  69. client.Login(context.Background(), &api.LoginRequest{
  70. Email: user.Email,
  71. Password: "hello1234",
  72. })
  73. project := initProject("project-test", client, t)
  74. resp, err := client.GetProject(context.Background(), project.ID)
  75. if err != nil {
  76. t.Fatalf("%v\n", err)
  77. }
  78. // make sure user is admin and project name is correct
  79. if resp.Name != "project-test" {
  80. t.Errorf("project name incorrect: expected %s, got %s\n", "project-test", resp.Name)
  81. }
  82. if len(resp.Roles) != 1 {
  83. t.Fatalf("project role length is not 1")
  84. }
  85. if resp.Roles[0].Kind != models.RoleAdmin {
  86. t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
  87. }
  88. if resp.Roles[0].UserID != user.ID {
  89. t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
  90. }
  91. }
  92. func TestCreateProjectCandidates(t *testing.T) {
  93. email := "create_project_candidates_test@example.com"
  94. client := api.NewClient(baseURL, "cookie_create_project_candidates_test.json")
  95. user := initUser(email, client, t)
  96. client.Login(context.Background(), &api.LoginRequest{
  97. Email: user.Email,
  98. Password: "hello1234",
  99. })
  100. project := initProject("project-test", client, t)
  101. resp, err := client.CreateProjectCandidates(
  102. context.Background(),
  103. project.ID,
  104. &api.CreateProjectCandidatesRequest{
  105. Kubeconfig: OIDCAuthWithoutData,
  106. },
  107. )
  108. if err != nil {
  109. t.Fatalf("%v\n", err)
  110. }
  111. // make sure length is 1
  112. if len(resp) != 1 {
  113. t.Fatalf("candidates length is not 1\n")
  114. }
  115. // make sure auth mechanism is OIDC, project id is correct, and cluster info is correct
  116. if resp[0].AuthMechanism != models.OIDC {
  117. t.Errorf("oidc auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp[0].AuthMechanism)
  118. }
  119. if resp[0].ProjectID != project.ID {
  120. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp[0].ProjectID)
  121. }
  122. if resp[0].ClusterName != "cluster-test" {
  123. t.Errorf("cluster name incorrect: expected %s, got %s\n", "cluster-test", resp[0].ClusterName)
  124. }
  125. if resp[0].ClusterEndpoint != "https://localhost" {
  126. t.Errorf("cluster endpoint incorrect: expected %s, got %s\n", "https://localhost", resp[0].ClusterEndpoint)
  127. }
  128. // make sure correct actions need to be performed
  129. if len(resp[0].Actions) != 1 {
  130. t.Fatalf("actions length is not 1\n")
  131. }
  132. if resp[0].Actions[0].Name != models.OIDCIssuerDataAction {
  133. t.Errorf("action name incorrect: expected %s, got %s\n", models.OIDCIssuerDataAction, resp[0].Actions[0].Name)
  134. }
  135. }
  136. func TestGetProjectCandidates(t *testing.T) {
  137. email := "get_project_candidates_test@example.com"
  138. client := api.NewClient(baseURL, "cookie_get_project_candidates_test.json")
  139. user := initUser(email, client, t)
  140. client.Login(context.Background(), &api.LoginRequest{
  141. Email: user.Email,
  142. Password: "hello1234",
  143. })
  144. project := initProject("project-test", client, t)
  145. initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  146. resp, err := client.GetProjectCandidates(context.Background(), project.ID)
  147. if err != nil {
  148. t.Fatalf("%v\n", err)
  149. }
  150. // make sure length is 1
  151. if len(resp) != 1 {
  152. t.Fatalf("candidates length is not 1\n")
  153. }
  154. // make sure auth mechanism is OIDC, project id is correct, and cluster info is correct
  155. if resp[0].AuthMechanism != models.OIDC {
  156. t.Errorf("oidc auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp[0].AuthMechanism)
  157. }
  158. if resp[0].ProjectID != project.ID {
  159. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp[0].ProjectID)
  160. }
  161. if resp[0].ClusterName != "cluster-test" {
  162. t.Errorf("cluster name incorrect: expected %s, got %s\n", "cluster-test", resp[0].ClusterName)
  163. }
  164. if resp[0].ClusterEndpoint != "https://localhost" {
  165. t.Errorf("cluster endpoint incorrect: expected %s, got %s\n", "https://localhost", resp[0].ClusterEndpoint)
  166. }
  167. // make sure correct actions need to be performed
  168. if len(resp[0].Actions) != 1 {
  169. t.Fatalf("actions length is not 1\n")
  170. }
  171. if resp[0].Actions[0].Name != models.OIDCIssuerDataAction {
  172. t.Errorf("action name incorrect: expected %s, got %s\n", models.OIDCIssuerDataAction, resp[0].Actions[0].Name)
  173. }
  174. }
  175. func TestCreateProjectServiceAccount(t *testing.T) {
  176. email := "create_project_sa_test@example.com"
  177. client := api.NewClient(baseURL, "cookie_create_project_sa_test.json")
  178. user := initUser(email, client, t)
  179. client.Login(context.Background(), &api.LoginRequest{
  180. Email: user.Email,
  181. Password: "hello1234",
  182. })
  183. project := initProject("project-test", client, t)
  184. saCandidate := initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  185. resp, err := client.CreateProjectServiceAccount(
  186. context.Background(),
  187. project.ID,
  188. saCandidate.ID,
  189. api.CreateProjectServiceAccountRequest{
  190. &models.ServiceAccountAllActions{
  191. Name: models.OIDCIssuerDataAction,
  192. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  193. },
  194. },
  195. )
  196. if err != nil {
  197. t.Fatalf("%v\n", err)
  198. }
  199. // ensure project id and metadata is correct
  200. if resp.ProjectID != project.ID {
  201. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp.ProjectID)
  202. }
  203. if resp.Kind != "connector" {
  204. t.Errorf("service account kind incorrect: expected %s, got %s\n", "connector", resp.Kind)
  205. }
  206. if resp.AuthMechanism != models.OIDC {
  207. t.Errorf("service account auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp.AuthMechanism)
  208. }
  209. // verify clusters
  210. if len(resp.Clusters) != 1 {
  211. t.Fatalf("length of clusters is not 1")
  212. }
  213. if resp.Clusters[0].ServiceAccountID != resp.ID {
  214. t.Errorf("cluster's sa id is incorrect: expected %d, got %d\n", resp.ID, resp.Clusters[0].ServiceAccountID)
  215. }
  216. if resp.Clusters[0].Name != "cluster-test" {
  217. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "cluster-test", resp.Clusters[0].Name)
  218. }
  219. if resp.Clusters[0].Server != "https://localhost" {
  220. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "https://localhost", resp.Clusters[0].Server)
  221. }
  222. }
  223. const OIDCAuthWithoutData string = `
  224. apiVersion: v1
  225. clusters:
  226. - cluster:
  227. server: https://localhost
  228. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  229. name: cluster-test
  230. contexts:
  231. - context:
  232. cluster: cluster-test
  233. user: test-admin
  234. name: context-test
  235. current-context: context-test
  236. kind: Config
  237. preferences: {}
  238. users:
  239. - name: test-admin
  240. user:
  241. auth-provider:
  242. config:
  243. client-id: porter-api
  244. id-token: token
  245. idp-issuer-url: https://localhost
  246. idp-certificate-authority: /fake/path/to/ca.pem
  247. name: oidc
  248. `