Skip to content

Commit b907fce

Browse files
author
DanielePalaia
committed
refresh_token
1 parent c3a35ea commit b907fce

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

rabbitmq_amqp_python_client/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CommonValues(enum.Enum):
1818
key = "key"
1919
queue = "queues"
2020
bindings = "bindings"
21+
path_tokens = "/auth/tokens"
2122

2223

2324
class ExchangeType(enum.Enum):

rabbitmq_amqp_python_client/connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,8 @@ def active_producers(self) -> int:
383383
def active_consumers(self) -> int:
384384
"""Returns the number of active consumers"""
385385
return len(self._consumers)
386+
387+
def refresh_token(self, token: str) -> None:
388+
389+
management = self.management()
390+
management.refresh_token(token)

rabbitmq_amqp_python_client/management.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,14 @@ def queue_info(self, name: str) -> QueueInfo:
573573
message_count=queue_info["message_count"],
574574
consumer_count=queue_info["consumer_count"],
575575
)
576+
577+
def refresh_token(self, token: str) -> None:
578+
579+
self.request(
580+
token.encode(),
581+
CommonValues.path_tokens.value,
582+
CommonValues.command_put.value,
583+
[
584+
CommonValues.response_code_204.value,
585+
],
586+
)

tests/test_connection.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time
2-
from datetime import timedelta
2+
from datetime import datetime, timedelta
33

44
from rabbitmq_amqp_python_client import (
55
ConnectionClosed,
@@ -11,6 +11,7 @@
1111
)
1212

1313
from .http_requests import delete_all_connections
14+
from .utils import token
1415

1516

1617
def on_disconnected():
@@ -44,7 +45,7 @@ def test_connection_ssl(ssl_context) -> None:
4445
environment.close()
4546

4647

47-
def test_connection_auth(environment_auth: Environment) -> None:
48+
def test_connection_oauth(environment_auth: Environment) -> None:
4849

4950
connection = environment_auth.connection()
5051
connection.dial()
@@ -54,7 +55,7 @@ def test_connection_auth(environment_auth: Environment) -> None:
5455
connection.close()
5556

5657

57-
def test_connection_auth_with_timeout(environment_auth: Environment) -> None:
58+
def test_connection_oauth_with_timeout(environment_auth: Environment) -> None:
5859

5960
connection = environment_auth.connection()
6061
connection.dial()
@@ -65,10 +66,32 @@ def test_connection_auth_with_timeout(environment_auth: Environment) -> None:
6566
try:
6667
management = connection.management()
6768
management.declare_queue(QuorumQueueSpecification(name="test-queue"))
69+
management.close()
6870
except Exception:
6971
raised = True
7072

7173
assert raised is True
74+
75+
connection.close()
76+
77+
78+
def test_connection_oauth_refresh_token(environment_auth: Environment) -> None:
79+
80+
connection = environment_auth.connection()
81+
connection.dial()
82+
# let the token expire
83+
time.sleep(3)
84+
raised = False
85+
# token expired, refresh
86+
connection.refresh_token(token(datetime.now() + timedelta(milliseconds=2500)))
87+
88+
try:
89+
management = connection.management()
90+
management.declare_queue(QuorumQueueSpecification(name="test-queue"))
91+
except Exception:
92+
raised = True
93+
94+
assert raised is False
7295
connection.close()
7396

7497

0 commit comments

Comments
 (0)