Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit ca7c9e5

Browse files
author
Bowden Kelly
authored
Merge pull request #38 from dmt/master
change tests to not always pass
2 parents aa75060 + 98d1641 commit ca7c9e5

File tree

8 files changed

+161
-171
lines changed

8 files changed

+161
-171
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,4 @@
9898
"tslint": "^5.8.0",
9999
"typescript": "^2.4.0"
100100
}
101-
}
101+
}

src/app.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
import * as express from "express";
5+
import * as compression from "compression"; // compresses requests
6+
import * as session from "express-session";
7+
import * as bodyParser from "body-parser";
8+
import * as logger from "morgan";
9+
import * as lusca from "lusca";
10+
import * as dotenv from "dotenv";
11+
import * as mongo from "connect-mongo";
12+
import * as flash from "express-flash";
13+
import * as path from "path";
14+
import * as mongoose from "mongoose";
15+
import * as passport from "passport";
16+
import expressValidator = require("express-validator");
17+
18+
const MongoStore = mongo(session);
19+
20+
/**
21+
* Load environment variables from .env file, where API keys and passwords are configured.
22+
*/
23+
dotenv.config({ path: ".env.example" });
24+
25+
26+
/**
27+
* Controllers (route handlers).
28+
*/
29+
import * as homeController from "./controllers/home";
30+
import * as userController from "./controllers/user";
31+
import * as apiController from "./controllers/api";
32+
import * as contactController from "./controllers/contact";
33+
34+
/**
35+
* API keys and Passport configuration.
36+
*/
37+
import * as passportConfig from "./config/passport";
38+
39+
/**
40+
* Create Express server.
41+
*/
42+
const app = express();
43+
44+
/**
45+
* Connect to MongoDB.
46+
*/
47+
// mongoose.Promise = global.Promise;
48+
mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
49+
50+
mongoose.connection.on("error", () => {
51+
console.log("MongoDB connection error. Please make sure MongoDB is running.");
52+
process.exit();
53+
});
54+
55+
56+
57+
/**
58+
* Express configuration.
59+
*/
60+
app.set("port", process.env.PORT || 3000);
61+
app.set("views", path.join(__dirname, "../views"));
62+
app.set("view engine", "pug");
63+
app.use(compression());
64+
app.use(logger("dev"));
65+
app.use(bodyParser.json());
66+
app.use(bodyParser.urlencoded({ extended: true }));
67+
app.use(expressValidator());
68+
app.use(session({
69+
resave: true,
70+
saveUninitialized: true,
71+
secret: process.env.SESSION_SECRET,
72+
store: new MongoStore({
73+
url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
74+
autoReconnect: true
75+
})
76+
}));
77+
app.use(passport.initialize());
78+
app.use(passport.session());
79+
app.use(flash());
80+
app.use(lusca.xframe("SAMEORIGIN"));
81+
app.use(lusca.xssProtection(true));
82+
app.use((req, res, next) => {
83+
res.locals.user = req.user;
84+
next();
85+
});
86+
app.use((req, res, next) => {
87+
// After successful login, redirect back to the intended page
88+
if (!req.user &&
89+
req.path !== "/login" &&
90+
req.path !== "/signup" &&
91+
!req.path.match(/^\/auth/) &&
92+
!req.path.match(/\./)) {
93+
req.session.returnTo = req.path;
94+
} else if (req.user &&
95+
req.path == "/account") {
96+
req.session.returnTo = req.path;
97+
}
98+
next();
99+
});
100+
app.use(express.static(path.join(__dirname, "public"), { maxAge: 31557600000 }));
101+
102+
/**
103+
* Primary app routes.
104+
*/
105+
app.get("/", homeController.index);
106+
app.get("/login", userController.getLogin);
107+
app.post("/login", userController.postLogin);
108+
app.get("/logout", userController.logout);
109+
app.get("/forgot", userController.getForgot);
110+
app.post("/forgot", userController.postForgot);
111+
app.get("/reset/:token", userController.getReset);
112+
app.post("/reset/:token", userController.postReset);
113+
app.get("/signup", userController.getSignup);
114+
app.post("/signup", userController.postSignup);
115+
app.get("/contact", contactController.getContact);
116+
app.post("/contact", contactController.postContact);
117+
app.get("/account", passportConfig.isAuthenticated, userController.getAccount);
118+
app.post("/account/profile", passportConfig.isAuthenticated, userController.postUpdateProfile);
119+
app.post("/account/password", passportConfig.isAuthenticated, userController.postUpdatePassword);
120+
app.post("/account/delete", passportConfig.isAuthenticated, userController.postDeleteAccount);
121+
app.get("/account/unlink/:provider", passportConfig.isAuthenticated, userController.getOauthUnlink);
122+
123+
/**
124+
* API examples routes.
125+
*/
126+
app.get("/api", apiController.getApi);
127+
app.get("/api/facebook", passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getFacebook);
128+
129+
/**
130+
* OAuth authentication routes. (Sign in)
131+
*/
132+
app.get("/auth/facebook", passport.authenticate("facebook", { scope: ["email", "public_profile"] }));
133+
app.get("/auth/facebook/callback", passport.authenticate("facebook", { failureRedirect: "/login" }), (req, res) => {
134+
res.redirect(req.session.returnTo || "/");
135+
});
136+
137+
module.exports = app;

