@stanlemon/server-with-auth 0.1.3 → 0.1.6
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/jest.config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "@stanlemon/webdev/jest.config.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stanlemon/server-with-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
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",
|
|
@@ -14,11 +14,13 @@
|
|
|
14
14
|
"start": "NODE_ENV=development nodemon --ignore db.json ./app.js",
|
|
15
15
|
"lint": "eslint --ext js,jsx,ts,tsx ./",
|
|
16
16
|
"lint:format": "eslint --fix --ext js,jsx,ts,tsx ./",
|
|
17
|
-
"test": "
|
|
18
|
-
"test:
|
|
17
|
+
"test": "jest --detectOpenHandles",
|
|
18
|
+
"test:coverage": "jest --detectOpenHandles --coverage",
|
|
19
|
+
"test:watch": "jest --detectOpenHandles --watch"
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"@stanlemon/server": "*",
|
|
23
|
+
"@stanlemon/webdev": "*",
|
|
22
24
|
"bcryptjs": "^2.4.3",
|
|
23
25
|
"jsonwebtoken": "^8.5.1",
|
|
24
26
|
"lowdb": "^3.0.0",
|
|
@@ -29,7 +31,8 @@
|
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"@stanlemon/eslint-config": "*",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
+
"@types/supertest": "^2.0.12",
|
|
35
|
+
"nodemon": "^2.0.16",
|
|
36
|
+
"supertest": "^6.2.3"
|
|
34
37
|
}
|
|
35
38
|
}
|
package/src/createAppServer.js
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
} from "@stanlemon/server";
|
|
6
6
|
import passport from "passport";
|
|
7
7
|
import { Strategy as JwtStrategy, ExtractJwt } from "passport-jwt";
|
|
8
|
+
import shortid from "shortid";
|
|
8
9
|
import defaultUserSchema from "./schema/user.js";
|
|
9
10
|
import checkAuth from "./checkAuth.js";
|
|
10
11
|
import auth from "./routes/auth.js";
|
|
@@ -39,13 +40,17 @@ export default function createAppServer(options) {
|
|
|
39
40
|
updateUser,
|
|
40
41
|
} = { ...DEFAULTS, ...options };
|
|
41
42
|
|
|
43
|
+
const app = createBaseAppServer({ port, webpack, start });
|
|
44
|
+
|
|
45
|
+
if (process.env.NODE_ENV === "test") {
|
|
46
|
+
return app;
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
if (!process.env.JWT_SECRET) {
|
|
43
50
|
console.warn("You need to specify a secret.");
|
|
44
51
|
}
|
|
45
52
|
|
|
46
|
-
const secret = process.env.JWT_SECRET ||
|
|
47
|
-
|
|
48
|
-
const app = createBaseAppServer({ port, webpack, start });
|
|
53
|
+
const secret = process.env.JWT_SECRET || shortid.generate();
|
|
49
54
|
|
|
50
55
|
passport.use(
|
|
51
56
|
"jwt",
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { Low, JSONFile } from "lowdb";
|
|
1
|
+
import { Low, JSONFile, Memory } from "lowdb";
|
|
2
2
|
import { v4 as uuidv4 } from "uuid";
|
|
3
3
|
import shortid from "shortid";
|
|
4
4
|
import bcrypt from "bcryptjs";
|
|
5
5
|
|
|
6
|
+
const DEFAULT_ADAPTER =
|
|
7
|
+
process.env.NODE_ENV === "test" ? new Memory() : new JSONFile("./db.json");
|
|
8
|
+
|
|
6
9
|
export default class SimpleUsersDao {
|
|
7
|
-
constructor(seeds = [], adapter =
|
|
10
|
+
constructor(seeds = [], adapter = DEFAULT_ADAPTER) {
|
|
8
11
|
this.db = new Low(adapter);
|
|
9
12
|
|
|
10
13
|
this.db.read().then(() => {
|
|
@@ -16,6 +19,18 @@ export default class SimpleUsersDao {
|
|
|
16
19
|
});
|
|
17
20
|
}
|
|
18
21
|
|
|
22
|
+
getDb() {
|
|
23
|
+
return this.db;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
generateId() {
|
|
27
|
+
return uuidv4();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
generateVerificationToken() {
|
|
31
|
+
return shortid.generate();
|
|
32
|
+
}
|
|
33
|
+
|
|
19
34
|
getUserById = (userId) => {
|
|
20
35
|
return this.db.data.users
|
|
21
36
|
.filter((user) => {
|
|
@@ -42,7 +57,7 @@ export default class SimpleUsersDao {
|
|
|
42
57
|
|
|
43
58
|
const user = this.getUserByUsername(username);
|
|
44
59
|
|
|
45
|
-
if (!bcrypt.compareSync(password, user.password)) {
|
|
60
|
+
if (!user || !bcrypt.compareSync(password, user.password)) {
|
|
46
61
|
return false;
|
|
47
62
|
}
|
|
48
63
|
|
|
@@ -66,8 +81,8 @@ export default class SimpleUsersDao {
|
|
|
66
81
|
const data = {
|
|
67
82
|
...user,
|
|
68
83
|
password: bcrypt.hashSync(user.password, 10),
|
|
69
|
-
id:
|
|
70
|
-
verification_token:
|
|
84
|
+
id: this.generateId(),
|
|
85
|
+
verification_token: this.generateVerificationToken(),
|
|
71
86
|
created_at: now,
|
|
72
87
|
last_updated: now,
|
|
73
88
|
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { Memory } from "lowdb";
|
|
5
|
+
import SimpleUsersDao from "./simple-users-dao.js";
|
|
6
|
+
|
|
7
|
+
describe("simple-users-dao", () => {
|
|
8
|
+
// This is a user that we will reuse in our tests
|
|
9
|
+
const data = {
|
|
10
|
+
username: "test",
|
|
11
|
+
password: "password",
|
|
12
|
+
};
|
|
13
|
+
// This will be our user database
|
|
14
|
+
let users;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
// Before each test reset our users database
|
|
18
|
+
users = new SimpleUsersDao([], new Memory());
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("creates a user", async () => {
|
|
22
|
+
let user = await users.createUser(data);
|
|
23
|
+
|
|
24
|
+
expect(user.username).toEqual(data.username);
|
|
25
|
+
// The value should be encrypted now
|
|
26
|
+
expect(user.password).not.toEqual(data.password);
|
|
27
|
+
// These fields are added by the create process
|
|
28
|
+
expect(user.verification_token).not.toBeUndefined();
|
|
29
|
+
expect(user.created_at).not.toBeUndefined();
|
|
30
|
+
expect(user.last_updated).not.toBeUndefined();
|
|
31
|
+
|
|
32
|
+
const refresh = users.getUserByUsername(data.username);
|
|
33
|
+
|
|
34
|
+
// If we retrieve the user by username, it matches the object we got when we created it
|
|
35
|
+
expect(refresh).toMatchObject(user);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("creates a user with an existing username errors", async () => {
|
|
39
|
+
let hasError = false;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
// Create the user
|
|
43
|
+
await users.createUser(data);
|
|
44
|
+
// Attempt to create the user again, this will fail
|
|
45
|
+
await users.createUser(data);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
hasError = err;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
expect(hasError).not.toBe(false);
|
|
51
|
+
expect(hasError.message).toEqual("This username is already taken.");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("retrieve user by username and password", async () => {
|
|
55
|
+
const user1 = await users.createUser(data);
|
|
56
|
+
|
|
57
|
+
const user2 = users.getUserByUsernameAndPassword(
|
|
58
|
+
data.username,
|
|
59
|
+
data.password
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
expect(user1).toMatchObject(user2);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("retrieve user by username and wrong password fails", async () => {
|
|
66
|
+
await users.createUser(data);
|
|
67
|
+
|
|
68
|
+
const user2 = users.getUserByUsernameAndPassword(
|
|
69
|
+
data.username,
|
|
70
|
+
"wrong password"
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
expect(user2).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("retrieve user by username and undefined password fails", async () => {
|
|
77
|
+
await users.createUser(data);
|
|
78
|
+
|
|
79
|
+
const user2 = users.getUserByUsernameAndPassword(data.username, undefined);
|
|
80
|
+
|
|
81
|
+
expect(user2).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("retrieve user by username that does not exist fails", async () => {
|
|
85
|
+
const user = users.getUserByUsernameAndPassword("notarealuser", "password");
|
|
86
|
+
|
|
87
|
+
expect(user).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("retrieve user by username that is undefined fails", async () => {
|
|
91
|
+
const user = users.getUserByUsernameAndPassword(undefined, "password");
|
|
92
|
+
|
|
93
|
+
expect(user).toBe(false);
|
|
94
|
+
});
|
|
95
|
+
});
|
package/src/routes/auth.test.js
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
1
4
|
import request from "supertest";
|
|
2
5
|
import { Memory } from "lowdb";
|
|
3
6
|
import createAppServer from "../createAppServer";
|
|
4
7
|
import SimpleUsersDao from "../data/simple-users-dao.js";
|
|
5
8
|
|
|
9
|
+
// This suppresses a warning we don't need in tests
|
|
10
|
+
process.env.JWT_SECRET = "SECRET";
|
|
11
|
+
|
|
6
12
|
let users = new SimpleUsersDao([], new Memory());
|
|
7
13
|
|
|
14
|
+
// We want to explicitly test functionality we disable during testing
|
|
15
|
+
process.env.NODE_ENV = "override";
|
|
8
16
|
const app = createAppServer({ ...users, start: false });
|
|
17
|
+
process.env.NODE_ENV = "test";
|
|
9
18
|
|
|
10
19
|
describe("/auth", () => {
|
|
11
20
|
let userId;
|