aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/videos-filter.ts
blob: 7c487ae814ef25a0ea8b3b475aca8dc0541fbf06 (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
/* tslint:disable:no-unused-expression */

import 'mocha'
import {
  createUser,
  createVideoPlaylist,
  flushTests,
  killallServers,
  makeGetRequest,
  runServer,
  ServerInfo,
  setAccessTokensToServers, setDefaultVideoChannel,
  userLogin
} from '../../../../shared/utils'
import { UserRole } from '../../../../shared/models/users'
import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'

async function testEndpoints (server: ServerInfo, token: string, filter: string, playlistUUID: string, statusCodeExpected: number) {
  const paths = [
    '/api/v1/video-channels/root_channel/videos',
    '/api/v1/accounts/root/videos',
    '/api/v1/videos',
    '/api/v1/search/videos',
    '/api/v1/video-playlists/' + playlistUUID + '/videos'
  ]

  for (const path of paths) {
    await makeGetRequest({
      url: server.url,
      path,
      token,
      query: {
        filter
      },
      statusCodeExpected
    })
  }
}

describe('Test videos filters', function () {
  let server: ServerInfo
  let userAccessToken: string
  let moderatorAccessToken: string
  let playlistUUID: string

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

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

    await flushTests()

    server = await runServer(1)

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

    const user = { username: 'user1', password: 'my super password' }
    await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
    userAccessToken = await userLogin(server, user)

    const moderator = { username: 'moderator', password: 'my super password' }
    await createUser(
      {
        url: server.url,
        accessToken: server.accessToken,
        username: moderator.username,
        password: moderator.password,
        videoQuota: undefined,
        videoQuotaDaily: undefined,
        role: UserRole.MODERATOR
      }
    )
    moderatorAccessToken = await userLogin(server, moderator)

    const res = await createVideoPlaylist({
      url: server.url,
      token: server.accessToken,
      playlistAttrs: {
        displayName: 'super playlist',
        privacy: VideoPlaylistPrivacy.PUBLIC,
        videoChannelId: server.videoChannel.id
      }
    })
    playlistUUID = res.body.videoPlaylist.uuid
  })

  describe('When setting a video filter', function () {

    it('Should fail with a bad filter', async function () {
      await testEndpoints(server, server.accessToken, 'bad-filter', playlistUUID, 400)
    })

    it('Should succeed with a good filter', async function () {
      await testEndpoints(server, server.accessToken,'local', playlistUUID, 200)
    })

    it('Should fail to list all-local with a simple user', async function () {
      await testEndpoints(server, userAccessToken, 'all-local', playlistUUID, 401)
    })

    it('Should succeed to list all-local with a moderator', async function () {
      await testEndpoints(server, moderatorAccessToken, 'all-local', playlistUUID, 200)
    })

    it('Should succeed to list all-local with an admin', async function () {
      await testEndpoints(server, server.accessToken, 'all-local', playlistUUID, 200)
    })

    // Because we cannot authenticate the user on the RSS endpoint
    it('Should fail on the feeds endpoint with the all-local filter', async function () {
      await makeGetRequest({
        url: server.url,
        path: '/feeds/videos.json',
        statusCodeExpected: 401,
        query: {
          filter: 'all-local'
        }
      })
    })

    it('Should succeed on the feeds endpoint with the local filter', async function () {
      await makeGetRequest({
        url: server.url,
        path: '/feeds/videos.json',
        statusCodeExpected: 200,
        query: {
          filter: 'local'
        }
      })
    })
  })

  after(async function () {
    killallServers([ server ])

    // Keep the logs if the test failed
    if (this['ok']) {
      await flushTests()
    }
  })
})