add more setup documentation
[squeep-indie-auther] / src / logger.js
1 /* eslint-env mocha */
2 'use strict';
3
4 const assert = require('assert');
5 const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
6 const Logger = require('../../src/logger');
7 const Config = require('../../config');
8
9 describe('Logger', function () {
10 let config;
11 let logger;
12
13 beforeEach(function () {
14 config = new Config('test');
15 logger = new Logger(config);
16 Object.keys(Logger.nullLogger).forEach((level) => sinon.stub(logger.backend, level));
17 });
18
19 afterEach(function () {
20 sinon.restore();
21 });
22
23 it('logs', function () {
24 logger.info('testScope', 'message', { baz: 'quux' }, { foo: 1 }, 'more other');
25 assert(logger.backend.info.called);
26 });
27
28 it('logs BigInts', function () {
29 logger.info('testScope', 'message', { aBigInteger: BigInt(2) });
30 assert(logger.backend.info.called);
31 assert(logger.backend.info.args[0][0].includes('"2"'));
32 });
33
34 it('logs Errors', function () {
35 logger.error('testScope', 'message', { e: new Error('an error') });
36 assert(logger.backend.error.called);
37 assert(logger.backend.error.args[0][0].includes('an error'));
38 });
39
40 it('masks credentials', function () {
41 logger.info('testScope', 'message', {
42 ctx: {
43 parsedBody: {
44 identity: 'username',
45 credential: 'password',
46 },
47 },
48 });
49 assert(logger.backend.info.called);
50 assert(logger.backend.info.args[0][0].includes('"username"'));
51 assert(logger.backend.info.args[0][0].includes('"********"'));
52 });
53
54 it('masks noisy cookie header', function () {
55 logger.info('testScope', 'message', {
56 ctx: {
57 cookie: {
58 squeepSession: 'blahblahblahblahblah',
59 },
60 },
61 });
62 assert(logger.backend.info.called);
63 assert(logger.backend.info.args[0][0].includes('[scrubbed 20 bytes]'));
64 });
65
66 it('masks otp values', function () {
67 logger.info('teestScope', 'message', {
68 ctx: {
69 otpKey: '1234567890123456789012',
70 otpConfirmKey: '1234567890123456789012',
71 otpConfirmBox: 'xxxMysteryxxx',
72 otpState: 'xxxMysteryxxx',
73 }
74 });
75 assert(logger.backend.info.called);
76 assert(!logger.backend.info.args[0][0].includes('"1234567890123456789012"'));
77 assert(!logger.backend.info.args[0][0].includes('"xxxMysteryxxx"'));
78 });
79
80 it('strips uninteresting scope dross', function () {
81 logger.info('testScope', 'message', {
82 ctx: {
83 profilesScopes: {
84 profileScopes: {
85 'https://thuza.ratfeathers.com/': {
86 profile: {
87 description: 'Access detailed profile information, including name, image, and url.',
88 application: 'IndieAuth',
89 profiles: [
90 'https://thuza.ratfeathers.com/',
91 ],
92 isPermanent: true,
93 isManuallyAdded: false,
94 },
95 },
96 },
97 scopeIndex: {
98 profile: {
99 description: 'Access detailed profile information, including name, image, and url.',
100 application: 'IndieAuth',
101 profiles: [
102 'https://thuza.ratfeathers.com/',
103 ],
104 isPermanent: true,
105 isManuallyAdded: false,
106 },
107 email: {
108 description: 'Include email address with detailed profile information.',
109 application: 'IndieAuth',
110 profiles: [],
111 isPermanent: true,
112 isManuallyAdded: false,
113 },
114 },
115 },
116 },
117 });
118 assert(logger.backend.info.called);
119 });
120
121 it('strips uninteresting scope dross from session', function () {
122 logger.info('testScope', 'message', {
123 ctx: {
124 session: {
125 profileScopes: {
126 'https://thuza.ratfeathers.com/': {
127 profile: {
128 description: 'Access detailed profile information, including name, image, and url.',
129 application: 'IndieAuth',
130 profiles: [
131 'https://thuza.ratfeathers.com/',
132 ],
133 isPermanent: true,
134 isManuallyAdded: false,
135 },
136 },
137 },
138 scopeIndex: {
139 profile: {
140 description: 'Access detailed profile information, including name, image, and url.',
141 application: 'IndieAuth',
142 profiles: [
143 'https://thuza.ratfeathers.com/',
144 ],
145 isPermanent: true,
146 isManuallyAdded: false,
147 },
148 email: {
149 description: 'Include email address with detailed profile information.',
150 application: 'IndieAuth',
151 profiles: [],
152 isPermanent: true,
153 isManuallyAdded: false,
154 },
155 },
156 },
157 },
158 });
159 assert(logger.backend.info.called);
160 });
161
162 }); // Logger