Skip to content

Commit

Permalink
Merge pull request #3 from randomsync/set-username-pwd
Browse files Browse the repository at this point in the history
Add keyword for setting the username, password for broker auth.
  • Loading branch information
randomsync committed Mar 1, 2016
2 parents 04eb816 + c60f006 commit 83e633d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion src/MQTTLibrary/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.4.0'
VERSION = '0.5.0'
20 changes: 20 additions & 0 deletions tests/pubsub.txt
Original file line number Diff line number Diff line change
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 83e633d

Please sign in to comment.