From 671ee21355bb570aa165e1d700f904205287ade2 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Fri, 5 Apr 2024 17:50:04 -0700 Subject: [PATCH] Starting API unit tests!.... yay --- api/handlers/v1/auth.go | 13 +++-- api/handlers/v1/auth_test.go | 98 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 api/handlers/v1/auth_test.go diff --git a/api/handlers/v1/auth.go b/api/handlers/v1/auth.go index 9d8355a..6c66cdd 100644 --- a/api/handlers/v1/auth.go +++ b/api/handlers/v1/auth.go @@ -2,10 +2,11 @@ package v1 import ( "errors" - "go-cook/api/domain" - "go-cook/api/repositories" "net/http" + "git.jamestombleson.com/jtom38/go-cook/api/repositories" + "git.jamestombleson.com/jtom38/go-cook/api/domain" + "github.com/golang-jwt/jwt/v5" "github.com/labstack/echo/v4" ) @@ -57,11 +58,13 @@ func (h *Handler) AuthRegister(c echo.Context) error { }) } - return nil + return c.JSON(http.StatusCreated, domain.ErrorResponse{ + Success: true, + }) } func (h *Handler) AuthLogin(c echo.Context) error { - username := c.FormValue("name") + username := c.FormValue("username") password := c.FormValue("password") // Check to see if they are trying to login with the admin token @@ -148,7 +151,7 @@ func (h *Handler) RemoveScopes(c echo.Context) error { if err != nil { return h.ReturnUnauthorizedResponse(c, err.Error()) } - + request := domain.UpdateScopesRequest{} err = (&echo.DefaultBinder{}).BindBody(c, &request) if err != nil { diff --git a/api/handlers/v1/auth_test.go b/api/handlers/v1/auth_test.go new file mode 100644 index 0000000..800127d --- /dev/null +++ b/api/handlers/v1/auth_test.go @@ -0,0 +1,98 @@ +package v1_test + +import ( + "database/sql" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + + v1 "git.jamestombleson.com/jtom38/go-cook/api/handlers/v1" + "git.jamestombleson.com/jtom38/go-cook/api/domain" + + _ "github.com/glebarez/go-sqlite" + "github.com/labstack/echo/v4" + "github.com/pressly/goose/v3" + "github.com/stretchr/testify/assert" +) + +func setupInMemoryDb() (*sql.DB, error) { + db, err := sql.Open("sqlite", ":memory:") + if err != nil { + return nil, err + } + + err = goose.SetDialect("sqlite3") + if err != nil { + return nil, err + } + + err = goose.Up(db, "../../migrations") + if err != nil { + return nil, err + } + return db, nil +} + +func TestAuthCreateUser(t *testing.T) { + db, err := setupInMemoryDb() + if err != nil { + t.Error(err) + return + } + + h := v1.NewHandler(db, domain.EnvConfig{ + AdminToken: "lol", + JwtSecret: "lol", + }) + + e := echo.New() + form := make(url.Values) + form.Set("username", "test") + form.Set("password", "test1234!") + + req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/register", strings.NewReader(form.Encode())) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + if assert.NoError(t, h.AuthRegister(c)) { + assert.Equal(t, http.StatusCreated, rec.Code) + } +} + +func TestAuthLoginUser(t *testing.T) { + db, err := setupInMemoryDb() + if err != nil { + t.Error(err) + return + } + + h := v1.NewHandler(db, domain.EnvConfig{ + AdminToken: "lol", + JwtSecret: "lol", + }) + + e := echo.New() + form := make(url.Values) + form.Set("username", "test") + form.Set("password", "test1234!") + + req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/login", strings.NewReader(form.Encode())) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + // create the user + err = h.AuthRegister(c) + if err != nil { + t.Error(err) + t.FailNow() + } + + // now test to make sure it worked + if assert.NoError(t, h.AuthLogin(c)) { + assert.Equal(t, http.StatusOK, rec.Code) + } +}