From f016bdbc35b7efa44bd63413bde09433ea916c59 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Tue, 26 Jul 2016 03:43:04 -0400 Subject: added some unit testing, need a lot more --- test/mocha.opts | 1 + test/test.js | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_server.js | 13 +++++++ 3 files changed, 111 insertions(+) create mode 100644 test/mocha.opts create mode 100644 test/test.js create mode 100644 test/test_server.js (limited to 'test') diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..63b406d --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +--recursive \ No newline at end of file diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..a0fba3d --- /dev/null +++ b/test/test.js @@ -0,0 +1,97 @@ +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'); + + +// 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 + + +/* Test simple routes that do not require database queries */ +describe('Test Simple Route:', function () { + + var server; + before(function () { + server = require('./test_server'); + }); + + after(function () { + server.close(); + }); + + it('Get Homepage', function testSlash(done) { + chai.request(server) + .get('/') + .end(function(err, res){ + expect(res).to.have.status(200); + done(); + }); + }); + + it('Get About Page', function testSlash(done) { + chai.request(server) + .get('/about') + .end(function(err, res){ + expect(res).to.have.status(200); + done(); + }); + }); + + it('Test false route', function testSlash(done) { + chai.request(server) + .get('//foo/bar') + .end(function(err, res){ + expect(res).to.have.status(404); + done(); + }); + }); + +}); + +/* public_profile 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) { + chai.request(server) + .get('/public_profile/' + existingUser) + .end(function(err, res){ + expect(res).to.have.status(200); + done(); + }); + }); + + it('Public Profile (non-existing user)', function testSlash(done) { + chai.request(server) + .get('/public_profile/' + nonExistingUser) + .end(function(err, res){ + expect(res).to.have.status(200); + done(); + }); + }); + + it('Public Profile (empty case)', function testSlash(done) { + chai.request(server) + .get('/public_profile/' + nonExistingUser) + .end(function(err, res){ + expect(res).to.have.status(200); + done(); + }); + }); + +}); diff --git a/test/test_server.js b/test/test_server.js new file mode 100644 index 0000000..9514124 --- /dev/null +++ b/test/test_server.js @@ -0,0 +1,13 @@ +var app = require('../app'); +var http = require('http'); + +var server = http.createServer(app); + +var port = '3000'; +app.set('port', port); + +server.listen(port, function(){ + console.log('Listening on port 3000..'); +}); + +module.exports = server; \ No newline at end of file -- cgit v1.2.3 From 9c10936cf1420f160a5007c6baad7b25ffb6d014 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Tue, 26 Jul 2016 16:44:06 -0400 Subject: Added more unit tests, fixed some bugs in the code, fixed voting alert prompts and redirects --- test/data/exam.json | 60 +++++++++++++++++ test/mocha.opts | 4 +- test/test.js | 186 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 232 insertions(+), 18 deletions(-) create mode 100644 test/data/exam.json (limited to 'test') 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(); + }); + }); + + +}); + -- cgit v1.2.3 From fa98fa652f41a7c87fe76ab57448ab702e06c82c Mon Sep 17 00:00:00 2001 From: HumairAK Date: Tue, 26 Jul 2016 16:53:35 -0400 Subject: more unit tests --- test/test.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test.js b/test/test.js index 3b23916..dd47d48 100644 --- a/test/test.js +++ b/test/test.js @@ -207,7 +207,6 @@ describe('Search Route:', function(){ }); - /* Questions Listing route for a particular exam */ describe('Questions Search Route:', function(){ var server; @@ -230,7 +229,7 @@ describe('Questions Search Route:', function(){ }); }); - it.only('Search questions for a non existing exam', function testSlash(done) { + it('Search questions for a non existing exam', function testSlash(done) { var examID = exams.nonExisting._id.$oid; chai.request(server) .get('/questions/' + examID) @@ -244,6 +243,23 @@ describe('Questions Search Route:', function(){ }); }); + /* This differs from the earlier test because it will throw an error in db + * query and not return an exam at all, whereas before we pass in a + * legitimate hashed format id. Here we pass just random letters. + */ + it.only('Search questions for gibberish id', function testSlash(done) { + chai.request(server) + .get('/questions/batman') + .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(); + }); + }); + }); -- cgit v1.2.3 From 9f65445e7fcd5cc5d9d3183cb280b1ba13ac30b7 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Tue, 26 Jul 2016 17:31:27 -0400 Subject: more unit tests --- test/data/exam.json | 28 +++++++++++++--------------- test/test.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 20 deletions(-) (limited to 'test') diff --git a/test/data/exam.json b/test/data/exam.json index 0504679..ac9c0dc 100644 --- a/test/data/exam.json +++ b/test/data/exam.json @@ -1,32 +1,30 @@ { "existing" : { "_id": { - "$oid": "57917d4d2a73c0224fb6e763" + "$oid": "578a429640e1bf7e118a6b7b" }, - "course_code": "CSC373", - "year": "2016", - "term": "Midterm", - "type": "Winter", + "course_code": "CSC148", + "year": 2010, + "term": "fall", + "type": "midterm", "instructors": [ - "Robert Dane" + "Faith Ellen", + "Tom F." ], - "page_count": "20", - "questions_count": "2", + "page_count": 20, + "questions_count": 2, "questions_list": [ { "q_id": 1, - "question": "Question 1" + "question": "this is q1" }, { "q_id": 2, - "question": "Question 2" + "question": "this is q2" } ], - "upload_date": "2005-06-10", - "uploaded_by": [ - "humair", - "" - ] + "upload_date": "some date", + "uploaded_by": "some user name" }, "nonExisting" : { "_id": { diff --git a/test/test.js b/test/test.js index dd47d48..2105f02 100644 --- a/test/test.js +++ b/test/test.js @@ -8,8 +8,8 @@ var fs = require('fs'); // Database info, cross check with database docs to ensure these files either: -// a) exist -// b) do not exist +// We test get requests here mostly as those are the core functions of the +// website that do not alter the database. var examPull = fs.readFileSync("test/data/exam.json"); var exams = (JSON.parse(examPull)); @@ -19,8 +19,6 @@ 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 () { @@ -247,7 +245,7 @@ describe('Questions Search Route:', function(){ * query and not return an exam at all, whereas before we pass in a * legitimate hashed format id. Here we pass just random letters. */ - it.only('Search questions for gibberish id', function testSlash(done) { + it('Search questions for gibberish id', function testSlash(done) { chai.request(server) .get('/questions/batman') .end(function(err, res){ @@ -263,3 +261,46 @@ describe('Questions Search Route:', function(){ }); +/* Solutions listing for a particular exam question +* Pre-condition: Assume existing exam in db has at least question 1*/ +describe('Solutions Route:', function(){ + var server; + before(function () { + server = require('./test_server'); + }); + after(function () { + server.close(); + }); + + + it('Search solutions for an existing exam', function testSlash(done) { + var qID = 1; //Check the first question + var examID = exams.existing._id.$oid; + chai.request(server) + .get('/solutions/' + examID + '/' + qID) + .end(function(err, res){ + expect(res).to.have.status(200); + // Check for expected url path + var path = res.res.req.path; + assert.equal(path, '/solutions/' + examID + '/' + qID); + done(); + }); + }); + + it.only('Search solutions for a non existing exam', function testSlash(done) { + var qID = 1; //Check the first question + var examID = exams.nonExisting._id.$oid; + chai.request(server) + .get('/solutions/' + examID + '/' + qID) + .end(function(err, res){ + expect(res).to.have.status(200); + // Check for expected url path + var path = res.res.req.path; + assert.equal(path, '/solutions/' + examID + '/' + qID); + done(); + }); + }); + + +}); + -- cgit v1.2.3 From 4a22f913740efb294c394275ab21c990e9956389 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Tue, 26 Jul 2016 18:40:47 -0400 Subject: Added more unit tests and documentation --- test/routingDb_tests.js | 352 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 306 ----------------------------------------- 2 files changed, 352 insertions(+), 306 deletions(-) create mode 100644 test/routingDb_tests.js delete mode 100644 test/test.js (limited to 'test') diff --git a/test/routingDb_tests.js b/test/routingDb_tests.js new file mode 100644 index 0000000..998d34a --- /dev/null +++ b/test/routingDb_tests.js @@ -0,0 +1,352 @@ +var chai = require('chai'); +var chaiHttp = require('chai-http'); +var expect = require('chai').expect; +var assert = chai.assert; +var url = require("url"); +var path = require("path"); +var fs = require('fs'); + +/* + * Note: To remove dots and see the detailed reports for each test, omit the + * --reporter dot flag in mocha.opts + * + * To run test simply type $mocha in the terminal. Ensure preconditions listed + * below are met. Due to db queries, if tests fail due to time out, increase + * the time out flag in mocha.opts for --timeout. + * + */ +/* PRE-CONDITION: +* +* The existing exam/user in exam.json/user.json must be in the database +* for these tests to work. This is because these routes will require database +* queries, this allows us to test core modules from the database side along +* with front end routing. We cross check the appropriate route status codes +* and parameters to ensure that the proper paths with their appropriate +* parameters are being served without errors. We also test false queries +* to ensure that we get the 404 or redirected pages as expected. +* +* Also note that port 3000 must not be in use when running these tests. +* If a new port is required, simply change the port attritube in test_server.js +* +* */ + +// We test public get requests here mostly as those are the core functions of the +// website that do not add to or update the database. + +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); + +/* Test simple routes that do not require database queries */ +describe('Test Basic Route:', function () { + + var server; + before(function () { + server = require('./test_server'); + }); + + after(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('/') + .end(function(err, res){ + expect(res).to.have.status(200); + done(); + }); + }); + + it('Get About Page', function testSlash(done) { + chai.request(server) + .get('/about') + .end(function(err, res){ + expect(res).to.have.status(200); + done(); + }); + }); + + /* Sign up page form */ + it('Get Signup Page', function testSlash(done) { + chai.request(server) + .get('/user/signup') + .end(function(err, res){ + var path = res.res.req.path; + assert.equal(path, '/user/signup'); + expect(res).to.have.status(200); + done(); + }); + }); + + /* Sin in page form */ + it('Get signin Page', function testSlash(done) { + chai.request(server) + .get('/user/signin') + .end(function(err, res){ + var path = res.res.req.path; + assert.equal(path, '/user/signin'); + expect(res).to.have.status(200); + done(); + }); + }); +}); + +/* Public profile page response test */ +describe('User profile page (public) 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('/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(); + }); + }); + +}); + +/* Exam search response test */ +describe('Course search Route:', function(){ + var server; + before(function () { + server = require('./test_server'); + }); + after(function () { + server.close(); + }); + + /* Ensure that path contains the code params */ + it('Search existing exam', function testSlash(done) { + var code = exams.existing.course_code; + chai.request(server) + .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('Search non-existing exam', function testSlash(done) { + var code = exams.nonExisting.course_code; + chai.request(server) + .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(); + }); + }); + + // Test check for empty search. Should be re-directed to homepage + it('Search empty case', function testSlash(done) { + chai.request(server) + .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('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(); + }); + }); + + /* This differs from the earlier test because it will throw an error in db + * query and not return an exam at all, whereas before we pass in a + * legitimate hashed format id. Here we pass just random letters. + */ + it('Search questions for gibberish id', function testSlash(done) { + chai.request(server) + .get('/questions/batman') + .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(); + }); + }); + + +}); + +/* Solutions listing for a particular exam question +* Pre-condition: Assume existing exam in db has at least question 1*/ +describe('Solutions Route:', function(){ + var server; + before(function () { + server = require('./test_server'); + }); + after(function () { + server.close(); + }); + + + it('Search solutions for an existing exam', function testSlash(done) { + var qID = 1; //Check the first question + var examID = exams.existing._id.$oid; + chai.request(server) + .get('/solutions/' + examID + '/' + qID) + .end(function(err, res){ + expect(res).to.have.status(200); + // Check for expected url path + var path = res.res.req.path; + assert.equal(path, '/solutions/' + examID + '/' + qID); + done(); + }); + }); + + + /* COME BACK TO THIS AFTER GET_ALL_SOLUTIONS IS UPDATED*/ + it('Search solutions for a non existing exam', function testSlash(done) { + var qID = 1; //Check the first question + var examID = exams.nonExisting._id.$oid; + chai.request(server) + .get('/solutions/' + examID + '/' + qID) + .end(function(err, res){ + expect(res).to.have.status(200); + // Check for expected url path + var path = res.res.req.path; + assert.equal(path, '/solutions/' + examID + '/' + qID); + done(); + }); + }); + + +}); + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 2105f02..0000000 --- a/test/test.js +++ /dev/null @@ -1,306 +0,0 @@ -var chai = require('chai'); -var chaiHttp = require('chai-http'); -var expect = require('chai').expect; -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: -// We test get requests here mostly as those are the core functions of the -// website that do not alter the database. - -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); - -/* Test simple routes that do not require database queries */ -describe('Test Simple Route:', function () { - - var server; - before(function () { - server = require('./test_server'); - }); - - after(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('/') - .end(function(err, res){ - expect(res).to.have.status(200); - done(); - }); - }); - - it('Get About Page', function testSlash(done) { - chai.request(server) - .get('/about') - .end(function(err, res){ - expect(res).to.have.status(200); - 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('/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(); - }); - }); - -}); - -/* Exam search response test */ -describe('Course search Route:', function(){ - var server; - before(function () { - server = require('./test_server'); - }); - after(function () { - server.close(); - }); - - /* Ensure that path contains the code params */ - it('Search existing exam', function testSlash(done) { - var code = exams.existing.course_code; - chai.request(server) - .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('Search non-existing exam', function testSlash(done) { - var code = exams.nonExisting.course_code; - chai.request(server) - .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(); - }); - }); - - // Test check for empty search. Should be re-directed to homepage - it('Search empty case', function testSlash(done) { - chai.request(server) - .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('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(); - }); - }); - - /* This differs from the earlier test because it will throw an error in db - * query and not return an exam at all, whereas before we pass in a - * legitimate hashed format id. Here we pass just random letters. - */ - it('Search questions for gibberish id', function testSlash(done) { - chai.request(server) - .get('/questions/batman') - .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(); - }); - }); - - -}); - -/* Solutions listing for a particular exam question -* Pre-condition: Assume existing exam in db has at least question 1*/ -describe('Solutions Route:', function(){ - var server; - before(function () { - server = require('./test_server'); - }); - after(function () { - server.close(); - }); - - - it('Search solutions for an existing exam', function testSlash(done) { - var qID = 1; //Check the first question - var examID = exams.existing._id.$oid; - chai.request(server) - .get('/solutions/' + examID + '/' + qID) - .end(function(err, res){ - expect(res).to.have.status(200); - // Check for expected url path - var path = res.res.req.path; - assert.equal(path, '/solutions/' + examID + '/' + qID); - done(); - }); - }); - - it.only('Search solutions for a non existing exam', function testSlash(done) { - var qID = 1; //Check the first question - var examID = exams.nonExisting._id.$oid; - chai.request(server) - .get('/solutions/' + examID + '/' + qID) - .end(function(err, res){ - expect(res).to.have.status(200); - // Check for expected url path - var path = res.res.req.path; - assert.equal(path, '/solutions/' + examID + '/' + qID); - done(); - }); - }); - - -}); - -- cgit v1.2.3 From a546c76d5eb8dcf2efe8b305cdb997b64914728e Mon Sep 17 00:00:00 2001 From: HumairAK Date: Tue, 26 Jul 2016 18:53:36 -0400 Subject: added user test file --- test/data/user.json | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/data/user.json (limited to 'test') diff --git a/test/data/user.json b/test/data/user.json new file mode 100644 index 0000000..b8ca88d --- /dev/null +++ b/test/data/user.json @@ -0,0 +1,35 @@ +{ + "existingUser": { + "_id": { + "$oid": "57950d18543bc2fe7681f28c" + }, + "email": "kumar@kumar.com", + "user_name": "kumar", + "f_name": "KUMAR", + "l_name": "DAMANI", + "university": "", + "department": "", + "answered": 1, + "messages": 0, + "comments": 4, + "phone_num": "", + "followers": [] + }, + + "nonExistingUser": { + "_id": { + "$oid": "57950d18543bc2fe7681f28c" + }, + "email": "kumar@kumar.com", + "user_name": "kumarFake", + "f_name": "KUMAR", + "l_name": "DAMANI", + "university": "", + "department": "", + "answered": 1, + "messages": 0, + "comments": 4, + "phone_num": "", + "followers": [] + } +} \ No newline at end of file -- cgit v1.2.3