diff options
Diffstat (limited to 'server/tests/api/videos/videos-history.ts')
-rw-r--r-- | server/tests/api/videos/videos-history.ts | 139 |
1 files changed, 60 insertions, 79 deletions
diff --git a/server/tests/api/videos/videos-history.ts b/server/tests/api/videos/videos-history.ts index b25cff879..e4bc0bb3a 100644 --- a/server/tests/api/videos/videos-history.ts +++ b/server/tests/api/videos/videos-history.ts | |||
@@ -1,74 +1,66 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | 2 | ||
3 | import * as chai from 'chai' | ||
4 | import 'mocha' | 3 | import 'mocha' |
4 | import * as chai from 'chai' | ||
5 | import { | 5 | import { |
6 | cleanupTests, | 6 | cleanupTests, |
7 | createUser, | 7 | createSingleServer, |
8 | flushAndRunServer, | 8 | HistoryCommand, |
9 | getVideosListWithToken, | ||
10 | getVideoWithToken, | ||
11 | killallServers, | 9 | killallServers, |
12 | reRunServer, | 10 | PeerTubeServer, |
13 | searchVideoWithToken, | ||
14 | ServerInfo, | ||
15 | setAccessTokensToServers, | 11 | setAccessTokensToServers, |
16 | updateMyUser, | ||
17 | uploadVideo, | ||
18 | userLogin, | ||
19 | wait | 12 | wait |
20 | } from '../../../../shared/extra-utils' | 13 | } from '@shared/extra-utils' |
21 | import { Video, VideoDetails } from '../../../../shared/models/videos' | 14 | import { HttpStatusCode, Video } from '@shared/models' |
22 | import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/extra-utils/videos/video-history' | ||
23 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
24 | 15 | ||
25 | const expect = chai.expect | 16 | const expect = chai.expect |
26 | 17 | ||
27 | describe('Test videos history', function () { | 18 | describe('Test videos history', function () { |
28 | let server: ServerInfo = null | 19 | let server: PeerTubeServer = null |
29 | let video1UUID: string | 20 | let video1UUID: string |
30 | let video2UUID: string | 21 | let video2UUID: string |
31 | let video3UUID: string | 22 | let video3UUID: string |
32 | let video3WatchedDate: Date | 23 | let video3WatchedDate: Date |
33 | let userAccessToken: string | 24 | let userAccessToken: string |
25 | let command: HistoryCommand | ||
34 | 26 | ||
35 | before(async function () { | 27 | before(async function () { |
36 | this.timeout(30000) | 28 | this.timeout(30000) |
37 | 29 | ||
38 | server = await flushAndRunServer(1) | 30 | server = await createSingleServer(1) |
39 | 31 | ||
40 | await setAccessTokensToServers([ server ]) | 32 | await setAccessTokensToServers([ server ]) |
41 | 33 | ||
34 | command = server.history | ||
35 | |||
42 | { | 36 | { |
43 | const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) | 37 | const { uuid } = await server.videos.upload({ attributes: { name: 'video 1' } }) |
44 | video1UUID = res.body.video.uuid | 38 | video1UUID = uuid |
45 | } | 39 | } |
46 | 40 | ||
47 | { | 41 | { |
48 | const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' }) | 42 | const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } }) |
49 | video2UUID = res.body.video.uuid | 43 | video2UUID = uuid |
50 | } | 44 | } |
51 | 45 | ||
52 | { | 46 | { |
53 | const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' }) | 47 | const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } }) |
54 | video3UUID = res.body.video.uuid | 48 | video3UUID = uuid |
55 | } | 49 | } |
56 | 50 | ||
57 | const user = { | 51 | const user = { |
58 | username: 'user_1', | 52 | username: 'user_1', |
59 | password: 'super password' | 53 | password: 'super password' |
60 | } | 54 | } |
61 | await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password }) | 55 | await server.users.create({ username: user.username, password: user.password }) |
62 | userAccessToken = await userLogin(server, user) | 56 | userAccessToken = await server.login.getAccessToken(user) |
63 | }) | 57 | }) |
64 | 58 | ||
65 | it('Should get videos, without watching history', async function () { | 59 | it('Should get videos, without watching history', async function () { |
66 | const res = await getVideosListWithToken(server.url, server.accessToken) | 60 | const { data } = await server.videos.listWithToken() |
67 | const videos: Video[] = res.body.data | ||
68 | 61 | ||
69 | for (const video of videos) { | 62 | for (const video of data) { |
70 | const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id) | 63 | const videoDetails = await server.videos.getWithToken({ id: video.id }) |
71 | const videoDetails: VideoDetails = resDetail.body | ||
72 | 64 | ||
73 | expect(video.userHistory).to.be.undefined | 65 | expect(video.userHistory).to.be.undefined |
74 | expect(videoDetails.userHistory).to.be.undefined | 66 | expect(videoDetails.userHistory).to.be.undefined |
@@ -76,21 +68,21 @@ describe('Test videos history', function () { | |||
76 | }) | 68 | }) |
77 | 69 | ||
78 | it('Should watch the first and second video', async function () { | 70 | it('Should watch the first and second video', async function () { |
79 | await userWatchVideo(server.url, server.accessToken, video2UUID, 8) | 71 | await command.wathVideo({ videoId: video2UUID, currentTime: 8 }) |
80 | await userWatchVideo(server.url, server.accessToken, video1UUID, 3) | 72 | await command.wathVideo({ videoId: video1UUID, currentTime: 3 }) |
81 | }) | 73 | }) |
82 | 74 | ||
83 | it('Should return the correct history when listing, searching and getting videos', async function () { | 75 | it('Should return the correct history when listing, searching and getting videos', async function () { |
84 | const videosOfVideos: Video[][] = [] | 76 | const videosOfVideos: Video[][] = [] |
85 | 77 | ||
86 | { | 78 | { |
87 | const res = await getVideosListWithToken(server.url, server.accessToken) | 79 | const { data } = await server.videos.listWithToken() |
88 | videosOfVideos.push(res.body.data) | 80 | videosOfVideos.push(data) |
89 | } | 81 | } |
90 | 82 | ||
91 | { | 83 | { |
92 | const res = await searchVideoWithToken(server.url, 'video', server.accessToken) | 84 | const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' }) |
93 | videosOfVideos.push(res.body.data) | 85 | videosOfVideos.push(body.data) |
94 | } | 86 | } |
95 | 87 | ||
96 | for (const videos of videosOfVideos) { | 88 | for (const videos of videosOfVideos) { |
@@ -108,24 +100,21 @@ describe('Test videos history', function () { | |||
108 | } | 100 | } |
109 | 101 | ||
110 | { | 102 | { |
111 | const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID) | 103 | const videoDetails = await server.videos.getWithToken({ id: video1UUID }) |
112 | const videoDetails: VideoDetails = resDetail.body | ||
113 | 104 | ||
114 | expect(videoDetails.userHistory).to.not.be.undefined | 105 | expect(videoDetails.userHistory).to.not.be.undefined |
115 | expect(videoDetails.userHistory.currentTime).to.equal(3) | 106 | expect(videoDetails.userHistory.currentTime).to.equal(3) |
116 | } | 107 | } |
117 | 108 | ||
118 | { | 109 | { |
119 | const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID) | 110 | const videoDetails = await server.videos.getWithToken({ id: video2UUID }) |
120 | const videoDetails: VideoDetails = resDetail.body | ||
121 | 111 | ||
122 | expect(videoDetails.userHistory).to.not.be.undefined | 112 | expect(videoDetails.userHistory).to.not.be.undefined |
123 | expect(videoDetails.userHistory.currentTime).to.equal(8) | 113 | expect(videoDetails.userHistory.currentTime).to.equal(8) |
124 | } | 114 | } |
125 | 115 | ||
126 | { | 116 | { |
127 | const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID) | 117 | const videoDetails = await server.videos.getWithToken({ id: video3UUID }) |
128 | const videoDetails: VideoDetails = resDetail.body | ||
129 | 118 | ||
130 | expect(videoDetails.userHistory).to.be.undefined | 119 | expect(videoDetails.userHistory).to.be.undefined |
131 | } | 120 | } |
@@ -133,71 +122,64 @@ describe('Test videos history', function () { | |||
133 | 122 | ||
134 | it('Should have these videos when listing my history', async function () { | 123 | it('Should have these videos when listing my history', async function () { |
135 | video3WatchedDate = new Date() | 124 | video3WatchedDate = new Date() |
136 | await userWatchVideo(server.url, server.accessToken, video3UUID, 2) | 125 | await command.wathVideo({ videoId: video3UUID, currentTime: 2 }) |
137 | 126 | ||
138 | const res = await listMyVideosHistory(server.url, server.accessToken) | 127 | const body = await command.list() |
139 | 128 | ||
140 | expect(res.body.total).to.equal(3) | 129 | expect(body.total).to.equal(3) |
141 | 130 | ||
142 | const videos: Video[] = res.body.data | 131 | const videos = body.data |
143 | expect(videos[0].name).to.equal('video 3') | 132 | expect(videos[0].name).to.equal('video 3') |
144 | expect(videos[1].name).to.equal('video 1') | 133 | expect(videos[1].name).to.equal('video 1') |
145 | expect(videos[2].name).to.equal('video 2') | 134 | expect(videos[2].name).to.equal('video 2') |
146 | }) | 135 | }) |
147 | 136 | ||
148 | it('Should not have videos history on another user', async function () { | 137 | it('Should not have videos history on another user', async function () { |
149 | const res = await listMyVideosHistory(server.url, userAccessToken) | 138 | const body = await command.list({ token: userAccessToken }) |
150 | 139 | ||
151 | expect(res.body.total).to.equal(0) | 140 | expect(body.total).to.equal(0) |
152 | expect(res.body.data).to.have.lengthOf(0) | 141 | expect(body.data).to.have.lengthOf(0) |
153 | }) | 142 | }) |
154 | 143 | ||
155 | it('Should be able to search through videos in my history', async function () { | 144 | it('Should be able to search through videos in my history', async function () { |
156 | const res = await listMyVideosHistory(server.url, server.accessToken, '2') | 145 | const body = await command.list({ search: '2' }) |
157 | 146 | expect(body.total).to.equal(1) | |
158 | expect(res.body.total).to.equal(1) | ||
159 | 147 | ||
160 | const videos: Video[] = res.body.data | 148 | const videos = body.data |
161 | expect(videos[0].name).to.equal('video 2') | 149 | expect(videos[0].name).to.equal('video 2') |
162 | }) | 150 | }) |
163 | 151 | ||
164 | it('Should clear my history', async function () { | 152 | it('Should clear my history', async function () { |
165 | await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString()) | 153 | await command.remove({ beforeDate: video3WatchedDate.toISOString() }) |
166 | }) | 154 | }) |
167 | 155 | ||
168 | it('Should have my history cleared', async function () { | 156 | it('Should have my history cleared', async function () { |
169 | const res = await listMyVideosHistory(server.url, server.accessToken) | 157 | const body = await command.list() |
158 | expect(body.total).to.equal(1) | ||
170 | 159 | ||
171 | expect(res.body.total).to.equal(1) | 160 | const videos = body.data |
172 | |||
173 | const videos: Video[] = res.body.data | ||
174 | expect(videos[0].name).to.equal('video 3') | 161 | expect(videos[0].name).to.equal('video 3') |
175 | }) | 162 | }) |
176 | 163 | ||
177 | it('Should disable videos history', async function () { | 164 | it('Should disable videos history', async function () { |
178 | await updateMyUser({ | 165 | await server.users.updateMe({ |
179 | url: server.url, | ||
180 | accessToken: server.accessToken, | ||
181 | videosHistoryEnabled: false | 166 | videosHistoryEnabled: false |
182 | }) | 167 | }) |
183 | 168 | ||
184 | await userWatchVideo(server.url, server.accessToken, video2UUID, 8, HttpStatusCode.CONFLICT_409) | 169 | await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 }) |
185 | }) | 170 | }) |
186 | 171 | ||
187 | it('Should re-enable videos history', async function () { | 172 | it('Should re-enable videos history', async function () { |
188 | await updateMyUser({ | 173 | await server.users.updateMe({ |
189 | url: server.url, | ||
190 | accessToken: server.accessToken, | ||
191 | videosHistoryEnabled: true | 174 | videosHistoryEnabled: true |
192 | }) | 175 | }) |
193 | 176 | ||
194 | await userWatchVideo(server.url, server.accessToken, video1UUID, 8) | 177 | await command.wathVideo({ videoId: video1UUID, currentTime: 8 }) |
195 | |||
196 | const res = await listMyVideosHistory(server.url, server.accessToken) | ||
197 | 178 | ||
198 | expect(res.body.total).to.equal(2) | 179 | const body = await command.list() |
180 | expect(body.total).to.equal(2) | ||
199 | 181 | ||
200 | const videos: Video[] = res.body.data | 182 | const videos = body.data |
201 | expect(videos[0].name).to.equal('video 1') | 183 | expect(videos[0].name).to.equal('video 1') |
202 | expect(videos[1].name).to.equal('video 3') | 184 | expect(videos[1].name).to.equal('video 3') |
203 | }) | 185 | }) |
@@ -205,30 +187,29 @@ describe('Test videos history', function () { | |||
205 | it('Should not clean old history', async function () { | 187 | it('Should not clean old history', async function () { |
206 | this.timeout(50000) | 188 | this.timeout(50000) |
207 | 189 | ||
208 | killallServers([ server ]) | 190 | await killallServers([ server ]) |
209 | 191 | ||
210 | await reRunServer(server, { history: { videos: { max_age: '10 days' } } }) | 192 | await server.run({ history: { videos: { max_age: '10 days' } } }) |
211 | 193 | ||
212 | await wait(6000) | 194 | await wait(6000) |
213 | 195 | ||
214 | // Should still have history | 196 | // Should still have history |
215 | 197 | ||
216 | const res = await listMyVideosHistory(server.url, server.accessToken) | 198 | const body = await command.list() |
217 | 199 | expect(body.total).to.equal(2) | |
218 | expect(res.body.total).to.equal(2) | ||
219 | }) | 200 | }) |
220 | 201 | ||
221 | it('Should clean old history', async function () { | 202 | it('Should clean old history', async function () { |
222 | this.timeout(50000) | 203 | this.timeout(50000) |
223 | 204 | ||
224 | killallServers([ server ]) | 205 | await killallServers([ server ]) |
225 | 206 | ||
226 | await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } }) | 207 | await server.run({ history: { videos: { max_age: '5 seconds' } } }) |
227 | 208 | ||
228 | await wait(6000) | 209 | await wait(6000) |
229 | 210 | ||
230 | const res = await listMyVideosHistory(server.url, server.accessToken) | 211 | const body = await command.list() |
231 | expect(res.body.total).to.equal(0) | 212 | expect(body.total).to.equal(0) |
232 | }) | 213 | }) |
233 | 214 | ||
234 | after(async function () { | 215 | after(async function () { |