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 {
// HTTP status code for HTTP -> WS upgrade requests.
const websocketHost = new URL(settings.websocketUrl).hostname;
if (['localhost', '127.0.0.1'].indexOf(websocketHost) !== -1) {
/* istanbul ignore next */
warnOnce(
'Check that your H service is configured to allow ' +
'WebSocket connections from ' +
......@@ -128,7 +129,7 @@ export class StreamerService {
);
}
} else {
warnOnce('received unsupported notification', message.type);
warnOnce('Received unsupported notification', message.type);
}
};
......
......@@ -78,6 +78,7 @@ describe('StreamerService', () => {
let fakeGroups;
let fakeSession;
let fakeSettings;
let fakeWarnOnce;
let activeStreamer;
function createDefaultStreamer() {
......@@ -92,9 +93,7 @@ describe('StreamerService', () => {
beforeEach(function () {
fakeAuth = {
getAccessToken: function () {
return Promise.resolve('dummy-access-token');
},
getAccessToken: sinon.stub().resolves('dummy-access-token'),
};
fakeStore = fakeReduxStore(
......@@ -126,7 +125,10 @@ describe('StreamerService', () => {
websocketUrl: 'ws://example.com/ws',
};
fakeWarnOnce = sinon.stub();
$imports.$mock({
'../../shared/warn-once': fakeWarnOnce,
'../websocket': FakeSocket,
});
});
......@@ -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 () {
createDefaultStreamer();
return activeStreamer.connect().then(function () {
......@@ -207,9 +217,7 @@ describe('StreamerService', () => {
});
it('should not include credentials in the URL if the client has no access token', function () {
fakeAuth.getAccessToken = function () {
return Promise.resolve(null);
};
fakeAuth.getAccessToken.resolves(null);
createDefaultStreamer();
return activeStreamer.connect().then(function () {
......@@ -231,6 +239,15 @@ describe('StreamerService', () => {
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 () {
......@@ -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 () {
beforeEach(function () {
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