Add failure scenarios tests for connection, appendToStream, deleteStream (wip)

This commit is contained in:
Nicolas Dextraze
2016-03-11 15:55:27 -08:00
parent b0a0af536d
commit 0b63df85e7
16 changed files with 420 additions and 32 deletions

126
test/appendToStream_test.js Normal file
View File

@ -0,0 +1,126 @@
var uuid = require('uuid');
var client = require('../src/client');
var settings = {};
if (process.env.TESTS_VERBOSE_LOGGING === '1') {
settings.verboseLogging = true;
var FileLogger = require('../src/common/log/fileLogger');
settings.log = new FileLogger('appendToStream_test.log');
}
module.exports = {
setUp: function(cb) {
this.testStreamName = 'test-' + uuid.v4();
var connected = false;
this.conn = client.EventStoreConnection.create(settings, {host: 'localhost', port: 1113});
this.conn.connect()
.then(function() {
//Doesn't mean anything, connection is just initiated
})
.catch(function(err) {
cb(err);
});
this.conn.on('closed', function(reason){
if (connected) return;
cb(new Error("Connection failed: " + reason));
});
this.conn.on('connected', function() {
connected = true;
cb();
});
},
tearDown: function(cb) {
this.conn.close();
this.conn.on('closed', function() {
cb();
});
this.conn = null;
},
'Append One Event To Stream Happy Path': function(test) {
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, event)
.then(function(result) {
test.ok(result.nextExpectedVersion === 0, "Expected nextExpectedVersion === 0, but was " + result.nextExpectedVersion);
test.ok(result.logPosition, "No log position in result.");
test.done();
})
.catch(function(err) {
test.done(err);
});
},
'Append Multiple Events To Stream Happy Path': function(test) {
var events = [
client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent'),
client.createJsonEventData(uuid.v4(), {b: Math.random(), a: uuid.v4()}, null, 'otherEvent')
];
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, events)
.then(function(result) {
test.ok(result.nextExpectedVersion === 1, "Expected nextExpectedVersion === 1, but was " + result.nextExpectedVersion);
test.ok(result.logPosition, "No log position in result.");
test.done();
})
.catch(function(err) {
test.done(err);
});
},
'Append To Stream Wrong Expected Version': function(test) {
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
this.conn.appendToStream(this.testStreamName, 10, event)
.then(function(result) {
test.ok(false, "Append succeeded but should have failed.");
test.done();
})
.catch(function(err) {
var isWrongExpectedVersion = err instanceof client.WrongExpectedVersionError;
test.ok(isWrongExpectedVersion, "Expected WrongExpectedVersionError, got " + err.constructor.name);
if (!isWrongExpectedVersion) {
test.done(err);
return;
}
test.done();
});
},
'Append To Stream Deleted': function(test) {
var self = this;
this.conn.deleteStream(this.testStreamName, client.expectedVersion.noStream, true)
.then(function() {
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, event)
})
.then(function(result) {
test.ok(false, "Append succeeded but should have failed.");
test.done();
})
.catch(function(err) {
var isStreamDeleted = err instanceof client.StreamDeletedError;
test.ok(isStreamDeleted, "Expected StreamDeletedError, got " + err.constructor.name);
if (!isStreamDeleted) {
test.done(err);
return;
}
test.done();
});
},
'Append To Stream Access Denied': function(test) {
var self = this;
var metadata = {$acl: {$w: "$admins"}};
this.conn.setStreamMetadataRaw(this.testStreamName, client.expectedVersion.noStream, metadata)
.then(function() {
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, event)
})
.then(function(result) {
test.ok(false, "Append succeeded but should have failed.");
test.done();
})
.catch(function(err) {
var isStreamDeleted = err instanceof client.AccessDeniedError;
test.ok(isStreamDeleted, "Expected AccessDeniedError, got " + err.constructor.name);
if (!isStreamDeleted) {
test.done(err);
return;
}
test.done();
});
}
};

57
test/connection_test.js Normal file
View File

