~netlandish/links

2bdcb56e82846d7d41b18308221cf0467a05dbd4 — Yader Velasquez 9 months ago 2e16147
Fix core tests
M cmd/global.go => cmd/global.go +4 -0
@@ 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 {

M core/routes_test.go => core/routes_test.go +23 -41
@@ 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)
	})

M core/samples/create_link.json => core/samples/create_link.json +4 -1
@@ 3,7 3,10 @@
        "addLink": {
            "id": 81,
            "url": "http://foo.com",
            "baseUrlId": 26,
            "baseUrlId": {
                "valid": true,
                "int64": 26
            },
            "orgId": 1,
            "userId": 1,
            "visibility": 0,

M core/samples/detail_link.json => core/samples/detail_link.json +5 -1
@@ 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,

M core/samples/update_link.json => core/samples/update_link.json +4 -1
@@ 3,7 3,10 @@
        "updateLink": {
            "id": 3,
            "url": "https://edited.io/",
            "baseUrlId": 28,
            "baseUrlId": {
                "valid": true,
                "int64": 28
            },
            "orgId": 1,
            "userId": 1,
            "visibility": 0,