Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
92 changed files
with
25,296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const app = require('../app') | ||
const request = require('supertest') | ||
|
||
const databaseData = { | ||
host: 'localhost', | ||
user: 'root', | ||
password: '55', | ||
database: 'acerreseller' | ||
}; | ||
|
||
describe("Computer", () => { | ||
|
||
test('/computers should return 201', (done) => { | ||
request(app).get('/api/v1.0/computers').then((response) => { | ||
expect(response.statusCode).toBe(201) | ||
done(); | ||
}) | ||
}); | ||
}) | ||
|
||
|
||
describe("purchase", () => { | ||
|
||
const itemData = { | ||
userID: 5, | ||
title: "acer aspire 3", | ||
device: "laptop", | ||
type: "Gaming", | ||
CPU: "i7", | ||
RAM: "16GB", | ||
GPU: "GTX 1080", | ||
storage: "SSD", | ||
size: "500GB", | ||
price: 500 | ||
} | ||
|
||
|
||
test('/purchase should return 201', async () => { | ||
const response = await request(app).post('/api/v1.0/purchase') | ||
.send(itemData) | ||
expect(response.statusCode).toBe(201) | ||
}) | ||
}) | ||
|
||
describe("User", () => { | ||
let userData = { | ||
firstName: "Skengdo", | ||
surname: "jaabir", | ||
email: "twobunny@hotmail.co.uk", | ||
username: "code", | ||
password: "Password01." | ||
} | ||
|
||
test('/purchase should return 201', async () => { | ||
const response = await request(app).post('/api/v1.0/users') | ||
.send(userData) | ||
expect(response.statusCode).toBe(201) | ||
}) | ||
|
||
test('/users should return 201', (done) => { | ||
request(app).get('/api/v1.0/users').then((response) => { | ||
expect(response.statusCode).toBe(201) | ||
done(); | ||
}) | ||
}); | ||
|
||
}); | ||
|
||
describe("Login", () => { | ||
const loginData = { | ||
username: "abdos", | ||
password: "Password01." | ||
} | ||
test('/login should return 401', async () => { | ||
const response = await request(app).post('/api/v1.0/login') | ||
expect(response.statusCode).toBe(401) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const con = require('../database') | ||
|
||
const connData = { | ||
host: 'localhost', | ||
user: 'root', | ||
password: '', | ||
database: 'acerreseller' | ||
}; | ||
|
||
describe('Database', () => { | ||
|
||
test('Connect to database', done => { | ||
con.connect(connData, (err) => { | ||
expect(err).toBeNull() | ||
done() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const user = require('./models/user/user'); | ||
const items = require('./models/items/items'); | ||
const authentication = require('./models/authentication/authentication') | ||
const db = require('./database'); | ||
|
||
const express = require('express'); | ||
const cors = require('cors'); | ||
const bodyParser = require('body-parser'); | ||
const app = express(); | ||
const auth = require('basic-auth'); | ||
|
||
app.use(bodyParser.json()) | ||
app.use(cors()); | ||
app.options('*', cors()); | ||
app.use(express.static('public')); | ||
|
||
const databaseData = { | ||
host: 'localhost', | ||
user: 'root', | ||
password: '', | ||
database: 'acerreseller' | ||
}; | ||
|
||
app.get('/api/v1.0/computers', (req, res) => { | ||
|
||
items.getAll(databaseData, function (err, computerData) { | ||
|
||
res.status(201); | ||
res.json(computerData) | ||
}) | ||
}); | ||
|
||
app.post('/api/v1.0/purchase', (req, res) => { | ||
|
||
let itemData = { | ||
userID: req.body['userID'], | ||
title: req.body['title'], | ||
device: req.body['device'], | ||
type: req.body['type'], | ||
CPU: req.body['CPU'], | ||
RAM: req.body['RAM'], | ||
GPU: req.body['GPU'], | ||
storage: req.body['storage'], | ||
size: req.body['size'], | ||
price: req.body['price'] | ||
} | ||
|
||
items.add(databaseData, itemData, function () { | ||
|
||
res.status(201); | ||
res.end(JSON.stringify({ message: "purchases added successfully" })); | ||
}) | ||
}); | ||
|
||
|
||
app.post('/api/v1.0/users', (req, res) => { | ||
|
||
let userData = { | ||
firstName: req.body['firstName'], | ||
surname: req.body['surname'], | ||
email: req.body['email'], | ||
username: req.body['username'], | ||
password: req.body['password'] | ||
} | ||
|
||
user.add(databaseData, userData, function () { | ||
|
||
res.status(201); | ||
res.end(JSON.stringify({ message: "user added successfully" })); | ||
}) | ||
|
||
|
||
|
||
}); | ||
|
||
app.get('/api/v1.0/users', (req, res) => { | ||
|
||
user.getAll(databaseData, function (err, result) { | ||
|
||
res.status(201); | ||
res.json(result) | ||
}) | ||
}); | ||
|
||
|
||
|
||
app.post('/api/v1.0/login', (req, res) => { | ||
|
||
const loginData = auth(req); | ||
|
||
authentication.loginUser(databaseData, req, loginData, (err, result) => { | ||
|
||
if (err) { | ||
res.status(err.code); | ||
res.end(JSON.stringify(err)) | ||
return; | ||
} | ||
res.end(JSON.stringify(result)) | ||
}); | ||
}); | ||
|
||
module.exports = app; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const db = require('../../database') | ||
|
||
module.exports.loginUser = function (conData, request, loginData, callback) { | ||
|
||
if (request.headers === undefined || request.headers.authorization === undefined) { | ||
|
||
const err = { | ||
message: 'Authorization header missing', | ||
code: 401 | ||
}; | ||
callback(err); | ||
return; | ||
} | ||
|
||
if (loginData === undefined || loginData.name === undefined || loginData.pass === undefined) { | ||
|
||
const err = { | ||
message: 'missing username or password', | ||
code: 401 | ||
}; | ||
callback(err); | ||
return; | ||
} | ||
|
||
db.connect(conData, function (err, data) { | ||
|
||
if (err) { | ||
err.code = 500; | ||
callback(err); | ||
return; | ||
} | ||
|
||
data.query('SELECT userID FROM user WHERE username="' + loginData.name + '" AND password= "' + loginData.pass + '"', function (err, result) { | ||
|
||
if (err) { | ||
err.code = 500; | ||
callback(err); | ||
return; | ||
} | ||
|
||
|
||
if (result && result.length > 0) { | ||
callback(null, { userID: result[0].userID }); | ||
} | ||
else { | ||
const err = { | ||
message: 'wrong username or password', | ||
code: 401 | ||
}; | ||
callback(err); | ||
} | ||
|
||
|
||
}); | ||
}); | ||
|
||
} | ||
|
||
|
Oops, something went wrong.