File: issue267_test.go

package info (click to toggle)
golang-github-getkin-kin-openapi 0.124.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,288 kB
  • sloc: sh: 344; makefile: 4
file content (296 lines) | stat: -rw-r--r-- 8,499 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package openapi3filter

import (
	"net/http"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/getkin/kin-openapi/openapi3"
	"github.com/getkin/kin-openapi/routers/gorillamux"
)

func TestIssue267(t *testing.T) {
	spec := `
openapi: 3.0.0
info:
  description: This is a sample of the API
  version: 1.0.0
  title: sample API
tags:
  - name: authorization
    description: Create and validate authorization tokens using oauth
paths:
  /oauth2/token:
    post:
      tags:
        - authorization
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AccessTokenRequest'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/AccessTokenRequest'
      responses:
        '200':
          description: 'The request was successful and a token was issued.'
  /oauth2/any-token:
    post:
      tags:
        - authorization
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AnyTokenRequest'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/AnyTokenRequest'
      responses:
        '200':
          description: 'Any type of token request was successful.'
  /oauth2/all-token:
    post:
      tags:
        - authorization
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AllTokenRequest'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/AllTokenRequest'
      responses:
        '200':
          description: 'All type of token request was successful.'
components:
  schemas:
    AccessTokenRequest:
      description: 'Describes all of the potential access token requests that can be received'
      type: object
      oneOf:
        - $ref: '#/components/schemas/ClientCredentialsTokenRequest'
        - $ref: '#/components/schemas/RefreshTokenRequest'
    ClientCredentialsTokenRequest:
      description: 'The client_id and client_secret properties should only be sent in form data if the client does not support basic authentication for sending client credentials.'
      type: object
      properties:
        grant_type:
          type: string
          enum:
            - client_credentials
        scope:
          type: string
        client_id:
          type: string
        client_secret:
          type: string
      required:
        - grant_type
        - scope
        - client_id
        - client_secret
    RefreshTokenRequest:
      type: object
      properties:
        grant_type:
          type: string
          enum:
            - refresh_token
        client_id:
          type: string
        refresh_token:
          type: string
      required:
        - grant_type
        - client_id
        - refresh_token
    AnyTokenRequest:
      type: object
      anyOf:
        - $ref: '#/components/schemas/ClientCredentialsTokenRequest'
        - $ref: '#/components/schemas/RefreshTokenRequest'
        - $ref: '#/components/schemas/AdditionalTokenRequest'
    AdditionalTokenRequest:
      type: object
      properties:
        grant_type:
          type: string
          enum:
            - additional_grant
        additional_info:
          type: string
      required:
        - grant_type
        - additional_info
    AllTokenRequest:
      type: object
      allOf:
        - $ref: '#/components/schemas/ClientCredentialsTokenRequest'
        - $ref: '#/components/schemas/TrackingInfo'
    TrackingInfo:
      type: object
      properties:
        tracking_id:
          type: string
      required:
        - tracking_id
    `[1:]

	loader := openapi3.NewLoader()

	doc, err := loader.LoadFromData([]byte(spec))
	require.NoError(t, err)

	err = doc.Validate(loader.Context)
	require.NoError(t, err)

	router, err := gorillamux.NewRouter(doc)
	require.NoError(t, err)

	for _, testcase := range []struct {
		endpoint   string
		ct         string
		data       string
		shouldFail bool
	}{
		// application/json
		{
			endpoint:   "/oauth2/token",
			ct:         "application/json",
			data:       `{"grant_type":"client_credentials", "scope":"testscope", "client_id":"myclient", "client_secret":"mypass"}`,
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/token",
			ct:         "application/json",
			data:       `{"grant_type":"client_credentials", "scope":"testscope", "client_id":"myclient", "client_secret":"mypass","request":1}`,
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/json",
			data:       `{"grant_type":"client_credentials", "scope":"testscope", "client_id":"myclient", "client_secret":"mypass"}`,
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/json",
			data:       `{"grant_type":"refresh_token", "client_id":"myclient", "refresh_token":"someRefreshToken"}`,
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/json",
			data:       `{"grant_type":"additional_grant", "additional_info":"extraInfo"}`,
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/json",
			data:       `{"grant_type":"invalid_grant", "extra_field":"extraValue"}`,
			shouldFail: true,
		},
		{
			endpoint: "/oauth2/all-token",
			ct:       "application/json",
			data: `{
		      "grant_type": "client_credentials",
		      "scope": "testscope",
		      "client_id": "myclient",
		      "client_secret": "mypass",
		      "tracking_id": "123456"
		  }`,
			shouldFail: false,
		},

		{
			endpoint:   "/oauth2/all-token",
			ct:         "application/json",
			data:       `{"grant_type":"invalid", "client_id":"myclient", "extra_field":"extraValue"}`,
			shouldFail: true,
		},

		// application/x-www-form-urlencoded
		{
			endpoint:   "/oauth2/token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=client_credentials&scope=testscope&client_id=myclient&client_secret=mypass",
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=client_credentials&scope=testscope&client_id=myclient&client_secret=mypass&request=1",
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/token",
			ct:         "application/x-www-form-urlencoded",
			data:       "invalid_field=invalid_value",
			shouldFail: true,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=client_credentials&scope=testscope&client_id=myclient&client_secret=mypass",
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=refresh_token&client_id=myclient&refresh_token=someRefreshToken",
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=additional_grant&additional_info=extraInfo",
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/any-token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=invalid_grant&extra_field=extraValue",
			shouldFail: true,
		},
		{
			endpoint:   "/oauth2/all-token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=client_credentials&scope=testscope&client_id=myclient&client_secret=mypass&tracking_id=123456",
			shouldFail: false,
		},
		{
			endpoint:   "/oauth2/all-token",
			ct:         "application/x-www-form-urlencoded",
			data:       "grant_type=invalid&client_id=myclient&extra_field=extraValue",
			shouldFail: true,
		},
	} {
		t.Run(testcase.ct, func(t *testing.T) {
			data := strings.NewReader(testcase.data)
			req, err := http.NewRequest("POST", testcase.endpoint, data)
			require.NoError(t, err)
			req.Header.Add("Content-Type", testcase.ct)

			route, pathParams, err := router.FindRoute(req)
			require.NoError(t, err)

			validationInput := &RequestValidationInput{
				Request:    req,
				PathParams: pathParams,
				Route:      route,
			}
			err = ValidateRequest(loader.Context, validationInput)
			if testcase.shouldFail {
				require.Error(t, err, "This test case should fail "+testcase.data)
			} else {
				require.NoError(t, err, "This test case should pass "+testcase.data)
			}
		})
	}
}