-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New server models, more auth testing, more fixtures
- Loading branch information
Showing
9 changed files
with
296 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Base { | ||
constructor(fields, data) { | ||
if (!data) { | ||
return null; | ||
} | ||
return new Proxy(this, { | ||
get(target, property, receiver) { | ||
// if the property is in the schema, return it from the data object | ||
if (Object.hasOwn(fields, property)) { | ||
return data[property]; | ||
} | ||
// otherwise, dispatch it to the target with the proxy as this | ||
const { get, value } = | ||
Object.getOwnPropertyDescriptor( | ||
Object.getPrototypeOf(target), | ||
property, | ||
) ?? {}; | ||
// handle property accessor | ||
if (get) { | ||
return get.call(receiver); | ||
} | ||
// handle function | ||
if (value) { | ||
return value.bind(receiver); | ||
} | ||
// otherwise, return value directly off of target | ||
return target[property]; | ||
}, | ||
set(target, property, value, receiver) { | ||
// if the property is in the schema, set it in the data object | ||
if (Object.hasOwn(fields, property)) { | ||
data[property] = value; | ||
return true; | ||
} | ||
// otherwise, set on target with proxy as this | ||
const descriptor = | ||
Object.getOwnPropertyDescriptor( | ||
Object.getPrototypeOf(target), | ||
property, | ||
) ?? {}; | ||
const { set } = descriptor; | ||
if (set) { | ||
set.call(receiver, value); | ||
return true; | ||
} | ||
target[property] = value; | ||
return true; | ||
}, | ||
}); | ||
} | ||
} | ||
export default Base; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Prisma } from '@prisma/client'; | ||
import bcrypt from 'bcrypt'; | ||
import crypto from 'crypto'; | ||
|
||
import Base from './base.js'; | ||
|
||
class User extends Base { | ||
constructor(data) { | ||
super(Prisma.UserScalarFieldEnum, data); | ||
this.test = 'foo'; | ||
} | ||
|
||
set test2(newValue) { | ||
this._test2 = newValue; | ||
} | ||
|
||
get isActive() { | ||
return this.isApproved && this.isEmailVerified; | ||
} | ||
|
||
get isApproved() { | ||
return !!this.approvedAt; | ||
} | ||
|
||
get isEmailVerified() { | ||
return !!this.emailVerifiedAt; | ||
} | ||
|
||
generateEmailVerificationToken() { | ||
const buffer = crypto.randomBytes(3); | ||
this.emailVerificationToken = buffer.toString('hex').toUpperCase(); | ||
} | ||
|
||
async setPassword(password) { | ||
this.hashedPassword = await bcrypt.hash(password, 10); | ||
} | ||
|
||
async comparePassword(password) { | ||
return bcrypt.compare(password, this.hashedPassword); | ||
} | ||
} | ||
|
||
export default User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,20 @@ items: | |
firstName: Admin | ||
lastName: User | ||
email: [email protected] | ||
emailVerifiedAt: <%= new Date().toISOString() %> | ||
role: ADMIN | ||
hashedPassword: $2b$10$ICaCk3VVZUCtO9HySahquuQusQhEnRpXHdzxaceUUJPk0DTwN2e/W # test | ||
approvedAt: <%= new Date().toISOString() %> | ||
user2: | ||
firstName: Unverified | ||
lastName: Email | ||
email: [email protected] | ||
role: FIRST_RESPONDER | ||
hashedPassword: $2b$10$ICaCk3VVZUCtO9HySahquuQusQhEnRpXHdzxaceUUJPk0DTwN2e/W # test | ||
user3: | ||
firstName: Unapproved | ||
lastName: User | ||
email: [email protected] | ||
emailVerifiedAt: <%= new Date().toISOString() %> | ||
role: FIRST_RESPONDER | ||
hashedPassword: $2b$10$ICaCk3VVZUCtO9HySahquuQusQhEnRpXHdzxaceUUJPk0DTwN2e/W # test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { describe, it } from 'node:test'; | ||
import * as assert from 'node:assert'; | ||
|
||
import Base from '../../models/base.js'; | ||
|
||
class Test extends Base { | ||
constructor(data) { | ||
super({ property0: 'property0' }, data); | ||
this.property1 = 'value1'; | ||
} | ||
|
||
get property2() { | ||
return this.property0 + this.property1; | ||
} | ||
|
||
set property2(newValue) { | ||
const tokens = newValue.split(','); | ||
this.property0 = tokens[0]; | ||
this.property1 = tokens[1]; | ||
} | ||
|
||
function1() { | ||
return `${this.property0} accessed through function 1`; | ||
} | ||
} | ||
|
||
describe('Base', () => { | ||
describe('get', () => { | ||
it('proxies an underlying scalar field in the schema', () => { | ||
const test = new Test({ property0: 'value0' }); | ||
assert.deepStrictEqual(test.property0, 'value0'); | ||
}); | ||
|
||
it('gets a normal property', () => { | ||
const test = new Test({ property0: 'value0' }); | ||
assert.deepStrictEqual(test.property1, 'value1'); | ||
}); | ||
|
||
it('binds getters to the proxy so they can access proxied scalar fields', () => { | ||
const test = new Test({ property0: 'value0' }); | ||
assert.deepStrictEqual(test.property2, 'value0value1'); | ||
}); | ||
|
||
it('binds functions to the proxy so they can access proxied scalar fields', () => { | ||
const test = new Test({ property0: 'value0' }); | ||
assert.deepStrictEqual( | ||
test.function1(), | ||
'value0 accessed through function 1', | ||
); | ||
}); | ||
}); | ||
|
||
describe('set', () => { | ||
it('proxies to the underlying scalar field in the schema', () => { | ||
const data = { property0: 'value0' }; | ||
const test = new Test(data); | ||
test.property0 = 'newvalue0'; | ||
assert.deepStrictEqual(data.property0, 'newvalue0'); | ||
}); | ||
|
||
it('sets a normal property', () => { | ||
const test = new Test({ property0: 'value0' }); | ||
test.property1 = 'newvalue1'; | ||
assert.deepStrictEqual(test.property1, 'newvalue1'); | ||
}); | ||
|
||
it('binds setters to the proxy so they can set proxied scalar fields', () => { | ||
const data = { property0: 'value0' }; | ||
const test = new Test(data); | ||
test.property2 = 'newvalue0,newvalue1'; | ||
assert.deepStrictEqual(data.property0, 'newvalue0'); | ||
assert.deepStrictEqual(test.property1, 'newvalue1'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.