Skip to content

Commit 838de91

Browse files
committed
fix: resolve lint rule violations and unit test errors
1 parent bdf710f commit 838de91

8 files changed

Lines changed: 37 additions & 33 deletions

File tree

src/controllers/invoices/get-invoice-controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { escapeHtml, safeJsonForScript } from '../../utils/html'
12
import { path, pathEq } from 'ramda'
23
import { Request, Response } from 'express'
4+
35
import { createSettings } from '../../factories/settings-factory'
6+
47
import { FeeSchedule } from '../../@types/settings'
58
import { IController } from '../../@types/controllers'
6-
import { escapeHtml, safeJsonForScript } from '../../utils/html'
9+
710
import { getTemplate } from '../../utils/template-cache'
811

912

src/controllers/invoices/post-invoice-controller.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { FeeSchedule, Settings } from '../../@types/settings'
2-
import { fromBech32, toBech32 } from '../../utils/transform'
3-
import { getPublicKey, getRelayPrivateKey } from '../../utils/event'
1+
import { path } from 'ramda'
2+
43
import { Request, Response } from 'express'
54

6-
import { escapeHtml, safeJsonForScript } from '../../utils/html'
7-
import { createLogger } from '../../factories/logger-factory'
8-
import { getRemoteAddress } from '../../utils/http'
5+
import { FeeSchedule, Settings } from '../../@types/settings'
96
import { IController } from '../../@types/controllers'
107
import { Invoice } from '../../@types/invoice'
118
import { IPaymentsService } from '../../@types/services'
129
import { IRateLimiter } from '../../@types/utils'
1310
import { IUserRepository } from '../../@types/repositories'
14-
import { path } from 'ramda'
11+
12+
import { createLogger } from '../../factories/logger-factory'
13+
14+
import { escapeHtml, safeJsonForScript } from '../../utils/html'
15+
import { fromBech32, toBech32 } from '../../utils/transform'
16+
import { getPublicKey, getRelayPrivateKey } from '../../utils/event'
17+
import { getRemoteAddress } from '../../utils/http'
1518
import { getTemplate } from '../../utils/template-cache'
1619

1720

src/handlers/request-handlers/get-privacy-request-handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { NextFunction, Request, Response } from 'express'
2+
23
import { createSettings as settings } from '../../factories/settings-factory'
4+
35
import { escapeHtml } from '../../utils/html'
6+
47
import { getTemplate } from '../../utils/template-cache'
58

69
export const getPrivacyRequestHandler = (_req: Request, res: Response, next: NextFunction) => {

src/handlers/request-handlers/get-terms-request-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextFunction, Request, Response } from 'express'
2-
import { createSettings as settings } from '../../factories/settings-factory'
32
import { escapeHtml } from '../../utils/html'
43
import { getTemplate } from '../../utils/template-cache'
4+
import { createSettings as settings } from '../../factories/settings-factory'
55

66

77

src/handlers/request-handlers/root-request-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { NextFunction, Request, Response } from 'express'
22
import { path, pathEq } from 'ramda'
33
import { createSettings } from '../../factories/settings-factory'
4+
import { escapeHtml } from '../../utils/html'
45
import { FeeSchedule } from '../../@types/settings'
56
import { fromBech32 } from '../../utils/transform'
6-
import { escapeHtml } from '../../utils/html'
77
import { getTemplate } from '../../utils/template-cache'
88
import packageJson from '../../../package.json'
99

test/unit/controllers/invoices/post-invoice-controller.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ chai.use(sinonChai)
77
chai.use(chaiAsPromised)
88
const { expect } = chai
99

10-
import * as templateCache from '../../../../src/utils/template-cache'
1110
import * as eventUtils from '../../../../src/utils/event'
11+
import * as templateCache from '../../../../src/utils/template-cache'
1212
import { PostInvoiceController } from '../../../../src/controllers/invoices/post-invoice-controller'
1313

1414
const VALID_PUBKEY = 'a'.repeat(64)
@@ -26,6 +26,9 @@ const baseSettings = {
2626
},
2727
},
2828
limits: {},
29+
network: {
30+
remoteIpHeader: 'x-forwarded-for',
31+
},
2932
}
3033

3134
const makeController = (overrides: {
@@ -48,7 +51,7 @@ const makeController = (overrides: {
4851
)
4952
}
5053

51-
const makeRes = () => ({
54+
const makeRes = (): any => ({
5255
status: sinon.stub().returnsThis(),
5356
setHeader: sinon.stub().returnsThis(),
5457
send: sinon.stub().returnsThis(),
@@ -148,6 +151,7 @@ describe('PostInvoiceController', () => {
148151
it('returns 400 when pubkey is missing', async () => {
149152
const controller = makeController()
150153
const res = makeRes()
154+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
151155
const { pubkey: _, ...bodyWithoutPubkey } = validBody
152156

153157
await controller.handleRequest({ body: bodyWithoutPubkey } as any, res)

test/unit/utils/html.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('escapeHtml', () => {
2626
})
2727

2828
it('escapes all special characters together', () => {
29-
expect(escapeHtml(`<script>alert("it's a & test")</script>`)).to.equal(
29+
expect(escapeHtml('<script>alert("it\'s a & test")</script>')).to.equal(
3030
'&lt;script&gt;alert(&quot;it&#39;s a &amp; test&quot;)&lt;/script&gt;'
3131
)
3232
})
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,38 @@
11
import chai from 'chai'
2-
import sinon from 'sinon'
3-
import sinonChai from 'sinon-chai'
42

5-
chai.use(sinonChai)
63
const { expect } = chai
74

85
import * as fs from 'fs'
96
import { getTemplate } from '../../../src/utils/template-cache'
7+
import { join } from 'path'
8+
import { tmpdir } from 'os'
109

1110
describe('getTemplate', () => {
12-
let readFileSyncStub: sinon.SinonStub
11+
const tmpFile = join(tmpdir(), 'test-template.html')
1312

1413
beforeEach(() => {
15-
readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('template content' as any)
14+
fs.writeFileSync(tmpFile, 'template content')
1615
})
1716

1817
afterEach(() => {
19-
readFileSyncStub.restore()
18+
try { fs.unlinkSync(tmpFile) } catch (e) { /* ignore */ }
2019
})
2120

2221
it('returns the file content', () => {
23-
const result = getTemplate('./resources/index.html')
24-
22+
const result = getTemplate(tmpFile)
2523
expect(result).to.equal('template content')
2624
})
2725

28-
it('reads the file with utf8 encoding', () => {
29-
getTemplate('./resources/index.html')
30-
31-
expect(readFileSyncStub).to.have.been.calledWith('./resources/index.html', 'utf8')
32-
})
33-
3426
it('reads the file on every call in non-production mode', () => {
3527
// NODE_ENV is not 'production' in tests — cache is bypassed
36-
getTemplate('./resources/index.html')
37-
getTemplate('./resources/index.html')
28+
getTemplate(tmpFile)
29+
fs.writeFileSync(tmpFile, 'updated content')
30+
const result2 = getTemplate(tmpFile)
3831

39-
expect(readFileSyncStub).to.have.been.calledTwice
32+
expect(result2).to.equal('updated content')
4033
})
4134

4235
it('propagates errors thrown by readFileSync', () => {
43-
readFileSyncStub.throws(new Error('file not found'))
44-
45-
expect(() => getTemplate('./resources/missing.html')).to.throw('file not found')
36+
expect(() => getTemplate('./does-not-exist.html')).to.throw(/ENOENT/)
4637
})
4738
})

0 commit comments

Comments
 (0)