user_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package api_test
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. )
  8. func initUser(email string, client *api.Client, t *testing.T) *api.CreateUserResponse {
  9. t.Helper()
  10. resp, err := client.CreateUser(context.Background(), &api.CreateUserRequest{
  11. Email: email,
  12. Password: "hello1234",
  13. })
  14. if err != nil {
  15. t.Fatalf("%v\n", err)
  16. }
  17. return resp
  18. }
  19. func TestLogin(t *testing.T) {
  20. email := "login_test@example.com"
  21. client := api.NewClient(baseURL, "cookie_login_test.json")
  22. user := initUser(email, client, t)
  23. resp, err := client.Login(context.Background(), &api.LoginRequest{
  24. Email: user.Email,
  25. Password: "hello1234",
  26. })
  27. if err != nil {
  28. t.Fatalf("%v\n", err)
  29. }
  30. if resp.Email != user.Email {
  31. t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
  32. }
  33. }
  34. func TestLogout(t *testing.T) {
  35. email := "logout_test@example.com"
  36. client := api.NewClient(baseURL, "cookie_logout_test.json")
  37. user := initUser(email, client, t)
  38. client.Login(context.Background(), &api.LoginRequest{
  39. Email: user.Email,
  40. Password: "hello1234",
  41. })
  42. err := client.Logout(context.Background())
  43. if err != nil {
  44. t.Fatalf("%v\n", err)
  45. }
  46. // try to get the user and ensure 403
  47. _, err = client.AuthCheck(context.Background())
  48. if err != nil && !strings.Contains(err.Error(), "403") {
  49. t.Fatalf("%v\n", err)
  50. }
  51. }
  52. func TestAuthCheck(t *testing.T) {
  53. email := "auth_check_test@example.com"
  54. client := api.NewClient(baseURL, "cookie_auth_check_test.json")
  55. user := initUser(email, client, t)
  56. client.Login(context.Background(), &api.LoginRequest{
  57. Email: user.Email,
  58. Password: "hello1234",
  59. })
  60. resp, err := client.AuthCheck(context.Background())
  61. if err != nil {
  62. t.Fatalf("%v\n", err)
  63. }
  64. if resp.Email != user.Email {
  65. t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
  66. }
  67. }
  68. func TestGetUser(t *testing.T) {
  69. email := "get_user_test@example.com"
  70. client := api.NewClient(baseURL, "cookie_get_user_test.json")
  71. user := initUser(email, client, t)
  72. resp, err := client.GetUser(context.Background(), user.ID)
  73. if err != nil {
  74. t.Fatalf("%v\n", err)
  75. }
  76. if resp.Email != user.Email {
  77. t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
  78. }
  79. }
  80. func TestDeleteUser(t *testing.T) {
  81. email := "delete_user_test@example.com"
  82. client := api.NewClient(baseURL, "cookie_delete_user_test.json")
  83. user := initUser(email, client, t)
  84. err := client.DeleteUser(context.Background(), user.ID, &api.DeleteUserRequest{
  85. Password: "hello1234",
  86. })
  87. if err != nil {
  88. t.Fatalf("%v\n", err)
  89. }
  90. _, err = client.GetUser(context.Background(), user.ID)
  91. if err != nil && !strings.Contains(err.Error(), "could not find requested object") {
  92. t.Fatalf("%v\n", err)
  93. }
  94. }