Skip to content

Commit 54e9f2c

Browse files
allow to send events of other user, if the first one has permissions
1 parent 46e6bee commit 54e9f2c

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/eventfabric.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def send_event(self, event, requester=requests.post):
5454
headers = {'content-type': 'application/json'}
5555
if self.token:
5656
headers[self.session_header_name] = self.token
57-
url = self.endpoint("streams") + "/" + self.username + "/" + event.channel + "/"
57+
url = self.endpoint("streams") + "/" + (event.username or self.username) + "/" + event.channel + "/"
5858
response = requester(url,
5959
verify = False,
6060
data=json.dumps(event.value),
@@ -71,15 +71,18 @@ class Event(object):
7171
channel is a string with the name that identifies this kind of events
7272
username is the logged in username"""
7373

74-
def __init__(self, value, channel):
75-
74+
def __init__(self, value, channel, username=None):
7675
self.value = value
7776
self.channel = channel
77+
self.username = username
7878

7979
@property
8080
def json(self):
8181
"""return a json representation of the object"""
82-
return {"value": self.value, "channel": self.channel}
82+
res = {"value": self.value, "channel": self.channel}
83+
if self.username is not None:
84+
res["username"] = self.username
85+
return res
8386

8487
def __str__(self):
8588
return json.dumps(self.json)

tests/eventfabric_tests.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
class FakeResponse(object):
1616
"fake response object"
17-
def __init__(self, status_code=200, cookies=None):
17+
def __init__(self, status_code=200):
1818
self.status_code = status_code
19-
self.cookies = cookies
19+
def json(self):
20+
return {"token": "mytoken"}
2021

2122
def fake_post(storage, return_value):
2223
"build a fake post function"
@@ -38,7 +39,6 @@ def test_client_creation(self):
3839
self.assertEqual(client.username, USERNAME)
3940
self.assertEqual(client.password, PASSWORD)
4041
self.assertEqual(client.root_url, "http://localhost:8080/")
41-
self.assertEqual(client.cookies, None)
4242
self.assertEqual(client.credentials["username"], USERNAME)
4343
self.assertEqual(client.credentials["password"], PASSWORD)
4444

@@ -53,7 +53,7 @@ def test_login(self):
5353
client = ef.Client(USERNAME, PASSWORD,
5454
"http://localhost:8080/")
5555
storage = []
56-
requester = fake_post(storage, FakeResponse(200, "cookies!"))
56+
requester = fake_post(storage, FakeResponse(200))
5757
status, response = client.login(requester)
5858
args, kwargs = storage.pop()
5959
endpoint = args[0]
@@ -62,8 +62,6 @@ def test_login(self):
6262

6363
self.assertTrue(status)
6464
self.assertEqual(response.status_code, 200)
65-
self.assertEqual(response.cookies, "cookies!")
66-
self.assertEqual(response.cookies, client.cookies)
6765
self.assertEqual(data_arg, json.dumps(client.credentials))
6866
self.assertEqual(headers["content-type"], "application/json")
6967
self.assertEqual(endpoint, "http://localhost:8080/sessions")
@@ -84,7 +82,7 @@ def test_send_event(self):
8482

8583
self.assertTrue(status)
8684
self.assertEqual(response.status_code, 201)
87-
self.assertEqual(data_arg, json.dumps(event.json))
85+
self.assertEqual(data_arg, json.dumps(event.json["value"]))
8886
self.assertEqual(headers["content-type"], "application/json")
8987
self.assertEqual(endpoint, "http://localhost:8080/streams/" + USERNAME + "/" + channel + "/")
9088

0 commit comments

Comments
 (0)