]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos-history.ts
Fix test
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / videos-history.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { checkBadCountPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
6 import {
7 cleanupTests,
8 createSingleServer,
9 makeDeleteRequest,
10 makeGetRequest,
11 makePostBodyRequest,
12 makePutBodyRequest,
13 PeerTubeServer,
14 setAccessTokensToServers
15 } from '@shared/server-commands'
16
17 describe('Test videos history API validator', function () {
18 const myHistoryPath = '/api/v1/users/me/history/videos'
19 const myHistoryRemove = myHistoryPath + '/remove'
20 let viewPath: string
21 let server: PeerTubeServer
22 let videoId: number
23
24 // ---------------------------------------------------------------
25
26 before(async function () {
27 this.timeout(30000)
28
29 server = await createSingleServer(1)
30
31 await setAccessTokensToServers([ server ])
32
33 const { id, uuid } = await server.videos.upload()
34 viewPath = '/api/v1/videos/' + uuid + '/views'
35 videoId = id
36 })
37
38 describe('When notifying a user is watching a video', function () {
39
40 it('Should fail with a bad token', async function () {
41 const fields = { currentTime: 5 }
42 await makePutBodyRequest({ url: server.url, path: viewPath, fields, token: 'bad', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
43 })
44
45 it('Should succeed with the correct parameters', async function () {
46 const fields = { currentTime: 5 }
47
48 await makePutBodyRequest({
49 url: server.url,
50 path: viewPath,
51 fields,
52 token: server.accessToken,
53 expectedStatus: HttpStatusCode.NO_CONTENT_204
54 })
55 })
56 })
57
58 describe('When listing user videos history', function () {
59 it('Should fail with a bad start pagination', async function () {
60 await checkBadStartPagination(server.url, myHistoryPath, server.accessToken)
61 })
62
63 it('Should fail with a bad count pagination', async function () {
64 await checkBadCountPagination(server.url, myHistoryPath, server.accessToken)
65 })
66
67 it('Should fail with an unauthenticated user', async function () {
68 await makeGetRequest({ url: server.url, path: myHistoryPath, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
69 })
70
71 it('Should succeed with the correct params', async function () {
72 await makeGetRequest({ url: server.url, token: server.accessToken, path: myHistoryPath, expectedStatus: HttpStatusCode.OK_200 })
73 })
74 })
75
76 describe('When removing a specific user video history element', function () {
77 let path: string
78
79 before(function () {
80 path = myHistoryPath + '/' + videoId
81 })
82
83 it('Should fail with an unauthenticated user', async function () {
84 await makeDeleteRequest({ url: server.url, path, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
85 })
86
87 it('Should fail with a bad videoId parameter', async function () {
88 await makeDeleteRequest({
89 url: server.url,
90 token: server.accessToken,
91 path: myHistoryRemove + '/hi',
92 expectedStatus: HttpStatusCode.BAD_REQUEST_400
93 })
94 })
95
96 it('Should succeed with the correct parameters', async function () {
97 await makeDeleteRequest({
98 url: server.url,
99 token: server.accessToken,
100 path,
101 expectedStatus: HttpStatusCode.NO_CONTENT_204
102 })
103 })
104 })
105
106 describe('When removing all user videos history', function () {
107 it('Should fail with an unauthenticated user', async function () {
108 await makePostBodyRequest({ url: server.url, path: myHistoryPath + '/remove', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
109 })
110
111 it('Should fail with a bad beforeDate parameter', async function () {
112 const body = { beforeDate: '15' }
113 await makePostBodyRequest({
114 url: server.url,
115 token: server.accessToken,
116 path: myHistoryRemove,
117 fields: body,
118 expectedStatus: HttpStatusCode.BAD_REQUEST_400
119 })
120 })
121
122 it('Should succeed with a valid beforeDate param', async function () {
123 const body = { beforeDate: new Date().toISOString() }
124 await makePostBodyRequest({
125 url: server.url,
126 token: server.accessToken,
127 path: myHistoryRemove,
128 fields: body,
129 expectedStatus: HttpStatusCode.NO_CONTENT_204
130 })
131 })
132
133 it('Should succeed without body', async function () {
134 await makePostBodyRequest({
135 url: server.url,
136 token: server.accessToken,
137 path: myHistoryRemove,
138 expectedStatus: HttpStatusCode.NO_CONTENT_204
139 })
140 })
141 })
142
143 after(async function () {
144 await cleanupTests([ server ])
145 })
146 })