|
@@ -2,8 +2,19 @@ import { vi } from 'vitest';
|
|
|
|
|
|
|
|
import { configManager } from '~/server/service/config-manager/config-manager';
|
|
import { configManager } from '~/server/service/config-manager/config-manager';
|
|
|
|
|
|
|
|
|
|
+import NamedQuery from '../models/named-query';
|
|
|
import SearchService from './search';
|
|
import SearchService from './search';
|
|
|
|
|
|
|
|
|
|
+vi.mock('~/server/models/named-query', () => {
|
|
|
|
|
+ const mockModel = {
|
|
|
|
|
+ findOne: vi.fn(),
|
|
|
|
|
+ };
|
|
|
|
|
+ return {
|
|
|
|
|
+ NamedQuery: mockModel,
|
|
|
|
|
+ default: mockModel,
|
|
|
|
|
+ };
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
// Intercept the singleton import inside the search service
|
|
// Intercept the singleton import inside the search service
|
|
|
vi.mock('~/server/service/config-manager/config-manager', () => {
|
|
vi.mock('~/server/service/config-manager/config-manager', () => {
|
|
|
return {
|
|
return {
|
|
@@ -43,11 +54,8 @@ describe('searchParseQuery()', () => {
|
|
|
searchService = new SearchService(mockCrowi);
|
|
searchService = new SearchService(mockCrowi);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('should contain /user in the query not_prefix when user pages are disabled', async () => {
|
|
|
|
|
- // Mock disableUserPages value
|
|
|
|
|
- vi.mocked(configManager.getConfig).mockImplementation(() => {
|
|
|
|
|
- return true;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ it('should contain /user in the not_prefix query when user pages are disabled', async () => {
|
|
|
|
|
+ vi.mocked(configManager.getConfig).mockReturnValue(true);
|
|
|
|
|
|
|
|
const result = await (searchService as any).parseSearchQuery(
|
|
const result = await (searchService as any).parseSearchQuery(
|
|
|
'/user/settings',
|
|
'/user/settings',
|
|
@@ -61,11 +69,23 @@ describe('searchParseQuery()', () => {
|
|
|
expect(result.terms.prefix).toHaveLength(0);
|
|
expect(result.terms.prefix).toHaveLength(0);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ it('should contain /user in the not_prefix even when search query is not a user page', async () => {
|
|
|
|
|
+ vi.mocked(configManager.getConfig).mockReturnValue(true);
|
|
|
|
|
+
|
|
|
|
|
+ const result = await (searchService as any).parseSearchQuery(
|
|
|
|
|
+ '/new-task',
|
|
|
|
|
+ null,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ expect(configManager.getConfig).toHaveBeenCalledWith(
|
|
|
|
|
+ 'security:disableUserPages',
|
|
|
|
|
+ );
|
|
|
|
|
+ expect(result.terms.not_prefix).toContain('/user');
|
|
|
|
|
+ expect(result.terms.prefix).toHaveLength(0);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
it('should add specific user prefixes in the query when user pages are enabled', async () => {
|
|
it('should add specific user prefixes in the query when user pages are enabled', async () => {
|
|
|
- // Mock disableUserPages value
|
|
|
|
|
- vi.mocked(configManager.getConfig).mockImplementation(() => {
|
|
|
|
|
- return false;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ vi.mocked(configManager.getConfig).mockReturnValue(false);
|
|
|
|
|
|
|
|
const result = await (searchService as any).parseSearchQuery(
|
|
const result = await (searchService as any).parseSearchQuery(
|
|
|
'/user/settings',
|
|
'/user/settings',
|
|
@@ -76,6 +96,31 @@ describe('searchParseQuery()', () => {
|
|
|
'security:disableUserPages',
|
|
'security:disableUserPages',
|
|
|
);
|
|
);
|
|
|
expect(result.terms.not_prefix).not.toContain('/user');
|
|
expect(result.terms.not_prefix).not.toContain('/user');
|
|
|
|
|
+ expect(result.terms.not_prefix).not.toContain('/user/settings');
|
|
|
expect(result.terms.match).toContain('/user/settings');
|
|
expect(result.terms.match).toContain('/user/settings');
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ it('should filter user pages even when resolved from a named query alias', async () => {
|
|
|
|
|
+ vi.mocked(configManager.getConfig).mockReturnValue(true);
|
|
|
|
|
+
|
|
|
|
|
+ const shortcutName = 'my-shortcut';
|
|
|
|
|
+ const aliasPath = '/user/my-private-page';
|
|
|
|
|
+
|
|
|
|
|
+ // Mock the DB response
|
|
|
|
|
+ vi.mocked(NamedQuery.findOne).mockResolvedValue({
|
|
|
|
|
+ name: shortcutName,
|
|
|
|
|
+ aliasOf: aliasPath,
|
|
|
|
|
+ } as any);
|
|
|
|
|
+
|
|
|
|
|
+ const result = await (searchService as any).parseSearchQuery(
|
|
|
|
|
+ 'dummy',
|
|
|
|
|
+ shortcutName,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ expect(configManager.getConfig).toHaveBeenCalledWith(
|
|
|
|
|
+ 'security:disableUserPages',
|
|
|
|
|
+ );
|
|
|
|
|
+ expect(result.terms.not_prefix).toContain('/user');
|
|
|
|
|
+ expect(result.terms.match).toContain('/user/my-private-page');
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|