File: util_test.go

package info (click to toggle)
golang-github-zitadel-oidc 3.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 1,484 kB
  • sloc: makefile: 5
file content (147 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package oidc

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type jsonErrorTest struct{}

func (jsonErrorTest) MarshalJSON() ([]byte, error) {
	return nil, errors.New("test")
}

func Test_mergeAndMarshalClaims(t *testing.T) {
	type args struct {
		registered any
		claims     map[string]any
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{
			name: "encoder error",
			args: args{
				registered: jsonErrorTest{},
			},
			wantErr: true,
		},
		{
			name: "no claims",
			args: args{
				registered: struct {
					Foo string `json:"foo,omitempty"`
				}{
					Foo: "bar",
				},
			},
			want: "{\"foo\":\"bar\"}\n",
		},
		{
			name: "with claims",
			args: args{
				registered: struct {
					Foo string `json:"foo,omitempty"`
				}{
					Foo: "bar",
				},
				claims: map[string]any{
					"bar": "foo",
				},
			},
			want: "{\"bar\":\"foo\",\"foo\":\"bar\"}\n",
		},
		{
			name: "registered overwrites custom",
			args: args{
				registered: struct {
					Foo string `json:"foo,omitempty"`
				}{
					Foo: "bar",
				},
				claims: map[string]any{
					"foo": "Hello, World!",
				},
			},
			want: "{\"foo\":\"bar\"}\n",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := mergeAndMarshalClaims(tt.args.registered, tt.args.claims)
			if tt.wantErr {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
			}
			assert.Equal(t, tt.want, string(got))
		})
	}
}

func Test_unmarshalJSONMulti(t *testing.T) {
	type dst struct {
		Foo string `json:"foo,omitempty"`
	}

	type args struct {
		data         string
		destinations []any
	}
	tests := []struct {
		name    string
		args    args
		want    []any
		wantErr bool
	}{
		{
			name: "error",
			args: args{
				data: "~!~~",
				destinations: []any{
					&dst{},
					&map[string]any{},
				},
			},
			want: []any{
				&dst{},
				&map[string]any{},
			},
			wantErr: true,
		},
		{
			name: "success",
			args: args{
				data: "{\"bar\":\"foo\",\"foo\":\"bar\"}\n",
				destinations: []any{
					&dst{},
					&map[string]any{},
				},
			},
			want: []any{
				&dst{Foo: "bar"},
				&map[string]any{
					"foo": "bar",
					"bar": "foo",
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := unmarshalJSONMulti([]byte(tt.args.data), tt.args.destinations...)
			if tt.wantErr {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
			}
			assert.Equal(t, tt.want, tt.args.destinations)
		})
	}
}