forked from Cutlery/immich
		
	fix(server): validation events actually throwing an error (#8172)
* fix validation events * add e2e test
This commit is contained in:
		
							parent
							
								
									508f32c08a
								
							
						
					
					
						commit
						d6823b128c
					
				@ -1,10 +1,12 @@
 | 
				
			|||||||
import { LoginResponseDto } from '@immich/sdk';
 | 
					import { LoginResponseDto, getConfig } from '@immich/sdk';
 | 
				
			||||||
import { createUserDto } from 'src/fixtures';
 | 
					import { createUserDto } from 'src/fixtures';
 | 
				
			||||||
import { errorDto } from 'src/responses';
 | 
					import { errorDto } from 'src/responses';
 | 
				
			||||||
import { app, utils } from 'src/utils';
 | 
					import { app, asBearerAuth, utils } from 'src/utils';
 | 
				
			||||||
import request from 'supertest';
 | 
					import request from 'supertest';
 | 
				
			||||||
import { beforeAll, describe, expect, it } from 'vitest';
 | 
					import { beforeAll, describe, expect, it } from 'vitest';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const getSystemConfig = (accessToken: string) => getConfig({ headers: asBearerAuth(accessToken) });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe('/system-config', () => {
 | 
					describe('/system-config', () => {
 | 
				
			||||||
  let admin: LoginResponseDto;
 | 
					  let admin: LoginResponseDto;
 | 
				
			||||||
  let nonAdmin: LoginResponseDto;
 | 
					  let nonAdmin: LoginResponseDto;
 | 
				
			||||||
@ -60,4 +62,25 @@ describe('/system-config', () => {
 | 
				
			|||||||
      expect(body).toEqual(expect.objectContaining({ id: 'immich-map-dark' }));
 | 
					      expect(body).toEqual(expect.objectContaining({ id: 'immich-map-dark' }));
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  describe('PUT /system-config', () => {
 | 
				
			||||||
 | 
					    it('should require authentication', async () => {
 | 
				
			||||||
 | 
					      const { status, body } = await request(app).put('/system-config');
 | 
				
			||||||
 | 
					      expect(status).toBe(401);
 | 
				
			||||||
 | 
					      expect(body).toEqual(errorDto.unauthorized);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('should reject an invalid config entry', async () => {
 | 
				
			||||||
 | 
					      const { status, body } = await request(app)
 | 
				
			||||||
 | 
					        .put('/system-config')
 | 
				
			||||||
 | 
					        .set('Authorization', `Bearer ${admin.accessToken}`)
 | 
				
			||||||
 | 
					        .send({
 | 
				
			||||||
 | 
					          ...(await getSystemConfig(admin.accessToken)),
 | 
				
			||||||
 | 
					          storageTemplate: { enabled: true, hashVerificationEnabled: true, template: '{{foo}}' },
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      expect(status).toBe(400);
 | 
				
			||||||
 | 
					      expect(body).toEqual(errorDto.badRequest(expect.stringContaining('Invalid storage template')));
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,6 @@
 | 
				
			|||||||
import { SetMetadata } from '@nestjs/common';
 | 
					import { SetMetadata } from '@nestjs/common';
 | 
				
			||||||
 | 
					import { OnEvent, OnEventType } from '@nestjs/event-emitter';
 | 
				
			||||||
 | 
					import { OnEventOptions } from '@nestjs/event-emitter/dist/interfaces';
 | 
				
			||||||
import _ from 'lodash';
 | 
					import _ from 'lodash';
 | 
				
			||||||
import { setUnion } from 'src/utils/set';
 | 
					import { setUnion } from 'src/utils/set';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -122,3 +124,6 @@ export interface GenerateSqlQueries {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/** Decorator to enable versioning/tracking of generated Sql */
 | 
					/** Decorator to enable versioning/tracking of generated Sql */
 | 
				
			||||||
export const GenerateSql = (...options: GenerateSqlQueries[]) => SetMetadata(GENERATE_SQL_KEY, options);
 | 
					export const GenerateSql = (...options: GenerateSqlQueries[]) => SetMetadata(GENERATE_SQL_KEY, options);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const OnEventInternal = (event: OnEventType, options?: OnEventOptions) =>
 | 
				
			||||||
 | 
					  OnEvent(event, { suppressErrors: false, ...options });
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,4 @@
 | 
				
			|||||||
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
 | 
					import { BadRequestException, Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import { OnEvent } from '@nestjs/event-emitter';
 | 
					 | 
				
			||||||
import { Trie } from 'mnemonist';
 | 
					import { Trie } from 'mnemonist';
 | 
				
			||||||
import { R_OK } from 'node:constants';
 | 
					import { R_OK } from 'node:constants';
 | 
				
			||||||
import { EventEmitter } from 'node:events';
 | 
					import { EventEmitter } from 'node:events';
 | 
				
			||||||
@ -8,6 +7,7 @@ import path, { basename, parse } from 'node:path';
 | 
				
			|||||||
import picomatch from 'picomatch';
 | 
					import picomatch from 'picomatch';
 | 
				
			||||||
import { StorageCore } from 'src/cores/storage.core';
 | 
					import { StorageCore } from 'src/cores/storage.core';
 | 
				
			||||||
import { SystemConfigCore } from 'src/cores/system-config.core';
 | 
					import { SystemConfigCore } from 'src/cores/system-config.core';
 | 
				
			||||||
 | 
					import { OnEventInternal } from 'src/decorators';
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
  CreateLibraryDto,
 | 
					  CreateLibraryDto,
 | 
				
			||||||
  LibraryResponseDto,
 | 
					  LibraryResponseDto,
 | 
				
			||||||
@ -105,7 +105,7 @@ export class LibraryService extends EventEmitter {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @OnEvent(InternalEvent.VALIDATE_CONFIG)
 | 
					  @OnEventInternal(InternalEvent.VALIDATE_CONFIG)
 | 
				
			||||||
  validateConfig({ newConfig }: InternalEventMap[InternalEvent.VALIDATE_CONFIG]) {
 | 
					  validateConfig({ newConfig }: InternalEventMap[InternalEvent.VALIDATE_CONFIG]) {
 | 
				
			||||||
    const { scan } = newConfig.library;
 | 
					    const { scan } = newConfig.library;
 | 
				
			||||||
    if (!validateCronExpression(scan.cronExpression)) {
 | 
					    if (!validateCronExpression(scan.cronExpression)) {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,4 @@
 | 
				
			|||||||
import { Inject, Injectable } from '@nestjs/common';
 | 
					import { Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import { OnEvent } from '@nestjs/event-emitter';
 | 
					 | 
				
			||||||
import handlebar from 'handlebars';
 | 
					import handlebar from 'handlebars';
 | 
				
			||||||
import { DateTime } from 'luxon';
 | 
					import { DateTime } from 'luxon';
 | 
				
			||||||
import path from 'node:path';
 | 
					import path from 'node:path';
 | 
				
			||||||
@ -15,6 +14,7 @@ import {
 | 
				
			|||||||
} from 'src/constants';
 | 
					} from 'src/constants';
 | 
				
			||||||
import { StorageCore, StorageFolder } from 'src/cores/storage.core';
 | 
					import { StorageCore, StorageFolder } from 'src/cores/storage.core';
 | 
				
			||||||
import { SystemConfigCore } from 'src/cores/system-config.core';
 | 
					import { SystemConfigCore } from 'src/cores/system-config.core';
 | 
				
			||||||
 | 
					import { OnEventInternal } from 'src/decorators';
 | 
				
			||||||
import { AssetEntity, AssetType } from 'src/entities/asset.entity';
 | 
					import { AssetEntity, AssetType } from 'src/entities/asset.entity';
 | 
				
			||||||
import { AssetPathType } from 'src/entities/move.entity';
 | 
					import { AssetPathType } from 'src/entities/move.entity';
 | 
				
			||||||
import { SystemConfig } from 'src/entities/system-config.entity';
 | 
					import { SystemConfig } from 'src/entities/system-config.entity';
 | 
				
			||||||
@ -86,7 +86,7 @@ export class StorageTemplateService {
 | 
				
			|||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @OnEvent(InternalEvent.VALIDATE_CONFIG)
 | 
					  @OnEventInternal(InternalEvent.VALIDATE_CONFIG)
 | 
				
			||||||
  validate({ newConfig }: InternalEventMap[InternalEvent.VALIDATE_CONFIG]) {
 | 
					  validate({ newConfig }: InternalEventMap[InternalEvent.VALIDATE_CONFIG]) {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const { compiled } = this.compile(newConfig.storageTemplate.template);
 | 
					      const { compiled } = this.compile(newConfig.storageTemplate.template);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,4 @@
 | 
				
			|||||||
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
 | 
					import { BadRequestException, Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import { OnEvent } from '@nestjs/event-emitter';
 | 
					 | 
				
			||||||
import { instanceToPlain } from 'class-transformer';
 | 
					import { instanceToPlain } from 'class-transformer';
 | 
				
			||||||
import _ from 'lodash';
 | 
					import _ from 'lodash';
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
@ -13,6 +12,7 @@ import {
 | 
				
			|||||||
  supportedYearTokens,
 | 
					  supportedYearTokens,
 | 
				
			||||||
} from 'src/constants';
 | 
					} from 'src/constants';
 | 
				
			||||||
import { SystemConfigCore } from 'src/cores/system-config.core';
 | 
					import { SystemConfigCore } from 'src/cores/system-config.core';
 | 
				
			||||||
 | 
					import { OnEventInternal } from 'src/decorators';
 | 
				
			||||||
import { SystemConfigTemplateStorageOptionDto } from 'src/dtos/system-config-storage-template.dto';
 | 
					import { SystemConfigTemplateStorageOptionDto } from 'src/dtos/system-config-storage-template.dto';
 | 
				
			||||||
import { SystemConfigDto, mapConfig } from 'src/dtos/system-config.dto';
 | 
					import { SystemConfigDto, mapConfig } from 'src/dtos/system-config.dto';
 | 
				
			||||||
import { LogLevel, SystemConfig } from 'src/entities/system-config.entity';
 | 
					import { LogLevel, SystemConfig } from 'src/entities/system-config.entity';
 | 
				
			||||||
@ -61,7 +61,7 @@ export class SystemConfigService {
 | 
				
			|||||||
    return mapConfig(config);
 | 
					    return mapConfig(config);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @OnEvent(InternalEvent.VALIDATE_CONFIG)
 | 
					  @OnEventInternal(InternalEvent.VALIDATE_CONFIG)
 | 
				
			||||||
  validateConfig({ newConfig, oldConfig }: InternalEventMap[InternalEvent.VALIDATE_CONFIG]) {
 | 
					  validateConfig({ newConfig, oldConfig }: InternalEventMap[InternalEvent.VALIDATE_CONFIG]) {
 | 
				
			||||||
    if (!_.isEqual(instanceToPlain(newConfig.logging), oldConfig.logging) && this.getEnvLogLevel()) {
 | 
					    if (!_.isEqual(instanceToPlain(newConfig.logging), oldConfig.logging) && this.getEnvLogLevel()) {
 | 
				
			||||||
      throw new Error('Logging cannot be changed while the environment variable LOG_LEVEL is set.');
 | 
					      throw new Error('Logging cannot be changed while the environment variable LOG_LEVEL is set.');
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user