Skip to content

Commit 7093170

Browse files
committed
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
1 parent 7e11a9d commit 7093170

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,4 @@ install:
77
- "pip install -r requirements.txt"
88

99
script:
10-
- pybot -P src tests
11-
12-
branches:
13-
only:
14-
- master
15-
- dev
10+
- pybot -P src -e local-only tests

src/MQTTLibrary/MQTTKeywords.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ class MQTTKeywords(object):
1515

1616
def __init__(self, loop_timeout=LOOP_TIMEOUT):
1717
self._loop_timeout = convert_time(loop_timeout)
18+
self._username = None
19+
self._password = None
1820
#self._mqttc = mqtt.Client()
1921

22+
def set_username_and_password(self, username, password=None):
23+
self._username = username
24+
self._password = password
25+
2026
def connect(self, broker, port=1883, client_id="", clean_session=True):
2127

2228
""" 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):
5157
self._mqttc.on_connect = self._on_connect
5258
self._mqttc.on_disconnect = self._on_disconnect
5359

60+
if self._username:
61+
self._mqttc.username_pw_set(self._username, self._password)
62+
5463
self._mqttc.connect(broker, int(port))
5564

5665
timer_start = time.time()

tests/pubsub.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,23 @@
128128
| | @{messages} | Subscribe and Get Messages | client.id=${client} | topic=${topic}
129129
| | LOG | ${messages}
130130
| | Length Should Be | ${messages} | 0
131+
132+
| Publish to a broker that requires username, password authentication
133+
| | [Tags] | local-only
134+
| | ${time} | Get Time | epoch
135+
| | ${client} | Catenate | SEPARATOR=. | robot.mqtt | ${time}
136+
| | ${topic} | Set Variable | test
137+
| | Set username and password | user1 | test1
138+
| | Connect | 127.0.0.1 | 1883 | ${client}
139+
| | Publish | ${topic} | test message with username and password
140+
| | [Teardown] | Disconnect
141+
142+
| Publish to a broker that requires username, password authentication with invalid password
143+
| | [Tags] | local-only
144+
| | ${time} | Get Time | epoch
145+
| | ${client} | Catenate | SEPARATOR=. | robot.mqtt | ${time}
146+
| | ${topic} | Set Variable | test
147+
| | Set username and password | user1 | test
148+
| | Run Keyword and expect error | The client disconnected unexpectedly
149+
| | ... | Connect | 127.0.0.1 | 1883 | ${client}
150+
| | [Teardown] | Disconnect

0 commit comments

Comments
 (0)