Skip to content

Commit

Permalink
Adding JWT tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Dec 21, 2016
1 parent 1304ebd commit 5d66a2b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is an elastic REST API gateway for node.js",
"version": "4.1.9",
"version": "4.2.0",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down
11 changes: 8 additions & 3 deletions sample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var jwt = require("jsonwebtoken");

require("./index.js")({
port: 8000,
routes: require("./test/routes.js"),
Expand All @@ -10,7 +8,7 @@ require("./index.js")({
},
auth: {
local: {
enabled: true,
enabled: false,
auth: function (username, password, callback) {
if (username === "test" && password === 123) {
callback(null, {username: username, password: password});
Expand All @@ -19,6 +17,13 @@ require("./index.js")({
}
}
},
jwt: {
enabled: true,
auth: function (token, cb) {
cb(null, token);
},
secretOrKey: "jennifer"
},
protect: ["/uuid"]
},
security: {
Expand Down
56 changes: 56 additions & 0 deletions test/auth_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var hippie = require("hippie"),
emitter = require("events"),
jwt = require("jsonwebtoken"),
tenso = require("../index"),
routes = require("./routes.js"),
array = require("retsu"),
Expand Down Expand Up @@ -344,3 +345,58 @@ describe("Local", function () {
});
});
});

describe("JWT", function () {
var port = 8012,
secret = "jennifer",
token = jwt.sign({username: "[email protected]"}, secret);

tenso({
port: port,
routes: routes,
logging: {level: "error"},
auth: {
jwt: {
enabled: true,
auth: function (token, cb) {
if (token.username === '[email protected]') {
cb(null, token);
} else {
cb(new Error('Invalid token'), null);
}
},
secretOrKey: secret
},
security: {
csrf: false
},
protect: ["/uuid"]
}
});

this.timeout(5000);

it("GET /uuid - returns a uuid (authorized)", function (done) {
api(port, false)
.header('Authorization', 'Bearer ' + token)
.get("/uuid")
.expectStatus(200)
.expectValue("links", [{uri: "/", rel: "collection"}])
.expectValue("error", null)
.expectValue("status", 200)
.end(function (err) {
if (err) throw err;
done();
});
});

it("GET /uuid - returns an 'unauthorized' error", function (done) {
api(port, true)
.get("/uuid")
.expectStatus(401)
.end(function (err) {
if (err) throw err;
done();
});
});
});

0 comments on commit 5d66a2b

Please sign in to comment.