Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/namespaces/namespaces.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import {
Patch,
Delete,
Controller,
ValidationPipe,
} from '@nestjs/common';
import { UserId } from 'omniboxd/decorators/user-id.decorator';
import { CreateNamespaceDto } from './dto/create-namespace.dto';
import { UpdateNamespaceDto } from './dto/update-namespace.dto';

@Controller('api/v1/namespaces')
export class NamespacesController {
Expand Down Expand Up @@ -69,19 +72,19 @@ export class NamespacesController {
}

@Post()
async create(@Req() req, @Body('name') name: string) {
async create(@Req() req, @Body() createDto: CreateNamespaceDto) {
return await this.namespacesService.createAndJoinNamespace(
req.user.id,
name,
createDto.name,
);
}

@Patch(':namespaceId')
async update(
@Param('namespaceId') namespaceId: string,
@Body('name') name: string,
@Body() updateDto: UpdateNamespaceDto,
) {
return await this.namespacesService.update(namespaceId, { name });
return await this.namespacesService.update(namespaceId, updateDto);
}

@Delete(':namespaceId')
Expand Down
54 changes: 36 additions & 18 deletions src/namespaces/namespaces.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,10 @@ describe('NamespacesController (e2e)', () => {
name: '',
};

const response = await client
await client
.post('/api/v1/namespaces')
.send(createNamespaceDto)
.expect(HttpStatus.CREATED);

expect(response.body).toHaveProperty('id');
expect(response.body.name).toBe('');

// Clean up
await client.delete(`/api/v1/namespaces/${response.body.id}`);
.expect(HttpStatus.BAD_REQUEST);
});

it('should fail to create namespace without name', async () => {
Expand All @@ -81,7 +75,31 @@ describe('NamespacesController (e2e)', () => {
await client
.post('/api/v1/namespaces')
.send(createNamespaceDto)
.expect(HttpStatus.INTERNAL_SERVER_ERROR);
.expect(HttpStatus.BAD_REQUEST);
});

it('should fail to create namespace with duplicate name', async () => {
const createNamespaceDto = {
name: 'Duplicate Test Workspace',
};

// First, create a namespace
const firstResponse = await client
.post('/api/v1/namespaces')
.send(createNamespaceDto)
.expect(HttpStatus.CREATED);

// Try to create another namespace with the same name
const response = await client
.post('/api/v1/namespaces')
.send(createNamespaceDto)
.expect(HttpStatus.CONFLICT);

expect(response.body).toHaveProperty('code');
expect(response.body.code).toBe('namespace_conflict');

// Clean up the first namespace
await client.delete(`/api/v1/namespaces/${firstResponse.body.id}`);
});
});

Expand Down Expand Up @@ -349,7 +367,7 @@ describe('NamespacesController (e2e)', () => {
await client
.patch(`/api/v1/namespaces/${updateTestNamespaceId}`)
.send(updateNamespaceDto)
.expect(HttpStatus.OK); // Empty name might be allowed, depending on validation
.expect(HttpStatus.BAD_REQUEST); // Empty name might be allowed, depending on validation
});
});

Expand Down Expand Up @@ -503,19 +521,19 @@ describe('NamespacesController (e2e)', () => {
await client
.post('/api/v1/namespaces')
.send({ name: null })
.expect(HttpStatus.INTERNAL_SERVER_ERROR);

// Test update with null name
await client
.patch(`/api/v1/namespaces/${edgeTestNamespaceId}`)
.send({ name: null })
.expect(HttpStatus.INTERNAL_SERVER_ERROR);
.expect(HttpStatus.BAD_REQUEST);

// Test with undefined in request body
await client
.post('/api/v1/namespaces')
.send({ name: undefined })
.expect(HttpStatus.INTERNAL_SERVER_ERROR);
.expect(HttpStatus.BAD_REQUEST);

// Test update with null name
await client
.patch(`/api/v1/namespaces/${edgeTestNamespaceId}`)
.send({ name: null })
.expect(HttpStatus.OK);
});
});
});
19 changes: 11 additions & 8 deletions src/namespaces/namespaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ export class NamespacesService {
name: string,
manager: EntityManager,
): Promise<Namespace> {
if ((await manager.countBy(Namespace, { name })) > 0) {
throw new ConflictException({ code: 'namespace_conflict' });
}
const namespace = await manager.save(manager.create(Namespace, { name }));
const publicRoot = await this.resourcesService.createResource(
{
Expand All @@ -166,20 +169,20 @@ export class NamespacesService {

async update(
id: string,
updateNamespace: UpdateNamespaceDto,
updateDto: UpdateNamespaceDto,
manager?: EntityManager,
) {
const repo = manager
? manager.getRepository(Namespace)
: this.namespaceRepository;
const existNamespace = await this.getNamespace(id, manager);
if (!existNamespace) {
throw new ConflictException('The current namespace does not exist');
const namespace = await this.getNamespace(id, manager);
if (updateDto.name && updateDto.name !== namespace.name) {
if ((await repo.countBy({ name: updateDto.name })) > 0) {
throw new ConflictException({ code: 'namespace_conflict' });
}
namespace.name = updateDto.name;
}
each(updateNamespace, (value, key) => {
existNamespace[key] = value;
});
return await repo.update(id, existNamespace);
return await repo.update(id, namespace);
}

async delete(id: string) {
Expand Down