Skip to content

Commit

Permalink
refactor: 560/565 of all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Jun 2, 2023
1 parent 22c9ea9 commit 10ccbdb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
62 changes: 36 additions & 26 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,27 +549,7 @@ export async function createUser({

// * At this point, we can finally trust this data is valid and not malicious
try {
const infoDb = db.collection<InternalInfo>('info');
const pagesDb = db.collection<InternalPage>('pages');

await usersDb.insertOne(newUser);
const promisedUpdate = infoDb.updateOne({}, { $inc: { users: 1 } });

if (data.type === 'blogger') {
await Promise.all([
infoDb.updateOne({}, { $inc: { blogs: 1, pages: 1 } }),
pagesDb.insertOne({
__provenance,
_id: new ObjectId(),
blog_id: newUser._id,
createdAt: Date.now(),
totalViews: 0,
...defaultHomePage
})
]);
}

await promisedUpdate;
} catch (error) {
/* istanbul ignore else */
if (
Expand All @@ -584,6 +564,25 @@ export async function createUser({
throw error;
}

const infoDb = db.collection<InternalInfo>('info');
const promises: Promise<unknown>[] = [infoDb.updateOne({}, { $inc: { users: 1 } })];

// TODO: this should be implemented as a transaction
if (data.type === 'blogger') {
promises.push(
infoDb.updateOne({}, { $inc: { blogs: 1, pages: 1 } }),
db.collection<InternalPage>('pages').insertOne({
__provenance,
_id: new ObjectId(),
blog_id: newUser._id,
createdAt: Date.now(),
totalViews: 0,
...defaultHomePage
})
);
}

await Promise.all(promises);
return toPublicUser(newUser);
}

Expand Down Expand Up @@ -649,12 +648,7 @@ export async function createPage({

// * At this point, we can finally trust this data is valid and not malicious
try {
const infoDb = db.collection<InternalInfo>('info');

await Promise.all([
infoDb.updateOne({}, { $inc: { pages: 1 } }),
pagesDb.insertOne(newPage)
]);
await pagesDb.insertOne(newPage);
} catch (error) {
/* istanbul ignore else */
if (error instanceof MongoServerError && error.code == 11_000) {
Expand All @@ -665,6 +659,7 @@ export async function createPage({
throw error;
}

await db.collection<InternalInfo>('info').updateOne({}, { $inc: { pages: 1 } });
return toPublicPage(newPage);
}

Expand Down Expand Up @@ -731,6 +726,21 @@ export async function updateUser({
throw new ValidationError(ErrorMessage.UnknownField(restKeys[0]));
}

// ? Key update requires salt update and vice-versa
if (!!key !== !!salt) {
const { USER_SALT_LENGTH: maxSaltLength, USER_KEY_LENGTH: maxKeyLength } =
getEnv();

throw new ValidationError(
ErrorMessage.InvalidStringLength(
!!key ? 'salt' : 'key',
!!key ? maxSaltLength : maxKeyLength,
null,
'hexadecimal'
)
);
}

const db = await getDb({ name: 'app' });
const usersDb = db.collection<InternalUser>('users');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default withMiddleware(
{
descriptor: metadata.descriptor,
options: {
allowedContentTypes: { PUT: ['application/json', 'none'], DELETE: 'none' },
allowedContentTypes: ['application/json', 'none'],
allowedMethods: ['PUT', 'DELETE'],
apiVersion: '1'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default withMiddleware(
{
descriptor: metadata.descriptor,
options: {
allowedContentTypes: { POST: ['application/json', 'none'], GET: 'none' },
allowedContentTypes: ['application/json', 'none'],
allowedMethods: ['GET', 'POST'],
apiVersion: '1'
}
Expand Down
12 changes: 12 additions & 0 deletions test/backend/unit-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ describe('::createUser', () => {
{
email: '[email protected]',
salt: '0'.repeat(saltLength),
// * Not hexadecimal
key: 'x'.repeat(keyLength)
},
ErrorMessage.InvalidStringLength('key', keyLength, null, 'hexadecimal')
Expand Down Expand Up @@ -1417,9 +1418,20 @@ describe('::updateUser', () => {
ErrorMessage.InvalidStringLength('key', keyLength, null, 'hexadecimal')
],
[
// * Not hexadecimal
{ key: 'x'.repeat(keyLength) },
ErrorMessage.InvalidStringLength('key', keyLength, null, 'hexadecimal')
],
// * Key must always be paired with salt and vice-versa
[
{ key: 'a'.repeat(keyLength) },
ErrorMessage.InvalidStringLength('salt', saltLength, null, 'hexadecimal')
],
// * Key must always be paired with salt and vice-versa
[
{ salt: 'a'.repeat(saltLength) },
ErrorMessage.InvalidStringLength('key', keyLength, null, 'hexadecimal')
],
[{ banned: 'true' as unknown as boolean }, ErrorMessage.UnknownField('banned')],
[{ banned: null as unknown as boolean }, ErrorMessage.UnknownField('banned')],
[{ data: 1 } as PatchUser, ErrorMessage.UnknownField('data')],
Expand Down

0 comments on commit 10ccbdb

Please sign in to comment.