diff --git a/test/helpers/api-integration.helper.js b/test/helpers/api-integration.helper.js index 644576e3fc..838c5f622c 100644 --- a/test/helpers/api-integration.helper.js +++ b/test/helpers/api-integration.helper.js @@ -4,7 +4,6 @@ import { each, times, } from 'lodash'; -import { MongoClient as mongo } from 'mongodb'; import { v4 as generateUUID } from 'uuid'; import { ApiUser, ApiGroup } from './api-integration/api-classes'; @@ -17,26 +16,7 @@ export { requester }; export { translate } from './api-integration/translate'; -// 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); - }); - }); - }); -} +export { checkExistence, resetHabiticaDB } from './api-integration/mongo'; // Creates a new user and returns it // If you need the user to have specific requirements, @@ -152,33 +132,3 @@ export function createAndPopulateGroup (settings = {}) { }).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(); - }); - }); - }); - }); -} diff --git a/test/helpers/api-integration/mongo.js b/test/helpers/api-integration/mongo.js new file mode 100644 index 0000000000..64560229e2 --- /dev/null +++ b/test/helpers/api-integration/mongo.js @@ -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(); + }); + }); + }); + }); +}