From d38b82810638b9f664c9016fac2684454c273a77 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 8 Mar 2017 21:35:43 +0100 Subject: Add like/dislike system for videos --- server/tests/api/check-params/users.js | 76 ++++++++++++++++++++++++++++ server/tests/api/check-params/videos.js | 42 ++++++++++++++++ server/tests/api/multiple-pods.js | 89 ++++++++++++++++++++++++++++----- server/tests/api/single-pod.js | 34 +++++++++++++ server/tests/api/users.js | 29 +++++++++++ server/tests/utils/users.js | 13 +++++ server/tests/utils/videos.js | 20 +++++++- 7 files changed, 290 insertions(+), 13 deletions(-) (limited to 'server/tests') diff --git a/server/tests/api/check-params/users.js b/server/tests/api/check-params/users.js index 6edb54660..11e2bada4 100644 --- a/server/tests/api/check-params/users.js +++ b/server/tests/api/check-params/users.js @@ -9,11 +9,13 @@ const loginUtils = require('../../utils/login') const requestsUtils = require('../../utils/requests') const serversUtils = require('../../utils/servers') const usersUtils = require('../../utils/users') +const videosUtils = require('../../utils/videos') describe('Test users API validators', function () { const path = '/api/v1/users/' let userId = null let rootId = null + let videoId = null let server = null let userAccessToken = null @@ -47,6 +49,23 @@ describe('Test users API validators', function () { usersUtils.createUser(server.url, server.accessToken, username, password, next) }, + function (next) { + const name = 'my super name for pod' + const description = 'my super description for pod' + const tags = [ 'tag' ] + const file = 'video_short2.webm' + videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, file, next) + }, + function (next) { + videosUtils.getVideosList(server.url, function (err, res) { + if (err) throw err + + const videos = res.body.data + videoId = videos[0].id + + next() + }) + }, function (next) { const user = { username: 'user1', @@ -289,6 +308,63 @@ describe('Test users API validators', function () { }) }) + describe('When getting my video rating', function () { + it('Should fail with a non authenticated user', function (done) { + request(server.url) + .get(path + 'me/videos/' + videoId + '/rating') + .set('Authorization', 'Bearer faketoken') + .set('Accept', 'application/json') + .expect(401, done) + }) + + it('Should fail with an incorrect video uuid', function (done) { + request(server.url) + .get(path + 'me/videos/blabla/rating') + .set('Authorization', 'Bearer ' + userAccessToken) + .set('Accept', 'application/json') + .expect(400, done) + }) + + it('Should fail with an unknown video', function (done) { + request(server.url) + .get(path + 'me/videos/4da6fde3-88f7-4d16-b119-108df5630b06/rating') + .set('Authorization', 'Bearer ' + userAccessToken) + .set('Accept', 'application/json') + .expect(404, done) + }) + + it('Should success with the correct parameters', function (done) { + request(server.url) + .get(path + 'me/videos/' + videoId + '/rating') + .set('Authorization', 'Bearer ' + userAccessToken) + .set('Accept', 'application/json') + .expect(200, done) + }) + }) + + describe('When removing an user', function () { + it('Should fail with an incorrect id', function (done) { + request(server.url) + .delete(path + 'bla-bla') + .set('Authorization', 'Bearer ' + server.accessToken) + .expect(400, done) + }) + + it('Should fail with the root user', function (done) { + request(server.url) + .delete(path + rootId) + .set('Authorization', 'Bearer ' + server.accessToken) + .expect(400, done) + }) + + it('Should return 404 with a non existing id', function (done) { + request(server.url) + .delete(path + '45') + .set('Authorization', 'Bearer ' + server.accessToken) + .expect(404, done) + }) + }) + describe('When removing an user', function () { it('Should fail with an incorrect id', function (done) { request(server.url) diff --git a/server/tests/api/check-params/videos.js b/server/tests/api/check-params/videos.js index f8549a95b..0f5f40b8e 100644 --- a/server/tests/api/check-params/videos.js +++ b/server/tests/api/check-params/videos.js @@ -420,6 +420,48 @@ describe('Test videos API validator', function () { it('Should succeed with the correct parameters') }) + describe('When rating a video', function () { + let videoId + + before(function (done) { + videosUtils.getVideosList(server.url, function (err, res) { + if (err) throw err + + videoId = res.body.data[0].id + + return done() + }) + }) + + it('Should fail without a valid uuid', function (done) { + const data = { + rating: 'like' + } + requestsUtils.makePutBodyRequest(server.url, path + 'blabla/rate', server.accessToken, data, done) + }) + + it('Should fail with an unknown id', function (done) { + const data = { + rating: 'like' + } + requestsUtils.makePutBodyRequest(server.url, path + '4da6fde3-88f7-4d16-b119-108df5630b06/rate', server.accessToken, data, done, 404) + }) + + it('Should fail with a wrong rating', function (done) { + const data = { + rating: 'likes' + } + requestsUtils.makePutBodyRequest(server.url, path + videoId + '/rate', server.accessToken, data, done) + }) + + it('Should succeed with the correct parameters', function (done) { + const data = { + rating: 'like' + } + requestsUtils.makePutBodyRequest(server.url, path + videoId + '/rate', server.accessToken, data, done, 204) + }) + }) + describe('When removing a video', function () { it('Should have 404 with nothing', function (done) { request(server.url) diff --git a/server/tests/api/multiple-pods.js b/server/tests/api/multiple-pods.js index e02b6180b..552f10c6f 100644 --- a/server/tests/api/multiple-pods.js +++ b/server/tests/api/multiple-pods.js @@ -4,6 +4,7 @@ const chai = require('chai') const each = require('async/each') +const eachSeries = require('async/eachSeries') const expect = chai.expect const parallel = require('async/parallel') const series = require('async/series') @@ -378,7 +379,7 @@ describe('Test multiple pods', function () { }) }) - describe('Should update video views', function () { + describe('Should update video views, likes and dislikes', function () { let localVideosPod3 = [] let remoteVideosPod1 = [] let remoteVideosPod2 = [] @@ -419,7 +420,7 @@ describe('Test multiple pods', function () { ], done) }) - it('Should views multiple videos on owned servers', function (done) { + it('Should view multiple videos on owned servers', function (done) { this.timeout(30000) parallel([ @@ -440,18 +441,18 @@ describe('Test multiple pods', function () { }, function (callback) { - setTimeout(done, 22000) + setTimeout(callback, 22000) } ], function (err) { if (err) throw err - each(servers, function (server, callback) { + eachSeries(servers, function (server, callback) { videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err const videos = res.body.data - expect(videos.find(video => video.views === 3)).to.be.exist - expect(videos.find(video => video.views === 1)).to.be.exist + expect(videos.find(video => video.views === 3)).to.exist + expect(videos.find(video => video.views === 1)).to.exist callback() }) @@ -459,7 +460,7 @@ describe('Test multiple pods', function () { }) }) - it('Should views multiple videos on each servers', function (done) { + it('Should view multiple videos on each servers', function (done) { this.timeout(30000) parallel([ @@ -504,17 +505,17 @@ describe('Test multiple pods', function () { }, function (callback) { - setTimeout(done, 22000) + setTimeout(callback, 22000) } ], function (err) { if (err) throw err let baseVideos = null - each(servers, function (server, callback) { + eachSeries(servers, function (server, callback) { videosUtils.getVideosList(server.url, function (err, res) { if (err) throw err - const videos = res.body + const videos = res.body.data // Initialize base videos for future comparisons if (baseVideos === null) { @@ -522,10 +523,74 @@ describe('Test multiple pods', function () { return callback() } - for (let i = 0; i < baseVideos.length; i++) { - expect(baseVideos[i].views).to.equal(videos[i].views) + baseVideos.forEach(baseVideo => { + const sameVideo = videos.find(video => video.name === baseVideo.name) + expect(baseVideo.views).to.equal(sameVideo.views) + }) + + callback() + }) + }, done) + }) + }) + + it('Should like and dislikes videos on different services', function (done) { + this.timeout(30000) + + parallel([ + function (callback) { + videosUtils.rateVideo(servers[0].url, servers[0].accessToken, remoteVideosPod1[0], 'like', callback) + }, + + function (callback) { + videosUtils.rateVideo(servers[0].url, servers[0].accessToken, remoteVideosPod1[0], 'dislike', callback) + }, + + function (callback) { + videosUtils.rateVideo(servers[0].url, servers[0].accessToken, remoteVideosPod1[0], 'like', callback) + }, + + function (callback) { + videosUtils.rateVideo(servers[2].url, servers[2].accessToken, localVideosPod3[1], 'like', callback) + }, + + function (callback) { + videosUtils.rateVideo(servers[2].url, servers[2].accessToken, localVideosPod3[1], 'dislike', callback) + }, + + function (callback) { + videosUtils.rateVideo(servers[2].url, servers[2].accessToken, remoteVideosPod3[1], 'dislike', callback) + }, + + function (callback) { + videosUtils.rateVideo(servers[2].url, servers[2].accessToken, remoteVideosPod3[0], 'like', callback) + }, + + function (callback) { + setTimeout(callback, 22000) + } + ], function (err) { + if (err) throw err + + let baseVideos = null + eachSeries(servers, function (server, callback) { + videosUtils.getVideosList(server.url, function (err, res) { + if (err) throw err + + const videos = res.body.data + + // Initialize base videos for future comparisons + if (baseVideos === null) { + baseVideos = videos + return callback() } + baseVideos.forEach(baseVideo => { + const sameVideo = videos.find(video => video.name === baseVideo.name) + expect(baseVideo.likes).to.equal(sameVideo.likes) + expect(baseVideo.dislikes).to.equal(sameVideo.dislikes) + }) + callback() }) }, done) diff --git a/server/tests/api/single-pod.js b/server/tests/api/single-pod.js index 87d0e9a71..96e4aff9e 100644 --- a/server/tests/api/single-pod.js +++ b/server/tests/api/single-pod.js @@ -609,6 +609,40 @@ describe('Test a single pod', function () { }) }) + it('Should like a video', function (done) { + videosUtils.rateVideo(server.url, server.accessToken, videoId, 'like', function (err) { + if (err) throw err + + videosUtils.getVideo(server.url, videoId, function (err, res) { + if (err) throw err + + const video = res.body + + expect(video.likes).to.equal(1) + expect(video.dislikes).to.equal(0) + + done() + }) + }) + }) + + it('Should dislike the same video', function (done) { + videosUtils.rateVideo(server.url, server.accessToken, videoId, 'dislike', function (err) { + if (err) throw err + + videosUtils.getVideo(server.url, videoId, function (err, res) { + if (err) throw err + + const video = res.body + + expect(video.likes).to.equal(0) + expect(video.dislikes).to.equal(1) + + done() + }) + }) + }) + after(function (done) { process.kill(-server.app.pid) diff --git a/server/tests/api/users.js b/server/tests/api/users.js index bd95e78c2..f9568b874 100644 --- a/server/tests/api/users.js +++ b/server/tests/api/users.js @@ -10,6 +10,7 @@ const loginUtils = require('../utils/login') const podsUtils = require('../utils/pods') const serversUtils = require('../utils/servers') const usersUtils = require('../utils/users') +const requestsUtils = require('../utils/requests') const videosUtils = require('../utils/videos') describe('Test users', function () { @@ -138,6 +139,23 @@ describe('Test users', function () { videosUtils.uploadVideo(server.url, accessToken, name, description, tags, video, 204, done) }) + it('Should retrieve a video rating', function (done) { + videosUtils.rateVideo(server.url, accessToken, videoId, 'like', function (err) { + if (err) throw err + + usersUtils.getUserVideoRating(server.url, accessToken, videoId, function (err, res) { + if (err) throw err + + const rating = res.body + + expect(rating.videoId).to.equal(videoId) + expect(rating.rating).to.equal('like') + + done() + }) + }) + }) + it('Should not be able to remove the video with an incorrect token', function (done) { videosUtils.removeVideo(server.url, 'bad_token', videoId, 401, done) }) @@ -150,10 +168,21 @@ describe('Test users', function () { it('Should logout (revoke token)') + it('Should not be able to get the user informations') + it('Should not be able to upload a video') it('Should not be able to remove a video') + it('Should not be able to rate a video', function (done) { + const path = '/api/v1/videos/' + const data = { + rating: 'likes' + } + + requestsUtils.makePutBodyRequest(server.url, path + videoId, 'wrong token', data, done, 401) + }) + it('Should be able to login again') it('Should have an expired access token') diff --git a/server/tests/utils/users.js b/server/tests/utils/users.js index a2c010f64..7817160b9 100644 --- a/server/tests/utils/users.js +++ b/server/tests/utils/users.js @@ -5,6 +5,7 @@ const request = require('supertest') const usersUtils = { createUser, getUserInformation, + getUserVideoRating, getUsersList, getUsersListPaginationAndSort, removeUser, @@ -47,6 +48,18 @@ function getUserInformation (url, accessToken, end) { .end(end) } +function getUserVideoRating (url, accessToken, videoId, end) { + const path = '/api/v1/users/me/videos/' + videoId + '/rating' + + request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + function getUsersList (url, end) { const path = '/api/v1/users' diff --git a/server/tests/utils/videos.js b/server/tests/utils/videos.js index f94368437..177426076 100644 --- a/server/tests/utils/videos.js +++ b/server/tests/utils/videos.js @@ -16,7 +16,8 @@ const videosUtils = { searchVideoWithSort, testVideoImage, uploadVideo, - updateVideo + updateVideo, + rateVideo } // ---------------------- Export functions -------------------- @@ -236,6 +237,23 @@ function updateVideo (url, accessToken, id, name, description, tags, specialStat req.expect(specialStatus).end(end) } +function rateVideo (url, accessToken, id, rating, specialStatus, end) { + if (!end) { + end = specialStatus + specialStatus = 204 + } + + const path = '/api/v1/videos/' + id + '/rate' + + request(url) + .put(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send({ rating }) + .expect(specialStatus) + .end(end) +} + // --------------------------------------------------------------------------- module.exports = videosUtils -- cgit v1.2.3