aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/src/api/check-params/video-chapters.ts
blob: c59f88e79ca3162ddf27e1642cd630bbdc7b0e58 (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { HttpStatusCode, Video, VideoCreateResult, VideoPrivacy } from '@peertube/peertube-models'
import {
  PeerTubeServer,
  cleanupTests,
  createSingleServer,
  setAccessTokensToServers,
  setDefaultVideoChannel
} from '@peertube/peertube-server-commands'

describe('Test videos chapters API validator', function () {
  let server: PeerTubeServer
  let video: VideoCreateResult
  let live: Video
  let privateVideo: VideoCreateResult
  let userAccessToken: string

  // ---------------------------------------------------------------

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

    server = await createSingleServer(1)

    await setAccessTokensToServers([ server ])
    await setDefaultVideoChannel([ server ])

    video = await server.videos.upload()
    privateVideo = await server.videos.upload({ attributes: { privacy: VideoPrivacy.PRIVATE } })
    userAccessToken = await server.users.generateUserAndToken('user1')

    await server.config.enableLive({ allowReplay: false })

    const res = await server.live.quickCreate({ saveReplay: false, permanentLive: false })
    live = res.video
  })

  describe('When updating chapters', function () {

    it('Should fail without a valid uuid', async function () {
      await server.chapters.update({ videoId: '4da6fd', chapters: [], expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should fail with an unknown id', async function () {
      await server.chapters.update({
        videoId: 'ce0801ef-7124-48df-9b22-b473ace78797',
        chapters: [],
        expectedStatus: HttpStatusCode.NOT_FOUND_404
      })
    })

    it('Should fail without access token', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: [],
        token: null,
        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
      })
    })

    it('Should fail with a bad access token', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: [],
        token: 'toto',
        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
      })
    })

    it('Should fail with a another user access token', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: [],
        token: userAccessToken,
        expectedStatus: HttpStatusCode.FORBIDDEN_403
      })
    })

    it('Should fail with a wrong chapters param', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: 'hello' as any,
        expectedStatus: HttpStatusCode.BAD_REQUEST_400
      })
    })

    it('Should fail with a bad chapter title', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: [ { title: 'hello', timecode: 21 }, { title: '', timecode: 21 } ],
        expectedStatus: HttpStatusCode.BAD_REQUEST_400
      })

      await server.chapters.update({
        videoId: video.id,
        chapters: [ { title: 'hello', timecode: 21 }, { title: 'a'.repeat(150), timecode: 21 } ],
        expectedStatus: HttpStatusCode.BAD_REQUEST_400
      })
    })

    it('Should fail with a bad timecode', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: [ { title: 'hello', timecode: 21 }, { title: 'title', timecode: -5 } ],
        expectedStatus: HttpStatusCode.BAD_REQUEST_400
      })

      await server.chapters.update({
        videoId: video.id,
        chapters: [ { title: 'hello', timecode: 21 }, { title: 'title', timecode: 'hi' as any } ],
        expectedStatus: HttpStatusCode.BAD_REQUEST_400
      })
    })

    it('Should fail with non unique timecodes', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: [ { title: 'hello', timecode: 21 }, { title: 'title', timecode: 22 }, { title: 'hello', timecode: 21 } ],
        expectedStatus: HttpStatusCode.BAD_REQUEST_400
      })
    })

    it('Should fail to create chapters on a live', async function () {
      await server.chapters.update({
        videoId: live.id,
        chapters: [],
        expectedStatus: HttpStatusCode.BAD_REQUEST_400
      })
    })

    it('Should succeed with the correct params', async function () {
      await server.chapters.update({
        videoId: video.id,
        chapters: []
      })

      await server.chapters.update({
        videoId: video.id,
        chapters: [ { title: 'hello', timecode: 21 }, { title: 'hello 2', timecode: 35 } ]
      })
    })
  })

  describe('When listing chapters', function () {

    it('Should fail without a valid uuid', async function () {
      await server.chapters.list({ videoId: '4da6fd', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
    })

    it('Should fail with an unknown id', async function () {
      await server.chapters.list({ videoId: '4da6fde3-88f7-4d16-b119-108df5630b06', expectedStatus: HttpStatusCode.NOT_FOUND_404 })
    })

    it('Should not list private chapters to anyone', async function () {
      await server.chapters.list({ videoId: privateVideo.uuid, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
    })

    it('Should not list private chapters to another user', async function () {
      await server.chapters.list({ videoId: privateVideo.uuid, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
    })

    it('Should list chapters', async function () {
      await server.chapters.list({ videoId: privateVideo.uuid })
      await server.chapters.list({ videoId: video.uuid })
    })
  })

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