Skip to content
Permalink
Browse files
Merge pull request #9 from georgiak/Unit-Code-Quality
Unit code quality
  • Loading branch information
georgiak committed Nov 28, 2019
2 parents e21ec17 + 88add21 commit ea888378d432e612422274f7506e1db6ca5a7fd8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
@@ -1,23 +1,18 @@
#!/usr/bin/env node

//Routes File

'use strict'

/* MODULE IMPORTS */
const bcrypt = require('bcrypt-promise')
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
const staticDir = require('koa-static')
const bodyParser = require('koa-bodyparser')
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'})
const session = require('koa-session')
const sqlite = require('sqlite-async')
const fs = require('fs-extra')
const mime = require('mime-types')
const Database = require('sqlite-async')
//const jimp = require('jimp')

/* IMPORT CUSTOM MODULES */
const User = require('./modules/user')
@@ -55,7 +50,11 @@ router.get('/', async ctx => {
await ctx.render('error', {message: err.message})
}
})

/**
* The Users Interaction Page and Script
* @name User Page
* @route {GET} /myfiles
*/

// eslint-disable-next-line max-lines-per-function
router.get('/myfiles', async ctx => {
@@ -140,7 +139,6 @@ router.post('/register', koaBody, async ctx => {
try {
// extract the data from the request
const body = ctx.request.body
const {path, type} = ctx.request.files.avatar
// call the functions in the module
const user = await new User(dbName)
await user.register(body.user, body.pass)
@@ -208,7 +206,7 @@ router.post('/transfer', koaBody, async ctx => {

//TODO: username passed in email
//Send the link in email
// await Mailer.sendMail(email, link, userMessage)
await Mailer.sendMail(email, link, userMessage)

ctx.redirect('/myfiles?msg=Your file has been transfered!')

@@ -28,14 +28,16 @@ module.exports = class File {

async upload(extension, _name,_size,_date, _userName, path) {
try{

if (_name === '') throw new Error('String cannot be empty.')
if(_name.length >= 50) throw new Error('String size too big')
if(_size >= 1073741824) throw new Error('Size cannot be equal or greater than a GB')
const size = _size/1000

_date = _date.toString().slice(0,24)

const nameHash = Mailer.createHash(_name)

const nameExt = nameHash + '.' +extension
const nameExt = `${nameHash}.${extension}`


let targetPath = `/${ _userName }/${ nameExt}`
@@ -45,7 +47,7 @@ module.exports = class File {
await this.db.run(sql)

//save to directory on server
targetPath = 'private' + targetPath
targetPath = `private${targetPath}`
await fs.copy(path, targetPath)
console.log('File saved on server')

@@ -79,12 +81,14 @@ module.exports = class File {

async getFileInfo(_fileName, _userName) {
try {
_userName = ''
console.log(_userName)
//TODO: Only search table belonging to specific user
const sql = `SELECT * FROM Files
WHERE FileHash LIKE '${_fileName}'`

const data = await this.db.all(sql)

return data

} catch (err) {
@@ -104,4 +104,9 @@ body {
font-family: sans-serif;
color: white;
font-size: 20px;
}


.Error{
color:white;
}
@@ -1,6 +1,6 @@
'use strict'

const hashes = require('../modules/hashes.js')
const hashes = require('../modules/mailer.js')

describe('createHash()', () => {
// block of tests
@@ -0,0 +1,32 @@
'use strict'

const Uploads = require('../modules/upload.js')

describe('upload()', () => {
test('File Name missing', async done => {
expect.assertions(1)
const todo = await new Uploads()
await expect(todo.upload(
'png','',200,'Wed 20/20/19 15:20:20','path','Userpath')).rejects.toEqual(Error('String cannot be empty.'))
done()
})

test('File Not bigger than a GB', async done => {
expect.assertions(1)
const todo = await new Uploads()
await expect(todo.upload(
'png','name',1073741824,'Wed 20/20/19 15:20:20',
'path','Userpath')).rejects.toEqual(Error('Size cannot be equal or greater than a GB'))
done()
})

test('File Name string too long', async done => {
expect.assertions(1)
const todo = await new Uploads()
await expect(todo.upload(
'png','K8DbQxhva2qECr7urmCOVOayGnf6OUh2MCjYC9VaO04m9SXLui',
10750,'Wed 20/20/19 15:20:20','path','Userpath')).rejects.toEqual(Error('String size too big'))
done()
})

})
@@ -3,6 +3,7 @@

const Accounts = require('../modules/user.js')


describe('register()', () => {

test('register a valid account', async done => {
@@ -38,6 +39,7 @@ describe('register()', () => {
done()
})


})

describe('uploadPicture()', () => {
@@ -73,4 +75,5 @@ describe('login()', () => {
done()
})


})
@@ -8,7 +8,9 @@
<link href="login.css" type="text/css" rel="stylesheet" />
</head>
<body>
<section class = 'Error'>
<h1>An Error Has Occurred</h1>
<h2>{{message}}</h2>
</section>
</body>
</html>

0 comments on commit ea88837

Please sign in to comment.