1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-14 01:16:17 +02:00

chore: return 404 when adding environment to project that doesnt exist (#4635)

## About the changes

Returns a 404 response instead of 500 when trying to set an environment
on a project that doesn't exist
This commit is contained in:
David Leek 2023-09-08 12:36:13 +02:00 committed by GitHub
parent 2aa63fbacd
commit 10a62642d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -17,7 +17,7 @@ import {
getStandardResponses, getStandardResponses,
ProjectEnvironmentSchema, ProjectEnvironmentSchema,
} from '../../../openapi'; } from '../../../openapi';
import { OpenApiService } from '../../../services'; import { OpenApiService, ProjectService } from '../../../services';
const PREFIX = '/:projectId/environments'; const PREFIX = '/:projectId/environments';
@ -33,18 +33,25 @@ export default class EnvironmentsController extends Controller {
private openApiService: OpenApiService; private openApiService: OpenApiService;
private projectService: ProjectService;
constructor( constructor(
config: IUnleashConfig, config: IUnleashConfig,
{ {
environmentService, environmentService,
openApiService, openApiService,
}: Pick<IUnleashServices, 'environmentService' | 'openApiService'>, projectService,
}: Pick<
IUnleashServices,
'environmentService' | 'openApiService' | 'projectService'
>,
) { ) {
super(config); super(config);
this.logger = config.getLogger('admin-api/project/environments.ts'); this.logger = config.getLogger('admin-api/project/environments.ts');
this.environmentService = environmentService; this.environmentService = environmentService;
this.openApiService = openApiService; this.openApiService = openApiService;
this.projectService = projectService;
this.route({ this.route({
method: 'post', method: 'post',
@ -126,6 +133,7 @@ export default class EnvironmentsController extends Controller {
): Promise<void> { ): Promise<void> {
const { projectId } = req.params; const { projectId } = req.params;
const { environment } = req.body; const { environment } = req.body;
await this.projectService.getProject(projectId); // Validates that the project exists
await this.environmentService.addEnvironmentToProject( await this.environmentService.addEnvironmentToProject(
environment, environment,

View File

@ -159,3 +159,12 @@ test('Should throw an error if you try to set defaultStrategy other than flexibl
}) })
.expect(400); .expect(400);
}); });
test('Add environment to project should return 404 when given a projectid that does not exist', async () => {
await app.request
.post(`/api/admin/projects/unknown/environments`)
.send({
environment: 'default',
})
.expect(404);
});