Debian Codegraph @

pkg/op/keys_test.go

raw
1 package op_test
2
3 import (
4 "crypto/rsa"
5 "math/big"
6 "net/http"
7 "net/http/httptest"
8 "testing"
9
10 jose "github.com/go-jose/go-jose/v4"
11 "github.com/golang/mock/gomock"
12 "github.com/stretchr/testify/assert"
13
14 "github.com/zitadel/oidc/v3/pkg/oidc"
15 "github.com/zitadel/oidc/v3/pkg/op"
16 "github.com/zitadel/oidc/v3/pkg/op/mock"
17 )
18
19 func TestKeys(t *testing.T) {
20 type args struct {
21 k op.KeyProvider
22 }
23 type res struct {
24 statusCode int
25 contentType string
26 body string
27 }
28 tests := []struct {
29 name string
30 args args
31 res res
32 }{
33 {
34 name: "error",
35 args: args{
36 k: func() op.KeyProvider {
37 m := mock.NewMockKeyProvider(gomock.NewController(t))
38 m.EXPECT().KeySet(gomock.Any()).Return(nil, oidc.ErrServerError())
39 return m
40 }(),
41 },
42 res: res{
43 statusCode: http.StatusInternalServerError,
44 contentType: "application/json",
45 body: `{"error":"server_error"}
46 `,
47 },
48 },
49 {
50 name: "empty list",
51 args: args{
52 k: func() op.KeyProvider {
53 m := mock.NewMockKeyProvider(gomock.NewController(t))
54 m.EXPECT().KeySet(gomock.Any()).Return(nil, nil)
55 return m
56 }(),
57 },
58 res: res{
59 statusCode: http.StatusOK,
60 contentType: "application/json",
61 body: `{"keys":[]}
62 `,
63 },
64 },
65 {
66 name: "list",
67 args: args{
68 k: func() op.KeyProvider {
69 ctrl := gomock.NewController(t)
70 m := mock.NewMockKeyProvider(ctrl)
71 k := mock.NewMockKey(ctrl)
72 k.EXPECT().Key().Return(&rsa.PublicKey{
73 N: big.NewInt(1),
74 E: 1,
75 })
76 k.EXPECT().ID().Return("id")
77 k.EXPECT().Algorithm().Return(jose.RS256)
78 k.EXPECT().Use().Return("sig")
79 m.EXPECT().KeySet(gomock.Any()).Return([]op.Key{k}, nil)
80 return m
81 }(),
82 },
83 res: res{
84 statusCode: http.StatusOK,
85 contentType: "application/json",
86 body: `{"keys":[{"use":"sig","kty":"RSA","kid":"id","alg":"RS256","n":"AQ","e":"AQ"}]}
87 `,
88 },
89 },
90 }
91 for _, tt := range tests {
92 t.Run(tt.name, func(t *testing.T) {
93 w := httptest.NewRecorder()
94 op.Keys(w, httptest.NewRequest("GET", "/keys", nil), tt.args.k)
95 assert.Equal(t, tt.res.statusCode, w.Result().StatusCode)
96 assert.Equal(t, tt.res.contentType, w.Header().Get("content-type"))
97 assert.Equal(t, tt.res.body, w.Body.String())
98 })
99 }
100 }