const Attachment = require('../../models/Attachment') const User = require('../../models/User') const testUserName = 'jestTestUserAttachment' const password = 'Beans19934!!!' let newUserId = null let masterKey = null let newPushKey = null 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('Test Generate Push Key', () => { return Attachment.generatePushKey(newUserId) .then( (pushKey) => { newPushKey = pushKey return Attachment.generatePushKey(newUserId) }) .then( (pushKey) => { // expect a long, defined pushkey expect(pushKey).toBeDefined() expect(pushKey?.length).toBeGreaterThan(20) expect(pushKey).toMatch(newPushKey) }) }) test('Test get Push Key Bookmarklet', () => { return Attachment.getPushkeyBookmarklet(newUserId) .then(( bookmarklet => { // Expect a bookmarklet containting URL encoded pushkey from above const keyCheck = bookmarklet.includes(encodeURIComponent(newPushKey)) expect(bookmarklet).toBeDefined() expect(keyCheck).toBe(true) })) }) test('Test Push URL', () => { let url = 'https://www.solidscribe.com' return Attachment.pushUrl(newPushKey, url) .then(( results => { return Attachment.textSearch(newUserId, 'scribe') })) .then((results) => { expect(results.length == 1).toBe(true) }) }) test('Test Delete Push Key', () => { return Attachment.deletePushKey(newUserId) .then(( results => { // Expect a true bool expect(results).toBe(true) })) }) afterAll(done => { // Close Database const db = require('../../config/database') db.end() done() })