2023-08-14 18:51:46 +08:00
|
|
|
const assert = require('assert')
|
2025-05-07 23:38:31 +08:00
|
|
|
const { log, tokenFor, openIdConfiguration } = require('../utils')
|
2023-12-22 21:54:34 +08:00
|
|
|
const { reset, expectUser, expectVhost, expectResource, allow, verifyAll } = require('../mock_http_backend')
|
2024-11-27 17:40:23 +08:00
|
|
|
const { open: openAmqp, once: onceAmqp, on: onAmqp, close: closeAmqp } = require('../amqp')
|
|
|
|
|
|
|
|
var receivedAmqpMessageCount = 0
|
|
|
|
var untilConnectionEstablished = new Promise((resolve, reject) => {
|
|
|
|
onAmqp('connection_open', function(context) {
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
onAmqp('message', function (context) {
|
|
|
|
receivedAmqpMessageCount++
|
|
|
|
})
|
|
|
|
onceAmqp('sendable', function (context) {
|
|
|
|
context.sender.send({body:'first message'})
|
|
|
|
})
|
2023-08-14 18:51:46 +08:00
|
|
|
|
|
|
|
const profiles = process.env.PROFILES || ""
|
|
|
|
var backends = ""
|
|
|
|
for (const element of profiles.split(" ")) {
|
|
|
|
if ( element.startsWith("auth_backends-") ) {
|
|
|
|
backends = element.substring(element.indexOf("-")+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 21:54:34 +08:00
|
|
|
describe('Having AMQP 1.0 protocol enabled and the following auth_backends: ' + backends, function () {
|
|
|
|
let expectations = []
|
2023-12-27 22:03:26 +08:00
|
|
|
let username = process.env.RABBITMQ_AMQP_USERNAME
|
|
|
|
let password = process.env.RABBITMQ_AMQP_PASSWORD
|
2025-02-25 19:50:58 +08:00
|
|
|
let usemtls = process.env.AMQP_USE_MTLS
|
2024-11-27 17:40:23 +08:00
|
|
|
let amqp;
|
|
|
|
|
2025-02-25 19:50:58 +08:00
|
|
|
before(function () {
|
|
|
|
if (backends.includes("http") && (username.includes("http") || usemtls)) {
|
2023-12-22 21:54:34 +08:00
|
|
|
reset()
|
2025-02-25 19:50:58 +08:00
|
|
|
if (!usemtls) {
|
|
|
|
expectations.push(expectUser({ "username": username, "password": password}, "allow"))
|
|
|
|
} else {
|
|
|
|
expectations.push(expectUser({ "username": username}, "allow"))
|
|
|
|
}
|
2023-12-27 22:03:26 +08:00
|
|
|
expectations.push(expectVhost({ "username": username, "vhost": "/"}, "allow"))
|
|
|
|
expectations.push(expectResource({ "username": username, "vhost": "/", "resource": "queue", "name": "my-queue", "permission":"configure", "tags":""}, "allow"))
|
|
|
|
expectations.push(expectResource({ "username": username, "vhost": "/", "resource": "queue", "name": "my-queue", "permission":"read", "tags":""}, "allow"))
|
|
|
|
expectations.push(expectResource({ "username": username, "vhost": "/", "resource": "exchange", "name": "amq.default", "permission":"write", "tags":""}, "allow"))
|
2024-01-30 21:04:12 +08:00
|
|
|
}else if (backends.includes("oauth") && username.includes("oauth")) {
|
|
|
|
let oauthProviderUrl = process.env.OAUTH_PROVIDER_URL
|
|
|
|
let oauthClientId = process.env.OAUTH_CLIENT_ID
|
|
|
|
let oauthClientSecret = process.env.OAUTH_CLIENT_SECRET
|
2025-05-07 23:38:31 +08:00
|
|
|
log("oauthProviderUrl : " + oauthProviderUrl)
|
2024-01-30 23:30:23 +08:00
|
|
|
let openIdConfig = openIdConfiguration(oauthProviderUrl)
|
2025-05-07 23:38:31 +08:00
|
|
|
log("Obtained token_endpoint : " + openIdConfig.token_endpoint)
|
2024-01-30 23:30:23 +08:00
|
|
|
password = tokenFor(oauthClientId, oauthClientSecret, openIdConfig.token_endpoint)
|
2025-05-07 23:38:31 +08:00
|
|
|
log("Obtained access token : " + password)
|
2023-12-22 21:54:34 +08:00
|
|
|
}
|
2023-08-14 18:51:46 +08:00
|
|
|
})
|
|
|
|
|
2024-11-27 17:40:23 +08:00
|
|
|
it('can open an AMQP 1.0 connection', async function () {
|
|
|
|
amqp = openAmqp()
|
|
|
|
await untilConnectionEstablished
|
|
|
|
var untilMessageReceived = new Promise((resolve, reject) => {
|
|
|
|
onAmqp('message', function(context) {
|
2025-02-25 19:50:58 +08:00
|
|
|
if (receivedAmqpMessageCount == 2) resolve()
|
2024-11-27 17:40:23 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
amqp.sender.send({body:'second message'})
|
|
|
|
await untilMessageReceived
|
|
|
|
assert.equal(2, receivedAmqpMessageCount)
|
2023-08-14 18:51:46 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
2024-11-27 17:40:23 +08:00
|
|
|
if ( backends.includes("http") ) {
|
|
|
|
verifyAll(expectations)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (amqp != null) {
|
|
|
|
closeAmqp(amqp.connection)
|
2023-12-22 21:54:34 +08:00
|
|
|
}
|
2024-11-27 17:40:23 +08:00
|
|
|
} catch (error) {
|
2025-05-07 23:38:31 +08:00
|
|
|
error("Failed to close amqp10 connection due to " + error);
|
2024-11-27 17:40:23 +08:00
|
|
|
}
|
2023-08-14 18:51:46 +08:00
|
|
|
})
|
|
|
|
})
|