Skip to content

Instantly share code, notes, and snippets.

@dearfrankg
Last active October 24, 2017 00:38
Show Gist options
  • Save dearfrankg/f3c2e15b4db93bd442be2c5766b5fe29 to your computer and use it in GitHub Desktop.
Save dearfrankg/f3c2e15b4db93bd442be2c5766b5fe29 to your computer and use it in GitHub Desktop.
var app = require("../../../src/server/test-server.js");
var request = require("supertest");
var db = require("../../../src/server/models");
describe("PageBuilder API integration tests", () => {
describe("authentication", () => {
var token;
describe("Protected API endpoints without token", () => {
it("should deny access without a token", () => {
return request(app)
.get("/api/v1/terms")
.expect(401);
});
});
describe("API endpoint /api/v1/authenticate", () => {
it("should provide token with valid credentials", done => {
request(app)
.post("/api/v1/authenticate")
.send({ email: "[email protected]", password: "baz" })
.expect("Content-Type", /json/)
.expect(200)
.then(res => {
const actual = res.body;
expect(actual.message).toBe("ok");
expect(actual.token.length).toBeGreaterThan(10);
token = actual.token;
db.sequelize.close().then(() => {
done();
});
});
});
});
describe("Protected API endpoints", () => {
it("should grant access with a valid token", done => {
request(app)
.get("/api/v1/terms")
.set("authorization", `bearer ${token}`)
.expect("Content-Type", /json/)
.expect(200)
.then(res => {
expect(Array.isArray(res.body)).toBeTruthy();
expect(typeof res.body[0]).toBe("object");
db.sequelize.close().then(() => {
done();
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment