const Note = require('../../models/Note') const User = require('../../models/User') const testUserName = 'jestTestUserNote' const password = 'Beans1234!!!' const secondPassword = 'Rice1234!!!' let newUserId = null let masterKey = null let testNoteId = 0 let testNoteId2 = 0 const searchWord1 = 'beans' const searchWord2 = 'RICE' const updatedNoteText = 'Some Note Text for Testing more '+searchWord2+' is nice' beforeAll(() => { // Find and Delete Previous Test user, log in, get key return User.getByUserName(testUserName) .then((user) => { return User.deleteUser(user?.id, password) }) .then((results) => { return User.register(testUserName, password) }) .then(({ token, userId }) => { newUserId = userId return User.getMasterKey(userId, password) }) .then((newMasterKey) => { masterKey = newMasterKey return true }) .catch(((error) => { console.log(error) })) }) test('Create Note', () => { const noteTitle = 'Test Note' const noteText = 'Some Note Text for Testing' return Note.create(newUserId, noteTitle, noteText, masterKey) .then((noteId) => { testNoteId = noteId expect(noteId).toBeGreaterThan(0) }) }) test('Create Another Note', () => { const noteTitle = 'Test Note2' const noteText = 'Some Note Text for Testing more '+searchWord1 return Note.create(newUserId, noteTitle, noteText, masterKey) .then((noteId) => { testNoteId2 = noteId expect(noteId).toBeGreaterThan(0) }) }) test('Update a note', () => { return Note.update(newUserId, testNoteId, updatedNoteText, 'title', 0, 0, 0, 'hash', masterKey) .then((results) => { expect(results.changedRows).toEqual(1) }) }) test('Decrypt a note', () => { return Note.get(newUserId, testNoteId, masterKey) .then((noteData) => { expect(noteData.text).toMatch(updatedNoteText) }) }) test('Update note search index', () => { return Note.reindex(newUserId, masterKey) .then((results) => { expect(results).toBe(true) }) }) test('Search Encrypted Index', () => { const searchString = `${searchWord1} ${searchWord2}` return Note.encryptedIndexSearch(newUserId, searchString, null, masterKey) .then(({ids}) => { // Make sure beans is in one note and rice is in updated text expect(ids.length).toEqual(2) }) }) test('Search Encrypted Index no results', () => { return Note.encryptedIndexSearch(newUserId, 'zzz', null, masterKey) .then(({ids}) => { // Make sure beans is in one note and rice is in updated text expect(ids.length).toEqual(0) }) }) afterAll(done => { // Close Database const db = require('../../config/database') db.end() done() })