fix(plugin-users): fix update profile 500 (#766) (#767)

Fix #766
This commit is contained in:
Junyi 2022-08-22 20:37:15 +08:00 committed by GitHub
parent 86650f16d9
commit b6daa9ad69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 15 deletions

View File

@ -0,0 +1,52 @@
import Database from '@nocobase/database';
import { MockServer } from '@nocobase/test';
import { prepareApp } from './prepare';
describe('actions', () => {
let app: MockServer;
let db: Database;
let adminUser;
let agent;
let adminAgent;
let pluginUser;
beforeEach(async () => {
process.env.INIT_ROOT_EMAIL = 'test@nocobase.com';
process.env.INIT_ROOT_PASSWORD = '123456';
process.env.INIT_ROOT_NICKNAME = 'Test';
app = await prepareApp();
db = app.db;
pluginUser = app.getPlugin('@nocobase/plugin-users');
adminUser = await db.getRepository('users').findOne({
filter: {
email: process.env.INIT_ROOT_EMAIL
},
appends: ['roles']
});
agent = app.agent();
adminAgent = app.agent().auth(
pluginUser.jwtService.sign({
userId: adminUser.get('id'),
}),
{ type: 'bearer' },
);
});
afterEach(async () => {
await db.close();
});
it('update profile with roles', async () => {
const res2 = await adminAgent.resource('users').updateProfile({
filterByTk: adminUser.id,
values: {
nickname: 'a',
roles: adminUser.roles
}
});
expect(res2.status).toBe(200);
});
});

View File

@ -1,30 +1,41 @@
import Database from '@nocobase/database'; import Database from '@nocobase/database';
import PluginACL from '@nocobase/plugin-acl';
import { mockServer, MockServer } from '@nocobase/test'; import { mockServer, MockServer } from '@nocobase/test';
import supertest from 'supertest';
import PluginUsers from '../server'; import PluginUsers from '../server';
import { userPluginConfig } from './utils'; import { userPluginConfig } from './utils';
describe('actions', () => { describe('actions', () => {
let api: MockServer; let app: MockServer;
let db: Database; let db: Database;
let adminUser;
let agent; let agent;
let adminAgent;
let pluginUser; let pluginUser;
beforeEach(async () => { beforeEach(async () => {
api = mockServer(); app = mockServer();
await api.cleanDb(); await app.cleanDb();
process.env.INIT_ROOT_EMAIL = 'test@nocobase.com'; process.env.INIT_ROOT_EMAIL = 'test@nocobase.com';
process.env.INIT_ROOT_PASSWORD = '123456'; process.env.INIT_ROOT_PASSWORD = '123456';
process.env.INIT_ROOT_NICKNAME = 'Test'; process.env.INIT_ROOT_NICKNAME = 'Test';
api.plugin(PluginUsers, userPluginConfig); app.plugin(PluginUsers, userPluginConfig);
api.plugin(PluginACL);
await api.loadAndInstall(); await app.loadAndInstall();
db = api.db; db = app.db;
agent = supertest.agent(api.callback()); pluginUser = app.getPlugin('@nocobase/plugin-users');
pluginUser = api.getPlugin('@nocobase/plugin-users'); adminUser = await db.getRepository('users').findOne({
filter: {
email: process.env.INIT_ROOT_EMAIL
}
});
agent = app.agent();
adminAgent = app.agent().auth(
pluginUser.jwtService.sign({
userId: adminUser.get('id'),
}),
{ type: 'bearer' },
);
}); });
afterEach(async () => { afterEach(async () => {
@ -34,7 +45,7 @@ describe('actions', () => {
it('should login user with password', async () => { it('should login user with password', async () => {
const { INIT_ROOT_EMAIL, INIT_ROOT_PASSWORD } = process.env; const { INIT_ROOT_EMAIL, INIT_ROOT_PASSWORD } = process.env;
let response = await api.agent().resource('users').check(); let response = await agent.resource('users').check();
expect(response.body.data.id).toBeUndefined(); expect(response.body.data.id).toBeUndefined();
response = await agent.post('/users:signin').send({ response = await agent.post('/users:signin').send({
@ -51,4 +62,22 @@ describe('actions', () => {
response = await agent.get('/users:check').set({ Authorization: 'Bearer ' + token }); response = await agent.get('/users:check').set({ Authorization: 'Bearer ' + token });
expect(response.body.data.id).toBeDefined(); expect(response.body.data.id).toBeDefined();
}); });
it('update profile', async () => {
const res1 = await agent.resource('users').updateProfile({
filterByTk: adminUser.id,
values: {
nickname: 'a'
}
});
expect(res1.status).toBe(401);
const res2 = await adminAgent.resource('users').updateProfile({
filterByTk: adminUser.id,
values: {
nickname: 'a'
}
});
expect(res2.status).toBe(200);
});
}); });

View File

@ -107,11 +107,16 @@ export async function getUserByResetToken(ctx: Context, next: Next) {
export async function updateProfile(ctx: Context, next: Next) { export async function updateProfile(ctx: Context, next: Next) {
const { values } = ctx.action.params; const { values } = ctx.action.params;
if (!ctx.state.currentUser) { const { currentUser } = ctx.state;
if (!currentUser) {
ctx.throw(401); ctx.throw(401);
} }
await ctx.state.currentUser.update(values); const UserRepo = ctx.db.getRepository('users');
ctx.body = ctx.state.currentUser; const result = await UserRepo.update({
filterByTk: currentUser.id,
values
});
ctx.body = result;
await next(); await next();
} }