File: resource_server_test.go

package info (click to toggle)
golang-github-zitadel-oidc 3.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 1,520 kB
  • sloc: makefile: 5
file content (147 lines) | stat: -rw-r--r-- 3,131 bytes parent folder | download | duplicates (4)
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 rs

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/zitadel/oidc/v3/pkg/oidc"
)

func TestNewResourceServer(t *testing.T) {
	type args struct {
		issuer     string
		authorizer func() (any, error)
		options    []Option
	}
	type wantFields struct {
		issuer        string
		tokenURL      string
		introspectURL string
		authFn        func() (any, error)
	}
	tests := []struct {
		name       string
		args       args
		wantFields *wantFields
		wantErr    bool
	}{
		{
			name: "bad-discovery",
			args: args{
				issuer:     "https://127.0.0.1:65535",
				authorizer: nil,
				options:    []Option{},
			},
			wantFields: nil,
			wantErr:    true,
		},
		{
			name: "bad-discovery-with-static-tokenurl",
			args: args{
				issuer:     "https://127.0.0.1:65535",
				authorizer: nil,
				options: []Option{
					WithStaticEndpoints(
						"https://some.host/token-url",
						"",
					),
				},
			},
			wantFields: nil,
			wantErr:    true,
		},
		{
			name: "bad-discovery-with-static-introspecturl",
			args: args{
				issuer:     "https://127.0.0.1:65535",
				authorizer: nil,
				options: []Option{
					WithStaticEndpoints(
						"",
						"https://some.host/instrospect-url",
					),
				},
			},
			wantFields: nil,
			wantErr:    true,
		},
		{
			name: "bad-discovery-with-all-static-endpoints",
			args: args{
				issuer:     "https://127.0.0.1:65535",
				authorizer: nil,
				options: []Option{
					WithStaticEndpoints(
						"https://some.host/token-url",
						"https://some.host/instrospect-url",
					),
				},
			},
			wantFields: &wantFields{
				issuer:        "https://127.0.0.1:65535",
				tokenURL:      "https://some.host/token-url",
				introspectURL: "https://some.host/instrospect-url",
				authFn:        nil,
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := newResourceServer(context.Background(), tt.args.issuer, tt.args.authorizer, tt.args.options...)
			if tt.wantErr {
				assert.Error(t, err)
				return
			}
			require.NoError(t, err)
			if tt.wantFields == nil {
				return
			}
			assert.Equal(t, tt.wantFields.issuer, got.issuer)
			assert.Equal(t, tt.wantFields.tokenURL, got.tokenURL)
			assert.Equal(t, tt.wantFields.introspectURL, got.introspectURL)
		})
	}
}

func TestIntrospect(t *testing.T) {
	t.Skip("Skipping test that needs Internet access")
	type args struct {
		ctx   context.Context
		rp    ResourceServer
		token string
	}
	rp, err := newResourceServer(
		context.Background(),
		"https://accounts.spotify.com",
		nil,
	)
	require.NoError(t, err)
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{
			name: "missing-introspect-url",
			args: args{
				ctx:   context.Background(),
				rp:    rp,
				token: "my-token",
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, err := Introspect[*oidc.IntrospectionResponse](tt.args.ctx, tt.args.rp, tt.args.token)
			if tt.wantErr {
				assert.Error(t, err)
				return
			}
			require.NoError(t, err)
		})
	}
}