@@ -10,6 +10,7 @@ import (
10
10
11
11
"github.com/stretchr/testify/assert"
12
12
"github.com/stretchr/testify/require"
13
+ "gorm.io/gorm"
13
14
"goyave.dev/goyave/v5"
14
15
"goyave.dev/goyave/v5/slog"
15
16
"goyave.dev/goyave/v5/util/testutil"
@@ -69,7 +70,7 @@ func TestJWTController(t *testing.T) {
69
70
assert .NotEmpty (t , respBody ["token" ])
70
71
})
71
72
72
- t .Run ("Login_invalid " , func (t * testing.T ) {
73
+ t .Run ("Login_invalid_password " , func (t * testing.T ) {
73
74
server , user := prepareAuthenticatorTest (t )
74
75
server .Config ().Set ("auth.jwt.secret" , "secret" )
75
76
@@ -95,6 +96,32 @@ func TestJWTController(t *testing.T) {
95
96
assert .Equal (t , map [string ]string {"error" : server .Lang .GetDefault ().Get ("auth.invalid-credentials" )}, respBody )
96
97
})
97
98
99
+ t .Run ("Login_invalid_username" , func (t * testing.T ) {
100
+ server , user := prepareAuthenticatorTest (t )
101
+ server .Config ().Set ("auth.jwt.secret" , "secret" )
102
+
103
+ mockUserService := & MockUserService [TestUser ]{err : fmt .Errorf ("test errors: %w" , gorm .ErrRecordNotFound )}
104
+ controller := NewJWTController (mockUserService , "Password" )
105
+ server .RegisterRoutes (func (_ * goyave.Server , router * goyave.Router ) {
106
+ router .Controller (controller )
107
+ })
108
+
109
+ data := map [string ]any {
110
+ "username" : "wrong username" ,
111
+ "password" : user .Password ,
112
+ }
113
+ body , err := json .Marshal (data )
114
+ require .NoError (t , err )
115
+ request := httptest .NewRequest (http .MethodPost , "/login" , bytes .NewReader (body ))
116
+ request .Header .Set ("Content-Type" , "application/json" )
117
+ resp := server .TestRequest (request )
118
+ assert .Equal (t , http .StatusUnauthorized , resp .StatusCode )
119
+ respBody , err := testutil.ReadJSONBody [map [string ]string ](resp .Body )
120
+ assert .NoError (t , resp .Body .Close ())
121
+ require .NoError (t , err )
122
+ assert .Equal (t , map [string ]string {"error" : server .Lang .GetDefault ().Get ("auth.invalid-credentials" )}, respBody )
123
+ })
124
+
98
125
t .Run ("Login_token_func_error" , func (t * testing.T ) {
99
126
server , user := prepareAuthenticatorTest (t )
100
127
buf := & bytes.Buffer {}
0 commit comments