aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/middlewares/oauth.ts6
-rw-r--r--server/middlewares/user-right.ts10
-rw-r--r--server/middlewares/validators/follows.ts6
-rw-r--r--server/tests/api/check-params/follows.ts196
-rw-r--r--server/tests/api/check-params/jobs.ts4
-rw-r--r--server/tests/api/check-params/users.ts10
-rw-r--r--server/tests/api/check-params/video-abuses.ts4
-rw-r--r--server/tests/api/check-params/video-blacklist.ts4
-rw-r--r--server/tests/api/check-params/video-channels.ts4
-rw-r--r--server/tests/api/check-params/videos.ts4
-rw-r--r--server/tests/api/server/follows.ts4
-rw-r--r--server/tests/api/users/users.ts6
-rw-r--r--server/tests/api/videos/multiple-servers.ts4
-rw-r--r--server/tests/api/videos/video-privacy.ts4
-rw-r--r--server/tests/client.ts4
-rw-r--r--server/tests/real-world/tools/get-access-token.ts4
-rw-r--r--server/tests/utils/requests/check-api-params.ts36
-rw-r--r--server/tests/utils/requests/requests.ts46
-rw-r--r--server/tests/utils/server/servers.ts2
-rw-r--r--server/tests/utils/users/login.ts10
-rw-r--r--server/tests/utils/videos/videos.ts20
21 files changed, 235 insertions, 153 deletions
diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts
index e59168ea8..12872c4a5 100644
--- a/server/middlewares/oauth.ts
+++ b/server/middlewares/oauth.ts
@@ -17,7 +17,11 @@ function authenticate (req: express.Request, res: express.Response, next: expres
17 return res.sendStatus(500) 17 return res.sendStatus(500)
18 } 18 }
19 19
20 if (res.statusCode === 401 || res.statusCode === 400 || res.statusCode === 503) return res.end() 20 if (res.statusCode === 401 || res.statusCode === 400 || res.statusCode === 503) {
21 return res.json({
22 error: 'Authentication failed.'
23 }).end()
24 }
21 25
22 return next() 26 return next()
23 }) 27 })
diff --git a/server/middlewares/user-right.ts b/server/middlewares/user-right.ts
index 5bb5bdfbd..7cea7aa1e 100644
--- a/server/middlewares/user-right.ts
+++ b/server/middlewares/user-right.ts
@@ -8,8 +8,14 @@ function ensureUserHasRight (userRight: UserRight) {
8 return function (req: express.Request, res: express.Response, next: express.NextFunction) { 8 return function (req: express.Request, res: express.Response, next: express.NextFunction) {
9 const user = res.locals.oauth.token.user as UserModel 9 const user = res.locals.oauth.token.user as UserModel
10 if (user.hasRight(userRight) === false) { 10 if (user.hasRight(userRight) === false) {
11 logger.info('User %s does not have right %s to access to %s.', user.username, UserRight[userRight], req.path) 11 const message = `User ${user.username} does not have right ${UserRight[userRight]} to access to ${req.path}.`
12 return res.sendStatus(403) 12 logger.info(message)
13
14 return res.status(403)
15 .json({
16 error: message
17 })
18 .end()
13 } 19 }
14 20
15 return next() 21 return next()
diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts
index 7dadf6a19..991a2e175 100644
--- a/server/middlewares/validators/follows.ts
+++ b/server/middlewares/validators/follows.ts
@@ -41,7 +41,11 @@ const removeFollowingValidator = [
41 const follow = await ActorFollowModel.loadByActorAndTargetHost(serverActor.id, req.params.host) 41 const follow = await ActorFollowModel.loadByActorAndTargetHost(serverActor.id, req.params.host)
42 42
43 if (!follow) { 43 if (!follow) {
44 return res.status(404) 44 return res
45 .status(404)
46 .json({
47 error: `Follower ${req.params.host} not found.`
48 })
45 .end() 49 .end()
46 } 50 }
47 51
diff --git a/server/tests/api/check-params/follows.ts b/server/tests/api/check-params/follows.ts
index cdd2783df..d5fcb7477 100644
--- a/server/tests/api/check-params/follows.ts
+++ b/server/tests/api/check-params/follows.ts
@@ -4,14 +4,10 @@ import 'mocha'
4import * as request from 'supertest' 4import * as request from 'supertest'
5 5
6import { 6import {
7 createUser, 7 createUser, flushTests, killallServers, makeDeleteRequest, makePostBodyRequest, runServer, ServerInfo, setAccessTokensToServers,
8 flushTests, 8 userLogin
9 killallServers,
10 loginAndGetAccessToken,
11 runServer,
12 ServerInfo,
13 setAccessTokensToServers
14} from '../../utils' 9} from '../../utils'
10import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
15 11
16describe('Test server follows API validators', function () { 12describe('Test server follows API validators', function () {
17 let server: ServerInfo 13 let server: ServerInfo
@@ -19,7 +15,7 @@ describe('Test server follows API validators', function () {
19 // --------------------------------------------------------------- 15 // ---------------------------------------------------------------
20 16
21 before(async function () { 17 before(async function () {
22 this.timeout(45000) 18 this.timeout(20000)
23 19
24 await flushTests() 20 await flushTests()
25 server = await runServer(1) 21 server = await runServer(1)
@@ -31,81 +27,85 @@ describe('Test server follows API validators', function () {
31 let userAccessToken = null 27 let userAccessToken = null
32 28
33 before(async function () { 29 before(async function () {
34 await createUser(server.url, server.accessToken, 'user1', 'password') 30 const user = {
35 server.user = {
36 username: 'user1', 31 username: 'user1',
37 password: 'password' 32 password: 'password'
38 } 33 }
39 34
40 userAccessToken = await loginAndGetAccessToken(server) 35 await createUser(server.url, server.accessToken, user.username, user.password)
36 userAccessToken = await userLogin(server, user)
41 }) 37 })
42 38
43 describe('When adding follows', function () { 39 describe('When adding follows', function () {
44 const path = '/api/v1/server/following' 40 const path = '/api/v1/server/following'
45 const body = {
46 hosts: [ 'localhost:9002' ]
47 }
48 41
49 it('Should fail without hosts', async function () { 42 it('Should fail without hosts', async function () {
50 await request(server.url) 43 await makePostBodyRequest({
51 .post(path) 44 url: server.url,
52 .set('Authorization', 'Bearer ' + server.accessToken) 45 path,
53 .set('Accept', 'application/json') 46 token: server.accessToken,
54 .expect(400) 47 statusCodeExpected: 400
48 })
55 }) 49 })
56 50
57 it('Should fail if hosts is not an array', async function () { 51 it('Should fail if hosts is not an array', async function () {
58 await request(server.url) 52 await makePostBodyRequest({
59 .post(path) 53 url: server.url,
60 .send({ hosts: 'localhost:9002' }) 54 path,
61 .set('Authorization', 'Bearer ' + server.accessToken) 55 token: server.accessToken,
62 .set('Accept', 'application/json') 56 fields: { hosts: 'localhost:9002' },
63 .expect(400) 57 statusCodeExpected: 400
58 })
64 }) 59 })
65 60
66 it('Should fail if the array is not composed by hosts', async function () { 61 it('Should fail if the array is not composed by hosts', async function () {
67 await request(server.url) 62 await makePostBodyRequest({
68 .post(path) 63 url: server.url,
69 .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] }) 64 path,
70 .set('Authorization', 'Bearer ' + server.accessToken) 65 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
71 .set('Accept', 'application/json') 66 token: server.accessToken,
72 .expect(400) 67 statusCodeExpected: 400
68 })
73 }) 69 })
74 70
75 it('Should fail if the array is composed with http schemes', async function () { 71 it('Should fail if the array is composed with http schemes', async function () {
76 await request(server.url) 72 await makePostBodyRequest({
77 .post(path) 73 url: server.url,
78 .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] }) 74 path,
79 .set('Authorization', 'Bearer ' + server.accessToken) 75 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
80 .set('Accept', 'application/json') 76 token: server.accessToken,
81 .expect(400) 77 statusCodeExpected: 400
78 })
82 }) 79 })
83 80
84 it('Should fail if hosts are not unique', async function () { 81 it('Should fail if hosts are not unique', async function () {
85 await request(server.url) 82 await makePostBodyRequest({
86 .post(path) 83 url: server.url,
87 .send({ urls: [ 'localhost:9002', 'localhost:9002' ] }) 84 path,
88 .set('Authorization', 'Bearer ' + server.accessToken) 85 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
89 .set('Accept', 'application/json') 86 token: server.accessToken,
90 .expect(400) 87 statusCodeExpected: 400
88 })
91 }) 89 })
92 90
93 it('Should fail with an invalid token', async function () { 91 it('Should fail with an invalid token', async function () {
94 await request(server.url) 92 await makePostBodyRequest({
95 .post(path) 93 url: server.url,
96 .send(body) 94 path,
97 .set('Authorization', 'Bearer fake_token') 95 fields: { hosts: [ 'localhost:9002' ] },
98 .set('Accept', 'application/json') 96 token: 'fake_token',
99 .expect(401) 97 statusCodeExpected: 401
98 })
100 }) 99 })
101 100
102 it('Should fail if the user is not an administrator', async function () { 101 it('Should fail if the user is not an administrator', async function () {
103 await request(server.url) 102 await makePostBodyRequest({
104 .post(path) 103 url: server.url,
105 .send(body) 104 path,
106 .set('Authorization', 'Bearer ' + userAccessToken) 105 fields: { hosts: [ 'localhost:9002' ] },
107 .set('Accept', 'application/json') 106 token: userAccessToken,
108 .expect(403) 107 statusCodeExpected: 403
108 })
109 }) 109 })
110 }) 110 })
111 111
@@ -113,27 +113,15 @@ describe('Test server follows API validators', function () {
113 const path = '/api/v1/server/following' 113 const path = '/api/v1/server/following'
114 114
115 it('Should fail with a bad start pagination', async function () { 115 it('Should fail with a bad start pagination', async function () {
116 await request(server.url) 116 await checkBadStartPagination(server.url, path)
117 .get(path)
118 .query({ start: 'hello' })
119 .set('Accept', 'application/json')
120 .expect(400)
121 }) 117 })
122 118
123 it('Should fail with a bad count pagination', async function () { 119 it('Should fail with a bad count pagination', async function () {
124 await request(server.url) 120 await checkBadCountPagination(server.url, path)
125 .get(path)
126 .query({ count: 'hello' })
127 .set('Accept', 'application/json')
128 .expect(400)
129 }) 121 })
130 122
131 it('Should fail with an incorrect sort', async function () { 123 it('Should fail with an incorrect sort', async function () {
132 await request(server.url) 124 await checkBadSortPagination(server.url, path)
133 .get(path)
134 .query({ sort: 'hello' })
135 .set('Accept', 'application/json')
136 .expect(400)
137 }) 125 })
138 }) 126 })
139 127
@@ -141,27 +129,15 @@ describe('Test server follows API validators', function () {
141 const path = '/api/v1/server/followers' 129 const path = '/api/v1/server/followers'
142 130
143 it('Should fail with a bad start pagination', async function () { 131 it('Should fail with a bad start pagination', async function () {
144 await request(server.url) 132 await checkBadStartPagination(server.url, path)
145 .get(path)
146 .query({ start: 'hello' })
147 .set('Accept', 'application/json')
148 .expect(400)
149 }) 133 })
150 134
151 it('Should fail with a bad count pagination', async function () { 135 it('Should fail with a bad count pagination', async function () {
152 await request(server.url) 136 await checkBadCountPagination(server.url, path)
153 .get(path)
154 .query({ count: 'hello' })
155 .set('Accept', 'application/json')
156 .expect(400)
157 }) 137 })
158 138
159 it('Should fail with an incorrect sort', async function () { 139 it('Should fail with an incorrect sort', async function () {
160 await request(server.url) 140 await checkBadSortPagination(server.url, path)
161 .get(path)
162 .query({ sort: 'hello' })
163 .set('Accept', 'application/json')
164 .expect(400)
165 }) 141 })
166 }) 142 })
167 143
@@ -169,30 +145,40 @@ describe('Test server follows API validators', function () {
169 const path = '/api/v1/server/following' 145 const path = '/api/v1/server/following'
170 146
171 it('Should fail with an invalid token', async function () { 147 it('Should fail with an invalid token', async function () {
172 await request(server.url) 148 await makeDeleteRequest({
173 .delete(path + '/1') 149 url: server.url,
174 .set('Authorization', 'Bearer faketoken') 150 path: path + '/localhost:9002',
175 .set('Accept', 'application/json') 151 token: 'fake_token',
176 .expect(401) 152 statusCodeExpected: 401
153 })
177 }) 154 })
178 155
179 it('Should fail if the user is not an administrator', async function () { 156 it('Should fail if the user is not an administrator', async function () {
180 await request(server.url) 157 await makeDeleteRequest({
181 .delete(path + '/1') 158 url: server.url,
182 .set('Authorization', 'Bearer ' + userAccessToken) 159 path: path + '/localhost:9002',
183 .set('Accept', 'application/json') 160 token: userAccessToken,
184 .expect(403) 161 statusCodeExpected: 403
185 }) 162 })
186 163 })
187 it('Should fail we do not follow this server', async function () { 164
188 await request(server.url) 165 it('Should fail if we do not follow this server', async function () {
189 .delete(path + '/example.com') 166 await makeDeleteRequest({
190 .set('Authorization', 'Bearer ' + server.accessToken) 167 url: server.url,
191 .set('Accept', 'application/json') 168 path: path + '/example.com',
192 .expect(404) 169 token: server.accessToken,
170 statusCodeExpected: 404
171 })
172 })
173
174 it('Should succeed with the correct parameters', async function () {
175 await makeDeleteRequest({
176 url: server.url,
177 path: path + '/localhost:9002',
178 token: server.accessToken,
179 statusCodeExpected: 404
180 })
193 }) 181 })
194
195 it('Should succeed with the correct parameters')
196 }) 182 })
197 }) 183 })
198 184
diff --git a/server/tests/api/check-params/jobs.ts b/server/tests/api/check-params/jobs.ts
index 7a0dd6e8c..3795d1d64 100644
--- a/server/tests/api/check-params/jobs.ts
+++ b/server/tests/api/check-params/jobs.ts
@@ -3,7 +3,7 @@
3import 'mocha' 3import 'mocha'
4import * as request from 'supertest' 4import * as request from 'supertest'
5 5
6import { createUser, flushTests, getUserAccessToken, killallServers, runServer, ServerInfo, setAccessTokensToServers } from '../../utils' 6import { createUser, flushTests, userLogin, killallServers, runServer, ServerInfo, setAccessTokensToServers } from '../../utils'
7 7
8describe('Test jobs API validators', function () { 8describe('Test jobs API validators', function () {
9 const path = '/api/v1/jobs/' 9 const path = '/api/v1/jobs/'
@@ -26,7 +26,7 @@ describe('Test jobs API validators', function () {
26 password: 'my super password' 26 password: 'my super password'
27 } 27 }
28 await createUser(server.url, server.accessToken, user.username, user.password) 28 await createUser(server.url, server.accessToken, user.username, user.password)
29 userAccessToken = await getUserAccessToken(server, user) 29 userAccessToken = await userLogin(server, user)
30 }) 30 })
31 31
32 describe('When listing jobs', function () { 32 describe('When listing jobs', function () {
diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts
index 72488e5c4..b566a2f1e 100644
--- a/server/tests/api/check-params/users.ts
+++ b/server/tests/api/check-params/users.ts
@@ -11,13 +11,13 @@ import {
11 getVideosList, 11 getVideosList,
12 makePutBodyRequest, 12 makePutBodyRequest,
13 createUser, 13 createUser,
14 loginAndGetAccessToken, 14 serverLogin,
15 getUsersList, 15 getUsersList,
16 registerUser, 16 registerUser,
17 setAccessTokensToServers, 17 setAccessTokensToServers,
18 killallServers, 18 killallServers,
19 makePostBodyRequest, 19 makePostBodyRequest,
20 getUserAccessToken 20 userLogin
21} from '../../utils' 21} from '../../utils'
22import { UserRole } from '../../../../shared' 22import { UserRole } from '../../../../shared'
23 23
@@ -58,7 +58,7 @@ describe('Test users API validators', function () {
58 username: 'user1', 58 username: 'user1',
59 password: 'my super password' 59 password: 'my super password'
60 } 60 }
61 userAccessToken = await getUserAccessToken(server, user) 61 userAccessToken = await userLogin(server, user)
62 }) 62 })
63 63
64 describe('When listing users', function () { 64 describe('When listing users', function () {
@@ -304,7 +304,7 @@ describe('Test users API validators', function () {
304 password: 'my super password' 304 password: 'my super password'
305 } 305 }
306 306
307 userAccessToken = await loginAndGetAccessToken(server) 307 userAccessToken = await serverLogin(server)
308 const fields = { 308 const fields = {
309 username: 'user3', 309 username: 'user3',
310 email: 'test@example.com', 310 email: 'test@example.com',
@@ -675,7 +675,7 @@ describe('Test users API validators', function () {
675 email: 'test3@example.com', 675 email: 'test3@example.com',
676 password: 'my super password' 676 password: 'my super password'
677 } 677 }
678 userAccessToken = await loginAndGetAccessToken(server) 678 userAccessToken = await serverLogin(server)
679 679
680 const videoAttributes = { fixture: 'video_short2.webm' } 680 const videoAttributes = { fixture: 'video_short2.webm' }
681 await uploadVideo(server.url, userAccessToken, videoAttributes) 681 await uploadVideo(server.url, userAccessToken, videoAttributes)
diff --git a/server/tests/api/check-params/video-abuses.ts b/server/tests/api/check-params/video-abuses.ts
index eac12b6f0..e994ccd49 100644
--- a/server/tests/api/check-params/video-abuses.ts
+++ b/server/tests/api/check-params/video-abuses.ts
@@ -13,7 +13,7 @@ import {
13 setAccessTokensToServers, 13 setAccessTokensToServers,
14 killallServers, 14 killallServers,
15 makePostBodyRequest, 15 makePostBodyRequest,
16 getUserAccessToken 16 userLogin
17} from '../../utils' 17} from '../../utils'
18 18
19describe('Test video abuses API validators', function () { 19describe('Test video abuses API validators', function () {
@@ -35,7 +35,7 @@ describe('Test video abuses API validators', function () {
35 const password = 'my super password' 35 const password = 'my super password'
36 await createUser(server.url, server.accessToken, username, password) 36 await createUser(server.url, server.accessToken, username, password)
37 37
38 userAccessToken = await getUserAccessToken(server, { username, password }) 38 userAccessToken = await userLogin(server, { username, password })
39 39
40 // Upload a video 40 // Upload a video
41 const videoAttributes = {} 41 const videoAttributes = {}
diff --git a/server/tests/api/check-params/video-blacklist.ts b/server/tests/api/check-params/video-blacklist.ts
index eb16b3af0..c8b457182 100644
--- a/server/tests/api/check-params/video-blacklist.ts
+++ b/server/tests/api/check-params/video-blacklist.ts
@@ -13,7 +13,7 @@ import {
13 setAccessTokensToServers, 13 setAccessTokensToServers,
14 killallServers, 14 killallServers,
15 makePostBodyRequest, 15 makePostBodyRequest,
16 getUserAccessToken 16 userLogin
17} from '../../utils' 17} from '../../utils'
18 18
19describe('Test video blacklist API validators', function () { 19describe('Test video blacklist API validators', function () {
@@ -34,7 +34,7 @@ describe('Test video blacklist API validators', function () {
34 const username = 'user1' 34 const username = 'user1'
35 const password = 'my super password' 35 const password = 'my super password'
36 await createUser(server.url, server.accessToken, username, password) 36 await createUser(server.url, server.accessToken, username, password)
37 userAccessToken = await getUserAccessToken(server, { username, password }) 37 userAccessToken = await userLogin(server, { username, password })
38 38
39 // Upload a video 39 // Upload a video
40 const videoAttributes = {} 40 const videoAttributes = {}
diff --git a/server/tests/api/check-params/video-channels.ts b/server/tests/api/check-params/video-channels.ts
index 7103aec25..bf464152b 100644
--- a/server/tests/api/check-params/video-channels.ts
+++ b/server/tests/api/check-params/video-channels.ts
@@ -15,7 +15,7 @@ import {
15 makePostBodyRequest, 15 makePostBodyRequest,
16 getVideoChannelsList, 16 getVideoChannelsList,
17 createUser, 17 createUser,
18 getUserAccessToken 18 userLogin
19} from '../../utils' 19} from '../../utils'
20 20
21describe('Test videos API validator', function () { 21describe('Test videos API validator', function () {
@@ -40,7 +40,7 @@ describe('Test videos API validator', function () {
40 } 40 }
41 await createUser(server.url, server.accessToken, user.username, user.password) 41 await createUser(server.url, server.accessToken, user.username, user.password)
42 42
43 accessTokenUser = await getUserAccessToken(server, user) 43 accessTokenUser = await userLogin(server, user)
44 }) 44 })
45 45
46 describe('When listing a video channels', function () { 46 describe('When listing a video channels', function () {
diff --git a/server/tests/api/check-params/videos.ts b/server/tests/api/check-params/videos.ts
index 0aaa6e7c9..b0c850f0c 100644
--- a/server/tests/api/check-params/videos.ts
+++ b/server/tests/api/check-params/videos.ts
@@ -17,7 +17,7 @@ import {
17 makePostUploadRequest, 17 makePostUploadRequest,
18 getMyUserInformation, 18 getMyUserInformation,
19 createUser, 19 createUser,
20 getUserAccessToken 20 userLogin
21} from '../../utils' 21} from '../../utils'
22import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' 22import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
23 23
@@ -260,7 +260,7 @@ describe('Test videos API validator', function () {
260 } 260 }
261 await createUser(server.url, server.accessToken, user.username, user.password) 261 await createUser(server.url, server.accessToken, user.username, user.password)
262 262
263 const accessTokenUser = await getUserAccessToken(server, user) 263 const accessTokenUser = await userLogin(server, user)
264 const res = await getMyUserInformation(server.url, accessTokenUser) 264 const res = await getMyUserInformation(server.url, accessTokenUser)
265 const customChannelId = res.body.videoChannels[0].id 265 const customChannelId = res.body.videoChannels[0].id
266 266
diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.ts
index f77c0c67c..6c815ace8 100644
--- a/server/tests/api/server/follows.ts
+++ b/server/tests/api/server/follows.ts
@@ -10,7 +10,7 @@ import {
10} from '../../utils/index' 10} from '../../utils/index'
11import { dateIsValid, webtorrentAdd } from '../../utils/miscs/miscs' 11import { dateIsValid, webtorrentAdd } from '../../utils/miscs/miscs'
12import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, unfollow } from '../../utils/server/follows' 12import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, unfollow } from '../../utils/server/follows'
13import { getUserAccessToken } from '../../utils/users/login' 13import { userLogin } from '../../utils/users/login'
14import { createUser } from '../../utils/users/users' 14import { createUser } from '../../utils/users/users'
15import { 15import {
16 addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads, 16 addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
@@ -183,7 +183,7 @@ describe('Test follows', function () {
183 { 183 {
184 const user = { username: 'captain', password: 'password' } 184 const user = { username: 'captain', password: 'password' }
185 await createUser(servers[2].url, servers[2].accessToken, user.username, user.password) 185 await createUser(servers[2].url, servers[2].accessToken, user.username, user.password)
186 const userAccessToken = await getUserAccessToken(servers[2], user) 186 const userAccessToken = await userLogin(servers[2], user)
187 187
188 const resVideos = await getVideosList(servers[ 2 ].url) 188 const resVideos = await getVideosList(servers[ 2 ].url)
189 const video4 = resVideos.body.data.find(v => v.name === 'server3-4') 189 const video4 = resVideos.body.data.find(v => v.name === 'server3-4')
diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts
index 2e3a0b94f..298dbce2c 100644
--- a/server/tests/api/users/users.ts
+++ b/server/tests/api/users/users.ts
@@ -15,7 +15,7 @@ import {
15 getVideosList, 15 getVideosList,
16 killallServers, 16 killallServers,
17 login, 17 login,
18 loginAndGetAccessToken, 18 serverLogin,
19 makePutBodyRequest, 19 makePutBodyRequest,
20 rateVideo, 20 rateVideo,
21 registerUser, 21 registerUser,
@@ -193,7 +193,7 @@ describe('Test users', function () {
193 password: 'super password' 193 password: 'super password'
194 } 194 }
195 195
196 accessTokenUser = await loginAndGetAccessToken(server) 196 accessTokenUser = await serverLogin(server)
197 }) 197 })
198 198
199 it('Should be able to get the user information', async function () { 199 it('Should be able to get the user information', async function () {
@@ -511,7 +511,7 @@ describe('Test users', function () {
511 password: 'my super password' 511 password: 'my super password'
512 } 512 }
513 513
514 accessToken = await loginAndGetAccessToken(server) 514 accessToken = await serverLogin(server)
515 }) 515 })
516 516
517 it('Should have the correct video quota', async function () { 517 it('Should have the correct video quota', async function () {
diff --git a/server/tests/api/videos/multiple-servers.ts b/server/tests/api/videos/multiple-servers.ts
index 84f730a8e..0c6508e71 100644
--- a/server/tests/api/videos/multiple-servers.ts
+++ b/server/tests/api/videos/multiple-servers.ts
@@ -7,7 +7,7 @@ import * as request from 'supertest'
7import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model' 7import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
8 8
9import { 9import {
10 addVideoChannel, dateIsValid, doubleFollow, flushAndRunMultipleServers, flushTests, getUserAccessToken, getVideo, 10 addVideoChannel, dateIsValid, doubleFollow, flushAndRunMultipleServers, flushTests, userLogin, getVideo,
11 getVideoChannelsList, getVideosList, killallServers, rateVideo, removeVideo, ServerInfo, setAccessTokensToServers, testVideoImage, 11 getVideoChannelsList, getVideosList, killallServers, rateVideo, removeVideo, ServerInfo, setAccessTokensToServers, testVideoImage,
12 updateVideo, uploadVideo, wait, webtorrentAdd 12 updateVideo, uploadVideo, wait, webtorrentAdd
13} from '../../utils/index' 13} from '../../utils/index'
@@ -152,7 +152,7 @@ describe('Test multiple servers', function () {
152 password: 'super_password' 152 password: 'super_password'
153 } 153 }
154 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password) 154 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
155 const userAccessToken = await getUserAccessToken(servers[1], user) 155 const userAccessToken = await userLogin(servers[1], user)
156 156
157 const videoAttributes = { 157 const videoAttributes = {
158 name: 'my super name for server 2', 158 name: 'my super name for server 2',
diff --git a/server/tests/api/videos/video-privacy.ts b/server/tests/api/videos/video-privacy.ts
index de709f8f1..469274921 100644
--- a/server/tests/api/videos/video-privacy.ts
+++ b/server/tests/api/videos/video-privacy.ts
@@ -14,7 +14,7 @@ import {
14 wait 14 wait
15} from '../../utils/index' 15} from '../../utils/index'
16import { doubleFollow } from '../../utils/server/follows' 16import { doubleFollow } from '../../utils/server/follows'
17import { getUserAccessToken } from '../../utils/users/login' 17import { userLogin } from '../../utils/users/login'
18import { createUser } from '../../utils/users/users' 18import { createUser } from '../../utils/users/users'
19import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../utils/videos/videos' 19import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../utils/videos/videos'
20 20
@@ -78,7 +78,7 @@ describe('Test video privacy', function () {
78 } 78 }
79 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password) 79 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
80 80
81 const token = await getUserAccessToken(servers[0], user) 81 const token = await userLogin(servers[0], user)
82 await getVideoWithToken(servers[0].url, token, privateVideoUUID, 403) 82 await getVideoWithToken(servers[0].url, token, privateVideoUUID, 403)
83 }) 83 })
84 84
diff --git a/server/tests/client.ts b/server/tests/client.ts
index 8c4334c53..2be1cf5dd 100644
--- a/server/tests/client.ts
+++ b/server/tests/client.ts
@@ -9,7 +9,7 @@ import {
9 ServerInfo, 9 ServerInfo,
10 flushTests, 10 flushTests,
11 runServer, 11 runServer,
12 loginAndGetAccessToken, 12 serverLogin,
13 uploadVideo, 13 uploadVideo,
14 getVideosList 14 getVideosList
15} from './utils' 15} from './utils'
@@ -23,7 +23,7 @@ describe('Test a client controllers', function () {
23 await flushTests() 23 await flushTests()
24 24
25 server = await runServer(1) 25 server = await runServer(1)
26 server.accessToken = await loginAndGetAccessToken(server) 26 server.accessToken = await serverLogin(server)
27 27
28 const videoAttributes = { 28 const videoAttributes = {
29 name: 'my super name for server 1', 29 name: 'my super name for server 1',
diff --git a/server/tests/real-world/tools/get-access-token.ts b/server/tests/real-world/tools/get-access-token.ts
index 138883ae9..ee14733e3 100644
--- a/server/tests/real-world/tools/get-access-token.ts
+++ b/server/tests/real-world/tools/get-access-token.ts
@@ -2,7 +2,7 @@ import * as program from 'commander'
2 2
3import { 3import {
4 getClient, 4 getClient,
5 loginAndGetAccessToken 5 serverLogin
6} from '../../utils' 6} from '../../utils'
7 7
8program 8program
@@ -36,7 +36,7 @@ getClient(program.url)
36 server.client.id = res.body.client_id 36 server.client.id = res.body.client_id
37 server.client.secret = res.body.client_secret 37 server.client.secret = res.body.client_secret
38 38
39 return loginAndGetAccessToken(server) 39 return serverLogin(server)
40 }) 40 })
41 .then(accessToken => { 41 .then(accessToken => {
42 console.log(accessToken) 42 console.log(accessToken)
diff --git a/server/tests/utils/requests/check-api-params.ts b/server/tests/utils/requests/check-api-params.ts
new file mode 100644
index 000000000..fbd660629
--- /dev/null
+++ b/server/tests/utils/requests/check-api-params.ts
@@ -0,0 +1,36 @@
1import { makeGetRequest } from './requests'
2
3function checkBadStartPagination (url: string, path: string) {
4 return makeGetRequest({
5 url,
6 path,
7 query: { start: 'hello' },
8 statusCodeExpected: 400
9 })
10}
11
12function checkBadCountPagination (url: string, path: string) {
13 return makeGetRequest({
14 url,
15 path,
16 query: { count: 'hello' },
17 statusCodeExpected: 400
18 })
19}
20
21function checkBadSortPagination (url: string, path: string) {
22 return makeGetRequest({
23 url,
24 path,
25 query: { sort: 'hello' },
26 statusCodeExpected: 400
27 })
28}
29
30// ---------------------------------------------------------------------------
31
32export {
33 checkBadStartPagination,
34 checkBadCountPagination,
35 checkBadSortPagination
36}
diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts
index 52b7a4c29..eb02cf9e6 100644
--- a/server/tests/utils/requests/requests.ts
+++ b/server/tests/utils/requests/requests.ts
@@ -1,11 +1,43 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2 2
3function makeGetRequest (url: string, path: string) { 3function makeGetRequest (options: {
4 return request(url) 4 url: string,
5 .get(path) 5 path: string,
6 query?: any,
7 token?: string,
8 statusCodeExpected?: number
9}) {
10 if (!options.statusCodeExpected) options.statusCodeExpected = 400
11
12 const req = request(options.url)
13 .get(options.path)
6 .set('Accept', 'application/json') 14 .set('Accept', 'application/json')
7 .expect(200) 15
16 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
17 if (options.query) req.query(options.query)
18
19 return req
8 .expect('Content-Type', /json/) 20 .expect('Content-Type', /json/)
21 .expect(options.statusCodeExpected)
22}
23
24function makeDeleteRequest (options: {
25 url: string,
26 path: string,
27 token?: string,
28 statusCodeExpected?: number
29}) {
30 if (!options.statusCodeExpected) options.statusCodeExpected = 400
31
32 const req = request(options.url)
33 .delete(options.path)
34 .set('Accept', 'application/json')
35
36 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
37
38 return req
39 .expect('Content-Type', /json/)
40 .expect(options.statusCodeExpected)
9} 41}
10 42
11function makePostUploadRequest (options: { 43function makePostUploadRequest (options: {
@@ -48,9 +80,10 @@ function makePostBodyRequest (options: {
48 url: string, 80 url: string,
49 path: string, 81 path: string,
50 token?: string, 82 token?: string,
51 fields: { [ fieldName: string ]: any }, 83 fields?: { [ fieldName: string ]: any },
52 statusCodeExpected?: number 84 statusCodeExpected?: number
53}) { 85}) {
86 if (!options.fields) options.fields = {}
54 if (!options.statusCodeExpected) options.statusCodeExpected = 400 87 if (!options.statusCodeExpected) options.statusCodeExpected = 400
55 88
56 const req = request(options.url) 89 const req = request(options.url)
@@ -88,5 +121,6 @@ export {
88 makeGetRequest, 121 makeGetRequest,
89 makePostUploadRequest, 122 makePostUploadRequest,
90 makePostBodyRequest, 123 makePostBodyRequest,
91 makePutBodyRequest 124 makePutBodyRequest,
125 makeDeleteRequest
92} 126}
diff --git a/server/tests/utils/server/servers.ts b/server/tests/utils/server/servers.ts
index 8340fbc18..4add2f69a 100644
--- a/server/tests/utils/server/servers.ts
+++ b/server/tests/utils/server/servers.ts
@@ -114,7 +114,7 @@ function runServer (serverNumber: number, configOverride?: Object) {
114 } 114 }
115 115
116 return new Promise<ServerInfo>(res => { 116 return new Promise<ServerInfo>(res => {
117 server.app = fork(join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options) 117 server.app = fork(join(__dirname, '..', '..', '..', '..', 'dist', 'server.js'), [], options)
118 server.app.stdout.on('data', function onStdout (data) { 118 server.app.stdout.on('data', function onStdout (data) {
119 let dontContinue = false 119 let dontContinue = false
120 120
diff --git a/server/tests/utils/users/login.ts b/server/tests/utils/users/login.ts
index 855c4828d..04444e2f1 100644
--- a/server/tests/utils/users/login.ts
+++ b/server/tests/utils/users/login.ts
@@ -26,13 +26,13 @@ function login (url: string, client: Client, user: User, expectedStatus = 200) {
26 .expect(expectedStatus) 26 .expect(expectedStatus)
27} 27}
28 28
29async function loginAndGetAccessToken (server: Server) { 29async function serverLogin (server: Server) {
30 const res = await login(server.url, server.client, server.user, 200) 30 const res = await login(server.url, server.client, server.user, 200)
31 31
32 return res.body.access_token as string 32 return res.body.access_token as string
33} 33}
34 34
35async function getUserAccessToken (server: Server, user: User) { 35async function userLogin (server: Server, user: User) {
36 const res = await login(server.url, server.client, user, 200) 36 const res = await login(server.url, server.client, user, 200)
37 37
38 return res.body.access_token as string 38 return res.body.access_token as string
@@ -42,7 +42,7 @@ function setAccessTokensToServers (servers: ServerInfo[]) {
42 const tasks: Promise<any>[] = [] 42 const tasks: Promise<any>[] = []
43 43
44 for (const server of servers) { 44 for (const server of servers) {
45 const p = loginAndGetAccessToken(server).then(t => server.accessToken = t) 45 const p = serverLogin(server).then(t => server.accessToken = t)
46 tasks.push(p) 46 tasks.push(p)
47 } 47 }
48 48
@@ -53,7 +53,7 @@ function setAccessTokensToServers (servers: ServerInfo[]) {
53 53
54export { 54export {
55 login, 55 login,
56 loginAndGetAccessToken, 56 serverLogin,
57 getUserAccessToken, 57 userLogin,
58 setAccessTokensToServers 58 setAccessTokensToServers
59} 59}
diff --git a/server/tests/utils/videos/videos.ts b/server/tests/utils/videos/videos.ts
index 6de1b8c92..f64ebd2b0 100644
--- a/server/tests/utils/videos/videos.ts
+++ b/server/tests/utils/videos/videos.ts
@@ -21,25 +21,37 @@ type VideoAttributes = {
21function getVideoCategories (url: string) { 21function getVideoCategories (url: string) {
22 const path = '/api/v1/videos/categories' 22 const path = '/api/v1/videos/categories'
23 23
24 return makeGetRequest(url, path) 24 return makeGetRequest({
25 url,
26 path
27 })
25} 28}
26 29
27function getVideoLicences (url: string) { 30function getVideoLicences (url: string) {
28 const path = '/api/v1/videos/licences' 31 const path = '/api/v1/videos/licences'
29 32
30 return makeGetRequest(url, path) 33 return makeGetRequest({
34 url,
35 path
36 })
31} 37}
32 38
33function getVideoLanguages (url: string) { 39function getVideoLanguages (url: string) {
34 const path = '/api/v1/videos/languages' 40 const path = '/api/v1/videos/languages'
35 41
36 return makeGetRequest(url, path) 42 return makeGetRequest({
43 url,
44 path
45 })
37} 46}
38 47
39function getVideoPrivacies (url: string) { 48function getVideoPrivacies (url: string) {
40 const path = '/api/v1/videos/privacies' 49 const path = '/api/v1/videos/privacies'
41 50
42 return makeGetRequest(url, path) 51 return makeGetRequest({
52 url,
53 path
54 })
43} 55}
44 56
45function getVideo (url: string, id: number | string, expectedStatus = 200) { 57function getVideo (url: string, id: number | string, expectedStatus = 200) {