mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
fix: return full inbox on user-v3-api (#12146)
* fix inbox on user-v3-api + test * add @benkelaar suggestions
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import range from 'lodash/range';
|
||||
import {
|
||||
generateUser,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
@@ -46,4 +47,23 @@ describe('GET /user', () => {
|
||||
expect(returnedUser.apiToken).to.not.exist;
|
||||
expect(returnedUser.secret).to.not.exist;
|
||||
});
|
||||
|
||||
it('returns the full inbox', async () => {
|
||||
const otherUser = await generateUser();
|
||||
const amountOfMessages = 12;
|
||||
|
||||
const allMessagesPromise = range(amountOfMessages)
|
||||
.map(i => otherUser.post('/members/send-private-message', {
|
||||
toUserId: user.id,
|
||||
message: `Message Num: ${i}`,
|
||||
}));
|
||||
|
||||
await Promise.all(allMessagesPromise);
|
||||
|
||||
const returnedUser = await user.get('/user');
|
||||
|
||||
expect(returnedUser._id).to.equal(user._id);
|
||||
expect(returnedUser.inbox).to.exist;
|
||||
expect(Object.keys(returnedUser.inbox.messages)).to.have.a.lengthOf(amountOfMessages);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -63,7 +63,7 @@ describe('GET /inbox/conversations', () => {
|
||||
expect(messages[4].text).to.equal('first');
|
||||
});
|
||||
|
||||
it('returns four messages when using page-query ', async () => {
|
||||
it('returns five messages when using page-query ', async () => {
|
||||
const promises = [];
|
||||
|
||||
for (let i = 0; i < 10; i += 1) {
|
||||
|
||||
@@ -34,7 +34,10 @@ export async function sentMessage (sender, receiver, message, translate) {
|
||||
const PM_PER_PAGE = 10;
|
||||
|
||||
const getUserInboxDefaultOptions = {
|
||||
asArray: true, page: 0, conversation: null, mapProps: false,
|
||||
asArray: true,
|
||||
page: undefined,
|
||||
conversation: null,
|
||||
mapProps: false,
|
||||
};
|
||||
|
||||
export async function getUserInbox (user, optionParams = getUserInboxDefaultOptions) {
|
||||
|
||||
@@ -497,7 +497,9 @@ schema.methods.toJSONWithInbox = async function userToJSONWithInbox () {
|
||||
const toJSON = user.toJSON();
|
||||
|
||||
if (toJSON.inbox) {
|
||||
toJSON.inbox.messages = await inboxLib.getUserInbox(user, { asArray: false });
|
||||
toJSON.inbox.messages = await inboxLib.getUserInbox(user, {
|
||||
asArray: false,
|
||||
});
|
||||
}
|
||||
|
||||
return toJSON;
|
||||
|
||||
Reference in New Issue
Block a user