Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-09-17 09:44:13 -0400
committerMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-10-17 11:47:14 -0400
commit1f66a25658e32706b6fb6e80421f7f951475369a (patch)
tree40af4f32784c7ea3d6cff9f4955abf1a888fe49f /test
parentfb88ceeae65418fcefeb75b4e49a1aeeaaea3998 (diff)
downloadliza-1f66a25658e32706b6fb6e80421f7f951475369a.tar.gz
liza-1f66a25658e32706b6fb6e80421f7f951475369a.tar.bz2
liza-1f66a25658e32706b6fb6e80421f7f951475369a.zip
TokenDao: Better error handling for unknown tokens
Rather than replying with null, which complicates using the returned promise efficiently, we'll respond with a unique error that allows us to distinguish between a database failure and a missing token. These are more traditional errors, but we're moving toward structured logging, so I want error objects that provide more context. I'll explore that a bit more in next commit. Unfortunately, the untypedness of Promise rejections make for a less than desirable situation here. Async/await is not yet an option since we're still compiling to ES5 (have to support IE11), and TS compiles async/await into generators for environments that don't support them, which also are not available in ES5. * src/server/service/TokenedService.js (_getQuoteToken): Remove null check, since this situation can no longer occur. * src/server/token/MongoTokenDao.ts (getToken): Remove null from return type union; reject with `UnknownTokenError' instead. * src/server/token/TokenDao.ts: Modify interface accordingly. * src/server/token/UnknownTokenError.ts: New class. * test/server/token/MongoTokenDaoTest.ts: Modify tests accordingly. Add missing test for latest token namespace missing.
Diffstat (limited to 'test')
-rw-r--r--test/server/token/MongoTokenDaoTest.ts56
1 files changed, 46 insertions, 10 deletions
diff --git a/test/server/token/MongoTokenDaoTest.ts b/test/server/token/MongoTokenDaoTest.ts
index 74f9c13..47b1368 100644
--- a/test/server/token/MongoTokenDaoTest.ts
+++ b/test/server/token/MongoTokenDaoTest.ts
@@ -33,6 +33,7 @@ import {
} from "../../../src/server/token/Token";
import { DocumentId } from "../../../src/document/Document";
+import { UnknownTokenError } from "../../../src/server/token/UnknownTokenError";
import { expect, use as chai_use } from 'chai';
@@ -129,7 +130,7 @@ describe( 'server.token.TokenDao', () =>
data: "",
};
- ( <[string, TokenId, TokenQueryResult, TokenData][]>[
+ ( <[string, TokenId, TokenQueryResult, TokenData|null, any][]>[
[
'retrieves token by id',
<TokenId>'tok123',
@@ -150,10 +151,11 @@ describe( 'server.token.TokenDao', () =>
id: <TokenId>'tok123',
status: expected_status,
},
+ null,
],
[
- 'returns null for namespace if token is not found',
+ 'rejects for namespace if token is not found',
<TokenId>'tok123',
{
[field]: {
@@ -170,19 +172,21 @@ describe( 'server.token.TokenDao', () =>
},
},
null,
+ `${ns}.tok123`,
],
[
- 'returns null for field if namespace is not found',
+ 'rejects if namespace is not found',
<TokenId>'tok123',
{
[field]: {},
},
null,
+ ns,
],
[
- 'returns lastest modified token given no token id',
+ 'returns last modified token given no token id',
<TokenId>'',
{
[field]: {
@@ -201,23 +205,55 @@ describe( 'server.token.TokenDao', () =>
id: <TokenId>'toklast',
status: expected_status,
},
+ null,
+ ],
+
+ [
+ 'rejects unknown last modified token given no token id',
+ <TokenId>'',
+ {
+ [field]: {
+ [ns]: {},
+ },
+ },
+ null,
+ ns,
+ ],
+
+ [
+ 'rejects unknown namespace token given no token id',
+ <TokenId>'',
+ {
+ [field]: {},
+ },
+ null,
+ ns,
],
- ] ).forEach( ( [ label, tok_id, result, expected ] ) =>
+ ] ).forEach( ( [ label, tok_id, dbresult, expected, failure ] ) =>
it( label, () =>
{
const coll: MongoCollection = {
findOne( _selector, _fields, callback )
{
- callback( null, result );
+ callback( null, dbresult );
},
update() {},
};
- return expect(
- new Sut( coll, field, () => <UnixTimestamp>0 )
- .getToken( did, ns, tok_id )
- ).to.eventually.deep.equal( expected );
+ const result = new Sut( coll, field, () => <UnixTimestamp>0 )
+ .getToken( did, ns, tok_id );
+
+ return ( failure !== null )
+ ? Promise.all( [
+ expect( result ).to.eventually.be.rejectedWith(
+ UnknownTokenError, failure
+ ),
+ expect( result ).to.eventually.be.rejectedWith(
+ UnknownTokenError, ''+did
+ ),
+ ] )
+ : expect( result ).to.eventually.deep.equal( expected );
} )
);