Skip to content

Commit

Permalink
test: add proxy cache controller test.
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Dec 18, 2023
1 parent 615e97c commit b0bfa3b
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 123 deletions.
2 changes: 1 addition & 1 deletion app/core/service/ProxyCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class ProxyCacheService extends AbstractService {
return { proxyBytes, manifest };
}

async removeProxyCaches(fullname: string, fileType: DIST_NAMES, version?: string) {
async removeProxyCache(fullname: string, fileType: DIST_NAMES, version?: string) {
const storeKey = isPkgManifest(fileType)
? `/${PROXY_MODE_CACHED_PACKAGE_DIR_NAME}/${fullname}/${fileType}`
: `/${PROXY_MODE_CACHED_PACKAGE_DIR_NAME}/${fullname}/${version}/${fileType}`;
Expand Down
44 changes: 34 additions & 10 deletions app/port/controller/ProxyCacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
// Context,
// EggContext,
} from '@eggjs/tegg';
import { ForbiddenError, NotFoundError, UnauthorizedError } from 'egg-errors';
import { AbstractController } from './AbstractController';
import { ProxyCacheRepository } from '../../repository/ProxyCacheRepository';
import { Static } from 'egg-typebox-validate/typebox';
Expand All @@ -19,6 +20,7 @@ import {
ProxyCacheService,
isPkgManifest,
} from '../../core/service/ProxyCacheService';
import { SyncMode } from '../../common/constants';
// import { DIST_NAMES } from '../../../core/entity/Package';

@HTTPController()
Expand All @@ -37,6 +39,9 @@ export class ProxyCacheController extends AbstractController {
@HTTPQuery() pageSize: Static<typeof QueryPageOptions>['pageSize'],
@HTTPQuery() pageIndex: Static<typeof QueryPageOptions>['pageIndex'],
) {
if (this.config.cnpmcore.syncMode !== SyncMode.proxy) {
throw new ForbiddenError('proxy mode is not enabled');
}
return await this.proxyCacheRepository.listCachedFiles({
pageSize,
pageIndex,
Expand All @@ -48,17 +53,31 @@ export class ProxyCacheController extends AbstractController {
path: `/-/proxy-cache/:fullname(${FULLNAME_REG_STRING})`,
})
async showProxyCaches(@HTTPParam() fullname: string) {
return await this.proxyCacheRepository.findProxyCaches(fullname);
if (this.config.cnpmcore.syncMode !== SyncMode.proxy) {
throw new ForbiddenError('proxy mode is not enabled');
}
const result = await this.proxyCacheRepository.findProxyCaches(fullname);
if (result.length === 0) {
throw new NotFoundError();
}
return result;
}

@HTTPMethod({
method: HTTPMethodEnum.PATCH,
path: `/-/proxy-cache/:fullname(${FULLNAME_REG_STRING})`,
})
async refreshProxyCaches(@HTTPParam() fullname: string) {
if (this.config.cnpmcore.syncMode !== SyncMode.proxy) {
throw new ForbiddenError('proxy mode is not enabled');
}

const refreshList = await this.proxyCacheRepository.findProxyCaches(
fullname,
);
if (refreshList.length === 0) {
throw new NotFoundError();
}
const taskList = refreshList
// 仅manifests需要更新,指定版本的package.json文件发布后不会改变
.filter(i => isPkgManifest(i.fileType))
Expand All @@ -85,17 +104,21 @@ export class ProxyCacheController extends AbstractController {
async removeProxyCaches(@Context() ctx: EggContext, @HTTPParam() fullname: string) {
const isAdmin = await this.userRoleManager.isAdmin(ctx);
if (!isAdmin) {
return {
ok: false,
error: 'only admin can do this',
};
throw new UnauthorizedError('only admin can do this');
}

if (this.config.cnpmcore.syncMode !== SyncMode.proxy) {
throw new ForbiddenError('proxy mode is not enabled');
}

const proxyCachesList = await this.proxyCacheRepository.findProxyCaches(
fullname,
);
if (proxyCachesList.length === 0) {
throw new NotFoundError();
}

Check warning on line 119 in app/port/controller/ProxyCacheController.ts

View check run for this annotation

Codecov / codecov/patch

app/port/controller/ProxyCacheController.ts#L118-L119

Added lines #L118 - L119 were not covered by tests
const removingList = proxyCachesList.map(item => {
return this.proxyCacheService.removeProxyCaches(item.fullname, item.fileType, item.version);
return this.proxyCacheService.removeProxyCache(item.fullname, item.fileType, item.version);
});
await Promise.all(removingList);
return {
Expand All @@ -111,10 +134,11 @@ export class ProxyCacheController extends AbstractController {
async truncateProxyCaches(@Context() ctx: EggContext) {
const isAdmin = await this.userRoleManager.isAdmin(ctx);
if (!isAdmin) {
return {
ok: false,
error: 'only admin can do this',
};
throw new UnauthorizedError('only admin can do this');
}

if (this.config.cnpmcore.syncMode !== SyncMode.proxy) {
throw new ForbiddenError('proxy mode is not enabled');
}

// 需要手动清除对象存储上的缓存
Expand Down
Loading

0 comments on commit b0bfa3b

Please sign in to comment.