2024-11-15 02:38:27 +08:00
|
|
|
const fs = require('fs')
|
2023-12-22 21:54:34 +08:00
|
|
|
const assert = require('assert')
|
2024-09-04 17:06:54 +08:00
|
|
|
const { tokenFor, openIdConfiguration } = require('../utils')
|
2023-12-22 21:54:34 +08:00
|
|
|
const { reset, expectUser, expectVhost, expectResource, allow, verifyAll } = require('../mock_http_backend')
|
2024-01-30 23:30:23 +08:00
|
|
|
const mqtt = require('mqtt');
|
2023-12-22 21:54:34 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Having MQTT protocol enbled and the following auth_backends: ' + backends, function () {
|
|
|
|
let mqttOptions
|
|
|
|
let expectations = []
|
2024-11-15 02:38:27 +08:00
|
|
|
let mqttProtocol = process.env.MQTT_PROTOCOL || 'mqtt'
|
|
|
|
let usemtls = process.env.MQTT_USE_MTLS || false
|
2023-12-22 21:54:34 +08:00
|
|
|
let rabbit = process.env.RABBITMQ_HOSTNAME || 'localhost'
|
2024-11-15 02:38:27 +08:00
|
|
|
let mqttUrl = process.env.RABBITMQ_MQTT_URL || "mqtt://" + rabbit + ":1883"
|
2023-12-27 22:03:26 +08:00
|
|
|
let username = process.env.RABBITMQ_AMQP_USERNAME
|
|
|
|
let password = process.env.RABBITMQ_AMQP_PASSWORD
|
2024-11-15 02:38:27 +08:00
|
|
|
let client_id = process.env.RABBITMQ_AMQP_USERNAME || 'selenium-client'
|
|
|
|
|
2025-02-25 19:50:58 +08:00
|
|
|
before(function () {
|
|
|
|
if (backends.includes("http") && (username.includes("http") || usemtls)) {
|
2024-01-30 21:04:12 +08:00
|
|
|
reset()
|
2025-02-25 19:50:58 +08:00
|
|
|
if (!usemtls) {
|
|
|
|
expectations.push(expectUser({
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
"client_id": client_id,
|
|
|
|
"vhost": "/" }, "allow"))
|
|
|
|
} else {
|
|
|
|
expectations.push(expectUser({
|
|
|
|
"username": username,
|
|
|
|
"client_id": client_id,
|
|
|
|
"vhost": "/" }, "allow"))
|
|
|
|
}
|
2024-01-30 21:04:12 +08:00
|
|
|
expectations.push(expectVhost({ "username": username, "vhost": "/"}, "allow"))
|
2025-02-25 19:50:58 +08:00
|
|
|
|
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
|
2024-01-30 23:30:23 +08:00
|
|
|
let openIdConfig = openIdConfiguration(oauthProviderUrl)
|
|
|
|
console.log("Obtained token_endpoint : " + openIdConfig.token_endpoint)
|
|
|
|
password = tokenFor(oauthClientId, oauthClientSecret, openIdConfig.token_endpoint)
|
2024-01-30 21:04:12 +08:00
|
|
|
console.log("Obtained access token : " + password)
|
|
|
|
}
|
2023-12-22 21:54:34 +08:00
|
|
|
mqttOptions = {
|
|
|
|
clientId: client_id,
|
|
|
|
protocolId: 'MQTT',
|
2024-11-15 02:38:27 +08:00
|
|
|
protocol: mqttProtocol,
|
2023-12-22 21:54:34 +08:00
|
|
|
protocolVersion: 4,
|
|
|
|
keepalive: 10000,
|
|
|
|
clean: false,
|
2024-11-15 02:38:27 +08:00
|
|
|
reconnectPeriod: '1000'
|
|
|
|
}
|
|
|
|
if (mqttProtocol == 'mqtts') {
|
|
|
|
mqttOptions["ca"] = [fs.readFileSync(process.env.RABBITMQ_CERTS + "/ca_rabbitmq_certificate.pem")]
|
|
|
|
}
|
|
|
|
if (usemtls) {
|
|
|
|
mqttOptions["cert"] = fs.readFileSync(process.env.RABBITMQ_CERTS + "/client_rabbitmq_certificate.pem")
|
|
|
|
mqttOptions["key"] = fs.readFileSync(process.env.RABBITMQ_CERTS + "/client_rabbitmq_key.pem")
|
|
|
|
} else {
|
|
|
|
mqttOptions["username"] = username
|
|
|
|
mqttOptions["password"] = password
|
2023-12-22 21:54:34 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2025-02-25 19:50:58 +08:00
|
|
|
it('can open an MQTT connection', async function () {
|
2024-11-15 02:38:27 +08:00
|
|
|
var client = mqtt.connect(mqttUrl, mqttOptions)
|
2025-02-25 19:50:58 +08:00
|
|
|
let done = new Promise((resolve, reject) => {
|
|
|
|
client.on('error', function(err) {
|
|
|
|
reject(err)
|
|
|
|
client.end()
|
|
|
|
assert.fail("Mqtt connection failed due to " + err)
|
|
|
|
}),
|
|
|
|
client.on('connect', function(err) {
|
|
|
|
resolve("ok")
|
|
|
|
client.end()
|
|
|
|
})
|
2023-12-27 22:03:26 +08:00
|
|
|
})
|
2025-02-25 19:50:58 +08:00
|
|
|
assert.equal("ok", await done)
|
2023-12-22 21:54:34 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
if ( backends.includes("http") ) {
|
|
|
|
verifyAll(expectations)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|