src/server.ts

Lines changed: 3 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,6 @@
1-
/**
2-
* Module dependencies.
3-
*/
4-
import * as express from "express";
5-
import * as compression from "compression"; // compresses requests
6-
import * as session from "express-session";
7-
import * as expressValidator from "express-validator";
8-
import * as bodyParser from "body-parser";
9-
import * as logger from "morgan";
101
import * as errorHandler from "errorhandler";
11-
import * as lusca from "lusca";
12-
import * as dotenv from "dotenv";
13-
import * as mongo from "connect-mongo";
14-
import * as flash from "express-flash";
15-
import * as path from "path";
16-
import * as mongoose from "mongoose";
17-
import * as passport from "passport";
18-
19-
20-
const MongoStore = mongo(session);
21-
22-
/**
23-
* Load environment variables from .env file, where API keys and passwords are configured.
24-
*/
25-
dotenv.config({ path: ".env.example" });
26-
27-
28-
/**
29-
* Controllers (route handlers).
30-
*/
31-
import * as homeController from "./controllers/home";
32-
import * as userController from "./controllers/user";
33-
import * as apiController from "./controllers/api";
34-
import * as contactController from "./controllers/contact";
35-
36-
/**
37-
* API keys and Passport configuration.
38-
*/
39-
import * as passportConfig from "./config/passport";
40-
41-
/**
42-
* Create Express server.
43-
*/
44-
const app = express();
45-
46-
/**
47-
* Connect to MongoDB.
48-
*/
49-
// mongoose.Promise = global.Promise;
50-
mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI, {
51-
useMongoClient: true
52-
});
53-
mongoose.connection.on("error", () => {
54-
console.log("MongoDB connection error. Please make sure MongoDB is running.");
55-
process.exit();
56-
});
57-
58-
59-
60-
/**
61-
* Express configuration.
62-
*/
63-
app.set("port", process.env.PORT || 3000);
64-
app.set("views", path.join(__dirname, "../views"));
65-
app.set("view engine", "pug");
66-
app.use(compression());
67-
app.use(logger("dev"));
68-
app.use(bodyParser.json());
69-
app.use(bodyParser.urlencoded({ extended: true }));
70-
app.use(expressValidator());
71-
app.use(session({
72-
resave: true,
73-
saveUninitialized: true,
74-
secret: process.env.SESSION_SECRET,
75-
store: new MongoStore({
76-
url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
77-
autoReconnect: true
78-
})
79-
}));
80-
app.use(passport.initialize());
81-
app.use(passport.session());
82-
app.use(flash());
83-
app.use(lusca.xframe("SAMEORIGIN"));
84-
app.use(lusca.xssProtection(true));
85-
app.use((req, res, next) => {
86-
res.locals.user = req.user;
87-
next();
88-
});
89-
app.use((req, res, next) => {
90-
// After successful login, redirect back to the intended page
91-
if (!req.user &&
92-
req.path !== "/login" &&
93-
req.path !== "/signup" &&
94-
!req.path.match(/^\/auth/) &&
95-
!req.path.match(/\./)) {
96-
req.session.returnTo = req.path;
97-
} else if (req.user &&
98-
req.path == "/account") {
99-
req.session.returnTo = req.path;
100-
}
101-
next();
102-
});
103-
app.use(express.static(path.join(__dirname, "public"), { maxAge: 31557600000 }));
104-
105-
/**
106-
* Primary app routes.
107-
*/
108-
app.get("/", homeController.index);
109-
app.get("/login", userController.getLogin);
110-
app.post("/login", userController.postLogin);
111-
app.get("/logout", userController.logout);
112-
app.get("/forgot", userController.getForgot);
113-
app.post("/forgot", userController.postForgot);
114-
app.get("/reset/:token", userController.getReset);
115-
app.post("/reset/:token", userController.postReset);
116-
app.get("/signup", userController.getSignup);
117-
app.post("/signup", userController.postSignup);
118-
app.get("/contact", contactController.getContact);
119-
app.post("/contact", contactController.postContact);
120-
app.get("/account", passportConfig.isAuthenticated, userController.getAccount);
121-
app.post("/account/profile", passportConfig.isAuthenticated, userController.postUpdateProfile);
122-
app.post("/account/password", passportConfig.isAuthenticated, userController.postUpdatePassword);
123-
app.post("/account/delete", passportConfig.isAuthenticated, userController.postDeleteAccount);
124-
app.get("/account/unlink/:provider", passportConfig.isAuthenticated, userController.getOauthUnlink);
125-
126-
/**
127-
* API examples routes.
128-
*/
129-
app.get("/api", apiController.getApi);
130-
app.get("/api/facebook", passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getFacebook);
131-
132-
/**
133-
* OAuth authentication routes. (Sign in)
134-
*/
135-
app.get("/auth/facebook", passport.authenticate("facebook", { scope: ["email", "public_profile"] }));
136-
app.get("/auth/facebook/callback", passport.authenticate("facebook", { failureRedirect: "/login" }), (req, res) => {
137-
res.redirect(req.session.returnTo || "/");
138-
});
1392

