Fix server integration tests 2 (#10818)
## Context - Removing search* integration tests instead of fixing them because they will be replaced by global search very soon - Fixed billing + add missing seeds to make them work - Fixed integration tests not using consistently the correct "test" db - Fixed ci not running the with-db-reset configuration due to nx configuration being used twice for different level of the command - Enriched .env.test - Fixed parts where exceptions were not thrown properly and not caught by exception handler to convert to 400 when needed - Refactored feature flag service that had 2 different implementations in lab and admin panel + added tests - Fixed race condition when migrations are created at the same timestamp and doing the same type of operation, in this case object deletion could break because table could be deleted earlier than its relations - Fixed many integration tests that were not up to date since the CI has been broken for a while --------- Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
This commit is contained in:
@ -9,29 +9,12 @@ import {
|
||||
import { LoginTokenService } from 'src/engine/core-modules/auth/token/services/login-token.service';
|
||||
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service';
|
||||
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
|
||||
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
|
||||
import { FeatureFlag } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
|
||||
const UserFindOneMock = jest.fn();
|
||||
const WorkspaceFindOneMock = jest.fn();
|
||||
const FeatureFlagUpdateMock = jest.fn();
|
||||
const FeatureFlagSaveMock = jest.fn();
|
||||
const LoginTokenServiceGenerateLoginTokenMock = jest.fn();
|
||||
const EnvironmentServiceGetAllMock = jest.fn();
|
||||
|
||||
jest.mock(
|
||||
'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum',
|
||||
() => {
|
||||
return {
|
||||
FeatureFlagKey: {
|
||||
IsFlagEnabled: 'IS_FLAG_ENABLED',
|
||||
},
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
jest.mock(
|
||||
'../../environment/constants/environment-variables-group-metadata',
|
||||
() => ({
|
||||
@ -62,25 +45,12 @@ describe('AdminPanelService', () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
AdminPanelService,
|
||||
{
|
||||
provide: getRepositoryToken(Workspace, 'core'),
|
||||
useValue: {
|
||||
findOne: WorkspaceFindOneMock,
|
||||
},
|
||||
},
|
||||
{
|
||||
provide: getRepositoryToken(User, 'core'),
|
||||
useValue: {
|
||||
findOne: UserFindOneMock,
|
||||
},
|
||||
},
|
||||
{
|
||||
provide: getRepositoryToken(FeatureFlag, 'core'),
|
||||
useValue: {
|
||||
update: FeatureFlagUpdateMock,
|
||||
save: FeatureFlagSaveMock,
|
||||
},
|
||||
},
|
||||
{
|
||||
provide: LoginTokenService,
|
||||
useValue: {
|
||||
@ -112,80 +82,6 @@ describe('AdminPanelService', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('should update an existing feature flag if it exists', async () => {
|
||||
const workspaceId = 'workspace-id';
|
||||
const featureFlag = 'IsFlagEnabled' as FeatureFlagKey;
|
||||
const value = true;
|
||||
const existingFlag = {
|
||||
id: 'flag-id',
|
||||
key: 'IS_FLAG_ENABLED',
|
||||
value: false,
|
||||
};
|
||||
|
||||
WorkspaceFindOneMock.mockReturnValueOnce({
|
||||
id: workspaceId,
|
||||
featureFlags: [existingFlag],
|
||||
});
|
||||
|
||||
await service.updateWorkspaceFeatureFlags(workspaceId, featureFlag, value);
|
||||
|
||||
expect(FeatureFlagUpdateMock).toHaveBeenCalledWith(existingFlag.id, {
|
||||
value,
|
||||
});
|
||||
expect(FeatureFlagSaveMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should create a new feature flag if it does not exist', async () => {
|
||||
const workspaceId = 'workspace-id';
|
||||
const featureFlag = 'IsFlagEnabled' as FeatureFlagKey;
|
||||
const value = true;
|
||||
|
||||
WorkspaceFindOneMock.mockReturnValueOnce({
|
||||
id: workspaceId,
|
||||
featureFlags: [],
|
||||
});
|
||||
|
||||
await service.updateWorkspaceFeatureFlags(workspaceId, featureFlag, value);
|
||||
|
||||
expect(FeatureFlagSaveMock).toHaveBeenCalledWith({
|
||||
key: 'IS_FLAG_ENABLED',
|
||||
value,
|
||||
workspaceId,
|
||||
});
|
||||
expect(FeatureFlagUpdateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw an exception if the workspace is not found', async () => {
|
||||
const workspaceId = 'non-existent-workspace';
|
||||
const featureFlag = 'IsFlagEnabled' as FeatureFlagKey;
|
||||
const value = true;
|
||||
|
||||
WorkspaceFindOneMock.mockReturnValueOnce(null);
|
||||
|
||||
await expect(
|
||||
service.updateWorkspaceFeatureFlags(workspaceId, featureFlag, value),
|
||||
).rejects.toThrowError(
|
||||
new AuthException('Workspace not found', AuthExceptionCode.INVALID_INPUT),
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw an exception if the flag is not found', async () => {
|
||||
const workspaceId = 'non-existent-workspace';
|
||||
const featureFlag = 'IsUnknownFlagEnabled' as FeatureFlagKey;
|
||||
const value = true;
|
||||
|
||||
WorkspaceFindOneMock.mockReturnValueOnce(null);
|
||||
|
||||
await expect(
|
||||
service.updateWorkspaceFeatureFlags(workspaceId, featureFlag, value),
|
||||
).rejects.toThrowError(
|
||||
new AuthException(
|
||||
'Invalid feature flag key',
|
||||
AuthExceptionCode.INVALID_INPUT,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it('should impersonate a user and return workspace and loginToken on success', async () => {
|
||||
const mockUser = {
|
||||
id: 'user-id',
|
||||
|
||||
@ -7,20 +7,20 @@ import { AdminPanelResolver } from 'src/engine/core-modules/admin-panel/admin-pa
|
||||
import { AdminPanelService } from 'src/engine/core-modules/admin-panel/admin-panel.service';
|
||||
import { AuthModule } from 'src/engine/core-modules/auth/auth.module';
|
||||
import { DomainManagerModule } from 'src/engine/core-modules/domain-manager/domain-manager.module';
|
||||
import { FeatureFlag } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
|
||||
import { HealthModule } from 'src/engine/core-modules/health/health.module';
|
||||
import { RedisClientModule } from 'src/engine/core-modules/redis-client/redis-client.module';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([User, Workspace, FeatureFlag], 'core'),
|
||||
TypeOrmModule.forFeature([User], 'core'),
|
||||
AuthModule,
|
||||
DomainManagerModule,
|
||||
HealthModule,
|
||||
RedisClientModule,
|
||||
TerminusModule,
|
||||
FeatureFlagModule,
|
||||
],
|
||||
providers: [AdminPanelResolver, AdminPanelService, AdminPanelHealthService],
|
||||
exports: [AdminPanelService],
|
||||
|
||||
@ -12,6 +12,9 @@ import { UserLookup } from 'src/engine/core-modules/admin-panel/dtos/user-lookup
|
||||
import { UserLookupInput } from 'src/engine/core-modules/admin-panel/dtos/user-lookup.input';
|
||||
import { QueueMetricsTimeRange } from 'src/engine/core-modules/admin-panel/enums/queue-metrics-time-range.enum';
|
||||
import { AuthGraphqlApiExceptionFilter } from 'src/engine/core-modules/auth/filters/auth-graphql-api-exception.filter';
|
||||
import { FeatureFlagException } from 'src/engine/core-modules/feature-flag/feature-flag.exception';
|
||||
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
|
||||
import { UserInputError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
||||
import { HealthIndicatorId } from 'src/engine/core-modules/health/enums/health-indicator-id.enum';
|
||||
import { WorkerHealthIndicator } from 'src/engine/core-modules/health/indicators/worker.health';
|
||||
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
|
||||
@ -30,6 +33,7 @@ export class AdminPanelResolver {
|
||||
private adminService: AdminPanelService,
|
||||
private adminPanelHealthService: AdminPanelHealthService,
|
||||
private workerHealthIndicator: WorkerHealthIndicator,
|
||||
private featureFlagService: FeatureFlagService,
|
||||
) {}
|
||||
|
||||
@UseGuards(WorkspaceAuthGuard, UserAuthGuard, ImpersonateGuard)
|
||||
@ -53,13 +57,21 @@ export class AdminPanelResolver {
|
||||
async updateWorkspaceFeatureFlag(
|
||||
@Args() updateFlagInput: UpdateWorkspaceFeatureFlagInput,
|
||||
): Promise<boolean> {
|
||||
await this.adminService.updateWorkspaceFeatureFlags(
|
||||
updateFlagInput.workspaceId,
|
||||
updateFlagInput.featureFlag,
|
||||
updateFlagInput.value,
|
||||
);
|
||||
try {
|
||||
await this.featureFlagService.upsertWorkspaceFeatureFlag({
|
||||
workspaceId: updateFlagInput.workspaceId,
|
||||
featureFlag: updateFlagInput.featureFlag,
|
||||
value: updateFlagInput.value,
|
||||
});
|
||||
|
||||
return true;
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (error instanceof FeatureFlagException) {
|
||||
throw new UserInputError(error.message);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@UseGuards(WorkspaceAuthGuard, UserAuthGuard, AdminPanelGuard)
|
||||
|
||||
@ -18,15 +18,8 @@ import { EnvironmentVariablesGroup } from 'src/engine/core-modules/environment/e
|
||||
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
|
||||
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
|
||||
import { FeatureFlag } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import {
|
||||
FeatureFlagException,
|
||||
FeatureFlagExceptionCode,
|
||||
} from 'src/engine/core-modules/feature-flag/feature-flag.exception';
|
||||
import { featureFlagValidator } from 'src/engine/core-modules/feature-flag/validates/feature-flag.validate';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { userValidator } from 'src/engine/core-modules/user/user.validate';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { workspaceValidator } from 'src/engine/core-modules/workspace/workspace.validate';
|
||||
|
||||
@Injectable()
|
||||
export class AdminPanelService {
|
||||
@ -36,10 +29,6 @@ export class AdminPanelService {
|
||||
private readonly domainManagerService: DomainManagerService,
|
||||
@InjectRepository(User, 'core')
|
||||
private readonly userRepository: Repository<User>,
|
||||
@InjectRepository(Workspace, 'core')
|
||||
private readonly workspaceRepository: Repository<Workspace>,
|
||||
@InjectRepository(FeatureFlag, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlag>,
|
||||
) {}
|
||||
|
||||
async impersonate(userId: string, workspaceId: string) {
|
||||
@ -131,44 +120,6 @@ export class AdminPanelService {
|
||||
};
|
||||
}
|
||||
|
||||
async updateWorkspaceFeatureFlags(
|
||||
workspaceId: string,
|
||||
featureFlag: FeatureFlagKey,
|
||||
value: boolean,
|
||||
) {
|
||||
featureFlagValidator.assertIsFeatureFlagKey(
|
||||
featureFlag,
|
||||
new FeatureFlagException(
|
||||
'Invalid feature flag key',
|
||||
FeatureFlagExceptionCode.INVALID_FEATURE_FLAG_KEY,
|
||||
),
|
||||
);
|
||||
|
||||
const workspace = await this.workspaceRepository.findOne({
|
||||
where: { id: workspaceId },
|
||||
relations: ['featureFlags'],
|
||||
});
|
||||
|
||||
workspaceValidator.assertIsDefinedOrThrow(
|
||||
workspace,
|
||||
new AuthException('Workspace not found', AuthExceptionCode.INVALID_INPUT),
|
||||
);
|
||||
|
||||
const existingFlag = workspace.featureFlags?.find(
|
||||
(flag) => flag.key === FeatureFlagKey[featureFlag],
|
||||
);
|
||||
|
||||
if (existingFlag) {
|
||||
await this.featureFlagRepository.update(existingFlag.id, { value });
|
||||
} else {
|
||||
await this.featureFlagRepository.save({
|
||||
key: FeatureFlagKey[featureFlag],
|
||||
value,
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getEnvironmentVariablesGrouped(): EnvironmentVariablesOutput {
|
||||
const rawEnvVars = this.environmentService.getAll();
|
||||
const groupedData = new Map<
|
||||
|
||||
Reference in New Issue
Block a user