diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/data/exam.json | 60 | ||||
| -rw-r--r-- | test/mocha.opts | 4 | ||||
| -rw-r--r-- | test/test.js | 186 |
3 files changed, 232 insertions, 18 deletions
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(); + }); + }); + + +}); + |
