forked from RafaelGSS/fastify-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
158 lines (123 loc) · 3.8 KB
/
test.js
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
/* eslint-disable import/no-extraneous-dependencies */
'use strict';
const {promisify} = require('util')
, {test} = require('tap')
, fastify = require('fastify')
, fastifyAmqp = require('./index')
, waitABit = promisify(setTimeout);
test('localhost guest:guest to / not permitted', async subTest => {
subTest.plan(4);
const server = fastify();
subTest.teardown(() => server.close());
try {
await server.register(fastifyAmqp, {});
} catch (err) {
subTest.equal(typeof err, typeof {});
subTest.ok(err instanceof Error);
subTest.ok(err.message, 'Handshake terminated by server: 403 (ACCESS-REFUSED) with message "ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.');
subTest.notOk(server.amqp, 'Should not has amqp');
}
});
test('localhost me:me to / permitted', async subTest => {
subTest.plan(1);
const server = fastify();
subTest.teardown(() => server.close());
try {
await server.register(fastifyAmqp, {
'username': 'me',
'password': 'me'
});
} catch (err) {
subTest.fail('Should connect with these attributes!');
}
const {amqp} = server;
subTest.ok(amqp);
});
test('localhost me:me to / publish with channel and he cant', async subTest => {
subTest.plan(6);
const server = fastify();
try {
await server.register(fastifyAmqp, {
'username': 'me',
'password': 'me',
'connectionHandlers': {
'close': () => {
subTest.pass('Everything is fine');
}
},
'channelHandlers': {
'error': (hostname, aVhost, err) => {
const {message} = err;
subTest.ok(hostname, 'localhost');
subTest.ok(aVhost, '/');
subTest.ok(message, 'Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no exchange \'not-exists\' in vhost \'/\'"');
},
'close': () => {
subTest.pass('Everything is fine');
}
}
});
} catch (err) {
subTest.fail('Should connect with these attributes!');
}
const {amqp} = server;
subTest.ok(amqp);
const {'/': rootVh} = amqp;
const aChannel = await rootVh.createChannel();
aChannel.publish('not-exists', 'none', Buffer.from([1]));
await waitABit(1000);
server.close();
await waitABit(1000);
});
test('localhost me:me to / publish with confirm channel and he cant', async subTest => {
subTest.plan(6);
const server = fastify();
try {
await server.register(fastifyAmqp, {
'username': 'me',
'password': 'me',
'connectionHandlers': {
'close': () => {
subTest.pass('Everything is fine');
}
},
'channelHandlers': {
'error': (hostname, aVhost, err) => {
const {message} = err;
subTest.ok(hostname, 'localhost');
subTest.ok(aVhost, '/');
subTest.ok(message, 'Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no exchange \'not-exists\' in vhost \'/\'"');
},
'close': () => {
subTest.pass('Everything is fine');
}
}
});
} catch (err) {
subTest.fail('Should connect with these attributes!');
}
const {amqp} = server;
subTest.ok(amqp);
const {'/': rootVh} = amqp;
const aChannel = await rootVh.createConfirmChannel();
aChannel.publish('not-exists', 'none', Buffer.from([1]));
await waitABit(1000);
server.close();
await waitABit(1000);
});
test('localhost me:me to / permitted (specified)', async subTest => {
subTest.plan(1);
const app = fastify();
subTest.teardown(() => app.close());
try {
await app.register(fastifyAmqp, {
'username': 'me',
'password': 'me',
'vhost': '/'
});
} catch (err) {
subTest.fail('Should connect with these attributes!');
}
const {amqp} = app;
subTest.ok(amqp);
});