refactor(test): modify the test function login to be asynchronous and update the relevant test cases (#6181)

* refactor(test): modify the test function login to be asynchronous and update the relevant test cases
This commit is contained in:
Sheldon Guo 2025-02-08 13:53:30 +08:00 committed by GitHub
parent 4561ad02ec
commit feb9074e56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
34 changed files with 89 additions and 122 deletions

View File

@ -38,7 +38,7 @@ describe('middleware', () => {
const hasFn = vi.fn();
const addFn = vi.fn();
beforeEach(async () => {
await agent.loginWithJti(1);
await agent.login(1);
app.authManager.setTokenBlacklistService({
has: hasFn,
add: addFn,

View File

@ -74,9 +74,8 @@ interface Resource {
}
interface ExtendedAgent extends SuperAgentTest {
login: (user: any, roleName?: string) => ExtendedAgent;
loginUsingId: (userId: number, roleName?: string) => ExtendedAgent;
loginWithJti: (user: any, roleName?: string) => Promise<ExtendedAgent>;
login: (user: any, roleName?: string) => Promise<ExtendedAgent>;
loginUsingId: (userId: number, roleName?: string) => Promise<ExtendedAgent>;
resource: (name: string, resourceOf?: any) => Resource;
}
@ -129,27 +128,6 @@ export class MockServer extends Application {
const proxy = new Proxy(agent, {
get(target, method: string, receiver) {
if (['login', 'loginUsingId'].includes(method)) {
return (userOrId: any, roleName?: string) => {
return proxy
.auth(
jwt.sign(
{
userId: typeof userOrId === 'number' ? userOrId : userOrId?.id,
temp: true,
roleName,
signInTime: Date.now(),
},
process.env.APP_KEY,
{
expiresIn: '1d',
},
),
{ type: 'bearer' },
)
.set('X-Authenticator', 'basic');
};
}
if (method === 'loginWithJti') {
return async (userOrId: any, roleName?: string) => {
const userId = typeof userOrId === 'number' ? userOrId : userOrId?.id;
const tokenInfo = await authManager.tokenController.add({ userId });

View File

@ -42,7 +42,7 @@ describe('acl', () => {
},
});
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
uiSchemaRepository = db.getRepository('uiSchemas');
});
@ -126,7 +126,7 @@ describe('acl', () => {
userPlugin = app.getPlugin('users') as UsersPlugin;
const testAgent = await app.agent().loginWithJti(u1);
const testAgent = await app.agent().login(u1);
// @ts-ignore
const response1 = await testAgent.resource('repairs').list({
@ -247,7 +247,7 @@ describe('acl', () => {
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
const adminAgent = await app.agent().loginWithJti(rootUser);
const adminAgent = await app.agent().login(rootUser);
expect(await db.getCollection('roles').repository.count()).toBe(3);
@ -802,7 +802,7 @@ describe('acl', () => {
},
});
const userAgent = await app.agent().loginWithJti(user);
const userAgent = await app.agent().login(user);
const schema = {
'x-uid': 'test',
@ -864,7 +864,7 @@ describe('acl', () => {
filterByTk: 1,
});
const rootAgent = await app.agent().loginWithJti(rootUser);
const rootAgent = await app.agent().login(rootUser);
const response = await rootAgent
// @ts-ignore

View File

@ -138,7 +138,7 @@ describe('destroy action with acl', () => {
const a1 = await A.repository.findOne({ filter: { title: 'a1' } });
const response = await (await await app.agent().loginWithJti(1)).resource('a.bs', a1.get('id')).list();
const response = await (await app.agent().login(1)).resource('a.bs', a1.get('id')).list();
expect(response.statusCode).toEqual(200);
});
@ -173,13 +173,9 @@ describe('destroy action with acl', () => {
},
);
const response = await app
.agent()
.login(1)
.resource('posts')
.destroy({
filterByTk: p1.get('id'),
});
const response = await (await app.agent().login(1)).resource('posts').destroy({
filterByTk: p1.get('id'),
});
// should throw errors
expect(response.statusCode).toEqual(403);

View File

@ -114,7 +114,7 @@ describe('association test', () => {
},
});
const userAgent = await app.agent().loginWithJti(user);
const userAgent = await app.agent().login(user);
const postCommentsResp = await userAgent.resource('posts.userComments', post.get('id')).list({});
expect(postCommentsResp.statusCode).toEqual(200);
@ -168,9 +168,9 @@ describe.skip('association field acl', () => {
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
userAgent = await app.agent().loginWithJti(user);
userAgent = await app.agent().login(user);
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
await db.getRepository('collections').create({
values: {

View File

@ -55,9 +55,9 @@ describe('configuration', () => {
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
userAgent = await app.agent().loginWithJti(user);
userAgent = await app.agent().login(user);
guestAgent = app.agent();
});

View File

@ -24,7 +24,7 @@ describe('get action with acl', () => {
const users = await UserRepo.create({
values: [{ nickname: 'a', roles: [{ name: 'test' }] }],
});
userAgent = await app.agent().loginWithJti(users[0]);
userAgent = await app.agent().login(users[0]);
Post = app.db.collection({
name: 'posts',

View File

@ -30,7 +30,7 @@ describe('list action with acl', () => {
{ id: 3, nickname: 'a', roles: [{ name: 'user' }] },
],
});
userAgent = await app.agent().loginWithJti(users[0], 'user');
userAgent = await app.agent().login(users[0], 'user');
Post = app.db.collection({
name: 'posts',
@ -220,7 +220,7 @@ describe('list action with acl', () => {
// );
// @ts-ignore
const response = await (await app.agent().loginWithJti(users[0].id, 'user'))
const response = await (await app.agent().login(users[0].id, 'user'))
.set('X-With-ACL-Meta', true)
.resource('posts')
.list();
@ -391,7 +391,7 @@ describe('list association action with acl', () => {
},
});
const userAgent = await (await app.agent().loginWithJti(user, 'newRole')).set('X-With-ACL-Meta', true);
const userAgent = await (await app.agent().login(user, 'newRole')).set('X-With-ACL-Meta', true);
const createResp = await userAgent.resource('posts').create({
values: {

View File

@ -47,8 +47,8 @@ describe('middleware', () => {
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
adminAgent = await app.agent().loginWithJti(admin);
memberAgent = await app.agent().loginWithJti(member);
adminAgent = await app.agent().login(admin);
memberAgent = await app.agent().login(member);
await db.getRepository('collections').create({
values: {
@ -135,7 +135,7 @@ describe('middleware', () => {
},
},
});
const response = await (await app.agent().loginWithJti(member)).resource('posts').create({
const response = await (await app.agent().login(member)).resource('posts').create({
values: {},
});

View File

@ -77,7 +77,7 @@ describe('own test', () => {
pluginUser = app.getPlugin('users');
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
user = await db.getRepository('users').create({
values: {
@ -86,7 +86,7 @@ describe('own test', () => {
},
});
userAgent = await app.agent().loginWithJti(user);
userAgent = await app.agent().login(user);
});
it('should list without createBy', async () => {

View File

@ -44,7 +44,7 @@ describe('role check action', () => {
},
});
const agent = await app.agent().loginWithJti(user);
const agent = await app.agent().login(user);
// @ts-ignore
const response = await agent.resource('roles').check();
@ -92,7 +92,7 @@ describe('role check action', () => {
},
});
const agent: any = await app.agent().loginWithJti(user);
const agent: any = await app.agent().login(user);
const checkResp1 = await agent.resource('roles').check();
const actions = checkResp1.body.data.actions;

View File

@ -42,7 +42,7 @@ describe('role resource api', () => {
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
});
it('should grant resource by createRepository', async () => {

View File

@ -45,7 +45,7 @@ describe('role api', () => {
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
});
it('should have permission to users collection with strategy', async () => {
@ -64,7 +64,7 @@ describe('role api', () => {
},
});
const userAgent = await app.agent().loginWithJti(user1);
const userAgent = await app.agent().login(user1);
const response = await userAgent.resource('users').list();
expect(response.statusCode).toBe(200);

View File

@ -35,7 +35,7 @@ describe('scope api', () => {
});
const userPlugin = app.getPlugin('users') as UsersPlugin;
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
});
it('should create scope of resource', async () => {

View File

@ -36,7 +36,7 @@ describe('snippet', () => {
});
const userPlugin: any = app.getPlugin('users');
const userAgent: any = await app.agent().loginWithJti(user);
const userAgent: any = await app.agent().login(user);
const createCollectionResponse = await userAgent.resource('collections').create({});
expect(createCollectionResponse.statusCode).toEqual(403);
@ -57,7 +57,7 @@ describe('snippet', () => {
},
});
const userAgent: any = await app.agent().loginWithJti(testUser);
const userAgent: any = await app.agent().login(testUser);
const listResp = await userAgent.resource('users').list();
@ -79,7 +79,7 @@ describe('snippet', () => {
},
});
const userAgent: any = await app.agent().loginWithJti(testUser);
const userAgent: any = await app.agent().login(testUser);
const getResp = await userAgent.resource('dataSources.roles', 'main').get({
filterByTk: 'testRole',
@ -110,7 +110,7 @@ describe('snippet', () => {
},
});
const userAgent: any = await app.agent().loginWithJti(testUser);
const userAgent: any = await app.agent().login(testUser);
const getResp = await userAgent.resource('roles.dataSourcesCollections', 'testRole').list({
filter: {

View File

@ -36,7 +36,7 @@ describe('actions', () => {
});
agent = app.agent();
adminAgent = await app.agent().loginWithJti(adminUser);
adminAgent = await app.agent().login(adminUser);
});
afterEach(async () => {
@ -166,7 +166,7 @@ describe('actions', () => {
});
// list with user
const userAgent: any = (await app.agent().loginWithJti(testUser)).set('x-role', 'testRole');
const userAgent: any = (await app.agent().login(testUser)).set('x-role', 'testRole');
const listResp = await userAgent.resource('items').list({});
expect(listResp.status).toBe(200);

View File

@ -41,7 +41,7 @@ describe('acl', () => {
},
});
adminAgent = await app.agent().loginWithJti(admin);
adminAgent = await app.agent().login(admin);
uiSchemaRepository = db.getRepository('uiSchemas');
await db.getRepository('collections').create({
context: {},

View File

@ -40,7 +40,7 @@ describe('write role to acl', () => {
},
});
const agent = await app.agent().loginWithJti(user);
const agent = await app.agent().login(user);
// @ts-ignore
const response = await agent.resource('roles').check();

View File

@ -222,7 +222,7 @@ describe('actions', () => {
password: '12345',
},
});
const userAgent = await agent.loginWithJti(user, null);
const userAgent = await agent.login(user, null);
// Should check password consistency
const res = await userAgent.post('/auth:changePassword').set({ 'X-Authenticator': 'basic' }).send({
@ -256,7 +256,7 @@ describe('actions', () => {
password: '12345',
},
});
const res3 = await (await agent.loginWithJti(user1))
const res3 = await (await agent.login(user1))
.post('/auth:changePassword')
.set({ 'X-Authenticator': 'basic' })
.send({
@ -281,7 +281,7 @@ describe('actions', () => {
password: '12345',
},
});
const userAgent = await agent.loginWithJti(user);
const userAgent = await agent.login(user);
const res = await userAgent.post('/auth:changePassword').set({ 'X-Authenticator': 'basic' }).send({
oldPassword: '12345',
@ -445,7 +445,7 @@ describe('actions', () => {
password: '12345',
},
});
const userAgent = await agent.loginWithJti(user, null);
const userAgent = await agent.login(user, null);
const res = await userAgent.post('/auth:check').set({ 'X-Authenticator': 'basic' }).send();
expect(res.statusCode).toEqual(200);
expect(res.body.data.id).toBeDefined();

View File

@ -80,7 +80,7 @@ describe('auth', () => {
username: 'admin',
},
});
adminAgent = await app.agent().loginWithJti(user, 'admin');
adminAgent = await app.agent().login(user, 'admin');
class MockBaseAuth extends BaseAuth {
async validate() {

View File

@ -16,7 +16,7 @@ describe('backup files', () => {
beforeEach(async () => {
app = await createApp();
adminAgent = await app.agent().loginWithJti(1);
adminAgent = await app.agent().login(1);
});
afterEach(async () => {

View File

@ -42,7 +42,7 @@ describe('tree', () => {
});
const userPlugin = app.getPlugin('users');
const agent = await app.agent().loginWithJti(user);
const agent = await app.agent().login(user);
agent.set('X-With-ACL-Meta', 'true');
app.acl.allow('table_a', ['*']);
app.acl.allow('collections', ['*']);

View File

@ -117,7 +117,7 @@ describe('data source with acl', () => {
},
});
const adminAgent: any = (await app.agent().loginWithJti(adminUser)).set('x-data-source', 'mockInstance1');
const adminAgent: any = (await app.agent().login(adminUser)).set('x-data-source', 'mockInstance1');
const listRes = await adminAgent.resource('api/posts').list();
expect(listRes.status).toBe(200);
expect(middlewareFn).toBeCalledTimes(1);
@ -130,7 +130,7 @@ describe('data source with acl', () => {
},
});
const adminAgent: any = (await app.agent().loginWithJti(adminUser)).set('x-data-source', 'mockInstance1');
const adminAgent: any = (await app.agent().login(adminUser)).set('x-data-source', 'mockInstance1');
const postRes = await adminAgent.resource('api/posts').list({});
expect(postRes.status).toBe(200);
});
@ -142,7 +142,7 @@ describe('data source with acl', () => {
},
});
const adminAgent: any = await app.agent().loginWithJti(adminUser);
const adminAgent: any = await app.agent().login(adminUser);
await adminAgent.resource('dataSources.roles', 'mockInstance1').update({
filterByTk: 'member',
@ -195,9 +195,9 @@ describe('data source with acl', () => {
context: {},
});
const adminAgent: any = await app.agent().loginWithJti(adminUser);
const adminAgent: any = await app.agent().login(adminUser);
const testUserAgent: any = await app.agent().loginWithJti(testUser);
const testUserAgent: any = await app.agent().login(testUser);
const listRes = await testUserAgent.resource('posts').list({});
expect(listRes.status).toBe(403);
@ -251,10 +251,10 @@ describe('data source with acl', () => {
},
});
const adminAgent: any = await app.agent().loginWithJti(adminUser);
const adminAgent: any = await app.agent().login(adminUser);
// should get permission error
const testAgent = await app.agent().loginWithJti(testUser);
const testAgent = await app.agent().login(testUser);
const testUserAgent = getDataSourceAgent(testAgent, 'mockInstance1');
// @ts-ignore
@ -313,10 +313,10 @@ describe('data source with acl', () => {
},
});
const adminAgent: any = await app.agent().loginWithJti(adminUser);
const adminAgent: any = await app.agent().login(adminUser);
// should get permission error
const testUserAgent = getDataSourceAgent(await app.agent().loginWithJti(testUser), 'mockInstance1');
const testUserAgent = getDataSourceAgent(await app.agent().login(testUser), 'mockInstance1');
const createResourceResp = await adminAgent.resource('dataSources.rolesResourcesScopes', 'mockInstance1').create({
values: {
@ -402,7 +402,7 @@ describe('data source with acl', () => {
// call roles check
// @ts-ignore
const checkRep = await (await app.agent().loginWithJti(testUser)).resource('roles').check({});
const checkRep = await (await app.agent().login(testUser)).resource('roles').check({});
expect(checkRep.status).toBe(200);
const checkData = checkRep.body;
@ -419,21 +419,17 @@ describe('data source with acl', () => {
});
// update strategy
const updateRes = await app
.agent()
.login(adminUser)
.resource('dataSources.roles', 'main')
.update({
filterByTk: 'admin',
values: {
strategy: {
actions: [],
},
const updateRes = await (await app.agent().login(adminUser)).resource('dataSources.roles', 'main').update({
filterByTk: 'admin',
values: {
strategy: {
actions: [],
},
});
},
});
// get role
const adminRoleResp = await (await app.agent().loginWithJti(adminUser)).resource('dataSources.roles', 'main').get({
const adminRoleResp = await (await app.agent().login(adminUser)).resource('dataSources.roles', 'main').get({
filterByTk: 'admin',
});
@ -441,20 +437,16 @@ describe('data source with acl', () => {
expect(data.data.strategy.actions).toHaveLength(0);
// update role
const updateRoleRes = await app
.agent()
.login(adminUser)
.resource('roles')
.update({
filterByTk: 'admin',
values: {
snippets: ['pm.*'],
},
});
const updateRoleRes = await (await app.agent().login(adminUser)).resource('roles').update({
filterByTk: 'admin',
values: {
snippets: ['pm.*'],
},
});
expect(updateRoleRes.status).toBe(200);
const adminRoleResp2 = await (await app.agent().loginWithJti(adminUser)).resource('dataSources.roles', 'main').get({
const adminRoleResp2 = await (await app.agent().login(adminUser)).resource('dataSources.roles', 'main').get({
filterByTk: 'admin',
});

View File

@ -19,7 +19,7 @@ describe('external data source', () => {
beforeAll(async () => {
process.env.INIT_ROOT_USERNAME = 'test';
app = await createMockServer({
plugins: ['field-sort', 'data-source-manager', 'users', 'acl'],
plugins: ['field-sort', 'data-source-manager', 'users', 'acl', 'auth'],
});
db = app.db;
ctx = {
@ -40,7 +40,7 @@ describe('external data source', () => {
username: process.env.INIT_ROOT_USERNAME,
},
});
adminAgent = app.agent().login(adminUser);
adminAgent = await app.agent().login(adminUser);
});
it('should check permission for external data source', async () => {

View File

@ -43,7 +43,7 @@ describe('inapp message channels', () => {
],
});
userAgents = await Promise.all(users.map((user) => app.agent().loginWithJti(user)));
userAgents = await Promise.all(users.map((user) => app.agent().login(user)));
currUserAgent = userAgents[0];
currUserId = users[0].id;
});

View File

@ -33,7 +33,7 @@ describe('actions', () => {
});
agent = app.agent();
adminAgent = await app.agent().loginWithJti(adminUser);
adminAgent = await app.agent().login(adminUser);
});
afterEach(async () => {

View File

@ -36,7 +36,7 @@ describe('actions', () => {
});
agent = app.agent();
adminAgent = await app.agent().loginWithJti(adminUser);
adminAgent = await app.agent().login(adminUser);
});
afterEach(async () => {

View File

@ -42,7 +42,7 @@ describe('workflow > action-trigger', () => {
UserRepo = db.getCollection('users').repository;
root = await UserRepo.findOne({});
rootAgent = await app.agent().loginWithJti(root);
rootAgent = await app.agent().login(root);
users = await UserRepo.create({
values: [
@ -51,7 +51,7 @@ describe('workflow > action-trigger', () => {
],
});
userAgents = await Promise.all(users.map((user) => app.agent().loginWithJti(user)));
userAgents = await Promise.all(users.map((user) => app.agent().login(user)));
});
afterEach(() => app.destroy());
@ -812,8 +812,9 @@ describe('workflow > action-trigger', () => {
// values: { title: 't2' },
// triggerWorkflows: `${workflow.key}`,
// });
const res2 = await agent
.login(users[0])
const res2 = await (
await agent.login(users[0])
)
.set('x-data-source', 'another')
.post('/api/posts:create')
.query({ triggerWorkflows: `${workflow.key}` })

View File

@ -44,7 +44,7 @@ describe('workflow > instructions > manual > assignees', () => {
{ id: 3, nickname: 'b' },
]);
userAgents = users.map((user) => app.agent().login(user));
userAgents = await Promise.all(users.map((user) => app.agent().login(user)));
workflow = await WorkflowModel.create({
enabled: true,

View File

@ -44,7 +44,7 @@ describe('workflow > instructions > manual', () => {
{ id: 3, nickname: 'b' },
]);
userAgents = users.map((user) => app.agent().login(user));
userAgents = await Promise.all(users.map((user) => app.agent().login(user)));
workflow = await WorkflowModel.create({
enabled: true,

View File

@ -44,7 +44,7 @@ describe('workflow > instructions > manual', () => {
{ id: 3, nickname: 'b' },
]);
userAgents = await Promise.all(users.map((user) => app.agent().loginWithJti(user)));
userAgents = await Promise.all(users.map((user) => app.agent().login(user)));
workflow = await WorkflowModel.create({
enabled: true,

View File

@ -44,7 +44,7 @@ describe('workflow > instructions > manual', () => {
{ id: 3, nickname: 'b' },
]);
userAgents = await Promise.all(users.map((user) => app.agent().loginWithJti(user)));
userAgents = await Promise.all(users.map((user) => app.agent().login(user)));
workflow = await WorkflowModel.create({
enabled: true,

View File

@ -27,7 +27,7 @@ describe('workflow > actions > executions', () => {
plugins: ['users', 'acl', 'auth', 'data-source-manager'],
acl: true,
});
agent = app.agent().loginUsingId(1);
agent = await app.agent().loginUsingId(1);
db = app.db;
WorkflowModel = db.getCollection('workflows').model;
PostRepo = db.getCollection('posts').repository;
@ -47,7 +47,7 @@ describe('workflow > actions > executions', () => {
{ id: 3, nickname: 'b' },
],
});
userAgents = await Promise.all(users.map((user) => app.agent().loginWithJti(user)));
userAgents = await Promise.all(users.map((user) => app.agent().login(user)));
});
afterEach(async () => await app.destroy());

View File

@ -40,7 +40,7 @@ describe('workflow > triggers > collection', () => {
TagRepo = db.getCollection('tags').repository;
const user = await app.db.getRepository('users').findOne();
agent = await app.agent().loginWithJti(user);
agent = await app.agent().login(user);
});
afterEach(() => app.destroy());