goshort/internal/server/api/handler_test.go
Gustavo Maronato 4fef573447
Some checks failed
Check / checks (push) Failing after 4m11s
added frontend oidc support
2024-03-09 05:42:36 -05:00

276 lines
7.6 KiB
Go

package apiserver_test
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"git.maronato.dev/maronato/goshort/internal/config"
apiserver "git.maronato.dev/maronato/goshort/internal/server/api"
servermiddleware "git.maronato.dev/maronato/goshort/internal/server/middleware"
authmiddleware "git.maronato.dev/maronato/goshort/internal/server/middleware/auth"
configservice "git.maronato.dev/maronato/goshort/internal/service/config"
shortservice "git.maronato.dev/maronato/goshort/internal/service/short"
shortlogservice "git.maronato.dev/maronato/goshort/internal/service/shortlog"
tokenservice "git.maronato.dev/maronato/goshort/internal/service/token"
userservice "git.maronato.dev/maronato/goshort/internal/service/user"
bunstorage "git.maronato.dev/maronato/goshort/internal/storage/bun"
"git.maronato.dev/maronato/goshort/internal/storage/models"
randomutil "git.maronato.dev/maronato/goshort/internal/util/random"
"github.com/go-chi/chi/v5/middleware"
"github.com/stretchr/testify/assert"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
_ "modernc.org/sqlite" // Import the SQLite driver.
)
func newStorage(ctx context.Context, cfg *config.Config) *bunstorage.BunStorage {
filename := randomutil.GenerateSecureToken(16)
url := "file:memdb_" + filename + "?cache=shared&mode=memory&_foreign_keys=1"
sqldb, err := sql.Open("sqlite", url)
if err != nil {
panic(err)
}
db := bun.NewDB(sqldb, sqlitedialect.New())
str := bunstorage.NewBunStorage(cfg, db)
err = str.Start(ctx)
if err != nil {
panic(err)
}
return str
}
func setup(ctx context.Context, cfg *config.Config) (
*apiserver.APIHandler,
*shortservice.ShortService,
*userservice.UserService,
*tokenservice.TokenService, //nolint:unparam // We need to return this for testing purposes
*shortlogservice.ShortLogService, //nolint:unparam // We need to return this for testing purposes
) {
str := newStorage(ctx, cfg)
shorts := shortservice.NewShortService(str)
users := userservice.NewUserService(cfg, str)
tokens := tokenservice.NewTokenService(str)
shortLogs := shortlogservice.NewShortLogService(str)
cfgService := configservice.NewConfigService(cfg)
api := apiserver.NewAPIHandler(shorts, users, tokens, shortLogs, cfgService)
return api, shorts, users, tokens, shortLogs
}
func makeRequestServer(handler http.HandlerFunc) http.Handler {
lf := servermiddleware.NewLogFormatter(config.NewConfig())
lf.DisableStackPrinting()
rlogger := middleware.RequestLogger(lf)
// Add middlewares
handlerF := middleware.Recoverer(handler)
handlerF = rlogger(handlerF)
return handlerF
}
func makeRequestResponse(ctx context.Context, method, url string, body io.Reader) (*httptest.ResponseRecorder, *http.Request) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
panic(err)
}
rr := httptest.NewRecorder()
return rr, req
}
func withRequestUser(ctx context.Context, users *userservice.UserService) (context.Context, *models.User) {
user := &models.User{
Username: "user@example.com",
}
err := users.SetPassword(ctx, user, "mynewpassword")
if err != nil {
panic(err)
}
newUser, err := users.CreateUser(ctx, user)
if err != nil {
panic(err)
}
return authmiddleware.WithUser(ctx, newUser), newUser
}
func TestEndpointMe(t *testing.T) {
ctx := context.Background()
cfg := config.NewConfig()
api, _, users, _, _ := setup(ctx, cfg)
ctxUser, user := withRequestUser(ctx, users)
server := makeRequestServer(api.Me)
t.Run("WithUser", func(t *testing.T) {
res, req := makeRequestResponse(ctxUser, "GET", "/api/me", nil)
server.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
expectedJSON, err := json.Marshal(user)
assert.Nil(t, err)
assert.JSONEq(t, string(expectedJSON), res.Body.String())
})
t.Run("WithoutUser", func(t *testing.T) {
res, req := makeRequestResponse(ctx, "GET", "/api/me", nil)
server.ServeHTTP(res, req)
assert.Equal(t, http.StatusInternalServerError, res.Code)
})
}
func BenchmarkEndpointMe(b *testing.B) {
ctx := context.Background()
cfg := config.NewConfig()
api, _, users, _, _ := setup(ctx, cfg)
ctxUser, _ := withRequestUser(ctx, users)
server := makeRequestServer(api.Me)
for i := 0; i < b.N; i++ {
res, req := makeRequestResponse(ctxUser, "GET", "/api/me", nil)
server.ServeHTTP(res, req)
assert.Equal(b, http.StatusOK, res.Code)
}
}
func TestEndpointCreateShort(t *testing.T) {
ctx := context.Background()
cfg := config.NewConfig()
api, shorts, users, _, _ := setup(ctx, cfg)
ctxUser, _ := withRequestUser(ctx, users)
server := makeRequestServer(api.CreateShort)
tests := []struct {
name string
ctx context.Context //nolint:containedctx // We need to pass the context to test user presence
body io.Reader
responseCode int
shortName string
}{
{
name: "Valid",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{"url": "https://example.com"}`)),
responseCode: http.StatusCreated,
},
{
name: "InvalidURL",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{"url": "invalidurl"}`)),
responseCode: http.StatusBadRequest,
},
{
name: "InvalidJSON",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{"url": "https://example.com"`)),
responseCode: http.StatusBadRequest,
},
{
name: "WithoutUser",
ctx: ctx,
body: bytes.NewReader([]byte(`{"url": "https://example.com"}`)),
responseCode: http.StatusInternalServerError,
},
{
name: "WithoutURL",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{}`)),
responseCode: http.StatusBadRequest,
},
{
name: "EmptyURL",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{"url": ""}`)),
responseCode: http.StatusBadRequest,
},
{
name: "WithName",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{"url": "https://example.com", "name": "example"}`)),
responseCode: http.StatusCreated,
shortName: "example",
},
{
name: "WithInvalidName",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{"url": "https://example.com", "name": "invalid name"}`)),
responseCode: http.StatusBadRequest,
},
{
name: "WithExistingName",
ctx: ctxUser,
body: bytes.NewReader([]byte(`{"url": "https://example.com", "name": "example"}`)),
responseCode: http.StatusBadRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(tt.ctx, config.RequestTimeout)
defer cancel()
res, req := makeRequestResponse(ctx, "POST", "/api/shorts", tt.body)
server.ServeHTTP(res, req)
assert.Equal(t, tt.responseCode, res.Code)
if res.Code == http.StatusCreated {
var short models.Short
err := json.Unmarshal(res.Body.Bytes(), &short)
assert.Nil(t, err)
_, err = shorts.FindShortByID(ctx, short.ID)
assert.Nil(t, err)
assert.Equal(t, "https://example.com", short.URL)
if tt.shortName != "" {
assert.Equal(t, tt.shortName, short.Name)
}
}
})
}
}
func BenchmarkEndpointCreateShort(b *testing.B) {
ctx := context.Background()
cfg := config.NewConfig()
api, _, users, _, _ := setup(ctx, cfg)
ctxUser, _ := withRequestUser(ctx, users)
server := makeRequestServer(api.CreateShort)
body := []byte(`{"url": "https://example.com"}`)
for i := 0; i < b.N; i++ {
res, req := makeRequestResponse(ctxUser, "POST", "/api/shorts", bytes.NewReader(body))
server.ServeHTTP(res, req)
assert.Equal(b, http.StatusCreated, res.Code)
}
}