aboutsummaryrefslogtreecommitdiff
path: root/test/routingDb_tests.js
blob: 998d34a147d58f3093911ca6a4075608ca5e3f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
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();
            });
    });


});