Skip to content

Commit

Permalink
Run some integration tests with authentication (#113)
Browse files Browse the repository at this point in the history
* Support running some tests with authentication

* Enable authentication for Mosquitto

* Use authentication only with Mosquitto for now

* Use password-file with mosquitto-github-action

* Fix mosquitto.passwd
  • Loading branch information
Namoshek authored Jun 4, 2022
1 parent 5813fd2 commit 77f6854
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
9 changes: 8 additions & 1 deletion .ci/mosquitto.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Config file for mosquitto
per_listener_settings true

# Port to use for the default listener.
port 1883
listener 1883
allow_anonymous true

# Port to use for the default listener with authentication.
listener 1884
password_file /mosquitto/config/mosquitto.passwd
allow_anonymous false

# =================================================================
# Extra listeners
Expand Down
1 change: 1 addition & 0 deletions .ci/mosquitto.passwd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ci-test-user:$6$QypQBNSQKE5bg6Ec$nzACfxhQ9qiYFByPPM/6GP/9kOWwDzEftN0EJPkS6M0PWqL55jAbBxUO863oWwhJ2q/YaubfLbe3xwwhBuoStQ==
9 changes: 7 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@ jobs:
uses: Namoshek/mosquitto-github-action@v1
with:
version: '1.6'
ports: '1883:1883 8883:8883 8884:8884'
ports: '1883:1883 1884:1884 8883:8883 8884:8884'
certificates: ${{ github.workspace }}/.ci/tls
config: ${{ github.workspace }}/.ci/mosquitto.conf
password-file: ${{ github.workspace}}/.ci/mosquitto.passwd

- name: Start Mosquitto 2.0 message broker
if: matrix.mqtt-broker == 'mosquitto-2.0'
uses: Namoshek/mosquitto-github-action@v1
with:
version: '2.0'
ports: '1883:1883 8883:8883 8884:8884'
ports: '1883:1883 1884:1884 8883:8883 8884:8884'
certificates: ${{ github.workspace }}/.ci/tls
config: ${{ github.workspace }}/.ci/mosquitto.conf
password-file: ${{ github.workspace}}/.ci/mosquitto.passwd

- name: Start HiveMQ message broker
if: matrix.mqtt-broker == 'hivemq'
Expand Down Expand Up @@ -120,8 +122,11 @@ jobs:
env:
MQTT_BROKER_HOST: 'localhost'
MQTT_BROKER_PORT: 1883
MQTT_BROKER_PORT_WITH_AUTHENTICATION: ${{ (matrix.mqtt-broker == 'mosquitto-1.6' || matrix.mqtt-broker == 'mosquitto-2.0') && 1884 || 1883 }}
MQTT_BROKER_TLS_PORT: 8883
MQTT_BROKER_TLS_WITH_CLIENT_CERT_PORT: 8884
MQTT_BROKER_USERNAME: ${{ (matrix.mqtt-broker == 'mosquitto-1.6' || matrix.mqtt-broker == 'mosquitto-2.0') && 'ci-test-user' || '' }}
MQTT_BROKER_PASSWORD: ${{ (matrix.mqtt-broker == 'mosquitto-1.6' || matrix.mqtt-broker == 'mosquitto-2.0') && secrets.CI_MOSQUITTO_CI_TEST_USER_PASSWORD || '' }}
SKIP_TLS_TESTS: ${{ matrix.mqtt-broker == 'emqx' || matrix.mqtt-broker == 'rabbitmq' }}

- name: Dump Docker logs on failure
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<php>
<env name="MQTT_BROKER_HOST" value="localhost"/>
<env name="MQTT_BROKER_PORT" value="1883"/>
<env name="MQTT_BROKER_PORT_WITH_AUTHENTICATION" value="1883"/>
<env name="MQTT_BROKER_TLS_PORT" value="8883"/>
<env name="MQTT_BROKER_TLS_WITH_CLIENT_CERT_PORT" value="8884"/>
<env name="TLS_CERT_DIR" value=".ci/tls"/>
Expand Down
12 changes: 6 additions & 6 deletions tests/Feature/ConnectWithCustomConnectionSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConnectWithCustomConnectionSettingsTest extends TestCase
{
public function test_connecting_using_mqtt31_with_custom_connection_settings_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-custom-connection-settings', MqttClient::MQTT_3_1);
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPortWithAuthentication, 'test-custom-connection-settings', MqttClient::MQTT_3_1);

$connectionSettings = (new ConnectionSettings)
->setLastWillTopic('foo/last/will')
Expand All @@ -30,8 +30,8 @@ public function test_connecting_using_mqtt31_with_custom_connection_settings_wor
->setSocketTimeout(3)
->setResendTimeout(3)
->setKeepAliveInterval(30)
->setUsername(null)
->setPassword(null)
->setUsername($this->mqttBrokerUsername)
->setPassword($this->mqttBrokerPassword)
->setUseTls(false)
->setTlsCertificateAuthorityFile(null)
->setTlsCertificateAuthorityPath(null)
Expand All @@ -51,7 +51,7 @@ public function test_connecting_using_mqtt31_with_custom_connection_settings_wor

public function test_connecting_using_mqtt311_with_custom_connection_settings_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-custom-connection-settings', MqttClient::MQTT_3_1_1);
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPortWithAuthentication, 'test-custom-connection-settings', MqttClient::MQTT_3_1_1);

$connectionSettings = (new ConnectionSettings)
->setLastWillTopic('foo/last/will')
Expand All @@ -62,8 +62,8 @@ public function test_connecting_using_mqtt311_with_custom_connection_settings_wo
->setSocketTimeout(3)
->setResendTimeout(3)
->setKeepAliveInterval(30)
->setUsername(null)
->setPassword(null)
->setUsername($this->mqttBrokerUsername)
->setPassword($this->mqttBrokerPassword)
->setUseTls(false)
->setTlsCertificateAuthorityFile(null)
->setTlsCertificateAuthorityPath(null)
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
{
protected string $mqttBrokerHost;
protected int $mqttBrokerPort;
protected int $mqttBrokerPortWithAuthentication;
protected int $mqttBrokerTlsPort;
protected int $mqttBrokerTlsWithClientCertificatePort;

protected ?string $mqttBrokerUsername = null;
protected ?string $mqttBrokerPassword = null;

protected bool $skipTlsTests;
protected string $tlsCertificateDirectory;

Expand All @@ -28,9 +32,13 @@ protected function setUp(): void

$this->mqttBrokerHost = getenv('MQTT_BROKER_HOST');
$this->mqttBrokerPort = intval(getenv('MQTT_BROKER_PORT'));
$this->mqttBrokerPortWithAuthentication = intval(getenv('MQTT_BROKER_PORT_WITH_AUTHENTICATION'));
$this->mqttBrokerTlsPort = intval(getenv('MQTT_BROKER_TLS_PORT'));
$this->mqttBrokerTlsWithClientCertificatePort = intval(getenv('MQTT_BROKER_TLS_WITH_CLIENT_CERT_PORT'));

$this->mqttBrokerUsername = getenv('MQTT_BROKER_USERNAME') ?: null;
$this->mqttBrokerPassword = getenv('MQTT_BROKER_PASSWORD') ?: null;

$this->skipTlsTests = getenv('SKIP_TLS_TESTS') === 'true';
$this->tlsCertificateDirectory = rtrim(getenv('TLS_CERT_DIR'), '/');
}
Expand Down

0 comments on commit 77f6854

Please sign in to comment.