Skip to content

Commit

Permalink
Add testifylint
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Feb 19, 2024
1 parent 3319008 commit 3825239
Show file tree
Hide file tree
Showing 33 changed files with 430 additions and 699 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ linters:
- gosimple
- ineffassign
- staticcheck
- testifylint
disable-all: false
fast: false

Expand All @@ -39,6 +40,7 @@ issues:
max-same-issues: 0
exclude:
- should have a package comment
- for error assertions use require
exclude-rules:
- path: _test\.go
linters:
Expand Down
19 changes: 8 additions & 11 deletions auth/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"gorm.io/gorm/utils/tests"
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestAuthenticator(t *testing.T) {
response.Status(http.StatusOK)
})
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())

request = server.NewTestRequest(http.MethodGet, "/protected", nil)
request.Request().SetBasicAuth(user.Email, "incorrect password")
Expand All @@ -106,10 +107,8 @@ func TestAuthenticator(t *testing.T) {
})
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
body, err := testutil.ReadJSONBody[map[string]string](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, body)
})

Expand All @@ -127,15 +126,15 @@ func TestAuthenticator(t *testing.T) {
response.Status(http.StatusOK)
})
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())

request.Route = &goyave.Route{Meta: map[string]any{}}
resp = server.TestMiddleware(authenticator, request, func(response *goyave.Response, request *goyave.Request) {
assert.Nil(t, request.User)
response.Status(http.StatusOK)
})
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())
})

t.Run("MiddlewareUnauthorizer", func(t *testing.T) {
Expand All @@ -152,10 +151,8 @@ func TestAuthenticator(t *testing.T) {
})
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
body, err := testutil.ReadJSONBody[map[string]string](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"custom error key": server.Lang.GetDefault().Get("auth.invalid-credentials")}, body)
})
}
Expand Down
35 changes: 14 additions & 21 deletions auth/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"goyave.dev/goyave/v5"
"goyave.dev/goyave/v5/config"
"goyave.dev/goyave/v5/util/testutil"
Expand All @@ -26,7 +27,7 @@ func TestBasicAuthenticator(t *testing.T) {
response.Status(http.StatusOK)
})
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())
})

t.Run("wrong_password", func(t *testing.T) {
Expand All @@ -42,10 +43,8 @@ func TestBasicAuthenticator(t *testing.T) {
})
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
body, err := testutil.ReadJSONBody[map[string]string](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, body)
})

Expand All @@ -63,7 +62,7 @@ func TestBasicAuthenticator(t *testing.T) {
response.Status(http.StatusOK)
})
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())
})

t.Run("optional_wrong_password", func(t *testing.T) {
Expand All @@ -79,10 +78,8 @@ func TestBasicAuthenticator(t *testing.T) {
})
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
body, err := testutil.ReadJSONBody[map[string]string](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, body)
})

Expand All @@ -97,7 +94,7 @@ func TestBasicAuthenticator(t *testing.T) {
response.Status(http.StatusOK)
})
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())
})

t.Run("error_no_table", func(t *testing.T) {
Expand Down Expand Up @@ -133,7 +130,7 @@ func TestBasicAuthenticator(t *testing.T) {
request.Route = &goyave.Route{Meta: map[string]any{MetaAuth: true}}

err := authenticator.Authenticate(request, &TestUserPromoted{})
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, server.Lang.GetDefault().Get("auth.no-credentials-provided"), err.Error())
})
}
Expand All @@ -153,7 +150,7 @@ func TestConfigBasicAuthenticator(t *testing.T) {
response.Status(http.StatusOK)
})
assert.Equal(t, http.StatusOK, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())
})

t.Run("wrong_password", func(t *testing.T) {
Expand All @@ -170,10 +167,8 @@ func TestConfigBasicAuthenticator(t *testing.T) {
})
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
body, err := testutil.ReadJSONBody[map[string]string](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, body)
})

Expand All @@ -190,10 +185,8 @@ func TestConfigBasicAuthenticator(t *testing.T) {
})
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
body, err := testutil.ReadJSONBody[map[string]string](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.no-credentials-provided")}, body)
})
}
53 changes: 17 additions & 36 deletions auth/jwt_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"goyave.dev/goyave/v5"
"goyave.dev/goyave/v5/config"
"goyave.dev/goyave/v5/slog"
Expand All @@ -32,18 +33,14 @@ func TestJWTController(t *testing.T) {
"password": "secret",
}
body, err := json.Marshal(data)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
resp := server.TestRequest(request)
assert.Equal(t, http.StatusOK, resp.StatusCode)
respBody, err := testutil.ReadJSONBody[map[string]any](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.NotEmpty(t, respBody["token"])
})

Expand All @@ -61,18 +58,14 @@ func TestJWTController(t *testing.T) {
"password": "wrong password",
}
body, err := json.Marshal(data)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
resp := server.TestRequest(request)
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
respBody, err := testutil.ReadJSONBody[map[string]string](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.Equal(t, map[string]string{"error": server.Lang.GetDefault().Get("auth.invalid-credentials")}, respBody)
})

Expand All @@ -96,14 +89,12 @@ func TestJWTController(t *testing.T) {
"password": "secret",
}
body, err := json.Marshal(data)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
resp := server.TestRequest(request)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())
assert.NotEmpty(t, buf.String())
})

Expand All @@ -127,14 +118,12 @@ func TestJWTController(t *testing.T) {
"password": "secret",
}
body, err := json.Marshal(data)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
resp := server.TestRequest(request)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
_ = resp.Body.Close()
assert.NoError(t, resp.Body.Close())
assert.NotEmpty(t, buf.String())
})

Expand All @@ -155,18 +144,14 @@ func TestJWTController(t *testing.T) {
"pass": "secret",
}
body, err := json.Marshal(data)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
resp := server.TestRequest(request)
assert.Equal(t, http.StatusOK, resp.StatusCode)
respBody, err := testutil.ReadJSONBody[map[string]any](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
assert.NotEmpty(t, respBody["token"])
})

Expand All @@ -181,18 +166,14 @@ func TestJWTController(t *testing.T) {

data := map[string]any{}
body, err := json.Marshal(data)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/login", bytes.NewReader(body))
request.Header.Set("Content-Type", "application/json")
resp := server.TestRequest(request)
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
respBody, err := testutil.ReadJSONBody[map[string]*validation.ErrorResponse](resp.Body)
_ = resp.Body.Close()
if !assert.NoError(t, err) {
return
}
assert.NoError(t, resp.Body.Close())
require.NoError(t, err)
if assert.Contains(t, respBody, "error") && assert.NotNil(t, respBody["error"]) {
assert.Contains(t, respBody["error"].Body.Fields, "username")
assert.Contains(t, respBody["error"].Body.Fields, "password")
Expand Down
Loading

0 comments on commit 3825239

Please sign in to comment.