Update to standard 7. Goodbye snake_case, I used to love you
[oweals/peertube.git] / server / tests / api / users.js
1 'use strict'
2
3 const async = require('async')
4 const chai = require('chai')
5 const expect = chai.expect
6 const pathUtils = require('path')
7
8 const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
9 webtorrent.silent = true
10
11 const utils = require('./utils')
12
13 describe('Test users', function () {
14   let server = null
15   let accessToken = null
16   let videoId
17
18   before(function (done) {
19     this.timeout(20000)
20
21     async.series([
22       function (next) {
23         utils.flushTests(next)
24       },
25       function (next) {
26         utils.runServer(1, function (server1) {
27           server = server1
28           next()
29         })
30       }
31     ], done)
32   })
33
34   it('Should create a new client')
35
36   it('Should return the first client')
37
38   it('Should remove the last client')
39
40   it('Should not login with an invalid client id', function (done) {
41     const client = { id: 'client', password: server.client.secret }
42     utils.login(server.url, client, server.user, 400, function (err, res) {
43       if (err) throw err
44
45       expect(res.body.error).to.equal('invalid_client')
46       done()
47     })
48   })
49
50   it('Should not login with an invalid client password', function (done) {
51     const client = { id: server.client.id, password: 'coucou' }
52     utils.login(server.url, client, server.user, 400, function (err, res) {
53       if (err) throw err
54
55       expect(res.body.error).to.equal('invalid_client')
56       done()
57     })
58   })
59
60   it('Should not login with an invalid username', function (done) {
61     const user = { username: 'captain crochet', password: server.user.password }
62     utils.login(server.url, server.client, user, 400, function (err, res) {
63       if (err) throw err
64
65       expect(res.body.error).to.equal('invalid_grant')
66       done()
67     })
68   })
69
70   it('Should not login with an invalid password', function (done) {
71     const user = { username: server.user.username, password: 'mewthree' }
72     utils.login(server.url, server.client, user, 400, function (err, res) {
73       if (err) throw err
74
75       expect(res.body.error).to.equal('invalid_grant')
76       done()
77     })
78   })
79
80   it('Should not be able to upload a video', function (done) {
81     accessToken = 'mysupertoken'
82     utils.uploadVideo(server.url, accessToken, 'my super name', 'my super description', 'video_short.webm', 401, done)
83   })
84
85   it('Should be able to login', function (done) {
86     utils.login(server.url, server.client, server.user, 200, function (err, res) {
87       if (err) throw err
88
89       accessToken = res.body.access_token
90       done()
91     })
92   })
93
94   it('Should upload the video with the correct token', function (done) {
95     utils.uploadVideo(server.url, accessToken, 'my super name', 'my super description', 'video_short.webm', 204, function (err, res) {
96       if (err) throw err
97
98       utils.getVideosList(server.url, function (err, res) {
99         if (err) throw err
100
101         const video = res.body[0]
102         expect(video.author).to.equal('root')
103
104         videoId = video.id
105         done()
106       })
107     })
108   })
109
110   it('Should upload the video again with the correct token', function (done) {
111     utils.uploadVideo(server.url, accessToken, 'my super name 2', 'my super description 2', 'video_short.webm', 204, done)
112   })
113
114   it('Should not be able to remove the video with an incorrect token', function (done) {
115     utils.removeVideo(server.url, 'bad_token', videoId, 401, done)
116   })
117
118   it('Should not be able to remove the video with the token of another account')
119
120   it('Should be able to remove the video with the correct token', function (done) {
121     utils.removeVideo(server.url, accessToken, videoId, done)
122   })
123
124   it('Should logout')
125
126   it('Should not be able to upload a video')
127
128   it('Should not be able to remove a video')
129
130   it('Should be able to login again')
131
132   after(function (done) {
133     process.kill(-server.app.pid)
134
135     // Keep the logs if the test failed
136     if (this.ok) {
137       utils.flushTests(done)
138     } else {
139       done()
140     }
141   })
142 })