@stanlemon/server-with-auth 0.3.36 → 0.3.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app.js +1 -7
- package/app.test.js +53 -0
- package/package.json +2 -2
- package/src/createAppServer.js +0 -4
- package/src/index.js +1 -0
- package/src/utilities/testUtils.js +45 -0
package/app.js
CHANGED
|
@@ -29,7 +29,7 @@ const schemas = createSchemas({
|
|
|
29
29
|
email: Joi.string().email().required().label("Email"),
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
const app = createAppServer({
|
|
32
|
+
export const app = createAppServer({
|
|
33
33
|
port: 3003,
|
|
34
34
|
secure: ["/api/"],
|
|
35
35
|
schemas,
|
|
@@ -43,12 +43,6 @@ app.get(
|
|
|
43
43
|
handler(() => ({ hello: "world" }))
|
|
44
44
|
);
|
|
45
45
|
|
|
46
|
-
// Insecure endpoint
|
|
47
|
-
app.get(
|
|
48
|
-
"/hello/:name",
|
|
49
|
-
handler(({ name = "world" }) => ({ hello: name }))
|
|
50
|
-
);
|
|
51
|
-
|
|
52
46
|
// Secure endpoint
|
|
53
47
|
app.get(
|
|
54
48
|
"/api/users",
|
package/app.test.js
ADDED
|
@@ -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
|
+
});
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stanlemon/server-with-auth",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.37",
|
|
4
4
|
"description": "A basic express web server setup with authentication baked in.",
|
|
5
5
|
"author": "Stan Lemon <stanlemon@users.noreply.github.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"engines": {
|
|
8
|
-
"node": ">=22.
|
|
8
|
+
"node": ">=22.14.0"
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
11
|
"main": "./src/index.js",
|
package/src/createAppServer.js
CHANGED
|
@@ -58,10 +58,6 @@ export default function createAppServer(options) {
|
|
|
58
58
|
|
|
59
59
|
const app = createBaseAppServer({ port, webpack, start });
|
|
60
60
|
|
|
61
|
-
if (process.env.NODE_ENV === "test") {
|
|
62
|
-
return app;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
61
|
if (!process.env.COOKIE_SECRET) {
|
|
66
62
|
console.warn("You need to specify a cookie secret!");
|
|
67
63
|
}
|
package/src/index.js
CHANGED
|
@@ -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
|
+
}
|