mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
tests: Extract mongo functions into separate file
This commit is contained in:
@@ -4,7 +4,6 @@ import {
|
|||||||
each,
|
each,
|
||||||
times,
|
times,
|
||||||
} from 'lodash';
|
} from 'lodash';
|
||||||
import { MongoClient as mongo } from 'mongodb';
|
|
||||||
import { v4 as generateUUID } from 'uuid';
|
import { v4 as generateUUID } from 'uuid';
|
||||||
|
|
||||||
import { ApiUser, ApiGroup } from './api-integration/api-classes';
|
import { ApiUser, ApiGroup } from './api-integration/api-classes';
|
||||||
@@ -17,26 +16,7 @@ export { requester };
|
|||||||
|
|
||||||
export { translate } from './api-integration/translate';
|
export { translate } from './api-integration/translate';
|
||||||
|
|
||||||
// Useful for checking things that have been deleted,
|
export { checkExistence, resetHabiticaDB } from './api-integration/mongo';
|
||||||
// but you no longer have access to,
|
|
||||||
// like private parties or users
|
|
||||||
export function checkExistence (collectionName, id) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
mongo.connect('mongodb://localhost/habitrpg_test', (connectionError, db) => {
|
|
||||||
if (connectionError) return reject(connectionError);
|
|
||||||
let collection = db.collection(collectionName);
|
|
||||||
|
|
||||||
collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => {
|
|
||||||
if (findError) return reject(findError);
|
|
||||||
|
|
||||||
let exists = docs.length > 0;
|
|
||||||
|
|
||||||
db.close();
|
|
||||||
resolve(exists);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a new user and returns it
|
// Creates a new user and returns it
|
||||||
// If you need the user to have specific requirements,
|
// If you need the user to have specific requirements,
|
||||||
@@ -152,33 +132,3 @@ export function createAndPopulateGroup (settings = {}) {
|
|||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specifically helpful for the GET /groups tests,
|
|
||||||
// resets the db to an empty state and creates a tavern document
|
|
||||||
export function resetHabiticaDB () {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
|
||||||
if (err) return reject(err);
|
|
||||||
|
|
||||||
db.dropDatabase((dbErr) => {
|
|
||||||
if (dbErr) return reject(dbErr);
|
|
||||||
let groups = db.collection('groups');
|
|
||||||
|
|
||||||
groups.insertOne({
|
|
||||||
_id: 'habitrpg',
|
|
||||||
chat: [],
|
|
||||||
leader: '9',
|
|
||||||
name: 'HabitRPG',
|
|
||||||
type: 'guild',
|
|
||||||
privacy: 'public',
|
|
||||||
members: [],
|
|
||||||
}, (insertErr) => {
|
|
||||||
if (insertErr) return reject(insertErr);
|
|
||||||
|
|
||||||
db.close();
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
52
test/helpers/api-integration/mongo.js
Normal file
52
test/helpers/api-integration/mongo.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { MongoClient as mongo } from 'mongodb';
|
||||||
|
|
||||||
|
// Useful for checking things that have been deleted,
|
||||||
|
// but you no longer have access to,
|
||||||
|
// like private parties or users
|
||||||
|
export function checkExistence (collectionName, id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
mongo.connect('mongodb://localhost/habitrpg_test', (connectionError, db) => {
|
||||||
|
if (connectionError) return reject(connectionError);
|
||||||
|
let collection = db.collection(collectionName);
|
||||||
|
|
||||||
|
collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => {
|
||||||
|
if (findError) return reject(findError);
|
||||||
|
|
||||||
|
let exists = docs.length > 0;
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
resolve(exists);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Specifically helpful for the GET /groups tests,
|
||||||
|
// resets the db to an empty state and creates a tavern document
|
||||||
|
export function resetHabiticaDB () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
|
||||||
|
db.dropDatabase((dbErr) => {
|
||||||
|
if (dbErr) return reject(dbErr);
|
||||||
|
let groups = db.collection('groups');
|
||||||
|
|
||||||
|
groups.insertOne({
|
||||||
|
_id: 'habitrpg',
|
||||||
|
chat: [],
|
||||||
|
leader: '9',
|
||||||
|
name: 'HabitRPG',
|
||||||
|
type: 'guild',
|
||||||
|
privacy: 'public',
|
||||||
|
members: [],
|
||||||
|
}, (insertErr) => {
|
||||||
|
if (insertErr) return reject(insertErr);
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user