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