UNPKG

2.44 kBJavaScriptView Raw
1// Jasmine test for user authentication
2// requires file ./auth-data.json with login and password (not under git!)
3// Don't copy code from this, this is not the regular usage of the library.
4// This file was more used as a test and experiment code.
5
6var CodeGradX = require('../codegradxlib.js');
7var authData = require('./auth1-data.json'); // lambda student
8
9describe('CodeGradX', function () {
10 it('should be loaded', function () {
11 expect(CodeGradX).toBeDefined();
12 });
13
14 it('should send authentication request', function (done) {
15 var state = new CodeGradX.State();
16 function faildone (reason) {
17 state.debug('faildone', reason).show();
18 fail(reason);
19 done();
20 }
21 state.sendAXServer('x', {
22 path: '/direct/check',
23 method: 'POST',
24 headers: {
25 'Accept': 'application/json',
26 'Content-Type': 'application/x-www-form-urlencoded'
27 },
28 entity: authData
29 }).then(function (response) {
30 //console.log(response);
31 expect(response.status).toBeDefined();
32 expect(response.status.code).toBe(200);
33 expect(response.headers['Set-Cookie']).toBeDefined();
34 expect(response.entity.kind).toBe('authenticationAnswer');
35 //console.log(state.currentCookie);
36 expect(state.currentCookie).toBeDefined();
37 expect(state.currentCookie.length).toBeGreaterThan(0);
38 state.currentUser = new CodeGradX.User(response.entity);
39 expect(state.currentUser.lastname).toBe('FW4EX');
40 expect(CodeGradX.getCurrentState()).toBe(state);
41 state.sendAXServer('x', {
42 path: '/',
43 method: 'GET'
44 }).then(function (response) {
45 //console.log(response);
46 // Check that the received cookie is sent
47 expect(response.raw.request._header).toMatch(/\r\nCookie: u=U/);
48 expect(response.entity.kind).toBe('authenticationAnswer');
49 done();
50 }, faildone);
51 }, faildone);
52 }, 10*1000); // 10 seconds
53
54 it('again with getAuthenticatedUser', function (done) {
55 var state = new CodeGradX.State();
56 function faildone (reason) {
57 state.debug(reason).show();
58 fail(reason);
59 done();
60 }
61 state.getAuthenticatedUser(authData.login, authData.password)
62 .then(function (user) {
63 //console.log(user);
64 expect(user).toBeDefined();
65 expect(user.lastname).toBe('FW4EX');
66 expect(user).toBe(state.currentUser);
67 done();
68 }, faildone);
69 });
70
71});