Skip to content

Commit affa845

Browse files
committed
Add e2e test for server-with-auth
1 parent d4925fc commit affa845

File tree

6 files changed

+124
-18
lines changed

6 files changed

+124
-18
lines changed

apps/template/app.test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
*/
44
import request from "supertest";
55
import { app, db } from "./app.js";
6+
import { test__signupAndLogin } from "@stanlemon/server-with-auth";
7+
8+
async function signupAndLogin() {
9+
const { token } = await test__signupAndLogin(
10+
app,
11+
"test" + Math.random(),
12+
"p@$$w0rd!",
13+
{ name: "Test User", email: "test@test.com" }
14+
);
15+
return token;
16+
}
617

718
describe("/app", () => {
819
afterEach(() => {
@@ -11,40 +22,46 @@ describe("/app", () => {
1122
});
1223

1324
it("lists items", async () => {
25+
const token = await signupAndLogin();
1426
const response = await request(app)
1527
.get("/api/items")
16-
.set("Accept", "application/json");
28+
.set("Accept", "application/json")
29+
.set("Authorization", "Bearer " + token);
1730

18-
expect(response.headers["content-type"]).toMatch(/json/);
1931
expect(response.status).toEqual(200);
32+
expect(response.headers["content-type"]).toMatch(/json/);
2033
expect(response.body).toEqual([]);
2134
});
2235

2336
it("add item", async () => {
37+
const token = await signupAndLogin();
2438
const response = await request(app)
2539
.post("/api/items")
2640
.set("Accept", "application/json")
27-
.send({ item: "hello world" });
41+
.send({ item: "hello world" })
42+
.set("Authorization", "Bearer " + token);
2843

29-
expect(response.headers["content-type"]).toMatch(/json/);
3044
expect(response.status).toEqual(200);
45+
expect(response.headers["content-type"]).toMatch(/json/);
3146
expect(response.body).toMatchObject([{ item: "hello world" }]);
3247
});
3348

3449
it("delete item", async () => {
50+
const token = await signupAndLogin();
3551
const response1 = await request(app)
3652
.post("/api/items")
3753
.set("Accept", "application/json")
54+
.set("Authorization", "Bearer " + token)
3855
.send({ item: "hello world" });
39-
4056
const items = response1.body;
4157

4258
const response2 = await request(app)
4359
.delete(`/api/items/${items[0].id}`)
44-
.set("Accept", "application/json");
60+
.set("Accept", "application/json")
61+
.set("Authorization", "Bearer " + token);
4562

46-
expect(response2.headers["content-type"]).toMatch(/json/);
4763
expect(response2.status).toEqual(200);
64+
expect(response2.headers["content-type"]).toMatch(/json/);
4865
expect(response2.body).toMatchObject([]);
4966
});
5067
});

packages/server-with-auth/app.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const schemas = createSchemas({
2929
email: Joi.string().email().required().label("Email"),
3030
});
3131

32-
const app = createAppServer({
32+
export const app = createAppServer({
3333
port: 3003,
3434
secure: ["/api/"],
3535
schemas,
@@ -43,12 +43,6 @@ app.get(
4343
handler(() => ({ hello: "world" }))
4444
);
4545

46-
// Insecure endpoint
47-
app.get(
48-
"/hello/:name",
49-
handler(({ name = "world" }) => ({ hello: name }))
50-
);
51-
5246
// Secure endpoint
5347
app.get(
5448
"/api/users",

packages/server-with-auth/app.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
import request from "supertest";
5+
import { app } from "./app.js";
6+
import { signupAndLogin } from "./src/utilities/testUtils.js";
7+
8+
const username = "test" + Math.random();
9+
const password = "p@$$w0rd!";
10+
11+
describe("/app", () => {
12+
it("Insecure endpoint", async () => {
13+
// Insecure endpoint
14+
const response = await request(app)
15+
.get("/")
16+
.set("Accept", "application/json");
17+
18+
expect(response.headers["content-type"]).toMatch(/json/);
19+
expect(response.status).toEqual(200);
20+
expect(response.body).toEqual({ hello: "world" });
21+
});
22+
23+
it("Secure endpoint with no auth", async () => {
24+
const response = await request(app)
25+
.get("/api/users")
26+
.set("Accept", "application/json");
27+
28+
expect(response.status).toEqual(401);
29+
});
30+
31+
it("Secure endpoint with auth", async () => {
32+
const session = await signupAndLogin(app, username, password, {
33+
email: "test@test.com",
34+
fullName: "Test User",
35+
});
36+
37+
const token = session.token;
38+
39+
const response = await request(app)
40+
.get("/api/users")
41+
.set("Accept", "application/json")
42+
.set("Authorization", "Bearer " + token);
43+
44+
expect(response.status).toEqual(200);
45+
expect(response.headers["content-type"]).toMatch(/json/);
46+
expect(response.body.users[0]).toEqual(
47+
expect.objectContaining({
48+
id: session.id,
49+
username,
50+
})
51+
);
52+
});
53+
});

packages/server-with-auth/src/createAppServer.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ export default function createAppServer(options) {
5858

5959
const app = createBaseAppServer({ port, webpack, start });
6060

61-
if (process.env.NODE_ENV === "test") {
62-
return app;
63-
}
64-
6561
if (!process.env.COOKIE_SECRET) {
6662
console.warn("You need to specify a cookie secret!");
6763
}

packages/server-with-auth/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export {
2020
createInMemoryLowDb,
2121
createJsonFileLowDb,
2222
} from "./data/lowdb-user-dao.js";
23+
export { signupAndLogin as test__signupAndLogin } from "./utilities/testUtils.js";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import request from "supertest";
2+
import { v4 as uuidv4 } from "uuid";
3+
4+
/**
5+
* Utility function to create a user and login for testing purposes.
6+
* @param {Express.Application} app express application server
7+
* @param {string} username username to sign up and login with
8+
* @param {string} password password to sign up and login with
9+
* @returns {Promise<{ id: string, token: string, username: string }>} user session information
10+
*/
11+
export async function signupAndLogin(
12+
app,
13+
username = "test" + uuidv4(),
14+
password = "p@$$w0rd!",
15+
extra = {}
16+
) {
17+
const signup = await request(app)
18+
.post("/auth/signup")
19+
.set("Content-Type", "application/json")
20+
.set("Accept", "application/json")
21+
.send({
22+
username,
23+
password,
24+
...extra,
25+
})
26+
.expect(200);
27+
28+
const session = await request(app)
29+
.post("/auth/login")
30+
.set("Content-Type", "application/json")
31+
.set("Accept", "application/json")
32+
.send({
33+
username,
34+
password,
35+
})
36+
.expect(200);
37+
38+
expect(signup.body.user.id).toEqual(session.body.user.id);
39+
40+
return {
41+
id: session.body.user.id,
42+
token: session.body.token,
43+
username,
44+
};
45+
}

0 commit comments

Comments
 (0)