aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--node_simple.js2
-rw-r--r--routes/index.js70
-rw-r--r--routes/user.js27
-rw-r--r--test/data/exam.json60
-rw-r--r--test/mocha.opts4
-rw-r--r--test/test.js186
-rw-r--r--views/index.hbs5
7 files changed, 290 insertions, 64 deletions
diff --git a/node_simple.js b/node_simple.js
index 228e861..dced853 100644
--- a/node_simple.js
+++ b/node_simple.js
@@ -599,7 +599,6 @@ exports.retrieveUser = function (username, callback) {
}
else if (result.length) {
- console.log("retreive User: " + result[0]);
callback(true, false, result[0], "User retrieved");
}
@@ -1229,7 +1228,6 @@ exports.get_exam_byID = function (id, callback) {
}
else {
callback(true, false, docs[0]);
- console.log("DOCS: "+ docs);
}
});
})
diff --git a/routes/index.js b/routes/index.js
index cb91765..54bf058 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -14,6 +14,7 @@ router.get('/', function(req, res, next) {
res.render('index', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors});
req.session.errors = null;
req.session.success = null;
+ req.session.messages = null;
});
@@ -62,7 +63,6 @@ router.get('/exams/', function(req,res,next){
req.checkParams('id','Course code should be between 6').notEmpty().withMessage('Course code required').isLength({min: 6, max: 6});
var errors = req.validationErrors();
if (errors){
- console.log(errors);
req.session.errors = errors;
req.session.success = false;
res.redirect('/');
@@ -153,45 +153,45 @@ router.get('/search/:type', function(req, res, next) {
* comments = number of comments*/
router.get('/questions/:exam_id', function (req,res) {
var examID = req.params.exam_id;
- console.log(examID);
dbFile.get_exam_byID(examID, function(success, error, exam){
- /* [
- { q_id: 1, question: 'this is q1' },
- { q_id: 2, question: 'this is q2' }
- ]
- */
- var qList = exam.questions_list;
-
- // Add comments/solutions
- dbFile.get_exam_info_by_ID(examID, function (questionsInfo) {
- qList.forEach(function(question){
- question.count = 0;
- question.comments = 0;
-
- // Find q_id in questionsInfo, update comment/solutions count
- questionsInfo.forEach(function(q){
- if (question.q_id == q._id){
- question.count += q.count;
- question.comments += q.comments;
- }
+ if(success && exam){
+ var qList = exam.questions_list;
+ // Add comments/solutions
+ dbFile.get_exam_info_by_ID(examID, function (questionsInfo) {
+ qList.forEach(function(question){
+ question.count = 0;
+ question.comments = 0;
+
+ // Find q_id in questionsInfo, update comment/solutions count
+ questionsInfo.forEach(function(q){
+ if (question.q_id == q._id){
+ question.count += q.count;
+ question.comments += q.comments;
+ }
+ });
});
+
+ var examInfo = {
+ id : exam._id,
+ courseCode : exam.course_code,
+ term : toProperCase(exam.term),
+ type : toProperCase(exam.type),
+ year : exam.year,
+ instructors : exam.instructors.join(),
+ uploadDate : exam.upload_date,
+ uploader : exam.uploaded_by,
+ pageCount : exam.page_count,
+ questionCount : exam.questions_count
+ };
+ res.render('questions', {query: qList, examInfo: examInfo});
});
+ }else{
+ req.session.messages = {error : "Could not find exam."};
+ res.redirect('/');
+ }
+
- var examInfo = {
- id : exam._id,
- courseCode : exam.course_code,
- term : toProperCase(exam.term),
- type : toProperCase(exam.type),
- year : exam.year,
- instructors : exam.instructors.join(),
- uploadDate : exam.upload_date,
- uploader : exam.uploaded_by,
- pageCount : exam.page_count,
- questionCount : exam.questions_count
- };
- res.render('questions', {query: qList, examInfo: examInfo});
- });
});
});
diff --git a/routes/user.js b/routes/user.js
index 39ffbf1..6e9815e 100644
--- a/routes/user.js
+++ b/routes/user.js
@@ -312,20 +312,29 @@ router.post('/comment/submit/:examID/:qID/:solID', function(req, res, next){
}
});
-router.post('/solution/vote/:examID/:qID/:solID', loggedIn, function(req, res, next){
+router.post('/solution/vote/:examID/:qID/:solID', function(req, res, next){
var vote = req.body.vote;
var examID = req.params.examID;
var qID = req.params.qID;
var solutionID = req.params.solID;
- dbFile.vote_solution(solutionID, vote, function(voteCounted, statusMsg){
- if(voteCounted){
- console.log("Success!");
- }else{
- console.log("Action failed!");
- }
- console.log(statusMsg); // Change to display message above
+
+ if(req.isAuthenticated()){
+ dbFile.vote_solution(solutionID, vote, function(voteCounted, statusMsg){
+ if(voteCounted){
+ res.redirect('/solutions/' + examID + '/' + qID);
+ }else{
+ req.session.messages = {error : statusMsg};
+ res.redirect('/solutions/' + examID + '/' + qID);
+ }
+ });
+ } else { //User not logged in
+ var message = "Must be logged in to Vote!";
+ req.session.messages = {error : message};
res.redirect('/solutions/' + examID + '/' + qID);
- });
+ }
+
+
+
});
diff --git a/test/data/exam.json b/test/data/exam.json
new file mode 100644
index 0000000..0504679
--- /dev/null
+++ b/test/data/exam.json
@@ -0,0 +1,60 @@
+{
+ "existing" : {
+ "_id": {
+ "$oid": "57917d4d2a73c0224fb6e763"
+ },
+ "course_code": "CSC373",
+ "year": "2016",
+ "term": "Midterm",
+ "type": "Winter",
+ "instructors": [
+ "Robert Dane"
+ ],
+ "page_count": "20",
+ "questions_count": "2",
+ "questions_list": [
+ {
+ "q_id": 1,
+ "question": "Question 1"
+ },
+ {
+ "q_id": 2,
+ "question": "Question 2"
+ }
+ ],
+ "upload_date": "2005-06-10",
+ "uploaded_by": [
+ "humair",
+ ""
+ ]
+ },
+ "nonExisting" : {
+ "_id": {
+ "$oid": "999999999999999999999999"
+ },
+ "course_code": "CSC373",
+ "year": "2016",
+ "term": "Midterm",
+ "type": "Winter",
+ "instructors": [
+ "Robert Dane"
+ ],
+ "page_count": "20",
+ "questions_count": "2",
+ "questions_list": [
+ {
+ "q_id": 1,
+ "question": "Question 1"
+ },
+ {
+ "q_id": 2,
+ "question": "Question 2"
+ }
+ ],
+ "upload_date": "2005-06-10",
+ "uploaded_by": [
+ "humair",
+ ""
+ ]
+ }
+} \ No newline at end of file
diff --git a/test/mocha.opts b/test/mocha.opts
index 63b406d..241d505 100644
--- a/test/mocha.opts
+++ b/test/mocha.opts
@@ -1 +1,3 @@
---recursive \ No newline at end of file
+--recursive
+--timeout 5000
+--reporter dot \ No newline at end of file
diff --git a/test/test.js b/test/test.js
index a0fba3d..3b23916 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,20 +1,25 @@
var chai = require('chai');
var chaiHttp = require('chai-http');
var expect = require('chai').expect;
-var dbFile = require('../node_simple.js');
-
-chai.use(chaiHttp);
-
-var request = require('supertest');
+var assert = chai.assert;
+var url = require("url");
+var path = require("path");
+var fs = require('fs');
// Database info, cross check with database docs to ensure these files either:
// a) exist
// b) do not exist
-var existingUser = 'kumar'; // username must be in db
-var nonExistingUser = 'a1b2c3d4'; // username must not be in db
+var examPull = fs.readFileSync("test/data/exam.json");
+var exams = (JSON.parse(examPull));
+
+var usersPull = fs.readFileSync("test/data/user.json");
+var users = (JSON.parse(usersPull));
+
+chai.use(chaiHttp);
+var request = require('supertest');
/* Test simple routes that do not require database queries */
describe('Test Simple Route:', function () {
@@ -28,6 +33,20 @@ describe('Test Simple Route:', function () {
server.close();
});
+ /* First we test to see if a false route directs us to a 404 error
+ * this informs us that we are not sending a success code (200) at routes
+ * that do not exist. Otherwise the rest of the tests are meaningless for
+ * routing since everything would be responded with a 404 code. */
+ it('Test false route', function testSlash(done) {
+ chai.request(server)
+ .get('//foo/bar')
+ .end(function(err, res){
+ expect(res).to.have.status(404);
+ done();
+ });
+ });
+
+
it('Get Homepage', function testSlash(done) {
chai.request(server)
.get('/')
@@ -46,9 +65,48 @@ describe('Test Simple Route:', function () {
});
});
- it('Test false route', function testSlash(done) {
+});
+
+/* Public profile page response test */
+describe('User_profile_page Route:', function(){
+ var server;
+ before(function () {
+ server = require('./test_server');
+ });
+ after(function () {
+ server.close();
+ });
+
+ it('Public Profile (existing user)', function testSlash(done) {
+ var username = users.existingUser.user_name;
chai.request(server)
- .get('//foo/bar')
+ .get('/public_profile/' + username)
+ .end(function(err, res){
+ expect(res).to.have.status(200);
+
+ var path = res.res.req.path;
+ assert.equal(path, '/public_profile/' + username); // Check url path
+ done();
+ });
+ });
+
+ it('Public Profile (non-existing user)', function testSlash(done) {
+ var username = users.nonExistingUser.user_name;
+ chai.request(server)
+ .get('/public_profile/' + username)
+ .end(function(err, res){
+ expect(res).to.have.status(200);
+
+ var path = res.res.req.path;
+ assert.equal(path, '/user/' + username); // redirect to search
+ done();
+ });
+ });
+
+ // Should return 404 Since no username is specified
+ it('Public Profile (empty case)', function testSlash(done) {
+ chai.request(server)
+ .get('/public_profile/')
.end(function(err, res){
expect(res).to.have.status(404);
done();
@@ -57,8 +115,8 @@ describe('Test Simple Route:', function () {
});
-/* public_profile response test */
-describe('User_profile_page Route:', function(){
+/* Exam search response test */
+describe('Course search Route:', function(){
var server;
before(function () {
server = require('./test_server');
@@ -67,31 +125,125 @@ describe('User_profile_page Route:', function(){
server.close();
});
- it('Public Profile (existing user)', function testSlash(done) {
+ /* Ensure that path contains the code params */
+ it('Search existing exam', function testSlash(done) {
+ var code = exams.existing.course_code;
chai.request(server)
- .get('/public_profile/' + existingUser)
+ .get('/exams/' + code)
.end(function(err, res){
expect(res).to.have.status(200);
+ var path = res.res.req.path;
+ assert.equal(path, '/exams/' + code); // Check url path
done();
});
});
- it('Public Profile (non-existing user)', function testSlash(done) {
+ it('Search non-existing exam', function testSlash(done) {
+ var code = exams.nonExisting.course_code;
chai.request(server)
- .get('/public_profile/' + nonExistingUser)
+ .get('/exams/' + code)
.end(function(err, res){
expect(res).to.have.status(200);
+ var path = res.res.req.path;
+ assert.equal(path, '/exams/' + code);
done();
});
});
- it('Public Profile (empty case)', function testSlash(done) {
+ // Test check for empty search. Should be re-directed to homepage
+ it('Search empty case', function testSlash(done) {
chai.request(server)
- .get('/public_profile/' + nonExistingUser)
+ .get('/exams/')
.end(function(err, res){
expect(res).to.have.status(200);
+ var path = res.res.req.path;
+ assert.equal(path, '/'); // re-direct to home page
done();
});
});
+
+
});
+
+/* Search response test
+* Test to see if a user search directs to user route and likewise a course
+* search directs to the exam route (since it lists exams)*/
+describe('Search Route:', function(){
+ var server;
+ before(function () {
+ server = require('./test_server');
+ });
+ after(function () {
+ server.close();
+ });
+
+ /* Ensure that path contains the code params */
+ it('Path for exams', function testSlash(done) {
+ var code = exams.existing.course_code;
+ chai.request(server)
+ .get('/search/exams/' + code)
+ .end(function(err, res){
+ expect(res).to.have.status(404); // Since there is no search query
+ var path = res.res.req.path;
+ assert.equal(path, '/search/exams/' + code); // Check url path
+ done();
+ });
+ });
+
+ /* Ensure that path contains the code params */
+ it('Path for exams', function testSlash(done) {
+ var username = users.existingUser.user_name;
+ chai.request(server)
+ .get('/search/user/' + username)
+ .end(function(err, res){
+ expect(res).to.have.status(404); // Since there is no search query
+ var path = res.res.req.path;
+ assert.equal(path, '/search/user/' + username); // Check url path
+ done();
+ });
+ });
+
+
+});
+
+
+/* Questions Listing route for a particular exam */
+describe('Questions Search Route:', function(){
+ var server;
+ before(function () {
+ server = require('./test_server');
+ });
+ after(function () {
+ server.close();
+ });
+
+ it('Search questions for an existing exam', function testSlash(done) {
+ var examID = exams.existing._id.$oid;
+ chai.request(server)
+ .get('/questions/' + examID)
+ .end(function(err, res){
+ expect(res).to.have.status(200);
+ var path = res.res.req.path;
+ assert.equal(path, '/questions/' + examID); // Check url path
+ done();
+ });
+ });
+
+ it.only('Search questions for a non existing exam', function testSlash(done) {
+ var examID = exams.nonExisting._id.$oid;
+ chai.request(server)
+ .get('/questions/' + examID)
+ .end(function(err, res){
+ expect(res).to.have.status(200);
+ var path = res.res.req.path;
+
+ // Redirected to homepage (with err msg)
+ assert.equal(path, '/');
+ done();
+ });
+ });
+
+
+});
+
diff --git a/views/index.hbs b/views/index.hbs
index 6dffef9..214695b 100644
--- a/views/index.hbs
+++ b/views/index.hbs
@@ -9,6 +9,11 @@
<hr> <!-- Divider -->
<div class="search col-md-8 col-lg-8 col-md-offset-2">
<!-- Search-bar, Drop-down and Prompt -->
+ {{# if messages.error }}
+ <section class="alert alert-danger custom-Alert">
+ <h4>{{ messages.error }}</h4>
+ </section>
+ {{/if}}
{{# if success }}
<section class="alert alert-success">