fix: adjust code

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
pull/53521/head
Ferdinand Thiessen 2025-06-17 00:28:19 +07:00
parent afc8bce9d0
commit 50fd4418fa
No known key found for this signature in database
GPG Key ID: 45FAE7268762B400
3 changed files with 32 additions and 28 deletions

@ -5,7 +5,7 @@
import $ from 'jquery' import $ from 'jquery'
import { translate as t } from '@nextcloud/l10n' import { translate as t } from '@nextcloud/l10n'
import { getToken } from './OC/requesttoken.js' import { getRequestToken } from './OC/requesttoken.ts'
import getURLParameter from './Util/get-url-parameter.js' import getURLParameter from './Util/get-url-parameter.js'
import './jquery/showpassword.js' import './jquery/showpassword.js'
@ -138,7 +138,7 @@ window.addEventListener('DOMContentLoaded', function() {
t('core', 'Strong password'), t('core', 'Strong password'),
], ],
drawTitles: true, drawTitles: true,
nonce: btoa(getToken()), nonce: btoa(getRequestToken()),
}) })
$('#dbpass').showPassword().keyup() $('#dbpass').showPassword().keyup()

@ -3,26 +3,28 @@
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
*/ */
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' import { beforeAll, beforeEach, describe, expect, it } from '@jest/globals'
const requestToken = vi.hoisted(() => ({ // eslint-disable-next-line no-var
fetchRequestToken: vi.fn<() => Promise<string>>(), var requestToken = {
setRequestToken: vi.fn<(token: string) => void>(), fetchRequestToken: jest.fn<Promise<string>, []>(),
})) setRequestToken: jest.fn<void, [string]>(),
vi.mock('../../OC/requesttoken.ts', () => requestToken) }
jest.mock('../../OC/requesttoken.ts', () => requestToken)
const initialState = vi.hoisted(() => ({ loadState: vi.fn() })) // eslint-disable-next-line no-var
vi.mock('@nextcloud/initial-state', () => initialState) var initialState = { loadState: jest.fn() }
jest.mock('@nextcloud/initial-state', () => initialState)
describe('Session heartbeat', () => { describe('Session heartbeat', () => {
beforeAll(() => { beforeAll(() => {
vi.useFakeTimers() jest.useFakeTimers()
}) })
beforeEach(() => { beforeEach(() => {
vi.clearAllTimers() jest.clearAllTimers()
vi.resetModules() jest.resetModules()
vi.resetAllMocks() jest.resetAllMocks()
}) })
it('sends heartbeat half the session lifetime when heartbeat enabled', async () => { it('sends heartbeat half the session lifetime when heartbeat enabled', async () => {
@ -38,19 +40,19 @@ describe('Session heartbeat', () => {
expect(initialState.loadState).toBeCalledWith('core', 'config', {}) expect(initialState.loadState).toBeCalledWith('core', 'config', {})
// less than half, still nothing // less than half, still nothing
await vi.advanceTimersByTimeAsync(100 * 1000) await jest.advanceTimersByTimeAsync(100 * 1000)
expect(requestToken.fetchRequestToken).not.toBeCalled() expect(requestToken.fetchRequestToken).not.toBeCalled()
// reach past half, one call // reach past half, one call
await vi.advanceTimersByTimeAsync(60 * 1000) await jest.advanceTimersByTimeAsync(60 * 1000)
expect(requestToken.fetchRequestToken).toBeCalledTimes(1) expect(requestToken.fetchRequestToken).toBeCalledTimes(1)
// almost there to the next, still one // almost there to the next, still one
await vi.advanceTimersByTimeAsync(135 * 1000) await jest.advanceTimersByTimeAsync(135 * 1000)
expect(requestToken.fetchRequestToken).toBeCalledTimes(1) expect(requestToken.fetchRequestToken).toBeCalledTimes(1)
// past it, second call // past it, second call
await vi.advanceTimersByTimeAsync(5 * 1000) await jest.advanceTimersByTimeAsync(5 * 1000)
expect(requestToken.fetchRequestToken).toBeCalledTimes(2) expect(requestToken.fetchRequestToken).toBeCalledTimes(2)
}) })
@ -67,11 +69,11 @@ describe('Session heartbeat', () => {
expect(initialState.loadState).toBeCalledWith('core', 'config', {}) expect(initialState.loadState).toBeCalledWith('core', 'config', {})
// less than half, still nothing // less than half, still nothing
await vi.advanceTimersByTimeAsync(100 * 1000) await jest.advanceTimersByTimeAsync(100 * 1000)
expect(requestToken.fetchRequestToken).not.toBeCalled() expect(requestToken.fetchRequestToken).not.toBeCalled()
// more than one, still nothing // more than one, still nothing
await vi.advanceTimersByTimeAsync(300 * 1000) await jest.advanceTimersByTimeAsync(300 * 1000)
expect(requestToken.fetchRequestToken).not.toBeCalled() expect(requestToken.fetchRequestToken).not.toBeCalled()
}) })
@ -88,16 +90,16 @@ describe('Session heartbeat', () => {
expect(initialState.loadState).toBeCalledWith('core', 'config', {}) expect(initialState.loadState).toBeCalledWith('core', 'config', {})
// 30 / 55 seconds // 30 / 55 seconds
await vi.advanceTimersByTimeAsync(30 * 1000) await jest.advanceTimersByTimeAsync(30 * 1000)
expect(requestToken.fetchRequestToken).not.toBeCalled() expect(requestToken.fetchRequestToken).not.toBeCalled()
// 59 / 55 seconds should not be called except it does not limit // 59 / 55 seconds should not be called except it does not limit
await vi.advanceTimersByTimeAsync(29 * 1000) await jest.advanceTimersByTimeAsync(29 * 1000)
expect(requestToken.fetchRequestToken).not.toBeCalled() expect(requestToken.fetchRequestToken).not.toBeCalled()
// now one minute has passed // now one minute has passed
await vi.advanceTimersByTimeAsync(1000) await jest.advanceTimersByTimeAsync(1000)
expect(requestToken.fetchRequestToken).toHaveBeenCalledOnce() expect(requestToken.fetchRequestToken).toBeCalledTimes(1)
}) })
it('limit heartbeat to at least one minute', async () => { it('limit heartbeat to at least one minute', async () => {
@ -113,11 +115,11 @@ describe('Session heartbeat', () => {
expect(initialState.loadState).toBeCalledWith('core', 'config', {}) expect(initialState.loadState).toBeCalledWith('core', 'config', {})
// 23 hours // 23 hours
await vi.advanceTimersByTimeAsync(23 * 60 * 60 * 1000) await jest.advanceTimersByTimeAsync(23 * 60 * 60 * 1000)
expect(requestToken.fetchRequestToken).not.toBeCalled() expect(requestToken.fetchRequestToken).not.toBeCalled()
// one day - it should be called now // one day - it should be called now
await vi.advanceTimersByTimeAsync(60 * 60 * 1000) await jest.advanceTimersByTimeAsync(60 * 60 * 1000)
expect(requestToken.fetchRequestToken).toHaveBeenCalledOnce() expect(requestToken.fetchRequestToken).toBeCalledTimes(1)
}) })
}) })

@ -5,6 +5,7 @@
import type { Config } from 'jest' import type { Config } from 'jest'
// TODO: find a way to consolidate this in one place, with webpack.common.js // TODO: find a way to consolidate this in one place, with webpack.common.js
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const ignorePatterns = [ const ignorePatterns = [
'@buttercup/fetch', '@buttercup/fetch',
'@juliushaertl', '@juliushaertl',
@ -19,6 +20,7 @@ const ignorePatterns = [
'is-svg', 'is-svg',
'layerr', 'layerr',
'mime', 'mime',
'node-fetch',
'p-cancelable', 'p-cancelable',
'p-limit', 'p-limit',
'p-queue', 'p-queue',
@ -68,7 +70,7 @@ const config: Config = {
}], }],
}, },
transformIgnorePatterns: [ transformIgnorePatterns: [
'node_modules/(?!(' + ignorePatterns.join('|') + ')/)', // 'node_modules/(?!(' + ignorePatterns.join('|') + ')/)',
], ],
// Allow mocking svg files // Allow mocking svg files