project_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. if resp[0].Actions[0].Filename != "/fake/path/to/ca.pem" {
  136. t.Errorf("action filename incorrect: expected %s, got %s\n", "/fake/path/to/ca.pem", resp[0].Actions[0].Filename)
  137. }
  138. }
  139. func TestGetProjectCandidates(t *testing.T) {
  140. email := "get_project_candidates_test@example.com"
  141. client := api.NewClient(baseURL, "cookie_get_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. initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  149. resp, err := client.GetProjectCandidates(context.Background(), project.ID)
  150. if err != nil {
  151. t.Fatalf("%v\n", err)
  152. }
  153. // make sure length is 1
  154. if len(resp) != 1 {
  155. t.Fatalf("candidates length is not 1\n")
  156. }
  157. // make sure auth mechanism is OIDC, project id is correct, and cluster info is correct
  158. if resp[0].AuthMechanism != models.OIDC {
  159. t.Errorf("oidc auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp[0].AuthMechanism)
  160. }
  161. if resp[0].ProjectID != project.ID {
  162. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp[0].ProjectID)
  163. }
  164. if resp[0].ClusterName != "cluster-test" {
  165. t.Errorf("cluster name incorrect: expected %s, got %s\n", "cluster-test", resp[0].ClusterName)
  166. }
  167. if resp[0].ClusterEndpoint != "https://localhost" {
  168. t.Errorf("cluster endpoint incorrect: expected %s, got %s\n", "https://localhost", resp[0].ClusterEndpoint)
  169. }
  170. // make sure correct actions need to be performed
  171. if len(resp[0].Actions) != 1 {
  172. t.Fatalf("actions length is not 1\n")
  173. }
  174. if resp[0].Actions[0].Name != models.OIDCIssuerDataAction {
  175. t.Errorf("action name incorrect: expected %s, got %s\n", models.OIDCIssuerDataAction, resp[0].Actions[0].Name)
  176. }
  177. if resp[0].Actions[0].Filename != "/fake/path/to/ca.pem" {
  178. t.Errorf("action filename incorrect: expected %s, got %s\n", "/fake/path/to/ca.pem", resp[0].Actions[0].Filename)
  179. }
  180. }
  181. func TestCreateProjectServiceAccount(t *testing.T) {
  182. email := "create_project_sa_test@example.com"
  183. client := api.NewClient(baseURL, "cookie_create_project_sa_test.json")
  184. user := initUser(email, client, t)
  185. client.Login(context.Background(), &api.LoginRequest{
  186. Email: user.Email,
  187. Password: "hello1234",
  188. })
  189. project := initProject("project-test", client, t)
  190. saCandidate := initProjectCandidate(project.ID, OIDCAuthWithoutData, client, t)
  191. resp, err := client.CreateProjectServiceAccount(
  192. context.Background(),
  193. project.ID,
  194. saCandidate.ID,
  195. api.CreateProjectServiceAccountRequest{
  196. &models.ServiceAccountAllActions{
  197. Name: models.OIDCIssuerDataAction,
  198. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  199. },
  200. },
  201. )
  202. if err != nil {
  203. t.Fatalf("%v\n", err)
  204. }
  205. // ensure project id and metadata is correct
  206. if resp.ProjectID != project.ID {
  207. t.Errorf("project id incorrect: expected %d, got %d\n", project.ID, resp.ProjectID)
  208. }
  209. if resp.Kind != "connector" {
  210. t.Errorf("service account kind incorrect: expected %s, got %s\n", "connector", resp.Kind)
  211. }
  212. if resp.AuthMechanism != models.OIDC {
  213. t.Errorf("service account auth mechanism incorrect: expected %s, got %s\n", models.OIDC, resp.AuthMechanism)
  214. }
  215. // verify clusters
  216. if len(resp.Clusters) != 1 {
  217. t.Fatalf("length of clusters is not 1")
  218. }
  219. if resp.Clusters[0].ServiceAccountID != resp.ID {
  220. t.Errorf("cluster's sa id is incorrect: expected %d, got %d\n", resp.ID, resp.Clusters[0].ServiceAccountID)
  221. }
  222. if resp.Clusters[0].Name != "cluster-test" {
  223. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "cluster-test", resp.Clusters[0].Name)
  224. }
  225. if resp.Clusters[0].Server != "https://localhost" {
  226. t.Errorf("cluster's name is incorrect: expected %s, got %s\n", "https://localhost", resp.Clusters[0].Server)
  227. }
  228. }
  229. const OIDCAuthWithoutData string = `
  230. apiVersion: v1
  231. clusters:
  232. - cluster:
  233. server: https://localhost
  234. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  235. name: cluster-test
  236. contexts:
  237. - context:
  238. cluster: cluster-test
  239. user: test-admin
  240. name: context-test
  241. current-context: context-test
  242. kind: Config
  243. preferences: {}
  244. users:
  245. - name: test-admin
  246. user:
  247. auth-provider:
  248. config:
  249. client-id: porter-api
  250. id-token: token
  251. idp-issuer-url: https://localhost
  252. idp-certificate-authority: /fake/path/to/ca.pem
  253. name: oidc
  254. `