users.ts 10 KB

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