3 const assert
= require('assert');
4 const sinon
= require('sinon');
5 const StubDB
= require('../stub-db');
6 const StubLogger
= require('../stub-logger');
7 const Chores
= require('../../src/chores');
9 const snooze
= (ms
) => new Promise((resolve
) => setTimeout(resolve
, ms
));
11 const expectedException
= new Error('oh no');
13 describe('Chores', function () {
14 let chores
, stubLogger
, stubDb
, stubQueuePublisher
, options
;
15 beforeEach(function () {
16 stubLogger
= new StubLogger();
18 stubDb
= new StubDB();
20 stubQueuePublisher
= {
21 publish: sinon
.stub(),
24 afterEach(function () {
25 chores
?.stopAllChores();
29 describe('constructor', function () {
31 it('empty options, no cleaning', async
function () {
33 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
34 assert
.strictEqual(chores
.chores
.cleanTokens
.timeoutObj
, undefined);
35 assert
.strictEqual(chores
.chores
.cleanScopes
.timeoutObj
, undefined);
36 assert
.strictEqual(chores
.chores
.publishTickets
.timeoutObj
, undefined);
39 it('cleans scopes', async
function () {
45 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
47 assert(chores
.chores
.cleanScopes
.timeoutObj
);
48 assert(chores
.db
.scopeCleanup
.called
);
51 it('cleans tokens', async
function () {
57 codeValidityTimeoutMs: 10,
60 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
62 assert(chores
.chores
.cleanTokens
.timeoutObj
);
63 assert(chores
.db
.tokenCleanup
.called
);
66 it('publishes tickets', async
function () {
72 ticketRedeemedName: 'queue',
75 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
77 assert(chores
.chores
.publishTickets
.timeoutObj
);
78 assert(chores
.db
.ticketTokenGetUnpublished
.called
);
83 describe('cleanTokens', function () {
84 it('logs cleaning', async
function () {
91 codeValidityTimeoutMs: 10,
94 stubDb
.tokenCleanup
.resolves(cleaned
);
95 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
96 clearTimeout(chores
.cleanTokensTimeout
);
97 await chores
.cleanTokens();
98 assert(stubLogger
.info
.called
);
100 it('covers failure', async
function () {
106 codeValidityTimeoutMs: 10,
109 stubDb
.tokenCleanup
.rejects(expectedException
);
110 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
111 await assert
.rejects(() => chores
.cleanTokens(), expectedException
);
113 it('covers default', async
function () {
114 stubDb
.tokenCleanup
.resolves(0);
115 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, {
117 codeValidityTimeoutMs: 10,
120 await chores
.cleanTokens();
121 assert(stubDb
.tokenCleanup
.called
);
125 describe('cleanScopes', function () {
126 it('logs cleaning', async
function () {
133 stubDb
.scopeCleanup
.resolves(cleaned
);
134 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
135 clearTimeout(chores
.cleanScopesTimeout
);
136 await chores
.cleanScopes();
137 assert(stubLogger
.info
.called
);
139 it('covers failure', async
function () {
145 stubDb
.scopeCleanup
.rejects(expectedException
);
146 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
147 await assert
.rejects(() => chores
.cleanScopes(), expectedException
);
149 it('covers default', async
function () {
150 stubDb
.scopeCleanup
.resolves(0);
151 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, {});
152 await chores
.cleanScopes();
153 assert(stubDb
.scopeCleanup
.called
);
157 describe('publishTickets', function () {
158 beforeEach(function () {
161 ticketRedeemedName: 'queue',
164 stubDb
.ticketTokenGetUnpublished
.resolves([{
165 ticket: 'xxxTICKETxxx',
166 resource: 'https://resource.example.com/',
167 subject: 'https://subject.example.com/',
170 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
172 it('publishes a ticket', async
function () {
173 await chores
.publishTickets();
174 assert(stubQueuePublisher
.publish
.called
);
175 assert(stubDb
.ticketTokenPublished
.called
);
177 it('covers error', async
function () {
178 stubQueuePublisher
.publish
.rejects(expectedException
);
179 await chores
.publishTickets();
180 assert(stubQueuePublisher
.publish
.called
);
181 assert(stubDb
.ticketTokenPublished
.notCalled
);
183 }); // publishTickets