3441f940d40251caa678b4e20f5d1f2c839c2ddb
[squeep-indie-auther] / test / 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('strips uninteresting scope dross', function () {
55 logger.info('testScope', 'message', {
56 ctx: {
57 profilesScopes: {
58 profileScopes: {
59 'https://thuza.ratfeathers.com/': {
60 profile: {
61 description: 'Access detailed profile information, including name, image, and url.',
62 application: 'IndieAuth',
63 profiles: [
64 'https://thuza.ratfeathers.com/',
65 ],
66 isPermanent: true,
67 isManuallyAdded: false,
68 },
69 },
70 },
71 scopeIndex: {
72 profile: {
73 description: 'Access detailed profile information, including name, image, and url.',
74 application: 'IndieAuth',
75 profiles: [
76 'https://thuza.ratfeathers.com/',
77 ],
78 isPermanent: true,
79 isManuallyAdded: false,
80 },
81 email: {
82 description: 'Include email address with detailed profile information.',
83 application: 'IndieAuth',
84 profiles: [],
85 isPermanent: true,
86 isManuallyAdded: false,
87 },
88 },
89 },
90 },
91 });
92 assert(logger.backend.info.called);
93 });
94
95 it('strips uninteresting scope dross from session', function () {
96 logger.info('testScope', 'message', {
97 ctx: {
98 session: {
99 profileScopes: {
100 'https://thuza.ratfeathers.com/': {
101 profile: {
102 description: 'Access detailed profile information, including name, image, and url.',
103 application: 'IndieAuth',
104 profiles: [
105 'https://thuza.ratfeathers.com/',
106 ],
107 isPermanent: true,
108 isManuallyAdded: false,
109 },
110 },
111 },
112 scopeIndex: {
113 profile: {
114 description: 'Access detailed profile information, including name, image, and url.',
115 application: 'IndieAuth',
116 profiles: [
117 'https://thuza.ratfeathers.com/',
118 ],
119 isPermanent: true,
120 isManuallyAdded: false,
121 },
122 email: {
123 description: 'Include email address with detailed profile information.',
124 application: 'IndieAuth',
125 profiles: [],
126 isPermanent: true,
127 isManuallyAdded: false,
128 },
129 },
130 },
131 },
132 });
133 assert(logger.backend.info.called);
134 });
135
136 }); // Logger