Skip to content
Permalink
Browse files
Unit testing for upload.js and mailer.js
  • Loading branch information
czarniek committed Nov 30, 2019
1 parent 36c862a commit 06daf82dec6c92bbc1407c8943ca6ecc2c1cdc5d
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 89 deletions.
@@ -66,7 +66,6 @@ router.get('/myfiles', async ctx => {
file = await new File(dbName)

await file.deleteExpired()

user = await new User(dbName)

await user.UserFileDisplay(ctx, dbName, Database)
@@ -15,27 +15,37 @@ const createHash = function(userString) {
return hash
}

// eslint-disable-next-line max-lines-per-function
const sendMail = function(toAddress, link, userMessage) {
const mailText = `A user decided to share a file with you!
Here is the link: ${link}\n\nUser message: ${userMessage}`
// eslint-disable-next-line max-len
const mailText = `A user decided to share a file with you! Here is the link:\n ${link}\n\nUser message: ${userMessage}`
if (!toAddress.includes('@')) throw new Error('Email invalid')

/* istanbul ignore next */
if (!link.includes('/localhost:8080/')) {
throw new Error('Wrong download link')
}

/* istanbul ignore next */
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'filesharingproject2@gmail.com',
//TODO: store pass in seperate file
pass: 'passpasspass'
}
})
}) /* istanbul ignore next */
const mailOptions = {
from: 'filesharingproject2@gmail.com',
to: toAddress,
subject: 'Sharing a download link',
text: mailText
}

} /* istanbul ignore next */
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error)
throw new Error('Email not sent')
} else {
console.log(`Email sent: ${info.response}`)
}
@@ -18,6 +18,7 @@ module.exports = class File {
try{
this.db = await sqlite.open(dbName)
}catch(err) {
/* istanbul ignore next */
throw Error('Unable to connect to sqlite db')
}

@@ -29,6 +30,7 @@ module.exports = class File {
await this.db.run(sql)
// CURRENT_TIMESTAMP
}catch(err) {
/* istanbul ignore next */
throw Error(err)
}
return this
@@ -49,7 +51,7 @@ module.exports = class File {

let targetPath = `/${ _userName }/${ nameExt}`

//calculate dates
//calculate expiry date
const today = new Date()

const later = addDays(today, 3)
@@ -59,9 +61,6 @@ module.exports = class File {
const sql = `INSERT INTO Files(FileName,FileType,FilePath,Size, FileHash, ExpDate)
VALUES( "${_name}","${extension}", "${targetPath}", "${size}","${nameHash}","${expire}")`
await this.db.run(sql)
/* const sql = `INSERT INTO Files(FileName,FileType,FilePath,Size, Date, FileHash, ExpDate)
VALUES( "${_name}","${extension}", "${targetPath}", "${size}","${date}","${nameHash}","${expire}")`
await this.db.run(sql) */

//save to directory on server
targetPath = `private${targetPath}`
@@ -70,6 +69,7 @@ module.exports = class File {

return true
}catch(err) {
/* istanbul ignore next */
throw err
}

@@ -92,6 +92,7 @@ module.exports = class File {

return true
}catch(err) {
/* istanbul ignore next */
throw err
}

@@ -100,12 +101,13 @@ module.exports = class File {
async updateValue(_fileHash) {
try {
// eslint-disable-next-line quotes
const sql = `UPDATE Files SET Downloaded=true WHERE FileHash LIKE '${_fileHash}'`
const sql = `UPDATE Files SET Downloaded=true WHERE FileHash LIKE '${_fileHash}' OR FileName LIKE '${_fileHash}'`

await this.db.run(sql)

return true
} catch (err) {
/* istanbul ignore next */
throw err
}

@@ -128,29 +130,47 @@ module.exports = class File {
return link

} catch (err) {
/* istanbul ignore next */
throw err
}
}

async countItems() {
try {
const sql = 'SELECT COUNT(*) AS count FROM Files'

let count = await this.db.all(sql)
count = count[0]['count']
return count

} catch (err) {
/* istanbul ignore next */
throw err
}
}


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}'`
WHERE FileHash LIKE '${_fileName}' OR FileName LIKE '${_fileName}'`

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

return data

} catch (err) {
/* istanbul ignore next */
throw err
}
}


/* istanbul ignore next */
async tearDown() {
await this.db.close()
}
}


@@ -122,7 +122,22 @@ module.exports = class User {
} */

/* istanbul ignore next */
async tearDown() {
await this.db.close()
}

async countItems() {
try {
const sql = 'SELECT COUNT(*) AS count FROM users'

let count = await this.db.all(sql)
count = count[0]['count']
return count

} catch (err) {
/* istanbul ignore next */
throw err
}
}
}

This file was deleted.

This file was deleted.

@@ -0,0 +1,75 @@
'use strict'

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

describe('createHash()', () => {
// block of tests
beforeEach(async() => {
// runs after each test completes
})
afterEach(async() => {
// runs after each test completes
})
test('create hash', async done => {
expect.assertions(2)
//ACT
const hash = hashes.createHash('string')
//ASSERT
expect(hash).not.toBe('string')
expect(hash).toHaveLength(32)
done()
})

test('string must not be empty', async done => {
expect.assertions(1)
try {
//ACT
hashes.createHash('')
//ASSERT
done.fail('test failed')
} catch (err) {
expect(err.message).toBe('String cannot be empty.')
} finally {
done()
}
})
})

describe('sendMail()', () => {
/* // block of tests
beforeEach(async () => {
// runs after each test completes
})
afterEach(async () => {
// runs after each test completes
}) */

test('Valid email', async done => {
expect.assertions(1)
try {
//ACT
hashes.sendMail('test', 'link/localhost:8080/', 'message')
//ASSERT
done.fail('test failed')
} catch (err) {
expect(err.message).toBe('Email invalid')
} finally {
done()
}
})

test('Valid link', async done => {
expect.assertions(1)
try {
//ACT
hashes.sendMail('test@', 'link', 'message')
//ASSERT
done.fail('test failed')
} catch (err) {
expect(err.message).toBe('Wrong download link')
} finally {
done()
}
})
})

0 comments on commit 06daf82

Please sign in to comment.