|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2018 "Neo4j, Inc" |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +/* global jest, describe, afterEach, test, expect */ |
| 22 | +import { version } from 'project-root/package.json' |
| 23 | +import { createEpicMiddleware } from 'redux-observable' |
| 24 | +import { createBus } from 'suber' |
| 25 | +import { |
| 26 | + executeSystemCommand, |
| 27 | + executeSingleCommand, |
| 28 | + handleSingleCommandEpic |
| 29 | +} from './commandsDuck' |
| 30 | + |
| 31 | +jest.mock('services/bolt/bolt', () => { |
| 32 | + const orig = require.requireActual('services/bolt/bolt') |
| 33 | + return { |
| 34 | + ...orig, |
| 35 | + routedWriteTransaction: jest.fn(() => [ |
| 36 | + 'id', |
| 37 | + Promise.resolve({ records: [] }) |
| 38 | + ]) |
| 39 | + } |
| 40 | +}) |
| 41 | +const bolt = require.requireMock('services/bolt/bolt') |
| 42 | + |
| 43 | +jest.mock('shared/modules/settings/settingsDuck', () => { |
| 44 | + const orig = require.requireActual('shared/modules/settings/settingsDuck') |
| 45 | + return { |
| 46 | + ...orig, |
| 47 | + getCmdChar: () => ':', |
| 48 | + shouldUseCypherThread: () => false |
| 49 | + } |
| 50 | +}) |
| 51 | + |
| 52 | +jest.mock('shared/modules/params/paramsDuck', () => { |
| 53 | + const orig = require.requireActual('shared/modules/params/paramsDuck') |
| 54 | + return { |
| 55 | + ...orig, |
| 56 | + getParams: () => ({}) |
| 57 | + } |
| 58 | +}) |
| 59 | + |
| 60 | +jest.mock('shared/modules/dbMeta/dbMetaDuck', () => { |
| 61 | + const orig = require.requireActual('shared/modules/dbMeta/dbMetaDuck') |
| 62 | + return { |
| 63 | + ...orig, |
| 64 | + getVersion: () => '3.5.0' // support for tx metadata |
| 65 | + } |
| 66 | +}) |
| 67 | + |
| 68 | +describe('tx metadata with cypher', () => { |
| 69 | + afterEach(() => { |
| 70 | + bolt.routedWriteTransaction.mockReset() |
| 71 | + }) |
| 72 | + |
| 73 | + it('it adds tx metadata for user entered cypher queries', done => { |
| 74 | + // Given |
| 75 | + const bus = createBus() |
| 76 | + bus.applyReduxMiddleware(createEpicMiddleware(handleSingleCommandEpic)) |
| 77 | + const $$responseChannel = 'test-channel' |
| 78 | + const action = executeSingleCommand('RETURN 1', 'id', 'rqid') |
| 79 | + action.$$responseChannel = $$responseChannel |
| 80 | + |
| 81 | + bus.send(action.type, action) |
| 82 | + flushPromises().then(() => { |
| 83 | + expect(bolt.routedWriteTransaction).toHaveBeenCalledTimes(1) |
| 84 | + expect(bolt.routedWriteTransaction).toHaveBeenCalledWith( |
| 85 | + 'RETURN 1', |
| 86 | + {}, |
| 87 | + expect.objectContaining({ |
| 88 | + txMetadata: { app: `neo4j-browser_v${version}`, type: 'user-direct' } |
| 89 | + }) |
| 90 | + ) |
| 91 | + done() |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + it('it adds tx metadata for system cypher queries', done => { |
| 96 | + // Given |
| 97 | + const bus = createBus() |
| 98 | + bus.applyReduxMiddleware(createEpicMiddleware(handleSingleCommandEpic)) |
| 99 | + const $$responseChannel = 'test-channel2' |
| 100 | + const action = executeSystemCommand('RETURN 1') |
| 101 | + action.$$responseChannel = $$responseChannel |
| 102 | + |
| 103 | + bus.send(action.type, action) |
| 104 | + flushPromises().then(() => { |
| 105 | + expect(bolt.routedWriteTransaction).toHaveBeenCalledTimes(1) |
| 106 | + expect(bolt.routedWriteTransaction).toHaveBeenCalledWith( |
| 107 | + 'RETURN 1', |
| 108 | + {}, |
| 109 | + expect.objectContaining({ |
| 110 | + txMetadata: { app: `neo4j-browser_v${version}`, type: 'system' } |
| 111 | + }) |
| 112 | + ) |
| 113 | + done() |
| 114 | + }) |
| 115 | + }) |
| 116 | +}) |
| 117 | + |
| 118 | +function flushPromises () { |
| 119 | + return new Promise(resolve => setImmediate(resolve)) |
| 120 | +} |
0 commit comments