Debian Codegraph @

pkg/oidc/introspection_test.go

raw
1 package oidc
2
3 import (
4 "encoding/json"
5 "testing"
6
7 "github.com/muhlemmer/gu"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 func TestIntrospectionResponse_SetUserInfo(t *testing.T) {
13 tests := []struct {
14 name string
15 start *IntrospectionResponse
16 want *IntrospectionResponse
17 }{
18 {
19
20 name: "nil claims",
21 start: &IntrospectionResponse{},
22 want: &IntrospectionResponse{
23 Subject: userInfoData.Subject,
24 Username: userInfoData.PreferredUsername,
25 Address: userInfoData.Address,
26 UserInfoProfile: userInfoData.UserInfoProfile,
27 UserInfoEmail: userInfoData.UserInfoEmail,
28 UserInfoPhone: userInfoData.UserInfoPhone,
29 Claims: gu.MapCopy(userInfoData.Claims),
30 },
31 },
32 {
33
34 name: "merge claims",
35 start: &IntrospectionResponse{
36 Claims: map[string]any{
37 "hello": "world",
38 },
39 },
40 want: &IntrospectionResponse{
41 Subject: userInfoData.Subject,
42 Username: userInfoData.PreferredUsername,
43 Address: userInfoData.Address,
44 UserInfoProfile: userInfoData.UserInfoProfile,
45 UserInfoEmail: userInfoData.UserInfoEmail,
46 UserInfoPhone: userInfoData.UserInfoPhone,
47 Claims: map[string]any{
48 "foo": "bar",
49 "hello": "world",
50 },
51 },
52 },
53 }
54 for _, tt := range tests {
55 t.Run(tt.name, func(t *testing.T) {
56 tt.start.SetUserInfo(userInfoData)
57 assert.Equal(t, tt.want, tt.start)
58 })
59 }
60 }
61
62 func TestIntrospectionResponse_GetAddress(t *testing.T) {
63 // nil address
64 i := new(IntrospectionResponse)
65 assert.Equal(t, &UserInfoAddress{}, i.GetAddress())
66
67 i.Address = &UserInfoAddress{PostalCode: "1234"}
68 assert.Equal(t, i.Address, i.GetAddress())
69 }
70
71 func TestIntrospectionResponse_MarshalJSON(t *testing.T) {
72 got, err := json.Marshal(&IntrospectionResponse{
73 UserInfoProfile: UserInfoProfile{
74 PreferredUsername: "muhlemmer",
75 },
76 })
77 require.NoError(t, err)
78 assert.Equal(t, string(got), `{"active":false,"username":"muhlemmer","preferred_username":"muhlemmer"}`)
79 }