Commit 96723e83 authored by Robert Knight's avatar Robert Knight

Add missing tests for various error paths

I ignored one path that is only used in local development.
parent ca5b5393
...@@ -99,6 +99,7 @@ export class StreamerService { ...@@ -99,6 +99,7 @@ export class StreamerService {
// HTTP status code for HTTP -> WS upgrade requests. // HTTP status code for HTTP -> WS upgrade requests.
const websocketHost = new URL(settings.websocketUrl).hostname; const websocketHost = new URL(settings.websocketUrl).hostname;
if (['localhost', '127.0.0.1'].indexOf(websocketHost) !== -1) { if (['localhost', '127.0.0.1'].indexOf(websocketHost) !== -1) {
/* istanbul ignore next */
warnOnce( warnOnce(
'Check that your H service is configured to allow ' + 'Check that your H service is configured to allow ' +
'WebSocket connections from ' + 'WebSocket connections from ' +
...@@ -128,7 +129,7 @@ export class StreamerService { ...@@ -128,7 +129,7 @@ export class StreamerService {
); );
} }
} else { } else {
warnOnce('received unsupported notification', message.type); warnOnce('Received unsupported notification', message.type);
} }
}; };
......
...@@ -78,6 +78,7 @@ describe('StreamerService', () => { ...@@ -78,6 +78,7 @@ describe('StreamerService', () => {
let fakeGroups; let fakeGroups;
let fakeSession; let fakeSession;
let fakeSettings; let fakeSettings;
let fakeWarnOnce;
let activeStreamer; let activeStreamer;
function createDefaultStreamer() { function createDefaultStreamer() {
...@@ -92,9 +93,7 @@ describe('StreamerService', () => { ...@@ -92,9 +93,7 @@ describe('StreamerService', () => {
beforeEach(function () { beforeEach(function () {
fakeAuth = { fakeAuth = {
getAccessToken: function () { getAccessToken: sinon.stub().resolves('dummy-access-token'),
return Promise.resolve('dummy-access-token');
},
}; };
fakeStore = fakeReduxStore( fakeStore = fakeReduxStore(
...@@ -126,7 +125,10 @@ describe('StreamerService', () => { ...@@ -126,7 +125,10 @@ describe('StreamerService', () => {
websocketUrl: 'ws://example.com/ws', websocketUrl: 'ws://example.com/ws',
}; };
fakeWarnOnce = sinon.stub();
$imports.$mock({ $imports.$mock({
'../../shared/warn-once': fakeWarnOnce,
'../websocket': FakeSocket, '../websocket': FakeSocket,
}); });
}); });
...@@ -177,7 +179,15 @@ describe('StreamerService', () => { ...@@ -177,7 +179,15 @@ describe('StreamerService', () => {
}); });
}); });
describe('#connect()', function () { describe('#connect', function () {
beforeEach(() => {
sinon.stub(console, 'error');
});
afterEach(() => {
console.error.restore();
});
it('should create a websocket connection', function () { it('should create a websocket connection', function () {
createDefaultStreamer(); createDefaultStreamer();
return activeStreamer.connect().then(function () { return activeStreamer.connect().then(function () {
...@@ -207,9 +217,7 @@ describe('StreamerService', () => { ...@@ -207,9 +217,7 @@ describe('StreamerService', () => {
}); });
it('should not include credentials in the URL if the client has no access token', function () { it('should not include credentials in the URL if the client has no access token', function () {
fakeAuth.getAccessToken = function () { fakeAuth.getAccessToken.resolves(null);
return Promise.resolve(null);
};
createDefaultStreamer(); createDefaultStreamer();
return activeStreamer.connect().then(function () { return activeStreamer.connect().then(function () {
...@@ -231,6 +239,15 @@ describe('StreamerService', () => { ...@@ -231,6 +239,15 @@ describe('StreamerService', () => {
assert.ok(!fakeWebSocket.didClose); assert.ok(!fakeWebSocket.didClose);
}); });
}); });
it('throws an error if fetching the access token fails', async () => {
fakeAuth.getAccessToken.rejects(new Error('Getting token failed'));
createDefaultStreamer();
const connected = activeStreamer.connect();
await assert.rejects(connected, 'Getting token failed');
});
}); });
describe('#reconnect()', function () { describe('#reconnect()', function () {
...@@ -292,6 +309,44 @@ describe('StreamerService', () => { ...@@ -292,6 +309,44 @@ describe('StreamerService', () => {
}); });
}); });
it('logs an error if the connection fails', () => {
createDefaultStreamer();
activeStreamer.connect();
const event = new ErrorEvent('Something went wrong');
fakeWebSocket.emit('error', event);
assert.calledWith(
fakeWarnOnce,
'Error connecting to H push notification service:',
event
);
});
[null, false].forEach(message => {
it('ignores invalid messages', () => {
createDefaultStreamer();
activeStreamer.connect();
fakeWebSocket.notify(message);
});
});
it('ignores messages with an unknown type', () => {
createDefaultStreamer();
activeStreamer.connect();
fakeWebSocket.notify({
type: 'unknown-event',
});
assert.calledWith(
fakeWarnOnce,
'Received unsupported notification',
'unknown-event'
);
});
describe('annotation notifications', function () { describe('annotation notifications', function () {
beforeEach(function () { beforeEach(function () {
createDefaultStreamer(); createDefaultStreamer();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment