diff --git a/test/api/unit/top-level/dataexport.test.js b/test/api/unit/top-level/dataexport.test.js new file mode 100644 index 0000000000..3f6892ee9d --- /dev/null +++ b/test/api/unit/top-level/dataexport.test.js @@ -0,0 +1,79 @@ +import dataexport from '../../../../website/server/controllers/top-level/dataexport'; + +import * as Tasks from '../../../../website/server/models/task'; +import * as inboxLib from '../../../../website/server/libs/inbox'; + +describe('xml export', async () => { + let exported; + + const user = { + toJSON () { + return { + newMessages: { + '283171a5-422c-4991-bc78-95b1b5b51629': { + name: 'The Language Hackers', + value: true, + }, + '283171a6-422c-4991-bc78-95b1b5b51629': { + name: 'The Bug Hackers', + value: false, + }, + }, + inbox: {}, + pinnedItems: [], + unpinnedItems: [], + }; + }, + }; + + const response = { + locals: { user }, + set () {}, + status: () => ({ + send: data => { + exported = data; + }, + }), + }; + + beforeEach(() => { + const tasks = [{ + toJSON: () => ({ a: 'b', type: 'c' }), + }]; + const messages = [{ flags: { content: 'message' } }]; + + sinon.stub(Tasks.Task, 'find').returns({ exec: async () => tasks }); + sinon.stub(inboxLib, 'getUserInbox').resolves(messages); + }); + + afterEach(() => { + sinon.restore(); + }); + + it('maps the newMessages field to have id as a value in a list.', async () => { + await dataexport.exportUserDataXml.handler({}, response); + expect(exported).to.equal(` + + 283171a5-422c-4991-bc78-95b1b5b51629 + The Language Hackers + true + + + 283171a6-422c-4991-bc78-95b1b5b51629 + The Bug Hackers + false + + + + content + + + + + b + c + + +`); + }); +}); diff --git a/website/server/controllers/top-level/dataexport.js b/website/server/controllers/top-level/dataexport.js index 2a17f4e5e4..f1bc60ea28 100644 --- a/website/server/controllers/top-level/dataexport.js +++ b/website/server/controllers/top-level/dataexport.js @@ -112,14 +112,15 @@ async function _getUserDataForExport (user, xmlMode = false) { // object maps cant be parsed userData.inbox.messages = _(userData.inbox.messages) .map(m => { - const flags = Object.keys(m.flags); - m.flags = flags; + m.flags = Object.keys(m.flags); return m; }) .value(); - // _id gets parsed as an bytearray => which gets casted to a chararray => "weird chars" + userData.newMessages = _.map(userData.newMessages, (msg, id) => ({ id, ...msg })); + + // _id gets parsed as a bytearray => which gets cast to a chararray => "weird chars" userData.unpinnedItems = userData.unpinnedItems.map(i => ({ path: i.path, type: i.type, diff --git a/website/server/models/user/schema.js b/website/server/models/user/schema.js index f671479dc9..abc6426a56 100644 --- a/website/server/models/user/schema.js +++ b/website/server/models/user/schema.js @@ -1,4 +1,4 @@ -import mongoose from 'mongoose'; +import { Schema } from 'mongoose'; import validator from 'validator'; import shared from '../../../common'; import { // eslint-disable-line import/no-cycle @@ -10,8 +10,6 @@ import { schema as TagSchema } from '../tag'; import { schema as UserNotificationSchema } from '../userNotification'; import { schema as WebhookSchema } from '../webhook'; -const { Schema } = mongoose; - const RESTRICTED_EMAIL_DOMAINS = Object.freeze(['habitica.com', 'habitrpg.com']); // User schema definition