aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/channel-import-videos.ts
blob: 0ec5fc2b9af9e7e4423f3d1b924e3678037412cd (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { FIXTURE_URLS } from '@server/tests/shared'
import { areHttpImportTestsDisabled } from '@shared/core-utils'
import { HttpStatusCode } from '@shared/models'
import {
  ChannelsCommand,
  cleanupTests,
  createSingleServer,
  PeerTubeServer,
  setAccessTokensToServers,
  setDefaultVideoChannel
} from '@shared/server-commands'

describe('Test videos import in a channel API validator', function () {
  let server: PeerTubeServer
  const userInfo = {
    accessToken: '',
    channelName: 'fake_channel',
    id: -1,
    videoQuota: -1,
    videoQuotaDaily: -1
  }
  let command: ChannelsCommand

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

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

    server = await createSingleServer(1)

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

    const userCreds = {
      username: 'fake',
      password: 'fake_password'
    }

    {
      const user = await server.users.create({ username: userCreds.username, password: userCreds.password })
      userInfo.id = user.id
      userInfo.accessToken = await server.login.getAccessToken(userCreds)
    }

    command = server.channels
  })

  it('Should fail when HTTP upload is disabled', async function () {
    await server.config.disableImports()

    await command.importVideos({
      channelName: server.store.channel.name,
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      token: server.accessToken,
      expectedStatus: HttpStatusCode.FORBIDDEN_403
    })

    await server.config.enableImports()
  })

  it('Should fail when externalChannelUrl is not provided', async function () {
    await command.importVideos({
      channelName: server.store.channel.name,
      externalChannelUrl: null,
      token: server.accessToken,
      expectedStatus: HttpStatusCode.BAD_REQUEST_400
    })
  })

  it('Should fail when externalChannelUrl is malformed', async function () {
    await command.importVideos({
      channelName: server.store.channel.name,
      externalChannelUrl: 'not-a-url',
      token: server.accessToken,
      expectedStatus: HttpStatusCode.BAD_REQUEST_400
    })
  })

  it('Should fail with a bad sync id', async function () {
    await command.importVideos({
      channelName: server.store.channel.name,
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      videoChannelSyncId: 'toto' as any,
      token: server.accessToken,
      expectedStatus: HttpStatusCode.BAD_REQUEST_400
    })
  })

  it('Should fail with a unknown sync id', async function () {
    await command.importVideos({
      channelName: server.store.channel.name,
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      videoChannelSyncId: 42,
      token: server.accessToken,
      expectedStatus: HttpStatusCode.NOT_FOUND_404
    })
  })

  it('Should fail with no authentication', async function () {
    await command.importVideos({
      channelName: server.store.channel.name,
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      token: null,
      expectedStatus: HttpStatusCode.UNAUTHORIZED_401
    })
  })

  it('Should fail when sync is not owned by the user', async function () {
    await command.importVideos({
      channelName: server.store.channel.name,
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      token: userInfo.accessToken,
      expectedStatus: HttpStatusCode.FORBIDDEN_403
    })
  })

  it('Should fail when the user has no quota', async function () {
    await server.users.update({
      userId: userInfo.id,
      videoQuota: 0
    })

    await command.importVideos({
      channelName: 'fake_channel',
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      token: userInfo.accessToken,
      expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
    })

    await server.users.update({
      userId: userInfo.id,
      videoQuota: userInfo.videoQuota
    })
  })

  it('Should fail when the user has no daily quota', async function () {
    await server.users.update({
      userId: userInfo.id,
      videoQuotaDaily: 0
    })

    await command.importVideos({
      channelName: 'fake_channel',
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      token: userInfo.accessToken,
      expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
    })

    await server.users.update({
      userId: userInfo.id,
      videoQuotaDaily: userInfo.videoQuotaDaily
    })
  })

  it('Should succeed when sync is run by its owner', async function () {
    if (!areHttpImportTestsDisabled()) return

    await command.importVideos({
      channelName: 'fake_channel',
      externalChannelUrl: FIXTURE_URLS.youtubeChannel,
      token: userInfo.accessToken
    })
  })

  it('Should succeed when sync is run with root and for another user\'s channel', async function () {
    if (!areHttpImportTestsDisabled()) return

    await command.importVideos({
      channelName: 'fake_channel',
      externalChannelUrl: FIXTURE_URLS.youtubeChannel
    })
  })

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