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 { 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 './jquery/showpassword.js'
@ -138,7 +138,7 @@ window.addEventListener('DOMContentLoaded', function() {
t('core', 'Strong password'),
],
drawTitles: true,
nonce: btoa(getToken()),
nonce: btoa(getRequestToken()),
})
$('#dbpass').showPassword().keyup()

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

@ -5,6 +5,7 @@
import type { Config } from 'jest'
// 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 = [
'@buttercup/fetch',
'@juliushaertl',
@ -19,6 +20,7 @@ const ignorePatterns = [
'is-svg',
'layerr',
'mime',
'node-fetch',
'p-cancelable',
'p-limit',
'p-queue',
@ -68,7 +70,7 @@ const config: Config = {
}],
},
transformIgnorePatterns: [
'node_modules/(?!(' + ignorePatterns.join('|') + ')/)',
// 'node_modules/(?!(' + ignorePatterns.join('|') + ')/)',
],
// Allow mocking svg files