aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-27 00:03:48 +0000
committernanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-27 00:03:48 +0000
commit714368c049f8749ed22c6d1d07bb3adc783dc349 (patch)
tree8952248ebf3f85b141ef0d18ecf6a1becdc8b89b /test
parent8beb37ed67b5f2fa0f9aa85499a8f11b39c9d067 (diff)
parentb65166302f5296165da115ed96ce112433157a2d (diff)
Merge branch 'master' of https://github.com/HumairAK/solutions_repo
Diffstat (limited to 'test')
-rw-r--r--test/data/exam.json58
-rw-r--r--test/data/user.json35
-rw-r--r--test/mocha.opts3
-rw-r--r--test/routingDb_tests.js352
-rw-r--r--test/test_server.js13
5 files changed, 461 insertions, 0 deletions
diff --git a/test/data/exam.json b/test/data/exam.json
new file mode 100644
index 0000000..ac9c0dc
--- /dev/null
+++ b/test/data/exam.json
@@ -0,0 +1,58 @@
+{
+ "existing" : {
+ "_id": {
+ "$oid": "578a429640e1bf7e118a6b7b"
+ },
+ "course_code": "CSC148",
+ "year": 2010,
+ "term": "fall",
+ "type": "midterm",
+ "instructors": [
+ "Faith Ellen",
+ "Tom F."
+ ],
+ "page_count": 20,
+ "questions_count": 2,
+ "questions_list": [
+ {
+ "q_id": 1,
+ "question": "this is q1"
+ },
+ {
+ "q_id": 2,
+ "question": "this is q2"
+ }
+ ],
+ "upload_date": "some date",
+ "uploaded_by": "some user name"
+ },
+ "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/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
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..241d505
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1,3 @@
+--recursive
+--timeout 5000
+--reporter dot \ No newline at end of file
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_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