Skip to content

Commit

Permalink
Add keyword for setting the username, password for broker auth.
Browse files Browse the repository at this point in the history
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
randomsync committed Mar 1, 2016
1 parent 7e11a9d commit 7093170
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
7 changes: 1 addition & 6 deletions .travis.yml
Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/MQTTLibrary/MQTTKeywords.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions tests/pubsub.txt
Expand Up @@ -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.