fix e2e test

This commit is contained in:
wuzihao051119 2025-05-21 19:54:40 +08:00
parent 48d9caffdf
commit e26e8954e3
4 changed files with 43 additions and 9 deletions
e2e/src/api/specs
mobile/openapi/lib/model
server/src

View file

@ -143,7 +143,7 @@ describe('/api-keys', () => {
const { apiKey } = await create(user.accessToken, [Permission.All]);
const { status, body } = await request(app)
.put(`/api-keys/${apiKey.id}`)
.send({ name: 'new name' })
.send({ name: 'new name', permissions: [Permission.All] })
.set('Authorization', `Bearer ${admin.accessToken}`);
expect(status).toBe(400);
expect(body).toEqual(errorDto.badRequest('API Key not found'));
@ -153,13 +153,24 @@ describe('/api-keys', () => {
const { apiKey } = await create(user.accessToken, [Permission.All]);
const { status, body } = await request(app)
.put(`/api-keys/${apiKey.id}`)
.send({ name: 'new name' })
.send({
name: 'new name',
permissions: [
Permission.ActivityCreate,
Permission.ActivityRead,
Permission.ActivityUpdate,
],
})
.set('Authorization', `Bearer ${user.accessToken}`);
expect(status).toBe(200);
expect(body).toEqual({
id: expect.any(String),
name: 'new name',
permissions: [Permission.All],
permissions: [
Permission.ActivityCreate,
Permission.ActivityRead,
Permission.ActivityUpdate,
],
createdAt: expect.any(String),
updatedAt: expect.any(String),
});

View file

@ -14,25 +14,31 @@ class APIKeyUpdateDto {
/// Returns a new [APIKeyUpdateDto] instance.
APIKeyUpdateDto({
required this.name,
this.permissions = const [],
});
String name;
List<Permission> permissions;
@override
bool operator ==(Object other) => identical(this, other) || other is APIKeyUpdateDto &&
other.name == name;
other.name == name &&
_deepEquality.equals(other.permissions, permissions);
@override
int get hashCode =>
// ignore: unnecessary_parenthesis
(name.hashCode);
(name.hashCode) +
(permissions.hashCode);
@override
String toString() => 'APIKeyUpdateDto[name=$name]';
String toString() => 'APIKeyUpdateDto[name=$name, permissions=$permissions]';
Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
json[r'name'] = this.name;
json[r'permissions'] = this.permissions;
return json;
}
@ -46,6 +52,7 @@ class APIKeyUpdateDto {
return APIKeyUpdateDto(
name: mapValueOfType<String>(json, r'name')!,
permissions: Permission.listFromJson(json[r'permissions']),
);
}
return null;
@ -94,6 +101,7 @@ class APIKeyUpdateDto {
/// The list of required keys that must be present in a JSON.
static const requiredKeys = <String>{
'name',
'permissions',
};
}

View file

@ -53,7 +53,9 @@ describe(APIKeyController.name, () => {
});
it('should require a valid uuid', async () => {
const { status, body } = await request(ctx.getHttpServer()).put(`/api-keys/123`).send({ name: 'new name', permissions: Permission.ALL });
const { status, body } = await request(ctx.getHttpServer())
.put(`/api-keys/123`)
.send({ name: 'new name', permissions: Permission.ALL });
expect(status).toBe(400);
expect(body).toEqual(factory.responses.badRequest(['id must be a UUID']));
});

View file

@ -69,7 +69,7 @@ describe(ApiKeyService.name, () => {
mocks.apiKey.getById.mockResolvedValue(void 0);
await expect(sut.update(auth, id, { name: 'New Name' })).rejects.toBeInstanceOf(BadRequestException);
await expect(sut.update(auth, id, { name: 'New Name', permissions: [Permission.ALL] })).rejects.toBeInstanceOf(BadRequestException);
expect(mocks.apiKey.update).not.toHaveBeenCalledWith(id);
});
@ -82,10 +82,23 @@ describe(ApiKeyService.name, () => {
mocks.apiKey.getById.mockResolvedValue(apiKey);
mocks.apiKey.update.mockResolvedValue(apiKey);
await sut.update(auth, apiKey.id, { name: newName });
await sut.update(auth, apiKey.id, { name: newName, permissions: [Permission.ALL] });
expect(mocks.apiKey.update).toHaveBeenCalledWith(auth.user.id, apiKey.id, { name: newName });
});
it('should update permissions', async () => {
const auth = factory.auth();
const apiKey = factory.apiKey({ userId: auth.user.id });
const newPermissions = [Permission.ACTIVITY_CREATE, Permission.ACTIVITY_READ, Permission.ACTIVITY_UPDATE];
mocks.apiKey.getById.mockResolvedValue(apiKey);
mocks.apiKey.update.mockResolvedValue(apiKey);
await sut.update(auth, apiKey.id, { name: apiKey.name, permissions: newPermissions });
expect(mocks.apiKey.update).toHaveBeenCalledWith(auth.user.id, apiKey.id, { permission: newPermissions });
})
});
describe('delete', () => {