Skip to content

Commit

Permalink
🐛 FIX: Should sync public package when registryName not exists (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Aug 28, 2022
1 parent f139444 commit e06c841
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/port/controller/PackageSyncController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class PackageSyncController extends AbstractController {
if (params.syncDownloadData && !this.packageSyncerService.allowSyncDownloadData) {
throw new ForbiddenError('Not allow to sync package download data');
}
if (packageEntity?.registryId && packageEntity.registryId !== registry!.registryId) {
if (registry && packageEntity?.registryId && packageEntity.registryId !== registry.registryId) {
throw new ForbiddenError(`The package is synced from ${packageEntity.registryId}`);
}
const authorized = await this.userRoleManager.getAuthorizedUserAndToken(ctx);
Expand Down
30 changes: 30 additions & 0 deletions test/port/controller/PackageSyncController/createSyncTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,36 @@ describe('test/port/controller/PackageSyncController/createSyncTask.test.ts', ()
assert(res.body.error.includes('The package is synced from'));
});

it('should ignore the packageEntity registryId when registry not exists', async () => {
mock(app.config.cnpmcore, 'alwaysAuth', false);
await TestUtil.createPackage({
name: '@cnpm/banana',
registryId: 'mock_registry_id',
isPrivate: false,
});
const admin = await TestUtil.createAdmin();
// create registry
await app.httpRequest()
.post('/-/registry')
.set('authorization', admin.authorization)
.send(
{
name: 'cnpm',
host: 'https://r.cnpmjs.org/',
changeStream: 'https://r.cnpmjs.org/_changes',
type: 'cnpmcore',
});

const res = await app.httpRequest()
.put('/-/package/@cnpm/banana/syncs')
.set('authorization', admin.authorization)
.send()
.expect(201);
assert(res.body.ok === true);
assert(res.body.state === 'waiting');
assert(res.body.id);
});

it('should sync immediately and mock executeTask error when admin user request', async () => {
mock(app.config.cnpmcore, 'alwaysAuth', false);
mock.error(PackageSyncerService.prototype, 'executeTask');
Expand Down

0 comments on commit e06c841

Please sign in to comment.