9
"xelbot.com/reprogl/security"
12
type testDataItem struct {
17
var time1, _ = time.Parse(time.RFC822, "05 Jul 23 16:24 MSK")
18
var time2, _ = time.Parse(time.RFC822, "01 Feb 19 01:37 EET")
20
var testData = []testDataItem{
23
identity: security.Identity{ID: 13, Username: "pupa", Role: security.Admin},
24
deadline: deadline(time1),
26
jsonString: `{"a":{"i":13,"u":"pupa","r":"admin"},"d":"2023-07-05T16:24:00+03:00"}`,
30
identity: security.Identity{},
31
values: map[string]any{
32
"token": "random data",
35
deadline: deadline(time2),
37
jsonString: `{"a":{},"v":{"token":"random data","width":15},"d":"2019-02-01T02:37:00+03:00"}`,
41
identity: security.Identity{ID: 7, Username: "lupa", Role: security.User},
42
values: map[string]any{
46
jsonString: `{"a":{"i":7,"u":"lupa","r":"user"},"v":{"abc":"zxc"},"d":"0001-01-01T00:00:00Z"}`,
50
func TestJSONSerialization(t *testing.T) {
54
deserialized internalData
58
for _, value := range testData {
59
if serialized, err = codec.serialize(value.data); err != nil {
62
if deserialized, err = codec.deserialize(serialized); err != nil {
65
if fmt.Sprintf("%+v", deserialized) != fmt.Sprintf("%+v", value.data) {
66
t.Errorf("Expected %+v, got %+v", value.data, deserialized)
72
func TestJSONString(t *testing.T) {
80
for _, value := range testData {
81
if serialized, err = codec.serialize(value.data); err != nil {
84
str = strings.TrimRight(string(serialized), "\n")
85
if str != value.jsonString {
86
t.Errorf("Expected %s, got %+v", value.jsonString, str)