Permalink
Browse files

Add keyword for setting the username, password for broker auth.

The keyword 'set_username_and_password' stores the username and optional
password and uses the paho library's 'username_pw_set' before connecting. Fixes
  • Loading branch information...
1 parent 7e11a9d commit 7093170e18efb5258d39f94f2e23bb487ac9b8c7 @randomsync committed Mar 1, 2016
Showing with 30 additions and 6 deletions.
  1. +1 −6 .travis.yml
  2. +9 −0 src/MQTTLibrary/MQTTKeywords.py
  3. +20 −0 tests/pubsub.txt
View
@@ -7,9 +7,4 @@ install:
- "pip install -r requirements.txt"
script:
- - pybot -P src tests
-
-branches:
- only:
- - master
- - dev
+ - pybot -P src -e local-only tests
@@ -15,8 +15,14 @@ class MQTTKeywords(object):
def __init__(self, loop_timeout=LOOP_TIMEOUT):
self._loop_timeout = convert_time(loop_timeout)
+ self._username = None
+ self._password = None
#self._mqttc = mqtt.Client()
+ def set_username_and_password(self, username, password=None):
+ self._username = username
+ self._password = password
+
def connect(self, broker, port=1883, client_id="", clean_session=True):
""" Connect to an MQTT broker. This is a pre-requisite step for publish
@@ -51,6 +57,9 @@ def connect(self, broker, port=1883, client_id="", clean_session=True):
self._mqttc.on_connect = self._on_connect
self._mqttc.on_disconnect = self._on_disconnect
+ if self._username:
+ self._mqttc.username_pw_set(self._username, self._password)
+
self._mqttc.connect(broker, int(port))
timer_start = time.time()
View
@@ -128,3 +128,23 @@
| | @{messages} | Subscribe and Get Messages | client.id=${client} | topic=${topic}
| | LOG | ${messages}
| | Length Should Be | ${messages} | 0
+
+| Publish to a broker that requires username, password authentication
+| | [Tags] | local-only
+| | ${time} | Get Time | epoch
+| | ${client} | Catenate | SEPARATOR=. | robot.mqtt | ${time}
+| | ${topic} | Set Variable | test
+| | Set username and password | user1 | test1
+| | Connect | 127.0.0.1 | 1883 | ${client}
+| | Publish | ${topic} | test message with username and password
+| | [Teardown] | Disconnect
+
+| Publish to a broker that requires username, password authentication with invalid password
+| | [Tags] | local-only
+| | ${time} | Get Time | epoch
+| | ${client} | Catenate | SEPARATOR=. | robot.mqtt | ${time}
+| | ${topic} | Set Variable | test
+| | Set username and password | user1 | test
+| | Run Keyword and expect error | The client disconnected unexpectedly
+| | ... | Connect | 127.0.0.1 | 1883 | ${client}
+| | [Teardown] | Disconnect

0 comments on commit 7093170

Please sign in to comment.