From e0db9a897836f983b4d8247bf71a557519633bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 4 May 2023 09:47:58 -0400 Subject: [PATCH] test: fix assert.reject calls fix tests --- src/api/posts.js | 3 +++ src/api/topics.js | 6 ++++++ test/groups.js | 25 ++++++++++++++++++++----- test/posts.js | 9 ++++----- test/topics.js | 40 +++++++++++++++++++++++++++++----------- 5 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/api/posts.js b/src/api/posts.js index 06639255a1..cb2621ec81 100644 --- a/src/api/posts.js +++ b/src/api/posts.js @@ -388,6 +388,9 @@ postsAPI.deleteDiff = async (caller, { pid, timestamp }) => { }; postsAPI.getReplies = async (caller, { pid }) => { + if (!utils.isNumber(pid)) { + throw new Error('[[error:invalid-data]]'); + } const { uid } = caller; const canRead = await privileges.posts.can('topics:read', pid, caller.uid); if (!canRead) { diff --git a/src/api/topics.js b/src/api/topics.js index 9b8b030d96..7a6cabf966 100644 --- a/src/api/topics.js +++ b/src/api/topics.js @@ -279,11 +279,17 @@ topicsAPI.markRead = async (caller, { tid }) => { }; topicsAPI.markUnread = async (caller, { tid }) => { + if (!tid || caller.uid <= 0) { + throw new Error('[[error:invalid-data]]'); + } await topics.markUnread(tid, caller.uid); topics.pushUnreadCount(caller.uid); }; topicsAPI.bump = async (caller, { tid }) => { + if (!tid) { + throw new Error('[[error:invalid-tid]]'); + } const isAdminOrMod = await privileges.topics.isAdminOrMod(tid, caller.uid); if (!isAdminOrMod) { throw new Error('[[error:no-privileges]]'); diff --git a/test/groups.js b/test/groups.js index 06a31dd7c5..bfc6965c9e 100644 --- a/test/groups.js +++ b/test/groups.js @@ -737,7 +737,10 @@ describe('Groups', () => { const uid1 = await User.create({ username: utils.generateUUID().slice(0, 8) }); const uid2 = await User.create({ username: utils.generateUUID().slice(0, 8) }); - await assert.rejects(apiGroups.join({ uid: uid1 }, { slug: 'test', uid: uid2 }), { message: '[[error:not-allowed]]' }); + await assert.rejects( + apiGroups.join({ uid: uid1 }, { slug: 'test', uid: uid2 }), + { message: '[[error:not-allowed]]' } + ); }); it('should allow admins to join private groups', async () => { @@ -923,7 +926,10 @@ describe('Groups', () => { }); it('should error if not owner or admin', async () => { - await assert.rejects(apiGroups.accept({ uid: 0 }, { slug: 'privatecanjoin', uid: testUid }), { message: '[[error:no-privileges]]' }); + await assert.rejects( + apiGroups.accept({ uid: 0 }, { slug: 'privatecanjoin', uid: testUid }), + { message: '[[error:no-privileges]]' } + ); }); it('should accept membership of user', async () => { @@ -950,7 +956,10 @@ describe('Groups', () => { }); it('should error if user is not invited', async () => { - await assert.rejects(apiGroups.acceptInvite({ uid: adminUid }, { slug: 'privatecanjoin', uid: adminUid }), { message: '[[error:not-invited]]' }); + await assert.rejects( + apiGroups.acceptInvite({ uid: adminUid }, { slug: 'privatecanjoin', uid: adminUid }), + { message: '[[error:not-invited]]' } + ); }); it('should accept invite', async () => { @@ -982,7 +991,10 @@ describe('Groups', () => { }); it('should fail to kick user with invalid data', async () => { - await assert.rejects(apiGroups.leave({ uid: adminUid }, { slug: 'privatecanjoin', uid: 8721632 }), { message: '[[error:group-not-member]]' }); + await assert.rejects( + apiGroups.leave({ uid: adminUid }, { slug: 'privatecanjoin', uid: 8721632 }), + { message: '[[error:group-not-member]]' } + ); }); it('should kick user from group', async () => { @@ -1194,7 +1206,10 @@ describe('Groups', () => { }); it('should error if user is not member', async () => { - await assert.rejects(apiGroups.leave({ uid: adminUid }, { uid: 3, slug: 'newgroup' }), { message: '[[error:group-not-member]]' }); + await assert.rejects( + apiGroups.leave({ uid: adminUid }, { uid: 3, slug: 'newgroup' }), + { message: '[[error:group-not-member]]' } + ); }); it('should fail if trying to remove someone else from group', async () => { diff --git a/test/posts.js b/test/posts.js index bec4590adb..68aeae7338 100644 --- a/test/posts.js +++ b/test/posts.js @@ -605,11 +605,10 @@ describe('Post\'s', () => { it('should not delete first diff of a post', async () => { const timestamps = await posts.diffs.list(replyPid); - await assert.rejects(async () => { - await posts.diffs.delete(replyPid, timestamps[0], voterUid); - }, { - message: '[[error:invalid-data]]', - }); + await assert.rejects( + posts.diffs.delete(replyPid, timestamps[0], voterUid), + { message: '[[error:invalid-data]]' } + ); }); it('should delete a post diff', async () => { diff --git a/test/topics.js b/test/topics.js index b1f3145ede..8fd9b77cb5 100644 --- a/test/topics.js +++ b/test/topics.js @@ -287,33 +287,36 @@ describe('Topic\'s', () => { }); it('should error if pid is not a number', async () => { - assert.rejects(apiPosts.getReplies({ uid: 0 }, { pid: 'abc' }, '[[error:invalid-data]]')); + await assert.rejects( + apiPosts.getReplies({ uid: 0 }, { pid: 'abc' }), + { message: '[[error:invalid-data]]' } + ); }); it('should fail to create new reply with invalid user id', (done) => { topics.reply({ uid: null, content: 'test post', tid: newTopic.tid }, (err) => { - assert.equal(err.message, '[[error:no-privileges]]'); + assert.strictEqual(err.message, '[[error:no-privileges]]'); done(); }); }); it('should fail to create new reply with empty content', (done) => { topics.reply({ uid: topic.userId, content: '', tid: newTopic.tid }, (err) => { - assert.ok(err); + assert.strictEqual(err.message, '[[error:content-too-short, 8]]'); done(); }); }); it('should fail to create new reply with invalid topic id', (done) => { topics.reply({ uid: null, content: 'test post', tid: 99 }, (err) => { - assert.equal(err.message, '[[error:no-topic]]'); + assert.strictEqual(err.message, '[[error:no-topic]]'); done(); }); }); it('should fail to create new reply with invalid toPid', (done) => { topics.reply({ uid: topic.userId, content: 'test post', tid: newTopic.tid, toPid: '"onmouseover=alert(1);//' }, (err) => { - assert.equal(err.message, '[[error:invalid-pid]]'); + assert.strictEqual(err.message, '[[error:invalid-pid]]'); done(); }); }); @@ -1471,17 +1474,23 @@ describe('Topic\'s', () => { }); it('should fail with invalid data', async () => { - assert.rejects(apiTopics.markUnread({ uid: adminUid }, null), '[[error:invalid-data]]'); + await assert.rejects( + apiTopics.markUnread({ uid: adminUid }, { tid: null }), + { message: '[[error:invalid-data]]' } + ); }); it('should fail if topic does not exist', async () => { - assert.rejects(apiTopics.markUnread({ uid: adminUid }, { tid: 1231082 }), '[[error:no-topic]]'); + await assert.rejects( + apiTopics.markUnread({ uid: adminUid }, { tid: 1231082 }), + { message: '[[error:no-topic]]' } + ); }); it('should mark topic unread', async () => { await apiTopics.markUnread({ uid: adminUid }, { tid }); const hasRead = await topics.hasReadTopic(tid, adminUid); - assert.equal(hasRead, false); + assert.strictEqual(hasRead, false); }); it('should fail with invalid data', (done) => { @@ -1556,15 +1565,24 @@ describe('Topic\'s', () => { }); it('should fail with invalid data', async () => { - assert.rejects(apiTopics.bump({ uid: adminUid }, null, '[[error:invalid-tid]]')); + await assert.rejects( + apiTopics.bump({ uid: adminUid }, { tid: null }), + { message: '[[error:invalid-tid]]' } + ); }); it('should fail with invalid data', async () => { - assert.rejects(apiTopics.bump({ uid: 0 }, { tid: [tid] }, '[[error:no-privileges]]')); + await assert.rejects( + apiTopics.bump({ uid: 0 }, { tid: [tid] }), + { message: '[[error:no-privileges]]' } + ); }); it('should fail if user is not admin', async () => { - assert.rejects(apiTopics.bump({ uid: uid }, { tid }, '[[error:no-privileges]]')); + await assert.rejects( + apiTopics.bump({ uid: uid }, { tid }), + { message: '[[error:no-privileges]]' } + ); }); it('should mark topic unread for everyone', async () => {