Skip to content
Permalink
Browse files
Add files via upload
  • Loading branch information
abdos committed Jul 30, 2019
1 parent 107b95c commit b9d846e396708bc9fa9c252aa3526fb1c260c364
Show file tree
Hide file tree
Showing 19 changed files with 7,402 additions and 0 deletions.
@@ -0,0 +1,90 @@
const items = require('../models/items/items')

const connData = {
host: 'localhost',
user: 'root',
password: '',
database: 'classifiedads'
};

describe('getAll', () => {
test('retrieving all items with no error', done => {
items.getAll(connData, (err) => {
expect(err).toBeNull()
done()
})
})

test('check if items are being retrieved', done => {
items.getAll(connData, (err, computerData) => {
expect(computerData.length).toBeGreaterThan(1)
done()
})
})
})

describe('add', () => {

const itemData = {
userID: 1,
title: "testing title",
description: "A short description for the purpose of a test",
location: "NN4 5BQ",
condition: "New",
image: "test image",
mobile: "07485621456",
price: 5000
}

const wrongItemData = {
userID: undefined,
title: undefined,
description: undefined,
location: undefined,
condition: undefined,
image: undefined,
mobile: undefined,
price: undefined
}

const correctItemData = {
userID: 1,
title: "testing title",
description: "A short description for the purpose of a test",
location: "NN4 5BQ",
condition: "New",
image: "test image",
mobile: "07485621456",
price: 5000
}

test('add item with no error', done => {
items.add(connData, itemData, (err) => {
expect(err).toBeNull()
done()
})
})

test('check if item has been added', done => {
items.add(connData, itemData, (err, result) => {
expect(result.protocol41).toBeTruthy()
expect(result.changedRows).toEqual(0)
expect(result.affectedRows).toBe(1)
done()
})
})

test('itemData can not contain undefined', done => {
items.add(connData, itemData, () => {
expect(itemData).not.toBe(wrongItemData)
done()
})
})

test('itemData must contain items', done => {
items.add(connData, itemData, () => {
expect(itemData).toEqual(correctItemData)
done()
})
})
})
@@ -0,0 +1,90 @@
const user = require('../models/user/user')

const userData = {
firstName: "Salahtest",
surname: "Abdotest",
email: "abdostest@uni.coventry.ac.uk",
username: "abdostest",
password: "Password01."
}

const wrongUserData = {
firstName: undefined,
surname: undefined,
email: undefined,
username: undefined,
password: undefined
}

const correctUserData = {
firstName: "Salahtest",
surname: "Abdotest",
email: "abdostest@uni.coventry.ac.uk",
username: "abdostest",
password: "Password01."
}


describe('database connection', () => {
const connData = {
host: 'localhost',
user: 'root',
password: 'wrong',
database: 'classifiedads'
};

test('Trying to add user with incorrect database data', done => {
user.add(connData, userData, (err) => {
expect(err.message.length).toBeGreaterThan(1)
done()
})
})

test('Trying to get all user with incorrect database data', done => {
user.getAll(connData, (err) => {
expect(err.message.length).toBeGreaterThan(1)
done()
})
})

})

describe('add', () => {
const connData = {
host: 'localhost',
user: 'root',
password: '',
database: 'classifiedads'
};

test('add a new user without any errors', done => {
user.add(connData, userData, (err) => {
expect(err).toBeNull()
done()
})
})

test('check if user has been added', done => {
user.add(connData, userData, (err, result) => {
expect(result.protocol41).toBeTruthy()
expect(result.changedRows).toEqual(0)
expect(result.affectedRows).toBe(1)
done()
})
})

test('user can not contain undefined', done => {
user.add(connData, userData, () => {
expect(userData).not.toBe(wrongUserData)
done()
})
})

test('userData must contain user information', done => {
user.add(connData, userData, () => {
expect(userData).toEqual(correctUserData)
done()
})
})
})

@@ -0,0 +1,15 @@
const mysql = require('mysql');

exports.connect = function(conData, callback){

let conn = mysql.createConnection({
host: conData.host,
user: conData.user,
password: conData.password,
database: conData.database
});
conn.connect(function(err) {
if (err) callback(err);
callback(null, conn);
});
};
@@ -0,0 +1,63 @@
const db = require('../../database')
const auth = require('basic-auth');

