From 2bdcb56e82846d7d41b18308221cf0467a05dbd4 Mon Sep 17 00:00:00 2001 From: Yader Velasquez Date: Wed, 14 Feb 2024 16:17:26 -0600 Subject: [PATCH] Fix core tests --- cmd/global.go | 4 +++ core/routes_test.go | 64 +++++++++++++---------------------- core/samples/create_link.json | 5 ++- core/samples/detail_link.json | 6 +++- core/samples/update_link.json | 5 ++- 5 files changed, 40 insertions(+), 44 deletions(-) diff --git a/cmd/global.go b/cmd/global.go index 8e105bc..5e3c2c8 100644 --- a/cmd/global.go +++ b/cmd/global.go @@ -159,6 +159,10 @@ func NewWebTestServer(t *testing.T) (*server.Server, *echo.Echo) { return s }, "getUserByID": core.TmplGetUserByID, + "isTagUsedInFilter": func(tag string, activeTags string) bool { + return strings.Contains(activeTags, tag) + }, + "addQueryElement": links.AddQueryElement, }) err = srv.LoadTemplatesFS(links.TemplateFS, "templates/*.html", "templates/*.txt") if err != nil { diff --git a/core/routes_test.go b/core/routes_test.go index 7768b3e..bab6c5f 100644 --- a/core/routes_test.go +++ b/core/routes_test.go @@ -1,6 +1,7 @@ package core_test import ( + "database/sql" "links/cmd" "links/core" "links/models" @@ -28,6 +29,11 @@ func TestHandlers(t *testing.T) { defer srv.Shutdown() go srv.Run() dbCtx := cmd.NewDBContext(srv.DB, "America/Managua") + domains, err := models.GetDomains(dbCtx, &database.FilterOptions{ + Filter: sq.Expr("d.service = ?", models.DomainServiceList), + Limit: 1, + }) + c.NoError(err) t.Run("home link list", func(t *testing.T) { httpmock.Activate() @@ -44,15 +50,7 @@ func TestHandlers(t *testing.T) { User: loggedInUser, } ctx.SetPath("/home") - err = cmd.MakeRequestWithMW(srv, coreService.OrgLinksList, ctx, func() echo.MiddlewareFunc { - return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - gctx := c.(*server.Context) - gctx.Server.Session.Put(c.Request().Context(), "organization.Slug", "personal-org") - return next(gctx) - } - } - }) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinksList, ctx, domains[0]) c.NoError(err) htmlBody := recorder.Body.String() c.True(strings.Contains(htmlBody, "Link one")) @@ -74,7 +72,7 @@ func TestHandlers(t *testing.T) { User: cmd.NewTestUser(1, false, false, false, false), // No aunthenticated } ctx.SetPath("/recent") - err = cmd.MakeRequest(srv, coreService.OrgLinksList, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinksList, ctx, domains[0]) c.NoError(err) htmlBody := recorder.Body.String() c.True(strings.Contains(htmlBody, "Link one")) @@ -104,7 +102,7 @@ func TestHandlers(t *testing.T) { } ctx.SetPath("/add") // We get the error from the API call - err = cmd.MakeRequest(srv, coreService.OrgLinksCreate, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinksCreate, ctx, domains[0]) c.NoError(err) htmlBody := recorder.Body.String() c.True(strings.Contains(htmlBody, "Invalid URL")) @@ -132,7 +130,7 @@ func TestHandlers(t *testing.T) { } ctx.SetPath("/add") // We get the error from the API call - err = cmd.MakeRequest(srv, coreService.OrgLinksCreate, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinksCreate, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) @@ -144,7 +142,7 @@ func TestHandlers(t *testing.T) { orgLink := &models.OrgLink{ Title: "link", URL: "http://foo.com", - BaseURLID: baseURL.ID, + BaseURLID: sql.NullInt64{Int64: int64(baseURL.ID), Valid: true}, OrgID: 1, UserID: 1, Visibility: models.OrgLinkVisibilityPublic, @@ -176,7 +174,7 @@ func TestHandlers(t *testing.T) { ctx.SetParamNames("id") ctx.SetParamValues(strconv.Itoa(orgLink.ID)) // We get the error from the API call - err = cmd.MakeRequest(srv, coreService.OrgLinkUpdate, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinkUpdate, ctx, domains[0]) c.NoError(err) htmlBody := recorder.Body.String() c.True(strings.Contains(htmlBody, "Invalid URL")) @@ -211,7 +209,7 @@ func TestHandlers(t *testing.T) { ctx.SetParamNames("id") ctx.SetParamValues(strconv.Itoa(orgLink.ID)) // We get the error from the API call - err = cmd.MakeRequest(srv, coreService.OrgLinkUpdate, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinkUpdate, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) @@ -235,7 +233,7 @@ func TestHandlers(t *testing.T) { ctx.SetParamNames("id") ctx.SetParamValues("1") // We get the error from the API call - err = cmd.MakeRequest(srv, coreService.OrgLinkDetail, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinkDetail, ctx, domains[0]) c.NoError(err) htmlBody := recorder.Body.String() c.True(strings.Contains(htmlBody, "https://www.detail.org")) @@ -259,7 +257,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/link/:id/delete") ctx.SetParamNames("id") ctx.SetParamValues("1") - err = cmd.MakeRequest(srv, coreService.OrgLinkDelete, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.OrgLinkDelete, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) @@ -286,7 +284,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/:slug/domains/add") ctx.SetParamNames("slug") ctx.SetParamValues("personal-org") - err = cmd.MakeRequest(srv, coreService.DomainCreate, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.DomainCreate, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) @@ -313,7 +311,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/:slug/domains/add") ctx.SetParamNames("slug") ctx.SetParamValues("personal-org") - err = cmd.MakeRequest(srv, coreService.DomainCreate, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.DomainCreate, ctx, domains[0]) c.NoError(err) htmlBody := recorder.Body.String() c.True(strings.Contains(htmlBody, "error")) @@ -336,7 +334,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/:slug/domains") ctx.SetParamNames("slug") ctx.SetParamValues("personal-org") - err = cmd.MakeRequest(srv, coreService.DomainList, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.DomainList, ctx, domains[0]) c.NoError(err) htmlBody := recorder.Body.String() c.True(strings.Contains(htmlBody, "Domain one")) @@ -361,7 +359,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/:slug/domains/:id/delete") ctx.SetParamNames("slug", "id") ctx.SetParamValues("personal-org", "1") - err = cmd.MakeRequest(srv, coreService.DomainDelete, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.DomainDelete, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) @@ -384,7 +382,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/:slug/domains/:id/delete") ctx.SetParamNames("slug", "id") ctx.SetParamValues("personal-org", "1") - err = cmd.MakeRequest(srv, coreService.DomainDelete, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.DomainDelete, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) @@ -406,7 +404,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/q/:hash") ctx.SetParamNames("hash") ctx.SetParamValues("ab10") - err = cmd.MakeRequest(srv, coreService.QRRedirect, ctx) + err = cmd.MakeRequestWithDomain(srv, coreService.QRRedirect, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) @@ -428,15 +426,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/qr/:hash/detail") ctx.SetParamNames("hash") ctx.SetParamValues("ab10") - err = cmd.MakeRequestWithMW(srv, coreService.QRManageDetail, ctx, func() echo.MiddlewareFunc { - return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - gctx := c.(*server.Context) - gctx.Server.Session.Put(c.Request().Context(), "organization.Slug", "personal-org") - return next(gctx) - } - } - }) + err = cmd.MakeRequestWithDomain(srv, coreService.QRManageDetail, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusOK, recorder.Code) htmlBody := recorder.Body.String() @@ -462,15 +452,7 @@ func TestHandlers(t *testing.T) { ctx.SetPath("/qr/:id/delete") ctx.SetParamNames("id") ctx.SetParamValues("100") - err = cmd.MakeRequestWithMW(srv, coreService.QRManageDelete, ctx, func() echo.MiddlewareFunc { - return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - gctx := c.(*server.Context) - gctx.Server.Session.Put(c.Request().Context(), "organization.Slug", "personal-org") - return next(gctx) - } - } - }) + err = cmd.MakeRequestWithDomain(srv, coreService.QRManageDelete, ctx, domains[0]) c.NoError(err) c.Equal(http.StatusMovedPermanently, recorder.Code) }) diff --git a/core/samples/create_link.json b/core/samples/create_link.json index 48860b0..8456355 100644 --- a/core/samples/create_link.json +++ b/core/samples/create_link.json @@ -3,7 +3,10 @@ "addLink": { "id": 81, "url": "http://foo.com", - "baseUrlId": 26, + "baseUrlId": { + "valid": true, + "int64": 26 + }, "orgId": 1, "userId": 1, "visibility": 0, diff --git a/core/samples/detail_link.json b/core/samples/detail_link.json index 639a6a6..10d5cd9 100644 --- a/core/samples/detail_link.json +++ b/core/samples/detail_link.json @@ -4,7 +4,11 @@ "id": 30, "title": "Detail org", "url": "https://www.detail.org", - "baseUrlId": 32, + "baseUrlId": { + "valid": true, + "int64": 32 + + }, "orgId": 1, "userId": 1, "visibility": 0, diff --git a/core/samples/update_link.json b/core/samples/update_link.json index 6eff153..6562369 100644 --- a/core/samples/update_link.json +++ b/core/samples/update_link.json @@ -3,7 +3,10 @@ "updateLink": { "id": 3, "url": "https://edited.io/", - "baseUrlId": 28, + "baseUrlId": { + "valid": true, + "int64": 28 + }, "orgId": 1, "userId": 1, "visibility": 0, -- 2.45.2