Skip to content

Commit 01d7af7

Browse files
committed
improved tests coverage
1 parent fdf6025 commit 01d7af7

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

deserialization_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package fuego
22

33
import (
44
"context"
5+
"database/sql"
56
"errors"
67
"net/http/httptest"
8+
"reflect"
79
"strings"
810
"testing"
911

@@ -173,3 +175,23 @@ func TestReadURLEncoded(t *testing.T) {
173175
require.Equal(t, BodyTestWithInTransformerError{"a", 9}, res)
174176
})
175177
}
178+
179+
func TestConvertSQLNullString(t *testing.T) {
180+
t.Run("can convert sql.NullString", func(t *testing.T) {
181+
v := convertSQLNullString("test")
182+
require.Equal(t, "test", v.Interface().(sql.NullString).String)
183+
})
184+
185+
}
186+
187+
func TestConvertSQLNullBool(t *testing.T) {
188+
t.Run("convert sql.NullBool", func(t *testing.T) {
189+
v := convertSQLNullBool("false")
190+
require.Equal(t, false, v.Interface().(sql.NullBool).Bool)
191+
})
192+
193+
t.Run("cannot convert sql.NullBool", func(t *testing.T) {
194+
v := convertSQLNullBool("hello")
195+
require.Equal(t, v, reflect.Value{})
196+
})
197+
}

security_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"regexp"
8+
"strings"
89
"testing"
910
"time"
1011

@@ -367,3 +368,120 @@ func TestSecurity_RefreshHandler(t *testing.T) {
367368
require.Equal(t, JWTCookieName, authCookie.Name)
368369
})
369370
}
371+
372+
func TestSecurity_StdLoginHandler(t *testing.T) {
373+
security := NewSecurity()
374+
v := func(r *http.Request) (jwt.Claims, error) {
375+
if r.FormValue("user") != "test" || r.FormValue("password") != "test" {
376+
return nil, ErrUnauthorized
377+
}
378+
return jwt.MapClaims{"sub": "123"}, nil
379+
}
380+
loginHandler := security.StdLoginHandler(v)
381+
382+
t.Run("with incorrect ids", func(t *testing.T) {
383+
r := httptest.NewRequest("GET", "/", nil)
384+
w := httptest.NewRecorder()
385+
loginHandler(w, r)
386+
387+
cookies := w.Result().Cookies()
388+
require.Len(t, cookies, 0)
389+
})
390+
391+
t.Run("with correct ids", func(t *testing.T) {
392+
r := httptest.NewRequest("GET", "/?user=test&password=test", nil)
393+
w := httptest.NewRecorder()
394+
loginHandler(w, r)
395+
396+
cookies := w.Result().Cookies()
397+
require.Len(t, cookies, 1)
398+
authCookie := cookies[0]
399+
require.NotEmpty(t, authCookie)
400+
require.Equal(t, JWTCookieName, authCookie.Name)
401+
})
402+
}
403+
404+
func TestSecurity_LoginHandler(t *testing.T) {
405+
security := NewSecurity()
406+
v := func(user string, password string) (jwt.Claims, error) {
407+
if user != "test" || password != "test" {
408+
return nil, ErrUnauthorized
409+
}
410+
return jwt.MapClaims{"sub": "123"}, nil
411+
}
412+
loginHandler := security.LoginHandler(v)
413+
414+
t.Run("without ids", func(t *testing.T) {
415+
r := httptest.NewRequest("GET", "/", nil)
416+
w := httptest.NewRecorder()
417+
418+
s := NewServer()
419+
truc := httpHandler(s, loginHandler)
420+
truc.ServeHTTP(w, r)
421+
422+
cookies := w.Result().Cookies()
423+
require.Len(t, cookies, 0)
424+
})
425+
426+
t.Run("with incorrect ids", func(t *testing.T) {
427+
loginBody := `{"user": "hacker", "password": "hacker"}`
428+
r := httptest.NewRequest("GET", "/", strings.NewReader(loginBody))
429+
w := httptest.NewRecorder()
430+
431+
s := NewServer()
432+
truc := httpHandler(s, loginHandler)
433+
truc.ServeHTTP(w, r)
434+
435+
cookies := w.Result().Cookies()
436+
require.Len(t, cookies, 0)
437+
})
438+
439+
t.Run("with correct ids", func(t *testing.T) {
440+
loginBody := `{"user": "test", "password": "test"}`
441+
r := httptest.NewRequest("GET", "/", strings.NewReader(loginBody))
442+
w := httptest.NewRecorder()
443+
444+
s := NewServer()
445+
truc := httpHandler(s, loginHandler)
446+
truc.ServeHTTP(w, r)
447+
448+
cookies := w.Result().Cookies()
449+
require.Len(t, cookies, 1)
450+
authCookie := cookies[0]
451+
require.NotEmpty(t, authCookie)
452+
require.Equal(t, JWTCookieName, authCookie.Name)
453+
})
454+
}
455+
456+
func TestGetToken(t *testing.T) {
457+
t.Run("no token", func(t *testing.T) {
458+
ctx := context.Background()
459+
token, err := GetToken[any](ctx)
460+
require.Error(t, err)
461+
require.Empty(t, token)
462+
})
463+
464+
t.Run("with valid token", func(t *testing.T) {
465+
r := httptest.NewRequest("GET", "/", nil)
466+
ctx := context.WithValue(r.Context(), contextKeyJWT, jwt.MapClaims{"sub": "123"})
467+
468+
token, err := GetToken[jwt.MapClaims](ctx)
469+
require.NoError(t, err)
470+
sub, err := token.GetSubject()
471+
require.NoError(t, err)
472+
require.Equal(t, "123", sub)
473+
})
474+
475+
t.Run("with token of custom type", func(t *testing.T) {
476+
type MyToken struct {
477+
jwt.MapClaims
478+
Username string
479+
UserID string
480+
}
481+
r := httptest.NewRequest("GET", "/", nil)
482+
ctx := context.WithValue(r.Context(), contextKeyJWT, MyToken{MapClaims: jwt.MapClaims{"sub": "123"}})
483+
484+
_, err := GetToken[MyToken](ctx)
485+
require.Error(t, err)
486+
})
487+
}

0 commit comments

Comments
 (0)