2 /* eslint-disable node/no-unpublished-require */
5 const assert
= require('assert');
6 const sinon
= require('sinon');
7 const StubDB
= require('../stub-db');
8 const StubLogger
= require('../stub-logger');
9 const Chores
= require('../../src/chores');
11 const snooze
= (ms
) => new Promise((resolve
) => setTimeout(resolve
, ms
));
13 const expectedException
= new Error('oh no');
15 describe('Chores', function () {
16 let chores
, stubLogger
, stubDb
, stubQueuePublisher
, options
;
17 beforeEach(function () {
18 stubLogger
= new StubLogger();
20 stubDb
= new StubDB();
22 stubQueuePublisher
= {
23 publish: sinon
.stub(),
26 afterEach(function () {
27 chores
?.stopAllChores();
31 describe('constructor', function () {
33 it('empty options, no cleaning', async
function () {
35 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
36 assert
.strictEqual(chores
.chores
.cleanTokens
.timeoutObj
, undefined);
37 assert
.strictEqual(chores
.chores
.cleanScopes
.timeoutObj
, undefined);
38 assert
.strictEqual(chores
.chores
.publishTickets
.timeoutObj
, undefined);
41 it('cleans scopes', async
function () {
47 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
49 assert(chores
.chores
.cleanScopes
.timeoutObj
);
50 assert(chores
.db
.scopeCleanup
.called
);
53 it('cleans tokens', async
function () {
59 codeValidityTimeoutMs: 10,
62 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
64 assert(chores
.chores
.cleanTokens
.timeoutObj
);
65 assert(chores
.db
.tokenCleanup
.called
);
68 it('publishes tickets', async
function () {
74 ticketRedeemedName: 'queue',
77 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
79 assert(chores
.chores
.publishTickets
.timeoutObj
);
80 assert(chores
.db
.ticketTokenGetUnpublished
.called
);
85 describe('cleanTokens', function () {
86 it('logs cleaning', async
function () {
93 codeValidityTimeoutMs: 10,
96 stubDb
.tokenCleanup
.resolves(cleaned
);
97 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
98 clearTimeout(chores
.cleanTokensTimeout
);
99 await chores
.cleanTokens();
100 assert(stubLogger
.info
.called
);
102 it('covers failure', async
function () {
108 codeValidityTimeoutMs: 10,
111 stubDb
.tokenCleanup
.rejects(expectedException
);
112 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
113 await assert
.rejects(() => chores
.cleanTokens(), expectedException
);
115 it('covers default', async
function () {
116 stubDb
.tokenCleanup
.resolves(0);
117 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, {
119 codeValidityTimeoutMs: 10,
122 await chores
.cleanTokens();
123 assert(stubDb
.tokenCleanup
.called
);
127 describe('cleanScopes', function () {
128 it('logs cleaning', async
function () {
135 stubDb
.scopeCleanup
.resolves(cleaned
);
136 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
137 clearTimeout(chores
.cleanScopesTimeout
);
138 await chores
.cleanScopes();
139 assert(stubLogger
.info
.called
);
141 it('covers failure', async
function () {
147 stubDb
.scopeCleanup
.rejects(expectedException
);
148 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
149 await assert
.rejects(() => chores
.cleanScopes(), expectedException
);
151 it('covers default', async
function () {
152 stubDb
.scopeCleanup
.resolves(0);
153 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, {});
154 await chores
.cleanScopes();
155 assert(stubDb
.scopeCleanup
.called
);
159 describe('publishTickets', function () {
160 beforeEach(function () {
163 ticketRedeemedName: 'queue',
166 stubDb
.ticketTokenGetUnpublished
.resolves([{
167 ticket: 'xxxTICKETxxx',
168 resource: 'https://resource.example.com/',
169 subject: 'https://subject.example.com/',
172 chores
= new Chores(stubLogger
, stubDb
, stubQueuePublisher
, options
);
174 it('publishes a ticket', async
function () {
175 await chores
.publishTickets();
176 assert(stubQueuePublisher
.publish
.called
);
177 assert(stubDb
.ticketTokenPublished
.called
);
179 it('covers error', async
function () {
180 stubQueuePublisher
.publish
.rejects(expectedException
);
181 await chores
.publishTickets();
182 assert(stubQueuePublisher
.publish
.called
);
183 assert(stubDb
.ticketTokenPublished
.notCalled
);
185 }); // publishTickets