]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/friendsBasic.js
Server: split tests utils in multiple files
[github/Chocobozzz/PeerTube.git] / server / tests / api / friendsBasic.js
index 49e51804ffed71e6ae5e9c80340cfdd19b0373f9..2a6883acb3c894f592dfc744abfbef445459940b 100644 (file)
@@ -1,34 +1,42 @@
 'use strict'
 
-const async = require('async')
 const chai = require('chai')
+const each = require('async/each')
 const expect = chai.expect
-const request = require('supertest')
+const series = require('async/series')
 
-const utils = require('./utils')
+const loginUtils = require('../utils/login')
+const podsUtils = require('../utils/pods')
+const serversUtils = require('../utils/servers')
 
 describe('Test basic friends', function () {
   let servers = []
 
-  function testMadeFriends (servers, server_to_test, callback) {
+  function makeFriends (podNumber, callback) {
+    const server = servers[podNumber - 1]
+    return podsUtils.makeFriends(server.url, server.accessToken, callback)
+  }
+
+  function testMadeFriends (servers, serverToTest, callback) {
     const friends = []
     for (let i = 0; i < servers.length; i++) {
-      if (servers[i].url === server_to_test.url) continue
+      if (servers[i].url === serverToTest.url) continue
       friends.push(servers[i].url)
     }
 
-    utils.getFriendsList(server_to_test.url, function (err, res) {
+    podsUtils.getFriendsList(serverToTest.url, function (err, res) {
       if (err) throw err
 
       const result = res.body
-      const result_urls = [ result[0].url, result[1].url ]
       expect(result).to.be.an('array')
       expect(result.length).to.equal(2)
-      expect(result_urls[0]).to.not.equal(result_urls[1])
 
-      const error_string = 'Friends url do not correspond for ' + server_to_test.url
-      expect(friends).to.contain(result_urls[0], error_string)
-      expect(friends).to.contain(result_urls[1], error_string)
+      const resultUrls = [ result[0].url, result[1].url ]
+      expect(resultUrls[0]).to.not.equal(resultUrls[1])
+
+      const errorString = 'Friends url do not correspond for ' + serverToTest.url
+      expect(friends).to.contain(resultUrls[0], errorString)
+      expect(friends).to.contain(resultUrls[1], errorString)
       callback()
     })
   }
@@ -37,15 +45,23 @@ describe('Test basic friends', function () {
 
   before(function (done) {
     this.timeout(20000)
-    utils.flushAndRunMultipleServers(3, function (servers_run, urls_run) {
-      servers = servers_run
-      done()
+    serversUtils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) {
+      servers = serversRun
+
+      each(servers, function (server, callbackEach) {
+        loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
+          if (err) return callbackEach(err)
+
+          server.accessToken = accessToken
+          callbackEach()
+        })
+      }, done)
     })
   })
 
   it('Should not have friends', function (done) {
-    async.each(servers, function (server, callback) {
-      utils.getFriendsList(server.url, function (err, res) {
+    each(servers, function (server, callback) {
+      podsUtils.getFriendsList(server.url, function (err, res) {
         if (err) throw err
 
         const result = res.body
@@ -59,16 +75,10 @@ describe('Test basic friends', function () {
   it('Should make friends', function (done) {
     this.timeout(10000)
 
-    const path = '/api/v1/pods/makefriends'
-
-    async.series([
+    series([
       // The second pod make friend with the third
       function (next) {
-        request(servers[1].url)
-          .get(path)
-          .set('Accept', 'application/json')
-          .expect(204)
-          .end(next)
+        makeFriends(2, next)
       },
       // Wait for the request between pods
       function (next) {
@@ -76,7 +86,7 @@ describe('Test basic friends', function () {
       },
       // The second pod should have the third as a friend
       function (next) {
-        utils.getFriendsList(servers[1].url, function (err, res) {
+        podsUtils.getFriendsList(servers[1].url, function (err, res) {
           if (err) throw err
 
           const result = res.body
@@ -89,7 +99,7 @@ describe('Test basic friends', function () {
       },
       // Same here, the third pod should have the second pod as a friend
       function (next) {
-        utils.getFriendsList(servers[2].url, function (err, res) {
+        podsUtils.getFriendsList(servers[2].url, function (err, res) {
           if (err) throw err
 
           const result = res.body
@@ -102,11 +112,7 @@ describe('Test basic friends', function () {
       },
       // Finally the first pod make friend with the second pod
       function (next) {
-        request(servers[0].url)
-          .get(path)
-          .set('Accept', 'application/json')
-          .expect(204)
-          .end(next)
+        makeFriends(1, next)
       },
       // Wait for the request between pods
       function (next) {
@@ -116,25 +122,27 @@ describe('Test basic friends', function () {
     // Now each pod should be friend with the other ones
     function (err) {
       if (err) throw err
-      async.each(servers, function (server, callback) {
+      each(servers, function (server, callback) {
         testMadeFriends(servers, server, callback)
       }, done)
     })
   })
 
   it('Should not be allowed to make friend again', function (done) {
-    utils.makeFriends(servers[1].url, 409, done)
+    const server = servers[1]
+    podsUtils.makeFriends(server.url, server.accessToken, 409, done)
   })
 
   it('Should quit friends of pod 2', function (done) {
-    async.series([
+    series([
       // Pod 1 quit friends
       function (next) {
-        utils.quitFriends(servers[1].url, next)
+        const server = servers[1]
+        podsUtils.quitFriends(server.url, server.accessToken, next)
       },
       // Pod 1 should not have friends anymore
       function (next) {
-        utils.getFriendsList(servers[1].url, function (err, res) {
+        podsUtils.getFriendsList(servers[1].url, function (err, res) {
           if (err) throw err
 
           const result = res.body
@@ -146,8 +154,8 @@ describe('Test basic friends', function () {
       },
       // Other pods shouldn't have pod 1 too
       function (next) {
-        async.each([ servers[0].url, servers[2].url ], function (url, callback) {
-          utils.getFriendsList(url, function (err, res) {
+        each([ servers[0].url, servers[2].url ], function (url, callback) {
+          podsUtils.getFriendsList(url, function (err, res) {
             if (err) throw err
 
             const result = res.body
@@ -162,8 +170,9 @@ describe('Test basic friends', function () {
   })
 
   it('Should allow pod 2 to make friend again', function (done) {
-    utils.makeFriends(servers[1].url, function () {
-      async.each(servers, function (server, callback) {
+    const server = servers[1]
+    podsUtils.makeFriends(server.url, server.accessToken, function () {
+      each(servers, function (server, callback) {
         testMadeFriends(servers, server, callback)
       }, done)
     })
@@ -175,7 +184,7 @@ describe('Test basic friends', function () {
     })
 
     if (this.ok) {
-      utils.flushTests(done)
+      serversUtils.flushTests(done)
     } else {
       done()
     }