Skip to content

Commit

Permalink
Add some tests to registry service.
Browse files Browse the repository at this point in the history
  • Loading branch information
francisbrito committed May 11, 2018
1 parent efc1789 commit 39d5452
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/adapters/in-memory-entry-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const createInMemoryEntryStorage = context => ({
saveEntry(input) {
this.entries.push(input);

return Promise.resolve(null);
return Promise.resolve(input);
},
exists(entry) {
const matchingEntry = match => e => match.belongsTo === e.belongsTo && match.token === e.token;
Expand Down
5 changes: 4 additions & 1 deletion lib/dtos.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const joi = require('joi');
const VError = require('verror');

const { EntryValidationError } = require('./errors');
const { EntryValidationError, GroupIdValidationError } = require('./errors');

const matchSchemaOrThrow = ({ input, schema, error: errorType }) => {
const { error, value } = joi.validate(input, schema, {
Expand Down Expand Up @@ -43,3 +43,6 @@ const entrySchema = joi

exports.createEntryDto = input =>
matchSchemaOrThrow({ input, schema: entrySchema, error: EntryValidationError });

exports.createGroupIdDto = input =>
matchSchemaOrThrow({ input, schema: groupIdSchema, error: GroupIdValidationError });
14 changes: 10 additions & 4 deletions lib/registry.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const { createEntryDto } = require('./dtos');
const { createEntryDto, createGroupIdDto } = require('./dtos');

const createRegistry = ({ entryStorage }) => ({
getAllEntries() {
return entryStorage.getAllEntries();
return entryStorage.getAllEntries().then(entries => entries.map(createEntryDto));
},
getEntriesByGroupId(groupId) {
return entryStorage.getEntriesByGroupId(groupId);
return entryStorage
.getEntriesByGroupId(createGroupIdDto(groupId))
.then(entries => entries.map(createEntryDto));
},
saveEntry(input) {
return entryStorage.saveEntry(createEntryDto(input));
const entry = createEntryDto(input);

return this.exists(input)
.then(exists => (exists ? entry : entryStorage.saveEntry(entry)))
.then(createEntryDto);
},
exists(input) {
return entryStorage.exists(createEntryDto(input));
Expand Down
114 changes: 114 additions & 0 deletions lib/registry.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const test = require('ava');

const createRegistry = require('./registry');
const createInMemoryEntryStorage = require('./adapters/in-memory-entry-storage');
const { EntryValidationError, GroupIdValidationError } = require('./errors');

test('allows retrieving all entries', (t) => {
const expectedEntries = [
{ token: 'a', belongsTo: 'someone' },
{ token: 'b', belongsTo: 'someone' },
{ token: 'c', belongsTo: 'someone-else' },
{ token: 'd', belongsTo: 'yet-someone-else' },
];
const entryStorage = {
getAllEntries: () => Promise.resolve(expectedEntries),
};
const registry = createRegistry({ entryStorage });

return registry.getAllEntries().then((entries) => {
t.deepEqual(entries, expectedEntries);
});
});

test('allows retrieving all entries for a group id', (t) => {
const groupId = 'someone';
const entries = [
{ token: 'a', belongsTo: groupId },
{ token: 'b', belongsTo: groupId },
{ token: 'c', belongsTo: groupId },
{ token: 'd', belongsTo: 'someone-else' },
];
const entryStorage = createInMemoryEntryStorage({ entries });
const registry = createRegistry({ entryStorage });

return registry.getEntriesByGroupId(groupId).then((_entries) => {
t.is(_entries.length, 3);
t.true(_entries.every(({ belongsTo }) => belongsTo === groupId));
});
});

test('allows saving a new entry for a given group id', (t) => {
const entries = [];
const entryStorage = createInMemoryEntryStorage({ entries });
const registry = createRegistry({ entryStorage });
const input = { token: 'a', belongsTo: 'someone' };

return registry
.saveEntry(input)
.then(() => registry.getAllEntries())
.then((_entries) => {
t.is(_entries.length, 1);
t.is(_entries[0].token, 'a');
t.is(_entries[0].belongsTo, 'someone');
});
});

test('validates group id when looking for entries for that group id', (t) => {
const groupIdTooLong = 'a'.repeat(201);
const entryStorage = { getEntriesByGroupId: () => Promise.resolve([]) };
const registry = createRegistry({ entryStorage });

return t.throws(() => registry.getEntriesByGroupId(groupIdTooLong), {
name: GroupIdValidationError.name,
});
});

test('validates entry when saving a new entry', (t) => {
const groupIdTooLong = 'a'.repeat(201);
const tokenTooLong = 'a'.repeat(1025);
const entryStorage = { saveEntry: () => Promise.resolve({}) };
const registry = createRegistry({ entryStorage });

return Promise.all([
t.throws(() => registry.saveEntry({ token: tokenTooLong, belongsTo: 'someone' }), {
name: EntryValidationError.name,
message: /"token" length must be less than or equal to 1024 characters long/gi,
}),
t.throws(() => registry.saveEntry({ token: 'a', belongsTo: groupIdTooLong }), {
name: EntryValidationError.name,
message: /"belongsTo" length must be less than or equal to 200 characters long/gi,
}),
]);
});

test('strips unknown fields in entries', (t) => {
const expectedKeys = ['token', 'belongsTo'];
const entries = [{ token: 'a', belongsTo: 'someone', someFunky: 'property' }];
const entryStorage = {
getAllEntries: () => Promise.resolve(entries),
getEntriesByGroupId: () => Promise.resolve(entries),
saveEntry: () => Promise.resolve(entries[0]),
exists: () => Promise.resolve(false),
};
const registry = createRegistry({ entryStorage });
const checkMatchesExpectedKeys = e => t.deepEqual(Object.keys(e), expectedKeys);
const checkEveryEntryMatchesExpectedKeys = es => es.every(checkMatchesExpectedKeys);

return Promise.all([
registry.getAllEntries().then(checkEveryEntryMatchesExpectedKeys),
registry.getEntriesByGroupId('someone').then(checkEveryEntryMatchesExpectedKeys),
registry.saveEntry({ token: 'b', belongsTo: 'someone' }).then(checkMatchesExpectedKeys),
]);
});

test('ignores operation if token is already register for the given group id', (t) => {
const entries = [{ token: 'a', belongsTo: 'someone' }];
const entryStorage = createInMemoryEntryStorage({ entries });
const registry = createRegistry({ entryStorage });

return registry
.saveEntry({ token: 'a', belongsTo: 'someone' })
.then(() => registry.getAllEntries())
.then(_entries => t.is(_entries.length, 1));
});

0 comments on commit 39d5452

Please sign in to comment.