From 9639bd175726b73f8fe664b5ced12a72407b1f0b Mon Sep 17 00:00:00 2001 From: buoyantair Date: Mon, 29 Oct 2018 22:18:31 +0530 Subject: Move utils to /shared Move utils used by /server/tools/* & /server/tests/**/* into /shared folder. Issue: #1336 --- shared/utils/users/users.ts | 296 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 shared/utils/users/users.ts (limited to 'shared/utils/users/users.ts') diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts new file mode 100644 index 000000000..1b385aaf7 --- /dev/null +++ b/shared/utils/users/users.ts @@ -0,0 +1,296 @@ +import * as request from 'supertest' +import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../' + +import { UserRole } from '../../index' +import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' + +function createUser ( + url: string, + accessToken: string, + username: string, + password: string, + videoQuota = 1000000, + videoQuotaDaily = -1, + role: UserRole = UserRole.USER, + specialStatus = 200 +) { + const path = '/api/v1/users' + const body = { + username, + password, + role, + email: username + '@example.com', + videoQuota, + videoQuotaDaily + } + + return request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send(body) + .expect(specialStatus) +} + +function registerUser (url: string, username: string, password: string, specialStatus = 204) { + const path = '/api/v1/users/register' + const body = { + username, + password, + email: username + '@example.com' + } + + return request(url) + .post(path) + .set('Accept', 'application/json') + .send(body) + .expect(specialStatus) +} + +function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { + const path = '/api/v1/users/me' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) + .expect('Content-Type', /json/) +} + +function deleteMe (url: string, accessToken: string, specialStatus = 204) { + const path = '/api/v1/users/me' + + return request(url) + .delete(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) +} + +function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { + const path = '/api/v1/users/me/video-quota-used' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) + .expect('Content-Type', /json/) +} + +function getUserInformation (url: string, accessToken: string, userId: number) { + const path = '/api/v1/users/' + userId + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { + const path = '/api/v1/users/me/videos/' + videoId + '/rating' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) + .expect('Content-Type', /json/) +} + +function getUsersList (url: string, accessToken: string) { + const path = '/api/v1/users' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) { + const path = '/api/v1/users' + + return request(url) + .get(path) + .query({ start }) + .query({ count }) + .query({ sort }) + .query({ search }) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { + const path = '/api/v1/users' + + return request(url) + .delete(path + '/' + userId) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { + const path = '/api/v1/users' + let body: any + if (reason) body = { reason } + + return request(url) + .post(path + '/' + userId + '/block') + .send(body) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { + const path = '/api/v1/users' + + return request(url) + .post(path + '/' + userId + '/unblock') + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +function updateMyUser (options: { + url: string + accessToken: string, + currentPassword?: string, + newPassword?: string, + nsfwPolicy?: NSFWPolicyType, + email?: string, + autoPlayVideo?: boolean + displayName?: string, + description?: string +}) { + const path = '/api/v1/users/me' + + const toSend = {} + if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword + if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword + if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy + 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, + path, + token: options.accessToken, + fields: toSend, + statusCodeExpected: 204 + }) +} + +function updateMyAvatar (options: { + url: string, + accessToken: string, + fixture: string +}) { + const path = '/api/v1/users/me/avatar/pick' + + return updateAvatarRequest(Object.assign(options, { path })) +} + +function updateUser (options: { + url: string + userId: number, + accessToken: string, + email?: string, + videoQuota?: number, + videoQuotaDaily?: number, + role?: UserRole +}) { + const path = '/api/v1/users/' + options.userId + + const toSend = {} + if (options.email !== undefined && options.email !== null) toSend['email'] = options.email + if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota + if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily + if (options.role !== undefined && options.role !== null) toSend['role'] = options.role + + return makePutBodyRequest({ + url: options.url, + path, + token: options.accessToken, + fields: toSend, + statusCodeExpected: 204 + }) +} + +function askResetPassword (url: string, email: string) { + const path = '/api/v1/users/ask-reset-password' + + return makePostBodyRequest({ + url, + path, + fields: { email }, + statusCodeExpected: 204 + }) +} + +function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { + const path = '/api/v1/users/' + userId + '/reset-password' + + return makePostBodyRequest({ + url, + path, + fields: { password, verificationString }, + statusCodeExpected + }) +} + +function askSendVerifyEmail (url: string, email: string) { + const path = '/api/v1/users/ask-send-verify-email' + + return makePostBodyRequest({ + url, + path, + fields: { email }, + statusCodeExpected: 204 + }) +} + +function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) { + const path = '/api/v1/users/' + userId + '/verify-email' + + return makePostBodyRequest({ + url, + path, + fields: { verificationString }, + statusCodeExpected + }) +} + +// --------------------------------------------------------------------------- + +export { + createUser, + registerUser, + getMyUserInformation, + getMyUserVideoRating, + deleteMe, + getMyUserVideoQuotaUsed, + getUsersList, + getUsersListPaginationAndSort, + removeUser, + updateUser, + updateMyUser, + getUserInformation, + blockUser, + unblockUser, + askResetPassword, + resetPassword, + updateMyAvatar, + askSendVerifyEmail, + verifyEmail +} -- cgit v1.2.3 From 8b9a525a180cc9f3a98c334cc052dcfc8f36dcd4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 17 Dec 2018 15:52:38 +0100 Subject: Add history on server side Add ability to disable, clear and list user videos history --- shared/utils/users/users.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'shared/utils/users/users.ts') diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts index 554e42c01..61a7e3757 100644 --- a/shared/utils/users/users.ts +++ b/shared/utils/users/users.ts @@ -162,14 +162,15 @@ function unblockUser (url: string, userId: number | string, accessToken: string, function updateMyUser (options: { url: string - accessToken: string, - currentPassword?: string, - newPassword?: string, - nsfwPolicy?: NSFWPolicyType, - email?: string, + accessToken: string + currentPassword?: string + newPassword?: string + nsfwPolicy?: NSFWPolicyType + email?: string autoPlayVideo?: boolean - displayName?: string, + displayName?: string description?: string + videosHistoryEnabled?: boolean }) { const path = '/api/v1/users/me' @@ -181,6 +182,9 @@ function updateMyUser (options: { 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 + if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) { + toSend['videosHistoryEnabled'] = options.videosHistoryEnabled + } return makePutBodyRequest({ url: options.url, -- cgit v1.2.3 From b426edd4854adc6e65844d8c54b8998e792b5778 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 11 Feb 2019 09:30:29 +0100 Subject: Cleanup reset user password by admin And add some tests --- shared/utils/users/users.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'shared/utils/users/users.ts') diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts index 61a7e3757..7191b263e 100644 --- a/shared/utils/users/users.ts +++ b/shared/utils/users/users.ts @@ -213,11 +213,13 @@ function updateUser (options: { emailVerified?: boolean, videoQuota?: number, videoQuotaDaily?: number, + password?: string, role?: UserRole }) { const path = '/api/v1/users/' + options.userId const toSend = {} + if (options.password !== undefined && options.password !== null) toSend['password'] = options.password if (options.email !== undefined && options.email !== null) toSend['email'] = options.email if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota -- cgit v1.2.3