insomnia/app/backend/__tests__/database.test.js

85 lines
2.9 KiB
JavaScript
Raw Normal View History

import * as db from '../database';
2016-11-10 01:15:27 +00:00
import * as models from '../models';
2016-11-10 02:40:53 +00:00
import {PREVIEW_MODE_SOURCE} from '../../common/constants';
2016-09-04 21:32:36 +00:00
2016-09-08 06:54:35 +00:00
function loadFixture (name) {
const fixtures = require(`../__fixtures__/${name}`);
const promises = [];
for (const type of Object.keys(fixtures)) {
for (const doc of fixtures[type]) {
promises.push(db.insert(Object.assign({}, doc, {type})));
}
}
}
2016-09-04 21:32:36 +00:00
describe('requestCreate()', () => {
beforeEach(() => {
2016-11-10 01:15:27 +00:00
return db.initDB(models.types(), {inMemoryOnly: true}, true);
2016-09-04 21:32:36 +00:00
});
it('creates a valid request', async () => {
2016-09-04 21:32:36 +00:00
const now = Date.now();
const patch = {
name: 'My Request',
parentId: 'wrk_123'
};
2016-11-10 01:15:27 +00:00
const r = await models.request.create(patch);
expect(Object.keys(r).length).toBe(15);
2016-09-04 21:32:36 +00:00
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
expect(r._id).toMatch(/^req_[a-zA-Z0-9]{32}$/);
expect(r.created).toBeGreaterThanOrEqual(now);
expect(r.modified).toBeGreaterThanOrEqual(now);
expect(r.type).toBe('Request');
expect(r.name).toBe('My Request');
expect(r.url).toBe('');
expect(r.method).toBe('GET');
expect(r.body).toBe('');
expect(r.parameters).toEqual([]);
expect(r.headers).toEqual([]);
expect(r.authentication).toEqual({});
expect(r.metaSortKey).toBeLessThanOrEqual(-1 * now);
expect(r.metaPreviewMode).toEqual(PREVIEW_MODE_SOURCE);
expect(r.parentId).toBe('wrk_123');
2016-09-04 21:32:36 +00:00
});
it('throws when missing parentID', () => {
2016-11-10 01:15:27 +00:00
const fn = () => models.request.create({name: 'My Request'});
2016-09-04 21:32:36 +00:00
expect(fn).toThrowError('New Requests missing `parentId`');
});
});
2016-09-08 06:54:35 +00:00
describe('requestGroupDuplicate()', () => {
beforeEach(async () => {
2016-11-10 01:15:27 +00:00
await db.initDB(models.types(), {inMemoryOnly: true}, true);
await loadFixture('nestedfolders');
2016-09-08 06:54:35 +00:00
});
it('duplicates a RequestGroup', async () => {
2016-11-10 01:15:27 +00:00
const requestGroup = await models.requestGroup.getById('fld_1');
expect(requestGroup.name).toBe('Fld 1');
2016-09-08 06:54:35 +00:00
2016-11-10 01:15:27 +00:00
const newRequestGroup = await models.requestGroup.duplicate(requestGroup);
expect(newRequestGroup._id).not.toBe(requestGroup._id);
expect(newRequestGroup.name).toBe('Fld 1 (Copy)');
2016-09-08 06:54:35 +00:00
2016-11-10 01:15:27 +00:00
const allRequests = await models.request.all();
const allRequestGroups = await models.requestGroup.all();
const childRequests = await models.request.findByParentId(requestGroup._id);
const childRequestGroups = await models.requestGroup.findByParentId(requestGroup._id);
const newChildRequests = await models.request.findByParentId(newRequestGroup._id);
const newChildRequestGroups = await models.requestGroup.findByParentId(newRequestGroup._id);
// This asserting is pretty garbage but it at least checks
// to see that the recursion worked (for the most part)
expect(allRequests.length).toBe(8);
expect(allRequestGroups.length).toBe(5);
2016-09-08 06:54:35 +00:00
expect(childRequests.length).toBe(2);
expect(childRequestGroups.length).toBe(1);
2016-09-08 06:54:35 +00:00
expect(newChildRequests.length).toBe(2);
expect(newChildRequestGroups.length).toBe(1);
2016-09-08 06:54:35 +00:00
})
});