From ed56ad1193bb5bb0a81fb843a11eb90d3fed9861 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 26 Apr 2018 10:03:40 +0200 Subject: Add ability to update the user display name/description --- server/controllers/api/users.ts | 1 + server/helpers/custom-validators/users.ts | 5 +++++ server/initializers/constants.ts | 3 ++- server/middlewares/validators/users.ts | 3 ++- server/tests/api/users/users-multiple-servers.ts | 20 +++++++++++++++++++- server/tests/api/users/users.ts | 23 +++++++++++++++++++++++ server/tests/utils/users/users.ts | 2 ++ 7 files changed, 54 insertions(+), 3 deletions(-) (limited to 'server') diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index dcc4ef196..2342c23dd 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -303,6 +303,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr await sequelizeTypescript.transaction(async t => { await user.save({ transaction: t }) + if (body.displayName !== undefined) user.Account.name = body.displayName if (body.description !== undefined) user.Account.description = body.description await user.Account.save({ transaction: t }) diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index c0acb8218..b59b766de 100644 --- a/server/helpers/custom-validators/users.ts +++ b/server/helpers/custom-validators/users.ts @@ -22,6 +22,10 @@ function isUserUsernameValid (value: string) { return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`)) } +function isUserDisplayNameValid (value: string) { + return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.NAME)) +} + function isUserDescriptionValid (value: string) { return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.DESCRIPTION)) } @@ -60,6 +64,7 @@ export { isUserUsernameValid, isUserNSFWPolicyValid, isUserAutoPlayVideoValid, + isUserDisplayNameValid, isUserDescriptionValid, isAvatarFile } diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 9c9c3afc0..365b8617d 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -180,9 +180,10 @@ const CONFIG = { const CONSTRAINTS_FIELDS = { USERS: { + NAME: { min: 3, max: 120 }, // Length + DESCRIPTION: { min: 3, max: 250 }, // Length USERNAME: { min: 3, max: 20 }, // Length PASSWORD: { min: 6, max: 255 }, // Length - DESCRIPTION: { min: 3, max: 250 }, // Length VIDEO_QUOTA: { min: -1 } }, VIDEO_ABUSES: { diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 5dd8caa3f..247b704c4 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -7,7 +7,7 @@ import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { isAvatarFile, isUserAutoPlayVideoValid, - isUserDescriptionValid, + isUserDescriptionValid, isUserDisplayNameValid, isUserNSFWPolicyValid, isUserPasswordValid, isUserRoleValid, @@ -98,6 +98,7 @@ const usersUpdateValidator = [ ] const usersUpdateMeValidator = [ + body('displayName').optional().custom(isUserDisplayNameValid).withMessage('Should have a valid display name'), body('description').optional().custom(isUserDescriptionValid).withMessage('Should have a valid description'), body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'), body('email').optional().isEmail().withMessage('Should have a valid email attribute'), diff --git a/server/tests/api/users/users-multiple-servers.ts b/server/tests/api/users/users-multiple-servers.ts index a7f3aa8d3..8b9b63348 100644 --- a/server/tests/api/users/users-multiple-servers.ts +++ b/server/tests/api/users/users-multiple-servers.ts @@ -76,6 +76,22 @@ describe('Test users with multiple servers', function () { await wait(5000) }) + it('Should be able to update my display name', async function () { + this.timeout(10000) + + await updateMyUser({ + url: servers[0].url, + accessToken: servers[0].accessToken, + displayName: 'my super display name' + }) + + const res = await getMyUserInformation(servers[0].url, servers[0].accessToken) + user = res.body + expect(user.account.displayName).to.equal('my super display name') + + await wait(5000) + }) + it('Should be able to update my description', async function () { this.timeout(10000) @@ -87,6 +103,7 @@ describe('Test users with multiple servers', function () { const res = await getMyUserInformation(servers[0].url, servers[0].accessToken) user = res.body + expect(user.account.displayName).to.equal('my super display name') expect(user.account.description).to.equal('my super description updated') await wait(5000) @@ -111,7 +128,7 @@ describe('Test users with multiple servers', function () { await wait(5000) }) - it('Should have updated my avatar and my description on other servers too', async function () { + it('Should have updated my profile on other servers too', async function () { for (const server of servers) { const resAccounts = await getAccountsList(server.url, '-createdAt') @@ -122,6 +139,7 @@ describe('Test users with multiple servers', function () { const rootServer1Get = resAccount.body as Account expect(rootServer1Get.name).to.equal('root') expect(rootServer1Get.host).to.equal('localhost:9001') + expect(rootServer1Get.displayName).to.equal('my super display name') expect(rootServer1Get.description).to.equal('my super description updated') await testImage(server.url, 'avatar2-resized', rootServer1Get.avatar.path, '.png') diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 1192ef9e4..1ea599859 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -172,6 +172,7 @@ describe('Test users', function () { expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.roleLabel).to.equal('User') expect(user.id).to.be.a('number') + expect(user.account.displayName).to.equal('user_1') expect(user.account.description).to.be.null }) @@ -316,6 +317,7 @@ describe('Test users', function () { expect(user.nsfwPolicy).to.equal('do_not_list') expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.id).to.be.a('number') + expect(user.account.displayName).to.equal('user_1') expect(user.account.description).to.be.null }) @@ -347,6 +349,7 @@ describe('Test users', function () { expect(user.nsfwPolicy).to.equal('do_not_list') expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.id).to.be.a('number') + expect(user.account.displayName).to.equal('user_1') expect(user.account.description).to.be.null }) @@ -365,6 +368,25 @@ describe('Test users', function () { await testImage(server.url, 'avatar-resized', user.account.avatar.path, '.png') }) + it('Should be able to update my display name', async function () { + await updateMyUser({ + url: server.url, + accessToken: accessTokenUser, + displayName: 'new display name' + }) + + const res = await getMyUserInformation(server.url, accessTokenUser) + const user = res.body + + expect(user.username).to.equal('user_1') + expect(user.email).to.equal('updated@example.com') + expect(user.nsfwPolicy).to.equal('do_not_list') + expect(user.videoQuota).to.equal(2 * 1024 * 1024) + expect(user.id).to.be.a('number') + expect(user.account.displayName).to.equal('new display name') + expect(user.account.description).to.be.null + }) + it('Should be able to update my description', async function () { await updateMyUser({ url: server.url, @@ -380,6 +402,7 @@ describe('Test users', function () { expect(user.nsfwPolicy).to.equal('do_not_list') expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.id).to.be.a('number') + expect(user.account.displayName).to.equal('new display name') expect(user.account.description).to.equal('my super description updated') }) diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts index fc6b26c50..d31e57a25 100644 --- a/server/tests/utils/users/users.ts +++ b/server/tests/utils/users/users.ts @@ -132,6 +132,7 @@ function updateMyUser (options: { nsfwPolicy?: NSFWPolicyType, email?: string, autoPlayVideo?: boolean + displayName?: string, description?: string }) { const path = '/api/v1/users/me' @@ -142,6 +143,7 @@ function updateMyUser (options: { if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo if (options.email !== undefined && options.email !== null) toSend['email'] = options.email if (options.description !== undefined && options.description !== null) toSend['description'] = options.description + if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName return makePutBodyRequest({ url: options.url, -- cgit v1.2.3