mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
tests(api): Add sync method to api objects
This commit is contained in:
@@ -58,10 +58,10 @@ describe('POST /groups/:id', () => {
|
||||
description: 'New group description',
|
||||
});
|
||||
|
||||
let group = await user.get(`/groups/${usersGroup._id}`);
|
||||
await usersGroup.sync();
|
||||
|
||||
expect(group.name).to.eql('New Group Title');
|
||||
expect(group.description).to.eql('New group description');
|
||||
expect(usersGroup.name).to.eql('New Group Title');
|
||||
expect(usersGroup.description).to.eql('New group description');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../helpers/api-integration/v2';
|
||||
import { each, find } from 'lodash';
|
||||
import { each } from 'lodash';
|
||||
|
||||
describe('POST /groups/:id/join', () => {
|
||||
context('user is already a member of the group', () => {
|
||||
@@ -30,10 +30,9 @@ describe('POST /groups/:id/join', () => {
|
||||
it(`allows user to join a ${groupType}`, async () => {
|
||||
await invitee.post(`/groups/${group._id}/join`);
|
||||
|
||||
let members = (await invitee.get(`/groups/${group._id}`)).members;
|
||||
let userInGroup = find(members, '_id', invitee._id);
|
||||
await group.sync();
|
||||
|
||||
expect(userInGroup).to.exist;
|
||||
expect(group.members).to.include(invitee._id);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -79,10 +78,9 @@ describe('POST /groups/:id/join', () => {
|
||||
it('allows user to join a public guild', async () => {
|
||||
await user.post(`/groups/${group._id}/join`);
|
||||
|
||||
let members = (await user.get(`/groups/${group._id}`)).members;
|
||||
let userInGroup = find(members, '_id', user._id);
|
||||
await group.sync();
|
||||
|
||||
expect(userInGroup).to.exist;
|
||||
expect(group.members).to.include(user._id);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -105,7 +103,9 @@ describe('POST /groups/:id/join', () => {
|
||||
it('makes the joining user the leader', async () => {
|
||||
await user.post(`/groups/${group._id}/join`);
|
||||
|
||||
await expect(user.get(`/groups/${group._id}`)).to.eventually.have.deep.property('leader._id', user._id);
|
||||
await group.sync();
|
||||
|
||||
await expect(group.leader).to.eql(user._id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,6 @@ import {
|
||||
checkExistence,
|
||||
createAndPopulateGroup,
|
||||
} from '../../../helpers/api-integration/v2';
|
||||
import { find } from 'lodash';
|
||||
|
||||
describe('POST /groups/:id/leave', () => {
|
||||
context('user is not member of the group', () => {
|
||||
@@ -29,10 +28,9 @@ describe('POST /groups/:id/leave', () => {
|
||||
it('leaves the group', async () => {
|
||||
await user.post(`/groups/${group._id}/leave`);
|
||||
|
||||
let members = (await user.get(`/groups/${group._id}`)).members;
|
||||
let userInGroup = find(members, '_id', user._id);
|
||||
await group.sync();
|
||||
|
||||
expect(userInGroup).to.not.be.ok;
|
||||
expect(group.members).to.not.include(user._id);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -14,15 +14,17 @@ describe('PUT /user', () => {
|
||||
|
||||
context('Allowed Operations', () => {
|
||||
it('updates the user', async () => {
|
||||
let updatedUser = await user.put('/user', {
|
||||
await user.put('/user', {
|
||||
'profile.name': 'Frodo',
|
||||
'preferences.costume': true,
|
||||
'stats.hp': 14,
|
||||
});
|
||||
|
||||
expect(updatedUser.profile.name).to.eql('Frodo');
|
||||
expect(updatedUser.preferences.costume).to.eql(true);
|
||||
expect(updatedUser.stats.hp).to.eql(14);
|
||||
await user.sync();
|
||||
|
||||
expect(user.profile.name).to.eql('Frodo');
|
||||
expect(user.preferences.costume).to.eql(true);
|
||||
expect(user.stats.hp).to.eql(14);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
/* eslint-disable no-use-before-define */
|
||||
|
||||
import { requester } from './requester';
|
||||
import { updateDocument as updateDocumentInMongo } from './mongo';
|
||||
import {
|
||||
getDocument as getDocumentFromMongo,
|
||||
updateDocument as updateDocumentInMongo,
|
||||
} from './mongo';
|
||||
import {
|
||||
assign,
|
||||
each,
|
||||
@@ -25,6 +28,14 @@ class ApiObject {
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
async sync () {
|
||||
let updatedDoc = await getDocumentFromMongo(this._docType, this);
|
||||
|
||||
assign(this, updatedDoc);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export class ApiUser extends ApiObject {
|
||||
|
||||
@@ -66,6 +66,20 @@ export async function updateDocument (collectionName, doc, update) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getDocument (collectionName, doc) {
|
||||
let db = await connectToMongo();
|
||||
|
||||
let collection = db.collection(collectionName);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
collection.findOne({ _id: doc._id }, (lookupErr, found) => {
|
||||
if (lookupErr) throw new Error(`Error looking up ${collectionName}: ${lookupErr}`);
|
||||
db.close();
|
||||
resolve(found);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function connectToMongo () {
|
||||
return new Promise((resolve, reject) => {
|
||||
mongo.connect(DB_URI, (err, db) => {
|
||||
|
||||
Reference in New Issue
Block a user