users.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. import { omit } from 'lodash'
  2. import * as request from 'supertest'
  3. import { UserUpdateMe } from '../../models/users'
  4. import { UserAdminFlag } from '../../models/users/user-flag.model'
  5. import { UserRegister } from '../../models/users/user-register.model'
  6. import { UserRole } from '../../models/users/user-role'
  7. import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
  8. import { ServerInfo } from '../server/servers'
  9. import { userLogin } from './login'
  10. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  11. type CreateUserArgs = {
  12. url: string
  13. accessToken: string
  14. username: string
  15. password: string
  16. videoQuota?: number
  17. videoQuotaDaily?: number
  18. role?: UserRole
  19. adminFlags?: UserAdminFlag
  20. specialStatus?: number
  21. }
  22. function createUser (parameters: CreateUserArgs) {
  23. const {
  24. url,
  25. accessToken,
  26. username,
  27. adminFlags,
  28. password = 'password',
  29. videoQuota = 1000000,
  30. videoQuotaDaily = -1,
  31. role = UserRole.USER,
  32. specialStatus = HttpStatusCode.OK_200
  33. } = parameters
  34. const path = '/api/v1/users'
  35. const body = {
  36. username,
  37. password,
  38. role,
  39. adminFlags,
  40. email: username + '@example.com',
  41. videoQuota,
  42. videoQuotaDaily
  43. }
  44. return request(url)
  45. .post(path)
  46. .set('Accept', 'application/json')
  47. .set('Authorization', 'Bearer ' + accessToken)
  48. .send(body)
  49. .expect(specialStatus)
  50. }
  51. async function generateUserAccessToken (server: ServerInfo, username: string) {
  52. const password = 'my super password'
  53. await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
  54. return userLogin(server, { username, password })
  55. }
  56. function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
  57. const path = '/api/v1/users/register'
  58. const body = {
  59. username,
  60. password,
  61. email: username + '@example.com'
  62. }
  63. return request(url)
  64. .post(path)
  65. .set('Accept', 'application/json')
  66. .send(body)
  67. .expect(specialStatus)
  68. }
  69. function registerUserWithChannel (options: {
  70. url: string
  71. user: { username: string, password: string, displayName?: string }
  72. channel: { name: string, displayName: string }
  73. }) {
  74. const path = '/api/v1/users/register'
  75. const body: UserRegister = {
  76. username: options.user.username,
  77. password: options.user.password,
  78. email: options.user.username + '@example.com',
  79. channel: options.channel
  80. }
  81. if (options.user.displayName) {
  82. Object.assign(body, { displayName: options.user.displayName })
  83. }
  84. return makePostBodyRequest({
  85. url: options.url,
  86. path,
  87. fields: body,
  88. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  89. })
  90. }
  91. function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
  92. const path = '/api/v1/users/me'
  93. return request(url)
  94. .get(path)
  95. .set('Accept', 'application/json')
  96. .set('Authorization', 'Bearer ' + accessToken)
  97. .expect(specialStatus)
  98. .expect('Content-Type', /json/)
  99. }
  100. function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
  101. const path = '/api/v1/users/scoped-tokens'
  102. return makeGetRequest({
  103. url,
  104. path,
  105. token,
  106. statusCodeExpected
  107. })
  108. }
  109. function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
  110. const path = '/api/v1/users/scoped-tokens'
  111. return makePostBodyRequest({
  112. url,
  113. path,
  114. token,
  115. statusCodeExpected
  116. })
  117. }
  118. function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
  119. const path = '/api/v1/users/me'
  120. return request(url)
  121. .delete(path)
  122. .set('Accept', 'application/json')
  123. .set('Authorization', 'Bearer ' + accessToken)
  124. .expect(specialStatus)
  125. }
  126. function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
  127. const path = '/api/v1/users/me/video-quota-used'
  128. return request(url)
  129. .get(path)
  130. .set('Accept', 'application/json')
  131. .set('Authorization', 'Bearer ' + accessToken)
  132. .expect(specialStatus)
  133. .expect('Content-Type', /json/)
  134. }
  135. function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
  136. const path = '/api/v1/users/' + userId
  137. return request(url)
  138. .get(path)
  139. .query({ withStats })
  140. .set('Accept', 'application/json')
  141. .set('Authorization', 'Bearer ' + accessToken)
  142. .expect(HttpStatusCode.OK_200)
  143. .expect('Content-Type', /json/)
  144. }
  145. function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) {
  146. const path = '/api/v1/users/me/videos/' + videoId + '/rating'
  147. return request(url)
  148. .get(path)
  149. .set('Accept', 'application/json')
  150. .set('Authorization', 'Bearer ' + accessToken)
  151. .expect(specialStatus)
  152. .expect('Content-Type', /json/)
  153. }
  154. function getUsersList (url: string, accessToken: string) {
  155. const path = '/api/v1/users'
  156. return request(url)
  157. .get(path)
  158. .set('Accept', 'application/json')
  159. .set('Authorization', 'Bearer ' + accessToken)
  160. .expect(HttpStatusCode.OK_200)
  161. .expect('Content-Type', /json/)
  162. }
  163. function getUsersListPaginationAndSort (
  164. url: string,
  165. accessToken: string,
  166. start: number,
  167. count: number,
  168. sort: string,
  169. search?: string,
  170. blocked?: boolean
  171. ) {
  172. const path = '/api/v1/users'
  173. const query = {
  174. start,
  175. count,
  176. sort,
  177. search,
  178. blocked
  179. }
  180. return request(url)
  181. .get(path)
  182. .query(query)
  183. .set('Accept', 'application/json')
  184. .set('Authorization', 'Bearer ' + accessToken)
  185. .expect(HttpStatusCode.OK_200)
  186. .expect('Content-Type', /json/)
  187. }
  188. function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
  189. const path = '/api/v1/users'
  190. return request(url)
  191. .delete(path + '/' + userId)
  192. .set('Accept', 'application/json')
  193. .set('Authorization', 'Bearer ' + accessToken)
  194. .expect(expectedStatus)
  195. }
  196. function blockUser (
  197. url: string,
  198. userId: number | string,
  199. accessToken: string,
  200. expectedStatus = HttpStatusCode.NO_CONTENT_204,
  201. reason?: string
  202. ) {
  203. const path = '/api/v1/users'
  204. let body: any
  205. if (reason) body = { reason }
  206. return request(url)
  207. .post(path + '/' + userId + '/block')
  208. .send(body)
  209. .set('Accept', 'application/json')
  210. .set('Authorization', 'Bearer ' + accessToken)
  211. .expect(expectedStatus)
  212. }
  213. function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
  214. const path = '/api/v1/users'
  215. return request(url)
  216. .post(path + '/' + userId + '/unblock')
  217. .set('Accept', 'application/json')
  218. .set('Authorization', 'Bearer ' + accessToken)
  219. .expect(expectedStatus)
  220. }
  221. function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) {
  222. const path = '/api/v1/users/me'
  223. const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
  224. return makePutBodyRequest({
  225. url: options.url,
  226. path,
  227. token: options.accessToken,
  228. fields: toSend,
  229. statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
  230. })
  231. }
  232. function updateMyAvatar (options: {
  233. url: string
  234. accessToken: string
  235. fixture: string
  236. }) {
  237. const path = '/api/v1/users/me/avatar/pick'
  238. return updateAvatarRequest(Object.assign(options, { path }))
  239. }
  240. function updateUser (options: {
  241. url: string
  242. userId: number
  243. accessToken: string
  244. email?: string
  245. emailVerified?: boolean
  246. videoQuota?: number
  247. videoQuotaDaily?: number
  248. password?: string
  249. adminFlags?: UserAdminFlag
  250. role?: UserRole
  251. }) {
  252. const path = '/api/v1/users/' + options.userId
  253. const toSend = {}
  254. if (options.password !== undefined && options.password !== null) toSend['password'] = options.password
  255. if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
  256. if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
  257. if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
  258. if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
  259. if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
  260. if (options.adminFlags !== undefined && options.adminFlags !== null) toSend['adminFlags'] = options.adminFlags
  261. return makePutBodyRequest({
  262. url: options.url,
  263. path,
  264. token: options.accessToken,
  265. fields: toSend,
  266. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  267. })
  268. }
  269. function askResetPassword (url: string, email: string) {
  270. const path = '/api/v1/users/ask-reset-password'
  271. return makePostBodyRequest({
  272. url,
  273. path,
  274. fields: { email },
  275. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  276. })
  277. }
  278. function resetPassword (
  279. url: string,
  280. userId: number,
  281. verificationString: string,
  282. password: string,
  283. statusCodeExpected = HttpStatusCode.NO_CONTENT_204
  284. ) {
  285. const path = '/api/v1/users/' + userId + '/reset-password'
  286. return makePostBodyRequest({
  287. url,
  288. path,
  289. fields: { password, verificationString },
  290. statusCodeExpected
  291. })
  292. }
  293. function askSendVerifyEmail (url: string, email: string) {
  294. const path = '/api/v1/users/ask-send-verify-email'
  295. return makePostBodyRequest({
  296. url,
  297. path,
  298. fields: { email },
  299. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  300. })
  301. }
  302. function verifyEmail (
  303. url: string,
  304. userId: number,
  305. verificationString: string,
  306. isPendingEmail = false,
  307. statusCodeExpected = HttpStatusCode.NO_CONTENT_204
  308. ) {
  309. const path = '/api/v1/users/' + userId + '/verify-email'
  310. return makePostBodyRequest({
  311. url,
  312. path,
  313. fields: {
  314. verificationString,
  315. isPendingEmail
  316. },
  317. statusCodeExpected
  318. })
  319. }
  320. // ---------------------------------------------------------------------------
  321. export {
  322. createUser,
  323. registerUser,
  324. getMyUserInformation,
  325. getMyUserVideoRating,
  326. deleteMe,
  327. registerUserWithChannel,
  328. getMyUserVideoQuotaUsed,
  329. getUsersList,
  330. getUsersListPaginationAndSort,
  331. removeUser,
  332. updateUser,
  333. updateMyUser,
  334. getUserInformation,
  335. blockUser,
  336. unblockUser,
  337. askResetPassword,
  338. resetPassword,
  339. renewUserScopedTokens,
  340. updateMyAvatar,
  341. askSendVerifyEmail,
  342. generateUserAccessToken,
  343. verifyEmail,
  344. getUserScopedTokens
  345. }