File: keys_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 (100 lines) | stat: -rw-r--r-- 2,273 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
package op_test

import (
	"crypto/rsa"
	"math/big"
	"net/http"
	"net/http/httptest"
	"testing"

	jose "github.com/go-jose/go-jose/v4"
	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/assert"

	"github.com/zitadel/oidc/v3/pkg/oidc"
	"github.com/zitadel/oidc/v3/pkg/op"
	"github.com/zitadel/oidc/v3/pkg/op/mock"
)

func TestKeys(t *testing.T) {
	type args struct {
		k op.KeyProvider
	}
	type res struct {
		statusCode  int
		contentType string
		body        string
	}
	tests := []struct {
		name string
		args args
		res  res
	}{
		{
			name: "error",
			args: args{
				k: func() op.KeyProvider {
					m := mock.NewMockKeyProvider(gomock.NewController(t))
					m.EXPECT().KeySet(gomock.Any()).Return(nil, oidc.ErrServerError())
					return m
				}(),
			},
			res: res{
				statusCode:  http.StatusInternalServerError,
				contentType: "application/json",
				body: `{"error":"server_error"}
`,
			},
		},
		{
			name: "empty list",
			args: args{
				k: func() op.KeyProvider {
					m := mock.NewMockKeyProvider(gomock.NewController(t))
					m.EXPECT().KeySet(gomock.Any()).Return(nil, nil)
					return m
				}(),
			},
			res: res{
				statusCode:  http.StatusOK,
				contentType: "application/json",
				body: `{"keys":[]}
`,
			},
		},
		{
			name: "list",
			args: args{
				k: func() op.KeyProvider {
					ctrl := gomock.NewController(t)
					m := mock.NewMockKeyProvider(ctrl)
					k := mock.NewMockKey(ctrl)
					k.EXPECT().Key().Return(&rsa.PublicKey{
						N: big.NewInt(1),
						E: 1,
					})
					k.EXPECT().ID().Return("id")
					k.EXPECT().Algorithm().Return(jose.RS256)
					k.EXPECT().Use().Return("sig")
					m.EXPECT().KeySet(gomock.Any()).Return([]op.Key{k}, nil)
					return m
				}(),
			},
			res: res{
				statusCode:  http.StatusOK,
				contentType: "application/json",
				body: `{"keys":[{"use":"sig","kty":"RSA","kid":"id","alg":"RS256","n":"AQ","e":"AQ"}]}
`,
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			w := httptest.NewRecorder()
			op.Keys(w, httptest.NewRequest("GET", "/keys", nil), tt.args.k)
			assert.Equal(t, tt.res.statusCode, w.Result().StatusCode)
			assert.Equal(t, tt.res.contentType, w.Header().Get("content-type"))
			assert.Equal(t, tt.res.body, w.Body.String())
		})
	}
}