user_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package api_test
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. "github.com/porter-dev/porter/internal/models"
  8. )
  9. func initUser(email string, client *api.Client, t *testing.T) *api.CreateUserResponse {
  10. t.Helper()
  11. resp, err := client.CreateUser(context.Background(), &api.CreateUserRequest{
  12. Email: email,
  13. Password: "hello1234",
  14. })
  15. if err != nil {
  16. t.Fatalf("%v\n", err)
  17. }
  18. return resp
  19. }
  20. func TestLogin(t *testing.T) {
  21. email := "login_test@example.com"
  22. client := api.NewClient(baseURL, "cookie_login_test.json")
  23. user := initUser(email, client, t)
  24. resp, err := client.Login(context.Background(), &api.LoginRequest{
  25. Email: user.Email,
  26. Password: "hello1234",
  27. })
  28. if err != nil {
  29. t.Fatalf("%v\n", err)
  30. }
  31. if resp.Email != user.Email {
  32. t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
  33. }
  34. }
  35. func TestLogout(t *testing.T) {
  36. email := "logout_test@example.com"
  37. client := api.NewClient(baseURL, "cookie_logout_test.json")
  38. user := initUser(email, client, t)
  39. client.Login(context.Background(), &api.LoginRequest{
  40. Email: user.Email,
  41. Password: "hello1234",
  42. })
  43. err := client.Logout(context.Background())
  44. if err != nil {
  45. t.Fatalf("%v\n", err)
  46. }
  47. // try to get the user and ensure 403
  48. _, err = client.AuthCheck(context.Background())
  49. if err != nil && !strings.Contains(err.Error(), "403") {
  50. t.Fatalf("%v\n", err)
  51. }
  52. }
  53. func TestAuthCheck(t *testing.T) {
  54. email := "auth_check_test@example.com"
  55. client := api.NewClient(baseURL, "cookie_auth_check_test.json")
  56. user := initUser(email, client, t)
  57. client.Login(context.Background(), &api.LoginRequest{
  58. Email: user.Email,
  59. Password: "hello1234",
  60. })
  61. resp, err := client.AuthCheck(context.Background())
  62. if err != nil {
  63. t.Fatalf("%v\n", err)
  64. }
  65. if resp.Email != user.Email {
  66. t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
  67. }
  68. }
  69. func TestGetUser(t *testing.T) {
  70. email := "get_user_test@example.com"
  71. client := api.NewClient(baseURL, "cookie_get_user_test.json")
  72. user := initUser(email, client, t)
  73. resp, err := client.GetUser(context.Background(), user.ID)
  74. if err != nil {
  75. t.Fatalf("%v\n", err)
  76. }
  77. if resp.Email != user.Email {
  78. t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
  79. }
  80. }
  81. func TestListUserProjects(t *testing.T) {
  82. email := "list_user_projects@example.com"
  83. client := api.NewClient(baseURL, "cookie_list_user_projects.json")
  84. user := initUser(email, client, t)
  85. client.Login(context.Background(), &api.LoginRequest{
  86. Email: user.Email,
  87. Password: "hello1234",
  88. })
  89. project := initProject("project-test", client, t)
  90. projects, err := client.ListUserProjects(context.Background(), user.ID)
  91. if err != nil {
  92. t.Fatalf("%v\n", err)
  93. }
  94. if len(projects) != 1 {
  95. t.Fatalf("length of projects is not 1")
  96. }
  97. resp := projects[0]
  98. // make sure user is admin and project name is correct
  99. if resp.Name != project.Name {
  100. t.Errorf("project name incorrect: expected %s, got %s\n", project.Name, 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 TestDeleteUser(t *testing.T) {
  113. email := "delete_user_test@example.com"
  114. client := api.NewClient(baseURL, "cookie_delete_user_test.json")
  115. user := initUser(email, client, t)
  116. err := client.DeleteUser(context.Background(), user.ID, &api.DeleteUserRequest{
  117. Password: "hello1234",
  118. })
  119. if err != nil {
  120. t.Fatalf("%v\n", err)
  121. }
  122. _, err = client.GetUser(context.Background(), user.ID)
  123. if err != nil && !strings.Contains(err.Error(), "could not find requested object") {
  124. t.Fatalf("%v\n", err)
  125. }
  126. }