Skip to content

Commit 7d71708

Browse files
committed
Auth Tests
1 parent 9112823 commit 7d71708

File tree

12 files changed

+140
-73
lines changed

12 files changed

+140
-73
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ template(name="bunyan" type="string"
140140
node_modules/bunyan/bin/bunyan /var/log/myapp.log
141141
```
142142

143+
### Tests
144+
```sh
145+
$> npm run test
146+
```
147+
143148
# Official Laravel Echo Server Doc 1.5.7
144149

145150

laravel-echo-server.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@
5252
"mock": {
5353
"laravel_port": 7718
5454
}
55-
}
55+
},
56+
"console_log": true
5657
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"scripts": {
2020
"build": "tsc",
2121
"dev": "tsc -w",
22-
"prepublish": "npm run build"
22+
"prepublish": "npm run build",
23+
"test": "node_modules/.bin/_mocha --exit dist/test/auth/index.js "
2324
},
2425
"dependencies": {
2526
"bunyan": "^1.8.12",

src/channels/baseAuthChannel.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export class BaseAuthChannel {
152152
* Prepare headers for request to app server.
153153
*/
154154
protected prepareHeaders(socket: any): any {
155+
155156
let headers = {};
156157

157158
headers['Cookie'] = socket.request.headers.cookie;
@@ -161,9 +162,15 @@ export class BaseAuthChannel {
161162

162163
if (socket.request.headers['Authorization']) {
163164
headers['Authorization'] = socket.request.headers['Authorization']
165+
Log.success("Bearer Authorization");
166+
} else if (socket.request.headers['authorization']) {
167+
headers['Authorization'] = socket.request.headers['authorization']
168+
Log.success("Bearer Authorization");
164169
} else if (socket.request._query.token) {
165170
headers['Authorization'] = ' Bearer ' + socket.request._query.token;
171+
Log.success("Query Token Authorization");
166172
} else {
173+
Log.success("JWT_TOKEN Cookie Authorization");
167174
const cookies = cookie.parse(socket.request.headers.cookie);
168175
headers['Authorization'] = ' Bearer ' + cookies['jwt_token'];
169176
}

src/cli/cli.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,20 +317,8 @@ export class Cli {
317317
process.on('SIGHUP', process.exit);
318318
process.on('SIGTERM', process.exit);
319319

320-
if(process.env['LARAVEL_ECHO_SERVER_TEST']){
321-
options.authHost = `http://localhost:${options.dev.mock.laravel_port}`;
322-
options.devMode = true;
323-
options.log = "file";
320+
echo.run(options);
324321

325-
Log.info('Running TEST MODE');
326-
327-
let mock = new MockLaravel(options);
328-
mock.run();
329-
echo.run(options);
330-
} else {
331-
332-
echo.run(options);
333-
}
334322
});
335323
});
336324
}

src/default-options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const options = {
1212
"databasePath": "/dist/database/laravel-echo-server.sqlite"
1313
}
1414
},
15-
"devMode": false,
15+
"devMode": true,
1616
"testMode": false,
1717
"host": null,
1818
"port": 6001,
@@ -47,5 +47,6 @@ export const options = {
4747
"mock": {
4848
"laravel_port": 7718
4949
}
50-
}
50+
},
51+
"console_log": true
5152
};

