Matteo Scandolo | bd13aab | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
| 22 | const chai = require('chai'); |
| 23 | const expect = chai.expect; |
| 24 | const sinon = require('sinon'); |
| 25 | const sinonChai = require('sinon-chai'); |
| 26 | const mockery = require('mockery'); |
| 27 | chai.use(sinonChai); |
| 28 | const fakeredis = require('fakeredis'); |
| 29 | |
| 30 | const client = fakeredis.createClient('test-client'); |
| 31 | const publisher = fakeredis.createClient('test-client'); |
| 32 | |
| 33 | const socketSpy = sinon.spy(); |
| 34 | const mockSocket = { |
| 35 | get: () => { |
| 36 | return { |
| 37 | emit: socketSpy |
| 38 | } |
| 39 | } |
| 40 | }; |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 41 | |
| 42 | const mockRequest = { |
| 43 | get: () => { |
| 44 | return { |
| 45 | end: (fn) => { |
| 46 | fn(null, {body: [ |
| 47 | {name: 'Slice'}, |
| 48 | {name: 'Site'} |
| 49 | ]}); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 54 | const channelName = 'Site'; |
| 55 | |
| 56 | describe('The event system', () => { |
| 57 | |
| 58 | before((done) => { |
| 59 | |
| 60 | // Enable mockery to mock objects |
| 61 | mockery.enable({ |
| 62 | warnOnReplace: false, |
| 63 | warnOnUnregistered: false |
| 64 | }); |
| 65 | |
| 66 | // Stub the createClient method to *always* return the client created above |
| 67 | sinon.stub(fakeredis, 'createClient', () => client); |
| 68 | |
| 69 | // Override the redis module with our fakeredis instance |
| 70 | mockery.registerMock('redis', fakeredis); |
| 71 | |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 72 | // Override the superagent module with our mockRequest instance |
| 73 | mockery.registerMock('superagent', mockRequest); |
| 74 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 75 | // mock the socketIo client to have a spy |
| 76 | mockery.registerMock('./websocket.js', mockSocket); |
| 77 | |
| 78 | require('../src/controllers/redis.js'); |
| 79 | setTimeout(() => { |
| 80 | done(); |
| 81 | }, 1000); |
| 82 | }); |
| 83 | |
| 84 | after(() => { |
| 85 | mockery.disable(); |
| 86 | fakeredis.createClient.restore(); |
| 87 | }); |
| 88 | |
| 89 | // run after each test |
| 90 | beforeEach(() => { |
| 91 | client.unsubscribe(channelName); |
| 92 | client.subscribe(channelName); |
| 93 | publisher.flushdb(); |
Matteo Scandolo | 9f15c68 | 2017-11-20 15:49:55 -0800 | [diff] [blame] | 94 | socketSpy.reset(); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | it('should send a websocket event when it receive a redis event that is not JSON', (done) => { |
| 98 | publisher.publish(channelName, 'I am sending a message.'); |
| 99 | setTimeout(() => { |
| 100 | expect(socketSpy).to.have.been.called; |
Matteo Scandolo | 9f15c68 | 2017-11-20 15:49:55 -0800 | [diff] [blame] | 101 | expect(socketSpy).to.have.been.calledWith('update', { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 102 | model: channelName, |
| 103 | msg: 'I am sending a message.' |
| 104 | }); |
| 105 | done(); |
| 106 | }, 500) |
| 107 | }); |
| 108 | |
| 109 | it('should send a websocket event when it receive a redis event that is JSON', (done) => { |
| 110 | publisher.publish(channelName, JSON.stringify({msg: 'Json Message'})); |
| 111 | setTimeout(() => { |
| 112 | expect(socketSpy).to.have.been.called; |
Matteo Scandolo | 9f15c68 | 2017-11-20 15:49:55 -0800 | [diff] [blame] | 113 | expect(socketSpy).to.have.been.calledWith('update', { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 114 | model: channelName, |
| 115 | msg: {msg: 'Json Message'} |
| 116 | }); |
| 117 | done(); |
| 118 | }, 1000) |
| 119 | }); |
Matteo Scandolo | 9f15c68 | 2017-11-20 15:49:55 -0800 | [diff] [blame] | 120 | |
| 121 | it('should send a websocket event when an object has been removed', (done) => { |
| 122 | publisher.publish(channelName, JSON.stringify({msg: 'Deleted', deleted: true})); |
| 123 | setTimeout(() => { |
| 124 | expect(socketSpy).to.have.been.called; |
| 125 | expect(socketSpy).to.have.been.calledWith('remove', { |
| 126 | model: channelName, |
| 127 | msg: { |
| 128 | msg: 'Deleted', |
| 129 | deleted: true |
| 130 | }, |
| 131 | deleted: true |
| 132 | }); |
| 133 | |
| 134 | done(); |
| 135 | }, 1000) |
| 136 | }); |
| 137 | |
| 138 | it('should not send a websocket event if the channel is Diag', (done) => { |
| 139 | publisher.publish('Diag', JSON.stringify({msg: 'Json Message'})); |
| 140 | setTimeout(() => { |
| 141 | expect(socketSpy).not.to.have.been.called; |
| 142 | done(); |
| 143 | }, 1000) |
| 144 | }); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 145 | }); |
| 146 | })(); |