Skip to content

Commit

Permalink
tdd: remove t param on all t.Run()
Browse files Browse the repository at this point in the history
This is basically a lint fix
  • Loading branch information
Jaeiya committed Apr 24, 2024
1 parent ef22d9d commit d2f4e1a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
14 changes: 7 additions & 7 deletions internal/routes/data_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestDataRoute(t *testing.T) {
userID, err := u.GetRandomUserId()
rq.NoError(err, "should get random user id")

t.Run("panic on missing auth middleware", func(t *testing.T) {
t.Run("panic on missing auth middleware", func(*testing.T) {
r := router.NewRouter()
HandleData(r, tmpDir)
assert.PanicsWithValue(
Expand All @@ -46,7 +46,7 @@ func TestDataRoute(t *testing.T) {
)
})

t.Run("passes summary uri spec", func(t *testing.T) {
t.Run("passes summary uri spec", func(*testing.T) {
r := router.NewRouter()
_ = os.WriteFile(tmpDir+"/blog/public/public.json", []byte("test text"), 0o600)
HandleData(r, tmpDir, middleware.AuthGuard(u))
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestDataRoute(t *testing.T) {
assert.NotEmpty(rec.Header().Get("Last-Modified"), "should have Last-Modified header")
})

t.Run("summary uri returns 404 when files requested", func(t *testing.T) {
t.Run("summary uri returns 404 when files requested", func(*testing.T) {
r := router.NewRouter()
HandleData(r, tmpDir, middleware.AuthGuard(u))

Expand All @@ -95,7 +95,7 @@ func TestDataRoute(t *testing.T) {
assert.Equal(rec.Code, http.StatusNotFound)
})

t.Run("passes mdhtml uri spec", func(t *testing.T) {
t.Run("passes mdhtml uri spec", func(*testing.T) {
r := router.NewRouter()
HandleData(r, tmpDir, middleware.AuthGuard(u))
_ = os.WriteFile(tmpDir+"/blog/public/1234567890.mdhtml", []byte("i am mdhtml"), 0o600)
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestDataRoute(t *testing.T) {
assert.NotEmpty(rec.Header().Get("Last-Modified"), "should have Last-Modified header")
})

t.Run("mdhtml uri returns 404 when non-files requested", func(t *testing.T) {
t.Run("mdhtml uri returns 404 when non-files requested", func(*testing.T) {
r := router.NewRouter()
HandleData(r, tmpDir, middleware.AuthGuard(u))
_ = os.WriteFile(tmpDir+"/blog/public/0987654321.mdhtml", []byte("i am mdhtml"), 0o600)
Expand All @@ -146,7 +146,7 @@ func TestDataRoute(t *testing.T) {
assert.Equal(rec.Code, http.StatusNotFound, "expect not found")
})

t.Run("summary uri protects redeem routes", func(t *testing.T) {
t.Run("summary uri protects redeem routes", func(*testing.T) {
r := router.NewRouter()
HandleData(r, tmpDir, middleware.AuthGuard(u))

Expand Down Expand Up @@ -177,7 +177,7 @@ func TestDataRoute(t *testing.T) {
)
})

t.Run("mdhtml uri protects redeem routes", func(t *testing.T) {
t.Run("mdhtml uri protects redeem routes", func(*testing.T) {
r := router.NewRouter()
_ = os.WriteFile(tmpDir+"/blog/red33m/12345.mdhtml", []byte("test text"), 0o600)
HandleData(r, tmpDir, middleware.AuthGuard(u))
Expand Down
27 changes: 13 additions & 14 deletions internal/routes/index_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,48 @@ import (

func TestIndexRoute(t *testing.T) {
t.Parallel()
assert := assert.New(t)
dir := t.TempDir()
r := router.NewRouter()
err := os.WriteFile(dir+"/mock.html", []byte("index text"), 0o600)
require.NoError(t, err, "write mock file")

HandleIndex(r, dir+"/mock.html")

t.Run("panic if handling index without file", func(t *testing.T) {
t.Run("panic if handling index without file", func(*testing.T) {
r := router.NewRouter()
assert.PanicsWithValue(
t,
"index route needs a file path, not folder path",
func() { HandleIndex(r, dir+"/mock") },
"panic when passing an invalid index file",
)
})

t.Run("returns index on root url request", func(t *testing.T) {
t.Run("returns index on root url request", func(*testing.T) {
resp := testutils.MockRequest(r.Handler, "GET", "/", nil)
assert.Equal(t, http.StatusOK, resp.Code, "returns status ok")
assert.Equal(t, "index text", resp.Body.String(), "return index file")
assert.Equal(http.StatusOK, resp.Code, "returns status ok")
assert.Equal("index text", resp.Body.String(), "return index file")
assert.Equal(
t,
"text/html; charset=utf-8",
resp.Result().Header.Get("Content-Type"),
"should have html content type",
)
})

t.Run("returns index on index.html request", func(t *testing.T) {
t.Run("returns index on index.html request", func(*testing.T) {
resp := testutils.MockRequest(r.Handler, "GET", "/index.html", nil)
assert.Equal(t, http.StatusOK, resp.Code, "return status ok")
assert.Equal(t, "index text", resp.Body.String(), "return index contents")
assert.Equal(http.StatusOK, resp.Code, "return status ok")
assert.Equal("index text", resp.Body.String(), "return index contents")
})

t.Run("returns index on unknown uri request", func(t *testing.T) {
t.Run("returns index on unknown uri request", func(*testing.T) {
resp := testutils.MockRequest(r.Handler, "GET", "/unknown/url", nil)
assert.Equal(t, http.StatusOK, resp.Code, "return status ok")
assert.Equal(t, "index text", resp.Body.String(), "return index contents")
assert.Equal(http.StatusOK, resp.Code, "return status ok")
assert.Equal("index text", resp.Body.String(), "return index contents")
})

t.Run("return 404 on file request", func(t *testing.T) {
t.Run("return 404 on file request", func(*testing.T) {
resp := testutils.MockRequest(r.Handler, "GET", "/unknown/file.ext", nil)
assert.Equal(t, http.StatusNotFound, resp.Code, "return status 'not found'")
assert.Equal(http.StatusNotFound, resp.Code, "return status 'not found'")
})
}
10 changes: 5 additions & 5 deletions internal/routes/setup_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSetupRoute(t *testing.T) {
HandleSetup(r, dir+"/mock.txt", u)
expBody := "Malformed Authorization\n"

t.Run("detects bad authorization header", func(t *testing.T) {
t.Run("detects bad authorization header", func(*testing.T) {
authTable := []string{
"",
" ",
Expand All @@ -57,7 +57,7 @@ func TestSetupRoute(t *testing.T) {
}
})

t.Run("detects unauthorized request", func(t *testing.T) {
t.Run("detects unauthorized request", func(*testing.T) {
resp := testutils.MockRequest(r.Handler, "GET", "/setup", &map[string][]string{
"Authorization": {"Bearer gibberish"},
})
Expand All @@ -70,7 +70,7 @@ func TestSetupRoute(t *testing.T) {
)
})

t.Run("adds users", func(t *testing.T) {
t.Run("adds users", func(*testing.T) {
resp := testutils.MockRequest(r.Handler, "GET", "/setup", &map[string][]string{
"Authorization": {"Bearer setup"},
})
Expand All @@ -86,7 +86,7 @@ func TestSetupRoute(t *testing.T) {
assert.Equal("test text", resp.Body.String(), "returns expected body")
})

t.Run("detects authenticated user", func(t *testing.T) {
t.Run("detects authenticated user", func(*testing.T) {
id, _ := u.GetRandomUserId()
resp := testutils.MockRequest(r.Handler, "GET", "/setup", &map[string][]string{
"Authorization": {"Bearer " + id},
Expand All @@ -103,7 +103,7 @@ func TestSetupRoute(t *testing.T) {
assert.Equal("no", red33mVal, "should have red33m")
})

t.Run("detects redeem user", func(t *testing.T) {
t.Run("detects redeem user", func(*testing.T) {
id, _ := u.GetRandomUserId()
u.Update(id, true)
resp := testutils.MockRequest(r.Handler, "GET", "/setup", &map[string][]string{
Expand Down
12 changes: 5 additions & 7 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestWorkingDir(t *testing.T) {
t.Parallel()
t.Run("gets working directory", func(t *testing.T) {
t.Run("gets working directory", func(*testing.T) {
want, err := os.Getwd()
assert.NoError(t, err, "get working directory")
assert.Equal(t, want, Getwd())
Expand All @@ -18,28 +18,26 @@ func TestWorkingDir(t *testing.T) {

func TestID(t *testing.T) {
t.Parallel()
assert := assert.New(t)

Check failure on line 21 in internal/utils_test.go

View workflow job for this annotation

GitHub Actions / lint

import-shadowing: The name 'assert' shadows an import name (revive)

t.Run("long id", func(t *testing.T) {
t.Run("long id", func(*testing.T) {
assert.Greater(
t,
len(GetLongID()),
20,
"long id should be at least canonical length (21)",
)
})

t.Run("short id", func(t *testing.T) {
t.Run("short id", func(*testing.T) {
assert.Less(
t,
len(GetShortID()),
21,
"short id should be less than canonical length (21)",
)
})

t.Run("LengthDiff", func(t *testing.T) {
t.Run("LengthDiff", func(*testing.T) {
assert.GreaterOrEqual(
t,
len(GetLongID())-len(GetShortID()),
5,
"min distance between short & long id",
Expand Down

0 comments on commit d2f4e1a

Please sign in to comment.