src/echo-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class EchoServer {
222222

223223
socket.user_id = auth.channel_data.user_id;
224224

225-
Log.success(`User Id:${socket.user_id} AUTH Success ON NSP / Channel AKA Root Channel SocketID: ${socket.id}`);
225+
Log.success(`User Id:${socket.user_id} AUTH Success ON NSP / SocketID: ${socket.id}`);
226226
this.log.info(`User Id:${socket.user_id} with Socket:${socket.id} Auth Success ON NSP / Root Channel`);
227227
return this.startSubscribers(socket);
228228

src/log.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class Log {
2828
* @return {void}
2929
*/
3030
static title(message: any, force: boolean = false): void {
31+
if(options.console_log == false) return;
3132
if(force) {
3233
console.log(colors.bold(message));
3334
return;
@@ -45,7 +46,7 @@ export class Log {
4546
* @return {void}
4647
*/
4748
static subtitle(message: any, force: boolean = false): void {
48-
49+
if(options.console_log == false) return;
4950
if(force) {
5051
console.log(colors.h2.bold(message));
5152
return;
@@ -63,6 +64,7 @@ export class Log {
6364
* @return {void}
6465
*/
6566
static info(message: any, force: boolean = false): void {
67+
if(options.console_log == false) return;
6668
if(force) {
6769
console.log(colors.info(message));
6870
return;
@@ -80,6 +82,7 @@ export class Log {
8082
* @return {void}
8183
*/
8284
static success(message: any, force: boolean = false): void {
85+
if(options.console_log == false) return;
8386
if(force) {
8487
console.log(colors.green('\u2714 '), message);
8588
return;
@@ -98,6 +101,7 @@ export class Log {
98101
* @return {void}
99102
*/
100103
static error(message: any, force: boolean = false): void {
104+
if(options.console_log == false) return;
101105
if(force) {
102106
console.log(colors.error(message));
103107
return;
@@ -114,6 +118,7 @@ export class Log {
114118
* @return {void}
115119
*/
116120
static warning(message: any, force: boolean = false): void {
121+
if(options.console_log == false) return;
117122
if(force) {
118123
console.log(colors.warn('\u26A0 ' + message));
119124
return;

src/test/auth/index.ts

Lines changed: 108 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,150 @@
11
import {describe, before, after, it} from "mocha";
2-
//import {Log} from "../../log";
3-
import {MockLaravel} from "../mock_laravel";
4-
5-
const Echo = require("laravel-echo")
2+
import {expect} from "chai";
63

7-
const io = require('socket.io-client');
4+
import {MockLaravel} from "../mock_laravel";
85

9-
const mocha = require('mocha');
10-
const chai = require('chai');
11-
const expect = require('expect');
6+
const Echo = require("laravel-echo");
127

8+
let io = require('socket.io-client');
139

1410
const echo = require('../../index');
1511
const options = echo.defaultOptions;
1612

17-
options.authHost = `http://localhost:${options.dev.mock.laravel_port}`;
18-
options.devMode = true;
19-
options.log = "file";
13+
options.authHost = `http://localhost:${options.dev.mock.laravel_port}`;
14+
options.devMode = true;
15+
options.log = "file";
16+
options.console_log = false;
2017

2118
const mockLaravel = new MockLaravel(options);
2219

20+
describe('basic socket.io example', function () {
2321

24-
describe('basic socket.io example', function() {
25-
26-
var socket;
27-
22+
/** setup echo server and mock Laravel Server*/
2823
before(function (done) {
2924
echo.run(options).then(() => {
30-
mockLaravel.run().then(() => {
25+
mockLaravel.run().then(() => {
3126
return done();
3227
})
3328
})
3429
});
3530

31+
/** stop mock Laravel Server*/
3632
after(function (done) {
37-
//echo.stop();
3833
mockLaravel.stop();
3934
return done();
4035
});
4136

4237

43-
it('should Fail Auth and Disconnected', done => {
38+
it('should Fail Authenticate with Bearer Token Header and should be disconnected', done => {
4439

45-
let echo_client = new Echo({
46-
broadcaster: 'socket.io',
47-
host: 'http://localhost:4000',
48-
auth: {headers: {Authorization: 'Bearer ' + 'foo' }},
49-
client: io
40+
let echo_client = new Echo({
41+
broadcaster: 'socket.io',
42+
host: 'http://localhost:4000',
43+
client: io,
44+
transportOptions: {
45+
polling: {
46+
extraHeaders: {
47+
'Authorization': 'Bearer 11'
48+
}
49+
}
50+
}
51+
});
52+
53+
echo_client.connector.socket.on('disconnect', () => {
54+
expect(echo_client.connector.socket.connected).to.be.false;
55+
done();
56+
});
5057
});
5158

52-
/*echo_client.connector.socket.on('connect', () => {
59+
it('should Authenticate Correctly with Bearer Token Header', done => {
60+
61+
let echo_client = new Echo({
62+
broadcaster: 'socket.io',
63+
host: 'http://localhost:4000',
64+
client: io,
65+
transportOptions: {
66+
polling: {
67+
extraHeaders: {
68+
'Authorization': 'Bearer 1'
69+
}
70+
}
71+
}
72+
});
73+
74+
echo_client.connector.socket.on('connect', () => {
75+
expect(echo_client.connector.socket.connected).to.be.true;
76+
done();
77+
});
78+
})
5379

54-
});*/
80+
it('should Fail Authenticate with Query Token', done => {
5581

56-
echo_client.connector.socket.on('disconnect', () => {
57-
done();
58-
});
59-
})
60-
61-
/*
62-
beforeEach(function(done) {
63-
// Setup
64-
socket = io.connect('http://localhost:4000', {
65-
'reconnection delay' : 0
66-
, 'reopen delay' : 0
67-
, 'force new connection' : true
68-
, transports: ['websocket']
82+
let echo_client = new Echo({
83+
broadcaster: 'socket.io',
84+
host: 'http://localhost:4000?token=332',
85+
client: io,
6986
});
7087

71-
socket.on('connect', () => {
88+
echo_client.connector.socket.on('disconnect', () => {
89+
expect(echo_client.connector.socket.connected).to.be.false;
7290
done();
7391
});
92+
})
7493

75-
socket.on('disconnect', () => {
76-
// console.log('disconnected...');
94+
it('should Authenticate Correctly with Query Token', done => {
95+
96+
let echo_client = new Echo({
97+
broadcaster: 'socket.io',
98+
host: 'http://localhost:4000?token=2',
99+
client: io,
77100
});
78-
});*/
79-
/* it('should communicate', (done) => {
80-
// once connected, emit Hello World
81-
io_server.emit('echo', 'Hello World');
82-
83-
socket.once('echo', (message) => {
84-
// Check that the message matches
85-
expect(message).to.equal('Hello World');
101+
102+
echo_client.connector.socket.on('connect', () => {
103+
expect(echo_client.connector.socket.connected).to.be.true;
86104
done();
87105
});
106+
})
107+
108+
it('should Authenticate Correctly with Cookie Token', done => {
109+
110+
let echo_client = new Echo({
111+
broadcaster: 'socket.io',
112+
host: 'http://localhost:4000',
113+
client: io,
114+
transportOptions: {
115+
polling: {
116+
extraHeaders: {
117+
'Cookie': 'jwt_token=3'
118+
}
119+
}
120+
}
121+
});
88122

89-
io_server.on('connection', (socket) => {
90-
expect(socket).to.not.be.null;
123+
echo_client.connector.socket.on('connect', () => {
124+
expect(echo_client.connector.socket.connected).to.be.true;
125+
done();
126+
});
127+
})
128+
129+
it('should Fail Authenticate with Cookie Token', done => {
130+
131+
let echo_client = new Echo({
132+
broadcaster: 'socket.io',
133+
host: 'http://localhost:4000',
134+
client: io,
135+
transportOptions: {
136+
polling: {
137+
extraHeaders: {
138+
'Cookie': 'jwt_token=34444'
139+
}
140+
}
141+
}
142+
});
143+
144+
echo_client.connector.socket.on('disconnect', () => {
145+
expect(echo_client.connector.socket.connected).to.be.false;
146+
done();
91147
});
92-
});*/
148+
})
93149

94150
});

src/test/mock_laravel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class MockLaravel {
8989
broadcasting(req: any, res: any): any {
9090
const channel = req.body.channel_name;
9191

92-
const bearer = req.headers.authentication;
92+
const bearer = req.headers.authorization;
9393

9494
if(! bearer) return this.response401(req, res);
9595

src/test/start.mock.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ import {FsUtils} from "../utils/fsUtils";
55

66
let mock = new MockLaravel(FsUtils.getConfigfile());
77

8-
mock.init();
8+
mock.run().then(() => {
9+
console.log('LaraMock Running')
10+
})

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"src/index.ts",
1717
"src/cli/index.ts",
1818
"src/test/index.ts",
19+
"src/test/start.mock.ts",
1920
"src/test/auth/index.ts"
2021
],
2122
"exclude": [

0 commit comments

Comments
 (0)