Skip to content

Commit 80b9853

Browse files
committed
tests: cover uncovered security definitions validation
1 parent 58b1b8f commit 80b9853

File tree

5 files changed

+282
-3
lines changed

5 files changed

+282
-3
lines changed

src/Validators/SwaggerSpecValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ protected function validateSecurityDefinitions(): void
155155

156156
$this->validateFieldsPresent(self::REQUIRED_FIELDS['security_definition'], $parentId);
157157

158-
$this->validateFieldValue("{$parentId}.'type", self::ALLOWED_VALUES['security_definition_type']);
159-
$this->validateFieldValue("{$parentId}.'in", self::ALLOWED_VALUES['security_definition_in']);
160-
$this->validateFieldValue("{$parentId}.'flow", self::ALLOWED_VALUES['security_definition_flow']);
158+
$this->validateFieldValue("{$parentId}.type", self::ALLOWED_VALUES['security_definition_type']);
159+
$this->validateFieldValue("{$parentId}.in", self::ALLOWED_VALUES['security_definition_in']);
160+
$this->validateFieldValue("{$parentId}.flow", self::ALLOWED_VALUES['security_definition_flow']);
161161
}
162162
}
163163

tests/SwaggerServiceTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,21 @@ public function getConstructorInvalidTmpData(): array
271271
'exceptionMessage' => "Validation failed. Path parameters cannot be optional. "
272272
. "Set required=true for the 'username' parameters at operation 'paths./users.get'."
273273
],
274+
[
275+
'tmpDoc' => 'documentation/invalid_format__security_definition__type',
276+
'exception' => InvalidSwaggerSpecException::class,
277+
'exceptionMessage' => "Validation failed. Field 'securityDefinitions.0.type' has an invalid value: invalid. Allowed values: basic, apiKey, oauth2."
278+
],
279+
[
280+
'tmpDoc' => 'documentation/invalid_format__security_definition__flow',
281+
'exception' => InvalidSwaggerSpecException::class,
282+
'exceptionMessage' => "Validation failed. Field 'securityDefinitions.0.flow' has an invalid value: invalid. Allowed values: implicit, password, application, accessCode."
283+
],
284+
[
285+
'tmpDoc' => 'documentation/invalid_format__security_definition__in',
286+
'exception' => InvalidSwaggerSpecException::class,
287+
'exceptionMessage' => "Validation failed. Field 'securityDefinitions.0.in' has an invalid value: invalid. Allowed values: query, header."
288+
],
274289
];
275290
}
276291

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"swagger": "2.0",
3+
"host": "localhost",
4+
"basePath": "\/",
5+
"schemes": [],
6+
"paths": {
7+
"\/api\/users":
8+
{
9+
"post":
10+
{
11+
"tags": ["api"],
12+
"consumes": ["application\/x-www-form-urlencoded"],
13+
"produces": ["application\/json"],
14+
"parameters": [
15+
{
16+
"in": "body",
17+
"name": "body",
18+
"description": "",
19+
"required": true,
20+
"schema": {
21+
"$ref": "#/definitions/apiusersObject"
22+
}
23+
}
24+
],
25+
"responses":
26+
{
27+
"403":
28+
{
29+
"description": "Forbidden",
30+
"schema":
31+
{
32+
"example":
33+
{
34+
"message": "This action is unauthorized."
35+
}
36+
}
37+
}
38+
},
39+
"security": [],
40+
"description": "",
41+
"summary": "test"
42+
}
43+
}
44+
},
45+
"definitions": {
46+
"apiusersObject": {
47+
"type": "object",
48+
"properties": {
49+
"query": {
50+
"type": "string",
51+
"description": ""
52+
},
53+
"user_id": {
54+
"type": "integer",
55+
"description": "with_to_array_rule_string_name"
56+
},
57+
"is_email_enabled": {
58+
"type": "string",
59+
"description": "test_rule_without_to_string"
60+
}
61+
},
62+
"required": {
63+
"0": "query"
64+
},
65+
"example": {
66+
"first_name": "andrey",
67+
"last_name": "voronin"
68+
}
69+
}
70+
},
71+
"info": {
72+
"description": "This is automatically collected documentation",
73+
"version": "0.0.0",
74+
"title": "Name of Your Application",
75+
"termsOfService": "",
76+
"contact":
77+
{
78+
"email": "your@email.com"
79+
}
80+
},
81+
"securityDefinitions": [
82+
{
83+
"type": "basic",
84+
"in": "query",
85+
"flow": "invalid"
86+
}
87+
]
88+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"swagger": "2.0",
3+
"host": "localhost",
4+
"basePath": "\/",
5+
"schemes": [],
6+
"paths": {
7+
"\/api\/users":
8+
{
9+
"post":
10+
{
11+
"tags": ["api"],
12+
"consumes": ["application\/x-www-form-urlencoded"],
13+
"produces": ["application\/json"],
14+
"parameters": [
15+
{
16+
"in": "body",
17+
"name": "body",
18+
"description": "",
19+
"required": true,
20+
"schema": {
21+
"$ref": "#/definitions/apiusersObject"
22+
}
23+
}
24+
],
25+
"responses":
26+
{
27+
"403":
28+
{
29+
"description": "Forbidden",
30+
"schema":
31+
{
32+
"example":
33+
{
34+
"message": "This action is unauthorized."
35+
}
36+
}
37+
}
38+
},
39+
"security": [],
40+
"description": "",
41+
"summary": "test"
42+
}
43+
}
44+
},
45+
"definitions": {
46+
"apiusersObject": {
47+
"type": "object",
48+
"properties": {
49+
"query": {
50+
"type": "string",
51+
"description": ""
52+
},
53+
"user_id": {
54+
"type": "integer",
55+
"description": "with_to_array_rule_string_name"
56+
},
57+
"is_email_enabled": {
58+
"type": "string",
59+
"description": "test_rule_without_to_string"
60+
}
61+
},
62+
"required": {
63+
"0": "query"
64+
},
65+
"example": {
66+
"first_name": "andrey",
67+
"last_name": "voronin"
68+
}
69+
}
70+
},
71+
"info": {
72+
"description": "This is automatically collected documentation",
73+
"version": "0.0.0",
74+
"title": "Name of Your Application",
75+
"termsOfService": "",
76+
"contact":
77+
{
78+
"email": "your@email.com"
79+
}
80+
},
81+
"securityDefinitions": [
82+
{
83+
"type": "basic",
84+
"in": "invalid",
85+
"flow": "password"
86+
}
87+
]
88+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"swagger": "2.0",
3+
"host": "localhost",
4+
"basePath": "\/",
5+
"schemes": [],
6+
"paths": {
7+
"\/api\/users":
8+
{
9+
"post":
10+
{
11+
"tags": ["api"],
12+
"consumes": ["application\/x-www-form-urlencoded"],
13+
"produces": ["application\/json"],
14+
"parameters": [
15+
{
16+
"in": "body",
17+
"name": "body",
18+
"description": "",
19+
"required": true,
20+
"schema": {
21+
"$ref": "#/definitions/apiusersObject"
22+
}
23+
}
24+
],
25+
"responses":
26+
{
27+
"403":
28+
{
29+
"description": "Forbidden",
30+
"schema":
31+
{
32+
"example":
33+
{
34+
"message": "This action is unauthorized."
35+
}
36+
}
37+
}
38+
},
39+
"security": [],
40+
"description": "",
41+
"summary": "test"
42+
}
43+
}
44+
},
45+
"definitions": {
46+
"apiusersObject": {
47+
"type": "object",
48+
"properties": {
49+
"query": {
50+
"type": "string",
51+
"description": ""
52+
},
53+
"user_id": {
54+
"type": "integer",
55+
"description": "with_to_array_rule_string_name"
56+
},
57+
"is_email_enabled": {
58+
"type": "string",
59+
"description": "test_rule_without_to_string"
60+
}
61+
},
62+
"required": {
63+
"0": "query"
64+
},
65+
"example": {
66+
"first_name": "andrey",
67+
"last_name": "voronin"
68+
}
69+
}
70+
},
71+
"info": {
72+
"description": "This is automatically collected documentation",
73+
"version": "0.0.0",
74+
"title": "Name of Your Application",
75+
"termsOfService": "",
76+
"contact":
77+
{
78+
"email": "your@email.com"
79+
}
80+
},
81+
"securityDefinitions": [
82+
{
83+
"type": "invalid",
84+
"in": "query",
85+
"flow": "password"
86+
}
87+
]
88+
}

0 commit comments

Comments
 (0)