-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.test.js
205 lines (158 loc) · 6.75 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* eslint-env jest */
const bcrypt = require('bcrypt')
const { transactionPerTest } = require('objection-transactional-tests')
const { Model } = require('objection')
const Knex = require('knex')
const objectionPassword = require('./index')
// Set up knex
const knex = Knex({
client: 'sqlite3',
connection: {
filename: ':memory:'
},
useNullAsDefault: true
})
// Bind knex instance to objection
Model.knex(knex)
const ObjectionPassword = objectionPassword() // Mixin with default options.
// Objection model using default options
class SampleModel extends ObjectionPassword(Model) {
static get tableName () {
return 'sample_model'
}
}
beforeAll(async () => {
await knex.schema.createTable('sample_model', (table) => {
table.increments()
table.string('name')
table.string('password')
})
transactionPerTest()
})
afterAll(async () => {
await knex.schema.dropTable('sample_model')
knex.destroy()
})
describe('$beforeInsert', () => {
it('does not store the password in plaintext', async () => {
const password = 'hunter1'
const instance = await SampleModel.query().insert({ name: 'Dominic', password })
expect(instance.password).not.toEqual(password)
})
it('stores a verifiable password', async () => {
const password = 'hunter1'
const instance = await SampleModel.query().insert({ name: 'Dominic', password })
expect(await instance.verifyPassword(password)).toBe(true)
})
it('does not allow an empty password', async () => {
const insertQuery = SampleModel.query().insert({ name: 'Dominic', password: '' })
expect(insertQuery).rejects.toThrowError()
})
it('throws an error when attempting to hash a bcrypt hash', async () => {
const insertQuery = SampleModel.query().insert({
name: 'Dominic',
password: '$2a$12$sWSdI13BJ5ipPca/f8KTF.k4eFKsUtobfWdTBoQdj9g9I8JfLmZty'
})
expect(insertQuery).rejects.toThrowError()
})
})
describe('$beforeUpdate', () => {
it('does not store the password in plaintext after update', async () => {
const original = 'hunter1'
const updated = 'qwerty'
const instance = await SampleModel.query().insert({ name: 'Dominic', password: original })
await instance.$query().patch({ password: updated })
expect(instance.password).not.toEqual(original)
expect(instance.password).not.toEqual(updated)
})
it('creates new hash when updating password', async () => {
const original = 'hunter1'
const updated = 'qwerty'
const instance = await SampleModel.query().insert({ name: 'Dominic', password: original })
const bcryptSpy = jest.spyOn(bcrypt, 'hash')
await instance.$query().patch({ password: updated })
expect(bcryptSpy).toHaveBeenCalledTimes(1)
expect(await instance.verifyPassword(updated)).toBe(true)
expect(await instance.verifyPassword(original)).toBe(false)
})
it('ignores hashing password field when patching a record where password is not updated', async () => {
const bcryptSpy = jest.spyOn(bcrypt, 'hash')
const instance = await SampleModel.query().insert({ name: 'Dominic', password: 'hunter1' })
await instance.$query().patch({ name: 'Raphael' })
expect(bcryptSpy).toHaveBeenCalledTimes(1) // Once on creation (and 0 times on patch)
})
it('does not allow an empty password', async () => {
const instance = await SampleModel.query().insert({ name: 'Dominic', password: 'hunter1' })
const updateQuery = instance.$query().patch({ password: '' })
expect(updateQuery).rejects.toThrowError()
})
it('throws an error when attempting to hash a bcrypt hash', async () => {
const instance = await SampleModel.query().insert({ name: 'Dominic', password: 'hunter1' })
const updateQuery = instance.$query().patch({
password: '$2a$12$sWSdI13BJ5ipPca/f8KTF.k4eFKsUtobfWdTBoQdj9g9I8JfLmZty'
})
expect(updateQuery).rejects.toThrowError()
})
})
describe('options overrides', () => {
const generateCustomModel = (CustomizedMixin) => {
return class extends CustomizedMixin(Model) {
static get tableName () {
return 'sample_model'
}
}
}
it('can allow empty string password inserts', async () => {
const CustomizedMixin = objectionPassword({ allowEmptyPassword: true })
const CustomModel = generateCustomModel(CustomizedMixin)
const instance = await CustomModel.query().insert({ name: 'Dominic', password: '' })
expect(instance.password).toBe('')
})
it('can make passwords optional', async () => {
const CustomizedMixin = objectionPassword({ allowEmptyPassword: true })
const CustomModel = generateCustomModel(CustomizedMixin)
const instance = await CustomModel.query().insert({ name: 'Dominic' })
expect(instance.password).not.toBeDefined()
})
it('can allow updating a password to an empty string', async () => {
const CustomizedMixin = objectionPassword({ allowEmptyPassword: true })
const CustomModel = generateCustomModel(CustomizedMixin)
const instance = await CustomModel.query().insert({ name: 'Dominic', password: 'hunter1' })
await instance.$query().patch({ password: '' })
expect(instance.password).toBe('')
})
it('can allow unsetting a password (set to null)', async () => {
const CustomizedMixin = objectionPassword({ allowEmptyPassword: true })
const CustomModel = generateCustomModel(CustomizedMixin)
const instance = await CustomModel.query().insert({ name: 'Dominic', password: 'hunter1' })
await instance.$query().patch({ password: null })
expect(instance.password).toBe(null)
})
it('can override the default password field', async () => {
// Use the name field instead of the password field as the password used by the plugin
const CustomizedMixin = objectionPassword({ passwordField: 'name' })
const CustomModel = generateCustomModel(CustomizedMixin)
const name = 'Dominic'
const password = 'hunter1'
const instance = await CustomModel.query().insert({ name, password })
expect(await instance.verifyPassword(password)).toBe(false)
expect(await instance.verifyPassword(name)).toBe(true)
})
it('can set the number of bcrypt hashing rounds', async () => {
// Expect to be called with 13 instead of 12
const CustomizedMixin = objectionPassword({ rounds: 13 })
const CustomModel = generateCustomModel(CustomizedMixin)
const bcryptSpy = jest.spyOn(bcrypt, 'hash')
const password = 'hunter1'
await CustomModel.query().insert({ name: 'Dominic', password })
expect(bcryptSpy).toHaveBeenCalledWith(password, 13)
})
})
describe('isBcryptHash', () => {
it('returns true when given a bcrypt hash', async () => {
expect(SampleModel.isBcryptHash(await bcrypt.hash('hello world', 12))).toBe(true)
})
it('returns false when given a regular string', () => {
expect(SampleModel.isBcryptHash('hello world')).toBe(false)
})
})