]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/check-params/user-subscriptions.ts
Fix s3 mock cleanup
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / user-subscriptions.ts
index 9f7d15b27aa65c5af8b79a9a95a159201f77cb31..c4922c7a2d2ef2db6cb3f918adcf03f71411140e 100644 (file)
@@ -1,36 +1,29 @@
-/* tslint:disable:no-unused-expression */
-
-import 'mocha'
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
 import {
-  createUser,
-  flushTests,
-  getMyUserInformation,
-  killallServers,
+  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'
+import { checkBadStartPagination, checkBadCountPagination, checkBadSortPagination } from '@server/tests/shared'
 
 describe('Test user subscriptions API validators', function () {
   const path = '/api/v1/users/me/subscriptions'
-  let server: ServerInfo
+  let server: PeerTubeServer
   let userAccessToken = ''
-  let userChannelUUID: string
 
   // ---------------------------------------------------------------
 
   before(async function () {
     this.timeout(30000)
 
-    await flushTests()
-
-    server = await runServer(1)
+    server = await createSingleServer(1)
 
     await setAccessTokensToServers([ server ])
 
@@ -38,13 +31,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)
-
-    {
-      const res = await getMyUserInformation(server.url, server.accessToken)
-      userChannelUUID = res.body.videoChannels[ 0 ].uuid
-    }
+    await server.users.create({ username: user.username, password: user.password })
+    userAccessToken = await server.login.getAccessToken(user)
   })
 
   describe('When listing my subscriptions', function () {
@@ -64,16 +52,16 @@ describe('Test user subscriptions API validators', function () {
       await makeGetRequest({
         url: server.url,
         path,
-        statusCodeExpected: 401
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
-    it('Should success with the correct parameters', async function () {
-      await await makeGetRequest({
+    it('Should succeed with the correct parameters', async function () {
+      await makeGetRequest({
         url: server.url,
         path,
         token: userAccessToken,
-        statusCodeExpected: 200
+        expectedStatus: HttpStatusCode.OK_200
       })
     })
   })
@@ -97,16 +85,16 @@ describe('Test user subscriptions API validators', function () {
       await makeGetRequest({
         url: server.url,
         path,
-        statusCodeExpected: 401
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
-    it('Should success with the correct parameters', async function () {
-      await await makeGetRequest({
+    it('Should succeed with the correct parameters', async function () {
+      await makeGetRequest({
         url: server.url,
         path,
         token: userAccessToken,
-        statusCodeExpected: 200
+        expectedStatus: HttpStatusCode.OK_200
       })
     })
   })
@@ -116,8 +104,8 @@ describe('Test user subscriptions API validators', function () {
       await makePostBodyRequest({
         url: server.url,
         path,
-        fields: { uri: userChannelUUID + '@localhost:9001' },
-        statusCodeExpected: 401
+        fields: { uri: 'user1_channel@' + server.host },
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
@@ -127,7 +115,7 @@ describe('Test user subscriptions API validators', function () {
         path,
         token: server.accessToken,
         fields: { uri: 'root' },
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makePostBodyRequest({
@@ -135,7 +123,7 @@ describe('Test user subscriptions API validators', function () {
         path,
         token: server.accessToken,
         fields: { uri: 'root@' },
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
 
       await makePostBodyRequest({
@@ -143,17 +131,112 @@ describe('Test user subscriptions API validators', function () {
         path,
         token: server.accessToken,
         fields: { uri: 'root@hello@' },
-        statusCodeExpected: 400
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
       })
     })
 
-    it('Should success with the correct parameters', async function () {
+    it('Should succeed with the correct parameters', async function () {
+      this.timeout(20000)
+
       await makePostBodyRequest({
         url: server.url,
         path,
         token: server.accessToken,
-        fields: { uri: userChannelUUID + '@localhost:9001' },
-        statusCodeExpected: 204
+        fields: { uri: 'user1_channel@' + server.host },
+        expectedStatus: HttpStatusCode.NO_CONTENT_204
+      })
+
+      await waitJobs([ server ])
+    })
+  })
+
+  describe('When getting a subscription', function () {
+    it('Should fail with a non authenticated user', async function () {
+      await makeGetRequest({
+        url: server.url,
+        path: path + '/user1_channel@' + server.host,
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
+      })
+    })
+
+    it('Should fail with bad URIs', async function () {
+      await makeGetRequest({
+        url: server.url,
+        path: path + '/root',
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
+      })
+
+      await makeGetRequest({
+        url: server.url,
+        path: path + '/root@',
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
+      })
+
+      await makeGetRequest({
+        url: server.url,
+        path: path + '/root@hello@',
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.BAD_REQUEST_400
+      })
+    })
+
+    it('Should fail with an unknown subscription', async function () {
+      await makeGetRequest({
+        url: server.url,
+        path: path + '/root1@' + server.host,
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.NOT_FOUND_404
+      })
+    })
+
+    it('Should succeed with the correct parameters', async function () {
+      await makeGetRequest({
+        url: server.url,
+        path: path + '/user1_channel@' + server.host,
+        token: server.accessToken,
+        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@' + server.host },
+        token: server.accessToken,
+        expectedStatus: HttpStatusCode.OK_200
       })
     })
   })
@@ -162,8 +245,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 + '/' + userChannelUUID + '@localhost:9001',
-        statusCodeExpected: 401
+        path: path + '/user1_channel@' + server.host,
+        expectedStatus: HttpStatusCode.UNAUTHORIZED_401
       })
     })
 
@@ -172,49 +255,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@' + server.host,
         token: server.accessToken,
-        statusCodeExpected: 404
+        expectedStatus: HttpStatusCode.NOT_FOUND_404
       })
     })
 
-    it('Should success with the correct parameters', async function () {
+    it('Should succeed with the correct parameters', async function () {
       await makeDeleteRequest({
         url: server.url,
-        path: path + '/' + userChannelUUID + '@localhost:9001',
+        path: path + '/user1_channel@' + server.host,
         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 ])
   })
 })