module.exports.loginUser = function (conData, request, callback) { // function retrieve database data and request

if (request.headers === undefined || request.headers.authorization === undefined) { //checks if headers object or the values are undefined

const err = { //Retruns message header missing to front-end
message: 'Authorization header missing',
code: 401
};
callback(err);
return;
}

const loginData = auth(request); // saves encrypted login detail to "login"

if (loginData === undefined || loginData.name === undefined || loginData.pass === undefined) { //checks if headers object or the values are undefined


const err = { //return message login details missing to front-end
message: 'missing username or password',
code: 401
};
callback(err);
return;
}

db.connect(conData, function (err, data) { //connection made to the database

if (err) { //return error 500 if there were problems connecting to database
err.code = 500;
callback(err);
return;
}

data.query('SELECT userID,role, first_name FROM user WHERE username="' + loginData.name + '" AND password= "' + loginData.pass + '"', function (err, result) { //where the username and password equal in the user table return the result

if (err) { //return error 500 if there were problems connecting to database
err.code = 500;
callback(err);
return;
}


if (result && result.length > 0) { //check if return value cantains any value
callback(null, { userID: result[0].userID, role: result[0].role, firstName: result[0].first_name }); //send back results
}
else {
const err = { //return message innorrect login details to front-end
message: 'wrong username or password',
code: 401
};
callback(err);
}


});
});

}


@@ -0,0 +1,65 @@
const db = require('../../database')

module.exports.add = function (conData, favData, callback) {
db.connect(conData, function (err, conn) {

if (err) { //return error if there were problems connecting to database
callback(err);
return;
}

conn.query("INSERT INTO favourite (itemID, userID) VALUES (\"" + favData.itemID + "\", \"" + favData.userID + "\");", favData, function (err, result) { // insert into favourite setting the item ID of the selected item and user ID

callback(err, result);
});

})
};

module.exports.remove = function (conData, deleteData, callback) {
db.connect(conData, function (err, conn) {

if (err) { //return error if there were problems connecting to database
callback(err);
return;
}
conn.query('DELETE FROM favourite WHERE itemID= "' + deleteData.itemID + '" AND userID= "' + deleteData.userID + '"' , function (err, result){ //delete the users favourite where the item ID selected Equals the users ID

callback(err, result);
});

})
};

module.exports.getAll = function (conData, favData, callback) {
db.connect(conData, function (err, conn) {

if (err) {//return error if there were problems connecting to database
callback(err);
return;
}
//select all items where the userID is equal to the user ID sent
conn.query('SELECT itemID FROM favourite WHERE userID="' + favData.userID + '"', function (err, result) {

if (err) { //return error if there were problems excuting the query
err.code = 500;
callback(err);
return;
}


if (result && result.length > 0) { // if the result length return value return that value.
callback(null, result);
}
else { // if value returned is nothing there user has no saved favourites.
const err = {
message: 'User had no favourites',
code: 401
};
callback(err);
}


});
});
};
@@ -0,0 +1,50 @@
const db = require('../../database')


module.exports.getAll = function(conData, callback){

// conection to database
db.connect(conData, function(err, conn){

if (err) { //return error if there were problems connecting to database
callback(err);
return;
}

conn.query('SELECT * FROM items', function (err, computerData) {

callback(err, computerData);

});
});
};

module.exports.add = function(conData, itemData, callback){
db.connect(conData, function(err, conn){

if (err) { //return error if there were problems connecting to database
callback(err);
return;
} //insert a new item where the fields equal the value.
conn.query("INSERT INTO items (userID, title, description, location, item_condition, image, mobile, price) VALUES (\""+itemData.userID+"\", \""+itemData.title+"\", \""+itemData.description+"\", \""+itemData.location+"\", \""+itemData.condition+"\", \""+itemData.image+"\", \""+itemData.mobile+"\", \""+itemData.price+"\");", itemData, function(err,result){

callback(err, result);
});
});
};

module.exports.remove = function(conData, deleteData, callback){
db.connect(conData, function(err, conn){

if (err) { //return error if there were problems connecting to database
callback(err);
return;
}

conn.query('DELETE FROM items WHERE itemID= "' + deleteData.itemID + '"', function (err, result){ //delete item related to the ItemID

callback(err, result);
});

});
};

0 comments on commit b9d846e

Please sign in to comment.