| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package api_test
- import (
- "context"
- "strings"
- "testing"
- "github.com/porter-dev/porter/cli/cmd/api"
- "github.com/porter-dev/porter/internal/models"
- )
- func initUser(email string, client *api.Client, t *testing.T) *api.CreateUserResponse {
- t.Helper()
- resp, err := client.CreateUser(context.Background(), &api.CreateUserRequest{
- Email: email,
- Password: "hello1234",
- })
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- return resp
- }
- func TestLogin(t *testing.T) {
- email := "login_test@example.com"
- client := api.NewClient(baseURL, "cookie_login_test.json")
- user := initUser(email, client, t)
- resp, err := client.Login(context.Background(), &api.LoginRequest{
- Email: user.Email,
- Password: "hello1234",
- })
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- if resp.Email != user.Email {
- t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
- }
- }
- func TestLogout(t *testing.T) {
- email := "logout_test@example.com"
- client := api.NewClient(baseURL, "cookie_logout_test.json")
- user := initUser(email, client, t)
- client.Login(context.Background(), &api.LoginRequest{
- Email: user.Email,
- Password: "hello1234",
- })
- err := client.Logout(context.Background())
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- // try to get the user and ensure 403
- _, err = client.AuthCheck(context.Background())
- if err != nil && !strings.Contains(err.Error(), "403") {
- t.Fatalf("%v\n", err)
- }
- }
- func TestAuthCheck(t *testing.T) {
- email := "auth_check_test@example.com"
- client := api.NewClient(baseURL, "cookie_auth_check_test.json")
- user := initUser(email, client, t)
- client.Login(context.Background(), &api.LoginRequest{
- Email: user.Email,
- Password: "hello1234",
- })
- resp, err := client.AuthCheck(context.Background())
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- if resp.Email != user.Email {
- t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
- }
- }
- func TestGetUser(t *testing.T) {
- email := "get_user_test@example.com"
- client := api.NewClient(baseURL, "cookie_get_user_test.json")
- user := initUser(email, client, t)
- resp, err := client.GetUser(context.Background(), user.ID)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- if resp.Email != user.Email {
- t.Errorf("incorrect email: expected %s, got %s\n", user.Email, resp.Email)
- }
- }
- func TestListUserProjects(t *testing.T) {
- email := "list_user_projects@example.com"
- client := api.NewClient(baseURL, "cookie_list_user_projects.json")
- user := initUser(email, client, t)
- client.Login(context.Background(), &api.LoginRequest{
- Email: user.Email,
- Password: "hello1234",
- })
- project := initProject("project-test", client, t)
- projects, err := client.ListUserProjects(context.Background(), user.ID)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- if len(projects) != 1 {
- t.Fatalf("length of projects is not 1")
- }
- resp := projects[0]
- // make sure user is admin and project name is correct
- if resp.Name != project.Name {
- t.Errorf("project name incorrect: expected %s, got %s\n", project.Name, resp.Name)
- }
- if len(resp.Roles) != 1 {
- t.Fatalf("project role length is not 1")
- }
- if resp.Roles[0].Kind != models.RoleAdmin {
- t.Errorf("project role kind is incorrect: expected %s, got %s\n", models.RoleAdmin, resp.Roles[0].Kind)
- }
- if resp.Roles[0].UserID != user.ID {
- t.Errorf("project role user_id is incorrect: expected %d, got %d\n", user.ID, resp.Roles[0].UserID)
- }
- }
- func TestDeleteUser(t *testing.T) {
- email := "delete_user_test@example.com"
- client := api.NewClient(baseURL, "cookie_delete_user_test.json")
- user := initUser(email, client, t)
- err := client.DeleteUser(context.Background(), user.ID, &api.DeleteUserRequest{
- Password: "hello1234",
- })
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- _, err = client.GetUser(context.Background(), user.ID)
- if err != nil && !strings.Contains(err.Error(), "could not find requested object") {
- t.Fatalf("%v\n", err)
- }
- }
|