Skip to content

Commit 5bdafbf

Browse files
committed
Adding tests for different authorization methods
1 parent 36d170f commit 5bdafbf

File tree

7 files changed

+267
-6
lines changed

7 files changed

+267
-6
lines changed

lib/schemaUtils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,8 @@ module.exports = {
10821082
helper = {
10831083
type: 'basic',
10841084
basic: [
1085-
{ key: 'username', value: '<Basic Auth Username>' },
1086-
{ key: 'password', value: '<Basic Auth Password>' }
1085+
{ key: 'username', value: '{{basic_auth_username}}' },
1086+
{ key: 'password', value: '{{basic_auth_password}}' }
10871087
]
10881088
};
10891089
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: https://postman-echo.com/get
9+
security:
10+
- bear: []
11+
paths:
12+
/pets/{petId}:
13+
get:
14+
summary: Info for a specific pet
15+
operationId: showPetById
16+
tags:
17+
- pets
18+
parameters:
19+
- name: petId
20+
in: path
21+
required: true
22+
description: The id of the pet to retrieve
23+
schema:
24+
type: string
25+
responses:
26+
default:
27+
description: unexpected error
28+
content:
29+
application/json:
30+
schema:
31+
$ref: "#/components/schemas/Error"
32+
components:
33+
schemas:
34+
Error:
35+
type: object
36+
required:
37+
- code
38+
- message
39+
properties:
40+
code:
41+
type: integer
42+
format: int32
43+
message:
44+
type: string
45+
securitySchemes:
46+
bear:
47+
type: apiKey
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: https://postman-echo.com/get
9+
security:
10+
- bear: []
11+
paths:
12+
/pets/{petId}:
13+
get:
14+
summary: Info for a specific pet
15+
operationId: showPetById
16+
tags:
17+
- pets
18+
parameters:
19+
- name: petId
20+
in: path
21+
required: true
22+
description: The id of the pet to retrieve
23+
schema:
24+
type: string
25+
responses:
26+
default:
27+
description: unexpected error
28+
content:
29+
application/json:
30+
schema:
31+
$ref: "#/components/schemas/Error"
32+
components:
33+
schemas:
34+
Error:
35+
type: object
36+
required:
37+
- code
38+
- message
39+
properties:
40+
code:
41+
type: integer
42+
format: int32
43+
message:
44+
type: string
45+
securitySchemes:
46+
bear:
47+
type: http
48+
scheme: basic
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: https://postman-echo.com/get
9+
security:
10+
- bear: []
11+
paths:
12+
/pets/{petId}:
13+
get:
14+
summary: Info for a specific pet
15+
operationId: showPetById
16+
tags:
17+
- pets
18+
parameters:
19+
- name: petId
20+
in: path
21+
required: true
22+
description: The id of the pet to retrieve
23+
schema:
24+
type: string
25+
responses:
26+
default:
27+
description: unexpected error
28+
content:
29+
application/json:
30+
schema:
31+
$ref: "#/components/schemas/Error"
32+
components:
33+
schemas:
34+
Error:
35+
type: object
36+
required:
37+
- code
38+
- message
39+
properties:
40+
code:
41+
type: integer
42+
format: int32
43+
message:
44+
type: string
45+
securitySchemes:
46+
bear:
47+
type: http
48+
scheme: digest
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: https://postman-echo.com/get
9+
security:
10+
- bear: []
11+
paths:
12+
/pets/{petId}:
13+
get:
14+
summary: Info for a specific pet
15+
operationId: showPetById
16+
tags:
17+
- pets
18+
parameters:
19+
- name: petId
20+
in: path
21+
required: true
22+
description: The id of the pet to retrieve
23+
schema:
24+
type: string
25+
responses:
26+
default:
27+
description: unexpected error
28+
content:
29+
application/json:
30+
schema:
31+
$ref: "#/components/schemas/Error"
32+
components:
33+
schemas:
34+
Error:
35+
type: object
36+
required:
37+
- code
38+
- message
39+
properties:
40+
code:
41+
type: integer
42+
format: int32
43+
message:
44+
type: string
45+
securitySchemes:
46+
bear:
47+
type: http
48+
scheme: oauth1

test/unit/base.test.js

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ describe('CONVERT FUNCTION TESTS ', function() {
5959
invalidNullInfo = path.join(__dirname, INVALID_OPENAPI_PATH, '/invalid-null-info.json'),
6060
invalidNullInfoTitle = path.join(__dirname, INVALID_OPENAPI_PATH, '/invalid-info-null-title.json'),
6161
invalidNullInfoVersion = path.join(__dirname, INVALID_OPENAPI_PATH, '/invalid-info-null-version.json'),
62-
specWithAuth = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuth.yaml');
62+
specWithAuthBearer = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthBearer.yaml'),
63+
specWithAuthApiKey = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthApiKey.yaml'),
64+
specWithAuthDigest = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthDigest.yaml'),
65+
specWithAuthOauth1 = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthOauth1.yaml'),
66+
specWithAuthBasic = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthBasic.yaml');
6367

6468

6569
it('Should add collection level auth with type as `bearer`' +
@@ -1294,9 +1298,10 @@ describe('CONVERT FUNCTION TESTS ', function() {
12941298
});
12951299

12961300
describe('[Github #643] - Generated value for corresponding' +
1297-
' authorization should be as environment variable format', function(done) {
1298-
it('Should convert a collection and set auth placeholder as variable', function() {
1299-
var openapi = fs.readFileSync(specWithAuth, 'utf8');
1301+
' authorization should be as environment variable format', function() {
1302+
1303+
it('Should convert a collection and set bearer auth placeholder as variable', function(done) {
1304+
var openapi = fs.readFileSync(specWithAuthBearer, 'utf8');
13001305
Converter.convert({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
13011306
expect(err).to.be.null;
13021307
expect(conversionResult.result).to.equal(true);
@@ -1309,6 +1314,71 @@ describe('CONVERT FUNCTION TESTS ', function() {
13091314
done();
13101315
});
13111316
});
1317+
1318+
it('Should convert a collection and set basic auth placeholder as variable', function(done) {
1319+
var openapi = fs.readFileSync(specWithAuthBasic, 'utf8');
1320+
Converter.convert({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
1321+
expect(err).to.be.null;
1322+
expect(conversionResult.result).to.equal(true);
1323+
expect(conversionResult.output.length).to.equal(1);
1324+
expect(conversionResult.output[0].type).to.equal('collection');
1325+
expect(conversionResult.output[0].data).to.have.property('info');
1326+
expect(conversionResult.output[0].data).to.have.property('item');
1327+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1328+
expect(conversionResult.output[0].data.auth.basic[0].value).to.equal('{{basic_auth_username}}');
1329+
expect(conversionResult.output[0].data.auth.basic[1].value).to.equal('{{basic_auth_password}}');
1330+
done();
1331+
});
1332+
});
1333+
1334+
it('Should convert a collection and set digest auth placeholder as variable', function(done) {
1335+
var openapi = fs.readFileSync(specWithAuthDigest, 'utf8');
1336+
Converter.convert({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
1337+
expect(err).to.be.null;
1338+
expect(conversionResult.result).to.equal(true);
1339+
expect(conversionResult.output.length).to.equal(1);
1340+
expect(conversionResult.output[0].type).to.equal('collection');
1341+
expect(conversionResult.output[0].data).to.have.property('info');
1342+
expect(conversionResult.output[0].data).to.have.property('item');
1343+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1344+
expect(conversionResult.output[0].data.auth.digest[0].value).to.equal('{{digest_auth_username}}');
1345+
expect(conversionResult.output[0].data.auth.digest[1].value).to.equal('{{digest_auth_password}}');
1346+
expect(conversionResult.output[0].data.auth.digest[2].value).to.equal('{{realm}}');
1347+
done();
1348+
});
1349+
});
1350+
1351+
it('Should convert a collection and set oauth1 auth placeholder as variable', function(done) {
1352+
var openapi = fs.readFileSync(specWithAuthOauth1, 'utf8');
1353+
Converter.convert({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
1354+
expect(err).to.be.null;
1355+
expect(conversionResult.result).to.equal(true);
1356+
expect(conversionResult.output.length).to.equal(1);
1357+
expect(conversionResult.output[0].type).to.equal('collection');
1358+
expect(conversionResult.output[0].data).to.have.property('info');
1359+
expect(conversionResult.output[0].data).to.have.property('item');
1360+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1361+
expect(conversionResult.output[0].data.auth.oauth1[0].value).to.equal('{{oauth_10_consumer_key}}');
1362+
expect(conversionResult.output[0].data.auth.oauth1[1].value).to.equal('{{oauth_10_consumer_secret}}');
1363+
done();
1364+
});
1365+
});
1366+
1367+
it('Should convert a collection and set apiKey auth placeholder as variable', function(done) {
1368+
var openapi = fs.readFileSync(specWithAuthApiKey, 'utf8');
1369+
Converter.convert({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
1370+
expect(err).to.be.null;
1371+
expect(conversionResult.result).to.equal(true);
1372+
expect(conversionResult.output.length).to.equal(1);
1373+
expect(conversionResult.output[0].type).to.equal('collection');
1374+
expect(conversionResult.output[0].data).to.have.property('info');
1375+
expect(conversionResult.output[0].data).to.have.property('item');
1376+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1377+
expect(conversionResult.output[0].data.auth.apiKey[0].value).to.equal('{{api_key_name}}');
1378+
expect(conversionResult.output[0].data.auth.apiKey[1].value).to.equal('{{api_key}}');
1379+
done();
1380+
});
1381+
});
13121382
});
13131383
});
13141384

0 commit comments

Comments
 (0)