Skip to content

Commit a38b06f

Browse files
committed
fix: tests
1 parent 6ae7f77 commit a38b06f

File tree

4 files changed

+96
-15
lines changed

4 files changed

+96
-15
lines changed

src/config/acl.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ impl AclConfig {
5656
) -> bool {
5757
// Allows everything if no topics config was found.
5858
topics_config.map_or(true, |perms| {
59-
perms.topic.iter().any(|k| {
60-
k.allowed.iter().any(|k| *k == transaction_type)
61-
&& k.filter.matches_topic(topic_name)
62-
})
59+
perms
60+
.topic
61+
.iter()
62+
.find(|k| k.filter.matches_topic(topic_name))
63+
.map_or(true, |k| {
64+
k.allowed.iter().any(|k| *k == transaction_type)
65+
|| !k.denied.iter().all(|k| *k == transaction_type)
66+
})
6367
})
6468
}
6569
}

tests/foxmq.d/acl.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[permissions.test_user1]
2+
topic = [{ filter = "test_topic", allowed = ["publish"] }]
3+
4+
5+
[permissions.test_user2]
6+
topic = [{ filter = "test_topic", allowed = ["subscribe"] }]

tests/foxmq.d/cla.toml

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/mqtt/distribution.test.js

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const events = require("node:events");
66
const timers = require("node:timers/promises");
77

88
describe("publish to node 1, receive from node2", () => {
9+
910
test("synchronously", async () => {
1011
// Test v4 (3.1.1) and v5 (5.0) simultaneously
1112
const client1 = await mqtt.connectAsync("mqtt://localhost:1883", { protocolVersion: 4 });
@@ -269,22 +270,98 @@ describe("publish to node 1, receive from node2", () => {
269270
await client2.endAsync();
270271
}),
271272
test("ACL", async () => {
273+
async function waitForEventWithTimeout(emitter, eventName, timeoutMs) {
274+
const timeout = new Promise((_, reject) =>
275+
setTimeout(() => reject(new Error(`Timeout waiting for ${eventName}`)), timeoutMs)
276+
);
277+
278+
const eventPromise = events.once(emitter, eventName);
279+
280+
return Promise.race([eventPromise, timeout]);
281+
}
282+
283+
272284
// Can publish to test_topic but not subscribe
273-
const client1 = await mqtt.connectAsync("mqtt://localhost:1884", { protocolVersion: 5, username: "test_user1", password: "1234" });
285+
const client1 = await mqtt.connectAsync("mqtt://localhost:1883", { protocolVersion: 5, username: "test_user1", password: "1234" });
274286
// Can subscribe to test_topic but not publish
275-
const client2 = await mqtt.connectAsync("mqtt://localhost:1884", { protocolVersion: 5, username: "test_user2", password: "1234" });
287+
const client2 = await mqtt.connectAsync("mqtt://localhost:1883", { protocolVersion: 5, username: "test_user2", password: "1234" });
276288
// Can do anything anywhere
277-
const client3 = await mqtt.connectAsync("mqtt://localhost:1884", { protocolVersion: 5 });
289+
const client3 = await mqtt.connectAsync("mqtt://localhost:1883", { protocolVersion: 5 });
278290

279-
await client1.publishAsync("test_topic", "a test message");
280291

281-
await client2.subscribe("test_topic");
292+
// Client 2 can sub and client 1 can publish
293+
await client2.subscribeAsync("test_topic");
294+
295+
await client1.publishAsync("test_topic", "a test message");
282296

283297
const [topic, message] = await events.once(client2, 'message');
284298

285299
expect(topic.toString()).toBe("test_topic");
286300
expect(message.toString()).toBe("a test message");
287301

302+
// Client 2 cannot publish
303+
await client2.publishAsync("test_topic", "a test message");
304+
305+
try {
306+
await waitForEventWithTimeout(client2, 'message', 100);
307+
throw new Error('Test failed: event was received unexpectedly');
308+
} catch (err) {
309+
expect(err.message).toBe('Timeout waiting for message');
310+
}
311+
312+
313+
// Client 1 cannot sub
314+
315+
await client1.subscribeAsync("test_topic");
316+
317+
await client1.publishAsync("test_topic", "a test message");
318+
319+
try {
320+
await waitForEventWithTimeout(client1, 'message', 100);
321+
throw new Error('Test failed: event was received unexpectedly');
322+
} catch (err) {
323+
expect(err.message).toBe('Timeout waiting for message');
324+
}
325+
326+
// User 3 can do anything in any topic
327+
328+
await client3.subscribeAsync("test_topic");
329+
330+
await client3.publishAsync("test_topic", "a test message");
331+
332+
const [topic3, message3] = await events.once(client3, 'message');
333+
334+
expect(topic3.toString()).toBe("test_topic");
335+
expect(message3.toString()).toBe("a test message");
336+
337+
338+
// Client 1 can do whatever in other topics.
339+
340+
await client1.subscribeAsync("test_topic1");
341+
342+
await client1.publishAsync("test_topic1", "a test message");
343+
344+
const [topic_any_1, message_any_1] = await events.once(client1, 'message');
345+
346+
expect(topic_any_1.toString()).toBe("test_topic1");
347+
expect(message_any_1.toString()).toBe("a test message");
348+
349+
350+
// Client 2 can do whatever in other topics.
351+
352+
await client2.subscribeAsync("test_topic1");
353+
354+
await client2.publishAsync("test_topic1", "a test message");
355+
356+
const [topic_any_2, message_any_2] = await events.once(client2, 'message');
357+
358+
expect(topic_any_2.toString()).toBe("test_topic1");
359+
expect(message_any_2.toString()).toBe("a test message");
360+
361+
362+
await client1.endAsync();
363+
await client2.endAsync();
364+
await client3.endAsync();
288365
})
289366
;
290367
});

0 commit comments

Comments
 (0)