@ -0,0 +1,57 @@
var util = require('util');
var client = require('../src/client.js');
var consoleLogger = {
debug: function() {
var msg = util.format.apply(util, Array.prototype.slice.call(arguments));
util.log(msg);
},
info: function() {},
error: function() {}
};
var settings = {};//verboseLogging: true, log: consoleLogger};
module.exports = {
'Connect To Endpoint Happy Path': function(test) {
var tcpEndpoint = {hostname: 'localhost', port: 1113};
var conn = client.EventStoreConnection.create({}, tcpEndpoint);
conn.connect()
.catch(function(e) {
test.done(e);
});
conn.on('connected', function(endPoint){
test.deepEqual(endPoint, tcpEndpoint);
done();
});
conn.on('error', done);
function done(e) {
conn.close();
if (e) {
test.done(e);
return;
}
test.done();
}
},
'Connect To Endpoint That Don\'t Exist': function(test) {
var tcpEndpoint = {hostname: 'localhost', port: 1114};
var conn = client.EventStoreConnection.create({maxReconnections: 1}, tcpEndpoint);
conn.connect()
.catch(function (e) {
test.done(e);
});
conn.on('connected', function () {
test.ok(false, "Should not be able to connect.");
test.done();
});
conn.on('error', function (e) {
test.done(e);
});
conn.on('closed', function(reason) {
test.ok(reason.indexOf("Reconnection limit reached") === 0, "Wrong expected reason.");
test.done();
});
}
};

90
test/deleteStream_test.js Normal file
View File

@ -0,0 +1,90 @@
var uuid = require('uuid');
var client = require('../src/client');
var settings = {};
if (process.env.TESTS_VERBOSE_LOGGING === '1') {
settings.verboseLogging = true;
var FileLogger = require('../src/common/log/fileLogger');
settings.log = new FileLogger('deleteStream_test.log');
}
module.exports = {
setUp: function(cb) {
this.testStreamName = 'test-' + uuid.v4();
var connected = false;
this.conn = client.EventStoreConnection.create(settings, {host: 'localhost', port: 1113});
this.conn.connect()
.then(function() {
//Doesn't mean anything, connection is just initiated
})
.catch(function(err) {
cb(err);
});
this.conn.on('closed', function(reason){
if (connected) return;
cb(new Error("Connection failed: " + reason));
});
var self = this;
this.conn.on('connected', function() {
connected = true;
var events = [
client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent'),
client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent')
];
self.conn.appendToStream(self.testStreamName, client.expectedVersion.noStream, events)
.then(function() {
cb();
})
.catch(cb);
});
},
tearDown: function(cb) {
this.conn.close();
this.conn.on('closed', function() {
cb();
});
this.conn = null;
},
'Test Delete Stream Soft Happy Path': function(test) {
var self = this;
this.conn.deleteStream(this.testStreamName, 1, false)
.then(function(result) {
test.ok(result.logPosition, "No log position in result.");
return self.conn.getStreamMetadataRaw(self.testStreamName);
})
.then(function(metadata) {
test.ok(metadata.stream === self.testStreamName, "Metadata stream doesn't match.");
test.ok(metadata.isStreamDeleted === false, "Metadata says stream is deleted.");
test.ok(metadata.streamMetadata.$tb, "Expected Truncate Before to be set");
test.done();
})
.catch(function(err) {
test.done(err);
});
},
/*
This test fails because of a protobufjs error.
Client.ReadEventCompleted fails to decode because ResolvedIndexedEvent.event is null and it's marked as required.
Test will pass if messages.proto is modified so that ResolvedIndexedEvent.event is optional.
Unsure if it's a protobufjs issue or a GES issue. Need to duplicate this test with .Net Client.
'Test Delete Stream Hard Happy Path': function(test) {
var self = this;
this.conn.deleteStream(this.testStreamName, 1, true)
.then(function(result) {
test.ok(result.logPosition, "No log position in result.");
return self.conn.getStreamMetadataRaw(self.testStreamName);
})
.then(function(metadata) {
test.ok(metadata.stream === self.testStreamName, "Metadata stream doesn't match.");
test.ok(metadata.isStreamDeleted === true, "Metadata says stream is deleted.");
test.ok(metadata.streamMetadata === null, "Expected streamMetadata to be null.");
test.done();
})
.catch(function(err) {
test.done(err);
});
}
*/
};