]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/check-params/user-subscriptions.ts
shared/ typescript types dir server-commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / user-subscriptions.ts
index 6a6dd9a6f976f476a4ecbbc5cb575dca847fabac..95226dff58f18590b7186af5c409a0f85edc93dc 100644 (file)
@@ -1,24 +1,24 @@
-/* tslint:disable:no-unused-expression */
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
 import 'mocha'
-
 import {
-  createUser,
-  flushTests,
-  killallServers,
+  checkBadCountPagination,
+  checkBadSortPagination,
+  checkBadStartPagination,
+  cleanupTests,
+  createSingleServer,
   makeDeleteRequest,
   makeGetRequest,
   makePostBodyRequest,
-  runServer,
-  ServerInfo,
+  PeerTubeServer,
   setAccessTokensToServers,
-  userLogin
-} from '../../utils'
-import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
+  waitJobs
+} from '@shared/server-commands'
+import { HttpStatusCode } from '@shared/models'
 
 describe('Test user subscriptions API validators', function () {
   const path = '/api/v1/users/me/subscriptions'
-  let server: ServerInfo
+  let server: PeerTubeServer
   let userAccessToken = ''
 
   // ---------------------------------------------------------------
@@ -26,9 +26,7 @@ describe('Test user subscriptions API validators', function () {
   before(async function () {
     this.timeout(30000)
 
-    await flushTests()
-
-    server = await runServer(1)
+    server = await createSingleServer(1)
 
     await setAccessTokensToServers([ server ])
 
@@ -36,8 +34,8 @@ describe('Test user subscriptions API validators', function () {
       username: 'user1',
       password: 'my super password'
     }
-    await createUser(server.url, server.accessToken, user.username, user.password)
-    userAccessToken = await userLogin(server, user)
+    await server.users.create({ username: user.username, password: user.password })
+    userAccessToken = await server.login.getAccessToken(user)
   })
 
   describe('When listing my subscriptions', function () {
@@ -57,7 +55,7 @@ describe('Test user subscriptions API validators', function () {
       await makeGetRequest({
         url: server.url,
         path,
-        statusCodeExpected: 401
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
@@ -66,7 +64,7 @@ describe('Test user subscriptions API validators', function () {
         url: server.url,
         path,
         token: userAccessToken,
-        statusCodeExpected: 200
+        expectedStatus: HttpStatusCode.OK_200
       })
     })
   })
@@ -90,7 +88,7 @@ describe('Test user subscriptions API validators', function () {
       await makeGetRequest({
         url: server.url,
         path,
-        statusCodeExpected: 401
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
@@ -99,7 +97,7 @@ describe('Test user subscriptions API validators', function () {
         url: server.url,
         path,
         token: userAccessToken,
-        statusCodeExpected: 200
+        expectedStatus: HttpStatusCode.OK_200
       })
     })
   })
@@ -109,8 +107,8 @@ describe('Test user subscriptions API validators', function () {
       await makePostBodyRequest({
         url: server.url,
         path,
-        fields: { uri: 'user1_channel@localhost:9001' },
-        statusCodeExpected: 401
+        fields: { uri: 'user1_channel@localhost:' + server.port },
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
@@ -120,7 +118,7 @@ describe('Test user subscriptions API validators', function () {
         path,
         token: server.accessToken,
         fields: { uri: 'root' },
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makePostBodyRequest({
@@ -128,7 +126,7 @@ describe('Test user subscriptions API validators', function () {
         path,
         token: server.accessToken,
         fields: { uri: 'root@' },
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makePostBodyRequest({
@@ -136,18 +134,22 @@ describe('Test user subscriptions API validators', function () {
         path,
         token: server.accessToken,
         fields: { uri: 'root@hello@' },
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
     })
 
     it('Should succeed with the correct parameters', async function () {
+      this.timeout(20000)
+
       await makePostBodyRequest({
         url: server.url,
         path,
         token: server.accessToken,
-        fields: { uri: 'user1_channel@localhost:9001' },
-        statusCodeExpected: 204
+        fields: { uri: 'user1_channel@localhost:' + server.port },
+        expectedStatus: HttpStatusCode.NO_CONTENT_204
       })
+
+      await waitJobs([ server ])
     })
   })
 
@@ -155,8 +157,8 @@ describe('Test user subscriptions API validators', function () {
     it('Should fail with a non authenticated user', async function () {
       await makeGetRequest({
         url: server.url,
-        path: path + '/user1_channel@localhost:9001',
-        statusCodeExpected: 401
+        path: path + '/user1_channel@localhost:' + server.port,
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
@@ -165,39 +167,79 @@ describe('Test user subscriptions API validators', function () {
         url: server.url,
         path: path + '/root',
         token: server.accessToken,
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makeGetRequest({
         url: server.url,
         path: path + '/root@',
         token: server.accessToken,
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makeGetRequest({
         url: server.url,
         path: path + '/root@hello@',
         token: server.accessToken,
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
     })
 
     it('Should fail with an unknown subscription', async function () {
       await makeGetRequest({
         url: server.url,
-        path: path + '/root1@localhost:9001',
+        path: path + '/root1@localhost:' + server.port,
         token: server.accessToken,
-        statusCodeExpected: 404
+        expectedStatus: HttpStatusCode.NOT_FOUND_404
       })
     })
 
     it('Should succeed with the correct parameters', async function () {
       await makeGetRequest({
         url: server.url,
-        path: path + '/user1_channel@localhost:9001',
+        path: path + '/user1_channel@localhost:' + server.port,
         token: server.accessToken,
-        statusCodeExpected: 200
+        expectedStatus: HttpStatusCode.OK_200
+      })
+    })
+  })
+
+  describe('When checking if subscriptions exist', function () {
+    const existPath = path + '/exist'
+
+    it('Should fail with a non authenticated user', async function () {
+      await makeGetRequest({
+        url: server.url,
+        path: existPath,
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
+      })
+    })
+
+    it('Should fail with bad URIs', async function () {
+      await makeGetRequest({
+        url: server.url,
+        path: existPath,
+        query: { uris: 'toto' },
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
+      })
+
+      await makeGetRequest({
+        url: server.url,
+        path: existPath,
+        query: { 'uris[]': 1 },
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
+      })
+    })
+
+    it('Should succeed with the correct parameters', async function () {
+      await makeGetRequest({
+        url: server.url,
+        path: existPath,
+        query: { 'uris[]': 'coucou@localhost:' + server.port },
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.OK_200
       })
     })
   })
@@ -206,8 +248,8 @@ describe('Test user subscriptions API validators', function () {
     it('Should fail with a non authenticated user', async function () {
       await makeDeleteRequest({
         url: server.url,
-        path: path + '/user1_channel@localhost:9001',
-        statusCodeExpected: 401
+        path: path + '/user1_channel@localhost:' + server.port,
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
@@ -216,49 +258,44 @@ describe('Test user subscriptions API validators', function () {
         url: server.url,
         path: path + '/root',
         token: server.accessToken,
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makeDeleteRequest({
         url: server.url,
         path: path + '/root@',
         token: server.accessToken,
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makeDeleteRequest({
         url: server.url,
         path: path + '/root@hello@',
         token: server.accessToken,
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
     })
 
     it('Should fail with an unknown subscription', async function () {
       await makeDeleteRequest({
         url: server.url,
-        path: path + '/root1@localhost:9001',
+        path: path + '/root1@localhost:' + server.port,
         token: server.accessToken,
-        statusCodeExpected: 404
+        expectedStatus: HttpStatusCode.NOT_FOUND_404
       })
     })
 
     it('Should succeed with the correct parameters', async function () {
       await makeDeleteRequest({
         url: server.url,
-        path: path + '/user1_channel@localhost:9001',
+        path: path + '/user1_channel@localhost:' + server.port,
         token: server.accessToken,
-        statusCodeExpected: 204
+        expectedStatus: HttpStatusCode.NO_CONTENT_204
       })
     })
   })
 
   after(async function () {
-    killallServers([ server ])
-
-    // Keep the logs if the test failed
-    if (this['ok']) {
-      await flushTests()
-    }
+    await cleanupTests([ server ])
   })
 })