user_test.go 3.0 KB

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