Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #96 from subeeshcbabu/secoption
Browse files Browse the repository at this point in the history
security option for the unit test code generation
  • Loading branch information
subeeshcbabu authored Oct 31, 2016
2 parents 4aeee91 + 891b5de commit f134576
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

- Add `security` option by default to the unit test files generated for all the frameworks.

# v3.0.0

## Breaking changes
Expand Down
3 changes: 2 additions & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ module.exports = Generators.Base.extend({
testPath: this.testPath,
dataPath: this.dataPath,
securityPath: this.securityPath,
framework: this.framework
framework: this.framework,
securith: this.security
}
}, {
local: require.resolve('../test')
Expand Down
3 changes: 2 additions & 1 deletion generators/test/templates/express/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Test('<%=path%>', function (t) {
}));
App.use(Swaggerize({
api: apiPath,
handlers: Path.resolve(__dirname, '<%=handlerDir.replace(/\\/g,'/')%>')
handlers: Path.resolve(__dirname, '<%=handlerDir.replace(/\\/g,'/')%>')<%if (security) {%>,
security: Path.resolve(__dirname, '<%=securityPath.replace(/\\/g,'/')%>')<%}%>
}));
Parser.validate(apiPath, function (err, api) {
t.error(err, 'No parse error');
Expand Down
3 changes: 2 additions & 1 deletion generators/test/templates/hapi/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Test('<%=path%>', function (t) {
register: Swaggerize,
options: {
api: apiPath,
handlers: Path.join(__dirname, '<%=handlerDir.replace(/\\/g,'/')%>')
handlers: Path.join(__dirname, '<%=handlerDir.replace(/\\/g,'/')%>')<%if (security) {%>,
security: Path.resolve(__dirname, '<%=securityPath.replace(/\\/g,'/')%>')<%}%>
}
}, function (err) {
t.error(err, 'No error.');
Expand Down
3 changes: 2 additions & 1 deletion generators/test/templates/restify/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Test('<%=path%>', function (t) {
server.use(Restify.queryParser());
Swaggerize(server, {
api: apiPath,
handlers: Path.resolve(__dirname, '<%=handlerDir.replace(/\\/g,'/')%>')
handlers: Path.resolve(__dirname, '<%=handlerDir.replace(/\\/g,'/')%>')<%if (security) {%>,
security: Path.resolve(__dirname, '<%=securityPath.replace(/\\/g,'/')%>')<%}%>
});
Parser.validate(apiPath, function (err, api) {
t.error(err, 'No parse error');
Expand Down
4 changes: 3 additions & 1 deletion lib/routegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ module.exports = function routegen (generator, path, pathObj) {
mockgenPath: Util.relative(generator.genFilePath, generator.destinationPath(mockgenPath)),
dataPath: Util.relative(generator.genFilePath, generator.destinationPath(dataPath)),
handlerDir: Util.relative(generator.genFilePath, generator.destinationPath(generator.handlerPath)),
operations: []
operations: [],
security: generator.security,
securityPath: Util.relative(generator.genFilePath, generator.destinationPath(generator.securityPath))
};

Object.keys(pathObj).forEach(function (method) {
Expand Down
4 changes: 2 additions & 2 deletions test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Test('** app generator **', function (t) {
t.end();
});
});

t.test('scaffold app with options', function (t) {
var options = {
framework: 'hapi',
Expand All @@ -33,7 +33,7 @@ Test('** app generator **', function (t) {
t.end();
})
.on('end', function () {
TestSuite('app')(t, Util.options(options));
TestSuite('app')(t, Util.options(options), true);
t.end();
});
});
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,23 @@ Test('** test generator **', function (t) {
});
});

t.test('scaffold test files - with security handler', function (t) {
var options = {
testPath: 'mytest',//Custom test path
apiPath: Path.join(__dirname, './fixture/petstore.json'),
framework: 'hapi'
};
Helpers.run(Path.join( __dirname, '../generators/test'))
.withOptions(options)
.withPrompts(Util.prompt('test'))
.on('error', function (error) {
t.error(error);
t.end();
})
.on('end', function () {
TestSuite('test')(t, Util.options(options), true);
t.end();
});
});

});
4 changes: 2 additions & 2 deletions test/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function mockPrompt (name) {
apiPath: Path.join(__dirname, '../fixture/petstore_no_security.json')
},
test: {
framework: 'restify',
framework: 'express',
apiPath: Path.join(__dirname, '../fixture/petstore_no_security.json')
}
};
Expand All @@ -52,7 +52,7 @@ function mockPrompt (name) {
function mockOptions(options) {
var apiRelPath = './config/swagger.json';
options = options || {};
if ('.yml' === Path.extname(options.apiPath) || '.yaml' === Path.extname(options.apiPath)) {
if (options.apiPath && ('.yml' === Path.extname(options.apiPath) || '.yaml' === Path.extname(options.apiPath))) {
apiRelPath = './config/swagger.yaml';
}
return {
Expand Down
52 changes: 45 additions & 7 deletions test/util/testsuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ module.exports = function (generator) {
dataTest(t, options);
handlerTest(t, options);
},
test : function (t, options) {
test : function (t, options, secuirty) {
/**
* Test the generated `test`, `handler` and `data` files
*/
dataTest(t, options);
handlerTest(t, options);
testTest(t, options);
testTest(t, options, secuirty);
},
app : function (t, options) {
app : function (t, options, security) {
/**
* Test the generated `app`, `test`, `handler` and `data` files
*/
appTest(t, options);
appTest(t, options, security);
dataTest(t, options);
handlerTest(t, options);
testTest(t, options);
Expand Down Expand Up @@ -76,17 +76,37 @@ function handlerTest(tester, options) {
/**
* Test the generated `test` files
*/
function testTest(tester, options) {
function testTest(tester, options, security) {
//Data files
var testFiles = Util.routeFiles(options.testPath, options.apiPath);
var testFile = testFiles[0];
tester.test('scaffold test files', function(t) {
Assert.file(Util.routeFiles(options.testPath, options.apiPath));
Assert.file(testFiles);
t.end();
});
// Test file content
tester.test('test unitetst file content', function(t) {
Assert.fileContent([
[testFile, new RegExp(options.framework, 'i')],
[testFile, new RegExp('swaggerize-' + options.framework, 'i')],
[testFile, new RegExp(options.handlerPath, 'i')]
]);
t.end();
});
//If security is set to true test the securityPath
if (security) {
tester.test('test security path', function(t) {
Assert.fileContent([
[testFile, new RegExp(options.securityPath, 'i')]
]);
t.end();
});
}
}
/**
* Test the generated `app` files - `package.json`, `README.md` etc
*/
function appTest(tester, options) {
function appTest(tester, options, security) {
//Dot files
tester.test('scaffold dot files', function(t) {
Assert.file(Util.dotFiles);
Expand All @@ -109,4 +129,22 @@ function appTest(tester, options) {
]);
t.end();
});
// Server file content
tester.test('test server.js file content', function(t) {
Assert.fileContent([
['server.js', new RegExp(options.framework, 'i')],
['server.js', new RegExp('swaggerize-' + options.framework, 'i')],
['server.js', new RegExp(options.handlerPath, 'i')]
]);
t.end();
});
//If security is set to true test the securityPath
if (security) {
tester.test('test security path', function(t) {
Assert.fileContent([
['server.js', new RegExp(options.securityPath, 'i')]
]);
t.end();
});
}
}

0 comments on commit f134576

Please sign in to comment.