2019-09-09 13:52:15 -04:00
|
|
|
/**
|
|
|
|
* Token state management test
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2019 R-T Specialty, LLC.
|
|
|
|
*
|
|
|
|
* This file is part of the Liza Data Collection Framework.
|
|
|
|
*
|
|
|
|
* liza is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {
|
|
|
|
TokenQueryResult,
|
|
|
|
TokenStatus,
|
|
|
|
} from "../../../src/server/token/TokenQueryResult";
|
|
|
|
|
2019-09-09 15:41:29 -04:00
|
|
|
import {
|
|
|
|
default as Sut,
|
|
|
|
TokenData,
|
|
|
|
} from "../../../src/server/token/TokenDao";
|
2019-09-09 13:52:15 -04:00
|
|
|
|
2019-09-10 12:09:08 -04:00
|
|
|
import {
|
|
|
|
TokenId,
|
|
|
|
TokenNamespace,
|
|
|
|
} from "../../../src/server/token/Token";
|
|
|
|
|
2019-09-10 12:18:51 -04:00
|
|
|
import { DocumentId } from "../../../src/document/Document";
|
|
|
|
|
2019-09-10 12:09:08 -04:00
|
|
|
|
2019-09-09 15:41:29 -04:00
|
|
|
import { expect, use as chai_use } from 'chai';
|
2019-09-09 15:28:35 -04:00
|
|
|
chai_use( require( 'chai-as-promised' ) );
|
|
|
|
|
2019-09-09 13:52:15 -04:00
|
|
|
|
|
|
|
describe( 'server.token.TokenDao', () =>
|
|
|
|
{
|
|
|
|
describe( '#updateToken', () =>
|
|
|
|
{
|
2019-09-09 15:28:35 -04:00
|
|
|
it( 'updates token with given data', () =>
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
2019-09-09 16:23:15 -04:00
|
|
|
const field = 'foo_field';
|
2019-09-10 12:18:51 -04:00
|
|
|
const did = <DocumentId>12345;
|
2019-09-10 12:09:08 -04:00
|
|
|
const ns = <TokenNamespace>'namespace';
|
|
|
|
const tok_id = <TokenId>'tok123';
|
2019-09-09 16:23:15 -04:00
|
|
|
const tok_type = 'DONE';
|
|
|
|
const data = "some data";
|
2019-09-10 12:09:08 -04:00
|
|
|
const timestamp = <UnixTimestamp>12345;
|
2019-09-09 13:52:15 -04:00
|
|
|
|
|
|
|
const root = field + '.' + ns;
|
|
|
|
|
|
|
|
const coll: MongoCollection = {
|
|
|
|
update( selector: any, given_data: any, options, callback )
|
|
|
|
{
|
|
|
|
const expected_entry: TokenStatus = {
|
|
|
|
type: tok_type,
|
2019-09-09 16:23:15 -04:00
|
|
|
timestamp: timestamp,
|
2019-09-09 13:52:15 -04:00
|
|
|
data: data,
|
|
|
|
};
|
|
|
|
|
2019-09-10 12:18:51 -04:00
|
|
|
expect( selector.id ).to.equal( did );
|
2019-09-09 13:52:15 -04:00
|
|
|
|
|
|
|
expect( given_data ).to.deep.equal( {
|
|
|
|
$set: {
|
|
|
|
[`${root}.last`]: tok_id,
|
|
|
|
[`${root}.lastStatus`]: expected_entry,
|
|
|
|
[`${root}.${tok_id}.status`]: expected_entry,
|
|
|
|
},
|
|
|
|
$push: {
|
|
|
|
[`${root}.${tok_id}.statusLog`]: expected_entry,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
expect( ( <MongoQueryUpdateOptions>options ).upsert )
|
|
|
|
.to.be.true;
|
|
|
|
|
|
|
|
callback( null, {} );
|
|
|
|
},
|
|
|
|
|
|
|
|
findOne() {},
|
|
|
|
};
|
|
|
|
|
2019-09-09 16:23:15 -04:00
|
|
|
return new Sut( coll, field, () => timestamp )
|
2019-09-10 12:18:51 -04:00
|
|
|
.updateToken( did, ns, tok_id, tok_type, data );
|
2019-09-09 13:52:15 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
2019-09-09 15:28:35 -04:00
|
|
|
it( 'proxies error to callback', () =>
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
|
|
|
const expected_error = Error( "expected error" );
|
|
|
|
|
|
|
|
const coll: MongoCollection = {
|
|
|
|
update( _selector, _data, _options, callback )
|
|
|
|
{
|
|
|
|
callback( expected_error, {} );
|
|
|
|
},
|
|
|
|
|
|
|
|
findOne() {},
|
|
|
|
};
|
|
|
|
|
2019-09-09 15:28:35 -04:00
|
|
|
return expect(
|
2019-09-10 12:09:08 -04:00
|
|
|
new Sut( coll, 'foo', () => <UnixTimestamp>0 ).updateToken(
|
2019-09-10 12:18:51 -04:00
|
|
|
<DocumentId>0,
|
|
|
|
<TokenNamespace>'ns',
|
|
|
|
<TokenId>'id',
|
|
|
|
'DONE',
|
|
|
|
null
|
2019-09-10 12:09:08 -04:00
|
|
|
)
|
2019-09-09 15:28:35 -04:00
|
|
|
).to.eventually.be.rejectedWith( expected_error );
|
2019-09-09 13:52:15 -04:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
describe( '#getToken', () =>
|
|
|
|
{
|
|
|
|
const field = 'get_field';
|
2019-09-10 12:18:51 -04:00
|
|
|
const did = <DocumentId>12345;
|
2019-09-10 12:09:08 -04:00
|
|
|
const ns = <TokenNamespace>'get_ns';
|
2019-09-09 13:52:15 -04:00
|
|
|
|
|
|
|
const expected_status: TokenStatus = {
|
|
|
|
type: 'ACTIVE',
|
2019-09-10 12:09:08 -04:00
|
|
|
timestamp: <UnixTimestamp>0,
|
2019-09-09 13:52:15 -04:00
|
|
|
data: "",
|
|
|
|
};
|
|
|
|
|
2019-09-10 12:09:08 -04:00
|
|
|
( <[string, TokenId, TokenQueryResult, TokenData][]>[
|
2019-09-09 13:52:15 -04:00
|
|
|
[
|
|
|
|
'retrieves token by id',
|
2019-09-10 12:09:08 -04:00
|
|
|
<TokenId>'tok123',
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
|
|
|
[field]: {
|
|
|
|
[ns]: {
|
2019-09-10 12:09:08 -04:00
|
|
|
last: <TokenId>'tok123',
|
2019-09-09 13:52:15 -04:00
|
|
|
lastStatus: expected_status,
|
|
|
|
|
|
|
|
tok123: {
|
|
|
|
status: expected_status,
|
|
|
|
statusLog: [ expected_status ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-09-10 12:09:08 -04:00
|
|
|
id: <TokenId>'tok123',
|
2019-09-09 13:52:15 -04:00
|
|
|
status: expected_status,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
'returns null for namespace if token is not found',
|
2019-09-10 12:09:08 -04:00
|
|
|
<TokenId>'tok123',
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
|
|
|
[field]: {
|
|
|
|
[ns]: {
|
2019-09-10 12:09:08 -04:00
|
|
|
last: <TokenId>'something',
|
2019-09-09 13:52:15 -04:00
|
|
|
lastStatus: expected_status,
|
|
|
|
|
|
|
|
// just to make sure we don't grab another tok
|
|
|
|
othertok: {
|
|
|
|
status: expected_status,
|
|
|
|
statusLog: [ expected_status ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
'returns null for field if namespace is not found',
|
2019-09-10 12:09:08 -04:00
|
|
|
<TokenId>'tok123',
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
|
|
|
[field]: {},
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
'returns lastest modified token given no token id',
|
2019-09-10 12:09:08 -04:00
|
|
|
<TokenId>'',
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
|
|
|
[field]: {
|
|
|
|
[ns]: {
|
2019-09-10 12:09:08 -04:00
|
|
|
last: <TokenId>'toklast',
|
2019-09-09 13:52:15 -04:00
|
|
|
lastStatus: expected_status,
|
|
|
|
|
|
|
|
toklast: {
|
|
|
|
status: expected_status,
|
|
|
|
statusLog: [ expected_status ],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-09-10 12:09:08 -04:00
|
|
|
id: <TokenId>'toklast',
|
2019-09-09 13:52:15 -04:00
|
|
|
status: expected_status,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
] ).forEach( ( [ label, tok_id, result, expected ] ) =>
|
2019-09-09 15:28:35 -04:00
|
|
|
it( label, () =>
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
|
|
|
const coll: MongoCollection = {
|
|
|
|
findOne( _selector, _fields, callback )
|
|
|
|
{
|
|
|
|
callback( null, result );
|
|
|
|
},
|
|
|
|
|
|
|
|
update() {},
|
|
|
|
};
|
|
|
|
|
2019-09-09 15:28:35 -04:00
|
|
|
return expect(
|
2019-09-10 12:09:08 -04:00
|
|
|
new Sut( coll, field, () => <UnixTimestamp>0 )
|
2019-09-10 12:18:51 -04:00
|
|
|
.getToken( did, ns, tok_id )
|
2019-09-09 15:28:35 -04:00
|
|
|
).to.eventually.deep.equal( expected );
|
2019-09-09 13:52:15 -04:00
|
|
|
} )
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2019-09-09 15:28:35 -04:00
|
|
|
it( 'proxies error to callback', () =>
|
2019-09-09 13:52:15 -04:00
|
|
|
{
|
|
|
|
const expected_error = Error( "expected error" );
|
|
|
|
|
|
|
|
const coll: MongoCollection = {
|
|
|
|
findOne( _selector, _fields, callback )
|
|
|
|
{
|
|
|
|
callback( expected_error, {} );
|
|
|
|
},
|
|
|
|
|
|
|
|
update() {},
|
|
|
|
};
|
|
|
|
|
2019-09-09 15:28:35 -04:00
|
|
|
return expect(
|
2019-09-10 12:09:08 -04:00
|
|
|
new Sut( coll, 'foo', () => <UnixTimestamp>0 )
|
2019-09-10 12:18:51 -04:00
|
|
|
.getToken( <DocumentId>0, <TokenNamespace>'ns', <TokenId>'id' )
|
2019-09-09 15:28:35 -04:00
|
|
|
).to.eventually.be.rejectedWith( expected_error );
|
2019-09-09 13:52:15 -04:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|