const request = require('supertest')
const testUtils = {
+ dateIsValid: dateIsValid,
flushTests: flushTests,
getFriendsList: getFriendsList,
getVideo: getVideo,
getVideosList: getVideosList,
+ getVideosListPagination: getVideosListPagination,
+ getVideosListSort: getVideosListSort,
login: login,
loginAndGetAccessToken: loginAndGetAccessToken,
makeFriends: makeFriends,
flushAndRunMultipleServers: flushAndRunMultipleServers,
runServer: runServer,
searchVideo: searchVideo,
+ searchVideoWithPagination: searchVideoWithPagination,
+ searchVideoWithSort: searchVideoWithSort,
testImage: testImage,
uploadVideo: uploadVideo
}
// ---------------------- Export functions --------------------
+function dateIsValid (dateString) {
+ const dateToCheck = new Date(dateString)
+ const now = new Date()
+
+ // Check if the interval is more than 2 minutes
+ if (now - dateToCheck > 120000) return false
+
+ return true
+}
+
function flushTests (callback) {
exec('npm run clean:server:test', callback)
}
request(url)
.get(path)
+ .query({ sort: 'name' })
+ .set('Accept', 'application/json')
+ .expect(200)
+ .expect('Content-Type', /json/)
+ .end(end)
+}
+
+function getVideosListPagination (url, start, count, end) {
+ const path = '/api/v1/videos'
+
+ request(url)
+ .get(path)
+ .query({ start: start })
+ .query({ count: count })
+ .set('Accept', 'application/json')
+ .expect(200)
+ .expect('Content-Type', /json/)
+ .end(end)
+}
+
+function getVideosListSort (url, sort, end) {
+ const path = '/api/v1/videos'
+
+ request(url)
+ .get(path)
+ .query({ sort: sort })
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
})
}
-function searchVideo (url, search, end) {
+function searchVideo (url, search, field, end) {
+ if (!end) {
+ end = field
+ field = null
+ }
+
+ const path = '/api/v1/videos'
+ const req = request(url)
+ .get(path + '/search/' + search)
+ .set('Accept', 'application/json')
+
+ if (field) req.query({ field: field })
+ req.expect(200)
+ .expect('Content-Type', /json/)
+ .end(end)
+}
+
+function searchVideoWithPagination (url, search, field, start, count, end) {
+ const path = '/api/v1/videos'
+
+ request(url)
+ .get(path + '/search/' + search)
+ .query({ start: start })
+ .query({ count: count })
+ .query({ field: field })
+ .set('Accept', 'application/json')
+ .expect(200)
+ .expect('Content-Type', /json/)
+ .end(end)
+}
+
+function searchVideoWithSort (url, search, sort, end) {
const path = '/api/v1/videos'
request(url)
.get(path + '/search/' + search)
+ .query({ sort: sort })
.set('Accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)