Added test suites for all current models

This commit is contained in:
🐙PiperYxzzy
2022-05-01 20:49:03 +02:00
parent 5f85a5800e
commit 6e7b30be0a
12 changed files with 400 additions and 27 deletions

95
models/user_test.go Normal file
View File

@@ -0,0 +1,95 @@
package models
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/yxzzy-wtf/gin-gonic-prepack/util"
)
func TestUserGetJwt(t *testing.T) {
u := User{}
u.Uid = uuid.New()
jwtToken, maxAge := u.GetJwt()
if maxAge != int(time.Hour.Seconds()*24) {
t.Errorf("issued token with incorrect max age, expected %vs but was %vs", time.Hour.Seconds()*24, maxAge)
}
testClaims, err := util.ParseJwt(jwtToken, UserHmac)
if err != nil {
t.Errorf("tried to parse valid token but got error %v", err)
}
if testClaims["sub"] != u.Uid.String() {
t.Errorf("`sub` value of %v does not match expected of %v", testClaims["sub"], u.Uid)
}
if testClaims["role"] != "user" {
t.Errorf("`role` value of %v does not match expected of `user`", testClaims["role"])
}
if _, exists := testClaims["iat"]; !exists {
t.Errorf("`iat` does not exist in jwt")
}
if _, exists := testClaims["exp"]; !exists {
t.Errorf("`exp` does not exist in jwt")
}
}
func TestUserGetVerifyJwt(t *testing.T) {
u := User{}
u.Uid = uuid.New()
jwtToken := u.GetVerificationJwt()
testClaims, err := util.ParseJwt(jwtToken, UserHmac)
if err != nil {
t.Errorf("tried to parse valid token but got error %v", err)
}
if testClaims["sub"] != u.Uid.String() {
t.Errorf("`sub` value of %v does not match expected of %v", testClaims["sub"], u.Uid)
}
if testClaims["role"] != "verify" {
t.Errorf("`role` value of %v does not match expected of `verify`", testClaims["role"])
}
if _, exists := testClaims["iat"]; !exists {
t.Errorf("`iat` does not exist in jwt")
}
if _, exists := testClaims["exp"]; !exists {
t.Errorf("`exp` does not exist in jwt")
}
}
func TestUserGetResetJwt(t *testing.T) {
u := User{}
u.Uid = uuid.New()
jwtToken := u.GetResetPasswordJwt()
testClaims, err := util.ParseJwt(jwtToken, UserHmac)
if err != nil {
t.Errorf("tried to parse valid token but got error %v", err)
}
if testClaims["sub"] != u.Uid.String() {
t.Errorf("`sub` value of %v does not match expected of %v", testClaims["sub"], u.Uid)
}
if testClaims["role"] != "reset" {
t.Errorf("`role` value of %v does not match expected of `reset`", testClaims["role"])
}
if _, exists := testClaims["iat"]; !exists {
t.Errorf("`iat` does not exist in jwt")
}
if _, exists := testClaims["exp"]; !exists {
t.Errorf("`exp` does not exist in jwt")
}
}