aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/videos/videos-history.ts
blob: 6df26ab7dcbb547fb39e1f8665e43d46bd6531c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { wait } from '@shared/core-utils'
import { Video } from '@shared/models'
import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'

describe('Test videos history', function () {
  let server: PeerTubeServer = null
  let video1Id: number
  let video1UUID: string
  let video2UUID: string
  let video3UUID: string
  let video3WatchedDate: Date
  let userAccessToken: string

  before(async function () {
    this.timeout(120000)

    server = await createSingleServer(1)

    await setAccessTokensToServers([ server ])

    // 10 seconds long
    const fixture = 'video_short1.webm'

    {
      const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } })
      video1UUID = uuid
      video1Id = id
    }

    {
      const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } })
      video2UUID = uuid
    }

    {
      const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } })
      video3UUID = uuid
    }

    userAccessToken = await server.users.generateUserAndToken('user_1')
  })

  it('Should get videos, without watching history', async function () {
    const { data } = await server.videos.listWithToken()

    for (const video of data) {
      const videoDetails = await server.videos.getWithToken({ id: video.id })

      expect(video.userHistory).to.be.undefined
      expect(videoDetails.userHistory).to.be.undefined
    }
  })

  it('Should watch the first and second video', async function () {
    await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
    await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 })
  })

  it('Should return the correct history when listing, searching and getting videos', async function () {
    const videosOfVideos: Video[][] = []

    {
      const { data } = await server.videos.listWithToken()
      videosOfVideos.push(data)
    }

    {
      const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
      videosOfVideos.push(body.data)
    }

    for (const videos of videosOfVideos) {
      const video1 = videos.find(v => v.uuid === video1UUID)
      const video2 = videos.find(v => v.uuid === video2UUID)
      const video3 = videos.find(v => v.uuid === video3UUID)

      expect(video1.userHistory).to.not.be.undefined
      expect(video1.userHistory.currentTime).to.equal(3)

      expect(video2.userHistory).to.not.be.undefined
      expect(video2.userHistory.currentTime).to.equal(8)

      expect(video3.userHistory).to.be.undefined
    }

    {
      const videoDetails = await server.videos.getWithToken({ id: video1UUID })

      expect(videoDetails.userHistory).to.not.be.undefined
      expect(videoDetails.userHistory.currentTime).to.equal(3)
    }

    {
      const videoDetails = await server.videos.getWithToken({ id: video2UUID })

      expect(videoDetails.userHistory).to.not.be.undefined
      expect(videoDetails.userHistory.currentTime).to.equal(8)
    }

    {
      const videoDetails = await server.videos.getWithToken({ id: video3UUID })

      expect(videoDetails.userHistory).to.be.undefined
    }
  })

  it('Should have these videos when listing my history', async function () {
    video3WatchedDate = new Date()
    await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 })

    const body = await server.history.list()

    expect(body.total).to.equal(3)

    const videos = body.data
    expect(videos[0].name).to.equal('video 3')
    expect(videos[1].name).to.equal('video 1')
    expect(videos[2].name).to.equal('video 2')
  })

  it('Should not have videos history on another user', async function () {
    const body = await server.history.list({ token: userAccessToken })

    expect(body.total).to.equal(0)
    expect(body.data).to.have.lengthOf(0)
  })

  it('Should be able to search through videos in my history', async function () {
    const body = await server.history.list({ search: '2' })
    expect(body.total).to.equal(1)

    const videos = body.data
    expect(videos[0].name).to.equal('video 2')
  })

  it('Should clear my history', async function () {
    await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() })
  })

  it('Should have my history cleared', async function () {
    const body = await server.history.list()
    expect(body.total).to.equal(1)

    const videos = body.data
    expect(videos[0].name).to.equal('video 3')
  })

  it('Should disable videos history', async function () {
    await server.users.updateMe({
      videosHistoryEnabled: false
    })

    await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })

    const { data } = await server.history.list()
    expect(data[0].name).to.not.equal('video 2')
  })

  it('Should re-enable videos history', async function () {
    await server.users.updateMe({
      videosHistoryEnabled: true
    })

    await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })

    const { data } = await server.history.list()
    expect(data[0].name).to.equal('video 2')
  })

  it('Should not clean old history', async function () {
    this.timeout(50000)

    await killallServers([ server ])

    await server.run({ history: { videos: { max_age: '10 days' } } })

    await wait(6000)

    // Should still have history

    const body = await server.history.list()
    expect(body.total).to.equal(2)
  })

  it('Should clean old history', async function () {
    this.timeout(50000)

    await killallServers([ server ])

    await server.run({ history: { videos: { max_age: '5 seconds' } } })

    await wait(6000)

    const body = await server.history.list()
    expect(body.total).to.equal(0)
  })

  it('Should delete a specific history element', async function () {
    {
      await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 })
      await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
    }

    {
      const body = await server.history.list()
      expect(body.total).to.equal(2)
    }

    {
      await server.history.removeElement({ videoId: video1Id })

      const body = await server.history.list()
      expect(body.total).to.equal(1)
      expect(body.data[0].uuid).to.equal(video2UUID)
    }
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})