3+
const app = require("./app");
1404

1415
/**
1426
* Error Handler. Provides full stack - remove for production
@@ -146,9 +10,9 @@ app.use(errorHandler());
14610
/**
14711
* Start Express server.
14812
*/
149-
app.listen(app.get("port"), () => {
13+
const server = app.listen(app.get("port"), () => {
15014
console.log((" App is running at http://localhost:%d in %s mode"), app.get("port"), app.get("env"));
15115
console.log(" Press CTRL-C to stop\n");
15216
});
15317

154-
module.exports = app;
18+
export = server;

test/api.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import * as supertest from "supertest";
2-
import * as app from "../src/server";
1+
import * as request from "supertest";
2+
import * as app from "../src/app";
33

44
describe("GET /api", () => {
5-
const request = supertest(app);
6-
75
it("should return 200 OK", () => {
8-
request
9-
.get("/api")
6+
return request(app).get("/api")
107
.expect(200);
118
});
129
});

test/app.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import * as supertest from "supertest";
2-
import * as app from "../src/server";
1+
import * as request from "supertest";
2+
import * as app from "../src/app";
33

44
describe("GET /random-url", () => {
5-
const request = supertest(app);
6-
75
it("should return 404", (done) => {
8-
request.get("/reset")
6+
request(app).get("/reset")
97
.expect(404, done);
108
});
119
});

test/contact.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import * as supertest from "supertest";
2-
import * as app from "../src/server";
1+
import * as request from "supertest";
2+
import * as app from "../src/app";
33

44
describe("GET /contact", () => {
5-
const request = supertest(app);
6-
75
it("should return 200 OK", (done) => {
8-
request.get("/contact")
6+
request(app).get("/contact")
97
.expect(200, done);
108
});
119
});

test/home.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import * as supertest from "supertest";
2-
import * as app from "../src/server";
1+
import * as request from "supertest";
2+
import * as app from "../src/app";
33

44
describe("GET /", () => {
5-
const request = supertest(app);
6-
75
it("should return 200 OK", (done) => {
8-
request.get("/")
6+
request(app).get("/")
97
.expect(200, done);
108
});
119
});

test/user.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import * as supertest from "supertest";
2-
import * as app from "../src/server";
3-
4-
const request = supertest(app);
1+
import * as request from "supertest";
2+
import * as app from "../src/app";
53

64
describe("GET /login", () => {
7-
it("should return 200 OK", (done) => {
8-
request.get("/login")
9-
.expect(200, done);
5+
it("should return 200 OK", () => {
6+
return request(app).get("/login")
7+
.expect(200);
108
});
119
});
1210

1311
describe("GET /signup", () => {
14-
it("should return 200 OK", (done) => {
15-
request.get("/signup")
16-
.expect(200, done);
12+
it("should return 200 OK", () => {
13+
return request(app).get("/signup")
14+
.expect(200);
1715
});
1816
});

0 commit comments

Comments
 (0)