@stanlemon/server-with-auth 0.1.1 → 0.1.4

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/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@stanlemon/server-with-auth",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
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": ">=16.0"
8
+ "node": ">=16.13.0"
9
9
  },
10
10
  "type": "module",
11
11
  "main": "./src/index.js",
@@ -24,7 +24,7 @@
24
24
  "lowdb": "^3.0.0",
25
25
  "passport": "^0.5.2",
26
26
  "passport-jwt": "^4.0.0",
27
- "shortid": "^2.2.8",
27
+ "shortid": "^2.2.16",
28
28
  "uuid": "^8.3.2"
29
29
  },
30
30
  "devDependencies": {
@@ -35,9 +35,14 @@ export default class SimpleUsersDao {
35
35
  };
36
36
 
37
37
  getUserByUsernameAndPassword = (username, password) => {
38
+ // Treat this like a failed login.
39
+ if (!username || !password) {
40
+ return false;
41
+ }
42
+
38
43
  const user = this.getUserByUsername(username);
39
44
 
40
- if (!bcrypt.compareSync(password, user.password)) {
45
+ if (!user || !bcrypt.compareSync(password, user.password)) {
41
46
  return false;
42
47
  }
43
48
 
@@ -0,0 +1,92 @@
1
+ import { Memory } from "lowdb";
2
+ import SimpleUsersDao from "./simple-users-dao.js";
3
+
4
+ describe("simple-users-dao", () => {
5
+ // This is a user that we will reuse in our tests
6
+ const data = {
7
+ username: "test",
8
+ password: "password",
9
+ };
10
+ // This will be our user database
11
+ let users;
12
+
13
+ beforeEach(() => {
14
+ // Before each test reset our users database
15
+ users = new SimpleUsersDao([], new Memory());
16
+ });
17
+
18
+ it("creates a user", async () => {
19
+ let user = await users.createUser(data);
20
+
21
+ expect(user.username).toEqual(data.username);
22
+ // The value should be encrypted now
23
+ expect(user.password).not.toEqual(data.password);
24
+ // These fields are added by the create process
25
+ expect(user.verification_token).not.toBeUndefined();
26
+ expect(user.created_at).not.toBeUndefined();
27
+ expect(user.last_updated).not.toBeUndefined();
28
+
29
+ const refresh = users.getUserByUsername(data.username);
30
+
31
+ // If we retrieve the user by username, it matches the object we got when we created it
32
+ expect(refresh).toMatchObject(user);
33
+ });
34
+
35
+ it("creates a user with an existing username errors", async () => {
36
+ let hasError = false;
37
+
38
+ try {
39
+ // Create the user
40
+ await users.createUser(data);
41
+ // Attempt to create the user again, this will fail
42
+ await users.createUser(data);
43
+ } catch (err) {
44
+ hasError = err;
45
+ }
46
+
47
+ expect(hasError).not.toBe(false);
48
+ expect(hasError.message).toEqual("This username is already taken.");
49
+ });
50
+
51
+ it("retrieve user by username and password", async () => {
52
+ const user1 = await users.createUser(data);
53
+
54
+ const user2 = users.getUserByUsernameAndPassword(
55
+ data.username,
56
+ data.password
57
+ );
58
+
59
+ expect(user1).toMatchObject(user2);
60
+ });
61
+
62
+ it("retrieve user by username and wrong password fails", async () => {
63
+ await users.createUser(data);
64
+
65
+ const user2 = users.getUserByUsernameAndPassword(
66
+ data.username,
67
+ "wrong password"
68
+ );
69
+
70
+ expect(user2).toBe(false);
71
+ });
72
+
73
+ it("retrieve user by username and undefined password fails", async () => {
74
+ await users.createUser(data);
75
+
76
+ const user2 = users.getUserByUsernameAndPassword(data.username, undefined);
77
+
78
+ expect(user2).toBe(false);
79
+ });
80
+
81
+ it("retrieve user by username that does not exist fails", async () => {
82
+ const user = users.getUserByUsernameAndPassword("notarealuser", "password");
83
+
84
+ expect(user).toBe(false);
85
+ });
86
+
87
+ it("retrieve user by username that is undefined fails", async () => {
88
+ const user = users.getUserByUsernameAndPassword(undefined, "password");
89
+
90
+ expect(user).toBe(false);
91
+ });
92
+ });