Skip to content

Commit aefdcd3

Browse files
committed
fix: redirect, uniqueness and deprecations
1 parent e5b33d6 commit aefdcd3

File tree

4 files changed

+63
-58
lines changed

4 files changed

+63
-58
lines changed

stream/client/base.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,18 @@ def get_full_url(self, service_name, relative_url):
461461
if self.custom_api_port:
462462
base_url = f"{base_url}:{self.custom_api_port}"
463463

464-
url = base_url + "/" + service_name + "/" + self.version + "/" + relative_url
464+
url = (
465+
base_url
466+
+ "/"
467+
+ service_name
468+
+ "/"
469+
+ self.version
470+
+ "/"
471+
+ relative_url.replace(
472+
"//", "/"
473+
) # non-standard url will cause redirect and so can lose its body
474+
)
475+
465476
return url
466477

467478
def get_default_params(self):

stream/tests/conftest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import os
33
import sys
4+
from uuid import uuid4
45

56
import pytest
67

@@ -45,29 +46,29 @@ async def async_client():
4546

4647
@pytest.fixture
4748
def user1(async_client):
48-
return async_client.feed("user", "1")
49+
return async_client.feed("user", f"1-{uuid4()}")
4950

5051

5152
@pytest.fixture
5253
def user2(async_client):
53-
return async_client.feed("user", "2")
54+
return async_client.feed("user", f"2-{uuid4()}")
5455

5556

5657
@pytest.fixture
5758
def aggregated2(async_client):
58-
return async_client.feed("aggregated", "2")
59+
return async_client.feed("aggregated", f"2-{uuid4()}")
5960

6061

6162
@pytest.fixture
6263
def aggregated3(async_client):
63-
return async_client.feed("aggregated", "3")
64+
return async_client.feed("aggregated", f"3-{uuid4()}")
6465

6566

6667
@pytest.fixture
6768
def topic(async_client):
68-
return async_client.feed("topic", "1")
69+
return async_client.feed("topic", f"1-{uuid4()}")
6970

7071

7172
@pytest.fixture
7273
def flat3(async_client):
73-
return async_client.feed("flat", "3")
74+
return async_client.feed("flat", f"3-{uuid4()}")

stream/tests/test_async_client.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import stream
1111
from stream.exceptions import ApiKeyException, InputException
12-
from stream.tests.test_client import get_unique_postfix
1312

1413

1514
def assert_first_activity_id_equal(activities, correct_activity_id):
@@ -82,7 +81,7 @@ async def test_update_activities_create(async_client):
8281

8382
@pytest.mark.asyncio
8483
async def test_add_activity(async_client):
85-
feed = async_client.feed("user", "py1")
84+
feed = async_client.feed("user", f"py1-{uuid4()}")
8685
activity_data = {"actor": 1, "verb": "tweet", "object": 1}
8786
response = await feed.add_activity(activity_data)
8887
activity_id = response["id"]
@@ -93,7 +92,7 @@ async def test_add_activity(async_client):
9392

9493
@pytest.mark.asyncio
9594
async def test_add_activity_to_inplace_change(async_client):
96-
feed = async_client.feed("user", "py1")
95+
feed = async_client.feed("user", f"py1-{uuid4()}")
9796
team_feed = async_client.feed("user", "teamy")
9897
activity_data = {"actor": 1, "verb": "tweet", "object": 1}
9998
activity_data["to"] = [team_feed.id]
@@ -103,8 +102,8 @@ async def test_add_activity_to_inplace_change(async_client):
103102

104103
@pytest.mark.asyncio
105104
async def test_add_activities_to_inplace_change(async_client):
106-
feed = async_client.feed("user", "py1")
107-
team_feed = async_client.feed("user", "teamy")
105+
feed = async_client.feed("user", f"py1-{uuid4()}")
106+
team_feed = async_client.feed("user", f"teamy-{uuid4()}")
108107
activity_data = {"actor": 1, "verb": "tweet", "object": 1}
109108
activity_data["to"] = [team_feed.id]
110109
await feed.add_activities([activity_data])
@@ -116,7 +115,7 @@ async def test_add_activity_to(async_client):
116115
# test for sending an activities to the team feed using to
117116
feeds = ["user", "teamy", "team_follower"]
118117
user_feed, team_feed, team_follower_feed = map(
119-
lambda x: async_client.feed("user", x), feeds
118+
lambda x: async_client.feed("user", f"{x}-{uuid4()}"), feeds
120119
)
121120
await team_follower_feed.follow(team_feed.slug, team_feed.user_id)
122121
activity_data = {"actor": 1, "verb": "tweet", "object": 1, "to": [team_feed.id]}
@@ -202,8 +201,8 @@ async def test_add_activities(user1):
202201

203202
@pytest.mark.asyncio
204203
async def test_add_activities_to(async_client, user1):
205-
pyto2 = async_client.feed("user", "pyto2")
206-
pyto3 = async_client.feed("user", "pyto3")
204+
pyto2 = async_client.feed("user", f"pyto2-{uuid4()}")
205+
pyto3 = async_client.feed("user", f"pyto3-{uuid4()}")
207206

208207
to = [pyto2.id, pyto3.id]
209208
activity_data = [
@@ -230,7 +229,7 @@ async def test_add_activities_to(async_client, user1):
230229

231230
@pytest.mark.asyncio
232231
async def test_follow_and_source(async_client):
233-
feed = async_client.feed("user", "test_follow")
232+
feed = async_client.feed("user", f"test_follow-{uuid4()}")
234233
agg_feed = async_client.feed("aggregated", "test_follow")
235234
actor_id = random.randint(10, 100000)
236235
activity_data = {"actor": actor_id, "verb": "tweet", "object": 1}
@@ -248,14 +247,14 @@ async def test_follow_and_source(async_client):
248247

249248
@pytest.mark.asyncio
250249
async def test_empty_followings(async_client):
251-
asocial = async_client.feed("user", "asocialpython")
250+
asocial = async_client.feed("user", f"asocialpython-{uuid4()}")
252251
followings = await asocial.following()
253252
assert followings["results"] == []
254253

255254

256255
@pytest.mark.asyncio
257256
async def test_get_followings(async_client):
258-
social = async_client.feed("user", "psocial")
257+
social = async_client.feed("user", f"psocial-{uuid4()}")
259258
await social.follow("user", "apy")
260259
await social.follow("user", "bpy")
261260
await social.follow("user", "cpy")
@@ -271,17 +270,17 @@ async def test_get_followings(async_client):
271270

272271
@pytest.mark.asyncio
273272
async def test_empty_followers(async_client):
274-
asocial = async_client.feed("user", "asocialpython")
273+
asocial = async_client.feed("user", f"asocialpython-{uuid4()}")
275274
followers = await asocial.followers()
276275
assert followers["results"] == []
277276

278277

279278
@pytest.mark.asyncio
280279
async def test_get_followers(async_client):
281-
social = async_client.feed("user", "psocial")
282-
spammy1 = async_client.feed("user", "spammy1")
283-
spammy2 = async_client.feed("user", "spammy2")
284-
spammy3 = async_client.feed("user", "spammy3")
280+
social = async_client.feed("user", f"psocial-{uuid4()}")
281+
spammy1 = async_client.feed("user", f"spammy1-{uuid4()}")
282+
spammy2 = async_client.feed("user", f"spammy2-{uuid4()}")
283+
spammy3 = async_client.feed("user", f"spammy3-{uuid4()}")
285284
for feed in [spammy1, spammy2, spammy3]:
286285
await feed.follow("user", social.user_id)
287286
followers = await social.followers(offset=0, limit=2)
@@ -296,7 +295,7 @@ async def test_get_followers(async_client):
296295

297296
@pytest.mark.asyncio
298297
async def test_empty_do_i_follow(async_client):
299-
social = async_client.feed("user", "psocial")
298+
social = async_client.feed("user", f"psocial-{uuid4()}")
300299
await social.follow("user", "apy")
301300
await social.follow("user", "bpy")
302301
followings = await social.following(feeds=["user:missingpy"])
@@ -305,7 +304,7 @@ async def test_empty_do_i_follow(async_client):
305304

306305
@pytest.mark.asyncio
307306
async def test_do_i_follow(async_client):
308-
social = async_client.feed("user", "psocial")
307+
social = async_client.feed("user", f"psocial-{uuid4()}")
309308
await social.follow("user", "apy")
310309
await social.follow("user", "bpy")
311310
followings = await social.following(feeds=["user:apy"])
@@ -376,7 +375,7 @@ async def test_get(user1):
376375

377376
@pytest.mark.asyncio
378377
async def test_get_not_marked_seen(async_client):
379-
notification_feed = async_client.feed("notification", "test_mark_seen")
378+
notification_feed = async_client.feed("notification", f"test_mark_seen-{uuid4()}")
380379
response = await notification_feed.get(limit=3)
381380
activities = response["results"]
382381
for activity in activities:
@@ -385,7 +384,7 @@ async def test_get_not_marked_seen(async_client):
385384

386385
@pytest.mark.asyncio
387386
async def test_mark_seen_on_get(async_client):
388-
notification_feed = async_client.feed("notification", "test_mark_seen")
387+
notification_feed = async_client.feed("notification", f"test_mark_seen-{uuid4()}")
389388
response = await notification_feed.get(limit=100)
390389
activities = response["results"]
391390
for activity in activities:
@@ -433,7 +432,7 @@ async def test_mark_seen_on_get(async_client):
433432

434433
@pytest.mark.asyncio
435434
async def test_mark_read_by_id(async_client):
436-
notification_feed = async_client.feed("notification", "py2")
435+
notification_feed = async_client.feed("notification", f"py2-{uuid4()}")
437436
response = await notification_feed.get(limit=3)
438437
activities = response["results"]
439438
ids = []
@@ -515,7 +514,7 @@ async def test_uniqueness_topic(flat3, topic, user1):
515514
await flat3.follow("user", user1.user_id)
516515
# add the same activity twice
517516
now = datetime.now(tzlocal())
518-
tweet = f"My Way {get_unique_postfix()}"
517+
tweet = f"My Way {uuid4()}"
519518
activity_data = {
520519
"actor": 1,
521520
"verb": "tweet",
@@ -615,8 +614,8 @@ async def test_missing_actor(user1):
615614

616615
@pytest.mark.asyncio
617616
async def test_follow_many(async_client):
618-
sources = [async_client.feed("user", str(i)).id for i in range(10)]
619-
targets = [async_client.feed("flat", str(i)).id for i in range(10)]
617+
sources = [async_client.feed("user", f"{i}-{uuid4()}").id for i in range(10)]
618+
targets = [async_client.feed("flat", f"{i}-{uuid4()}").id for i in range(10)]
620619
feeds = [{"source": s, "target": t} for s, t in zip(sources, targets)]
621620
await async_client.follow_many(feeds)
622621

@@ -631,21 +630,21 @@ async def test_follow_many(async_client):
631630
response = await async_client.feed(*source.split(":")).following()
632631
follows = response["results"]
633632
assert len(follows) == 1
634-
assert follows[0]["feed_id"] in sources
635-
assert follows[0]["target_id"] == source
633+
assert follows[0]["feed_id"] == source
634+
assert follows[0]["target_id"] in targets
636635

637636

638637
@pytest.mark.asyncio
639638
async def test_follow_many_acl(async_client):
640-
sources = [async_client.feed("user", str(i)) for i in range(10)]
639+
sources = [async_client.feed("user", f"{i}-{uuid4()}") for i in range(10)]
641640
# ensure every source is empty first
642641
for feed in sources:
643642
response = await feed.get(limit=100)
644643
activities = response["results"]
645644
for activity in activities:
646645
await feed.remove_activity(activity["id"])
647646

648-
targets = [async_client.feed("flat", str(i)) for i in range(10)]
647+
targets = [async_client.feed("flat", f"{i}-{uuid4()}") for i in range(10)]
649648
# ensure every source is empty first
650649
for feed in targets:
651650
response = await feed.get(limit=100)
@@ -696,7 +695,7 @@ async def failing_unfollow():
696695
@pytest.mark.asyncio
697696
async def test_add_to_many(async_client):
698697
activity = {"actor": 1, "verb": "tweet", "object": 1, "custom": "data"}
699-
feeds = [async_client.feed("flat", str(i)).id for i in range(10, 20)]
698+
feeds = [async_client.feed("flat", f"{i}-{uuid4()}").id for i in range(10, 20)]
700699
await async_client.add_to_many(activity, feeds)
701700

702701
for feed in feeds:
@@ -732,7 +731,7 @@ async def test_get_activities_full(async_client):
732731
"foreign_id": fid,
733732
}
734733

735-
feed = async_client.feed("user", "test_get_activity")
734+
feed = async_client.feed("user", f"test_get_activity-{uuid4()}")
736735
response = await feed.add_activity(activity)
737736

738737
response = await async_client.get_activities(ids=[response["id"]])
@@ -760,7 +759,7 @@ async def test_get_activities_full_with_enrichment(async_client):
760759
"foreign_id": fid,
761760
}
762761

763-
feed = async_client.feed("user", "test_get_activity")
762+
feed = async_client.feed("user", f"test_get_activity-{uuid4()}")
764763
activity = await feed.add_activity(activity)
765764

766765
reaction1 = await async_client.reactions.add("like", activity["id"], "liker")
@@ -802,7 +801,7 @@ async def test_get_activities_full_with_enrichment_and_reaction_kinds(async_clie
802801
"foreign_id": fid,
803802
}
804803

805-
feed = async_client.feed("user", "test_get_activity")
804+
feed = async_client.feed("user", f"test_get_activity-{uuid4()}")
806805
activity = await feed.add_activity(activity)
807806

808807
await async_client.reactions.add("like", activity["id"], "liker")
@@ -983,16 +982,18 @@ async def test_reaction_add(async_client):
983982

984983
@pytest.mark.asyncio
985984
async def test_reaction_add_to_target_feeds(async_client):
985+
feed_id = f"user:michelle-{uuid4()}"
986986
r = await async_client.reactions.add(
987987
"superlike",
988988
"54a60c1e-4ee3-494b-a1e3-50c06acb5ed4",
989989
"mike",
990990
data={"popularity": 50},
991-
target_feeds=["user:michelle"],
991+
target_feeds=[feed_id],
992992
target_feeds_extra_data={"popularity": 100},
993993
)
994994
assert r["data"]["popularity"] == 50
995-
response = await async_client.feed("user", "michelle").get(limit=1)
995+
feed = async_client.feed(*feed_id.split(":"))
996+
response = await feed.get(limit=1)
996997
a = response["results"][0]
997998
assert r["id"] in a["reaction"]
998999
assert a["verb"] == "superlike"
@@ -1003,12 +1004,12 @@ async def test_reaction_add_to_target_feeds(async_client):
10031004
r["id"],
10041005
"rob",
10051006
data={"popularity": 60},
1006-
target_feeds=["user:michelle"],
1007+
target_feeds=[feed_id],
10071008
target_feeds_extra_data={"popularity": 200},
10081009
)
10091010

10101011
assert child["data"]["popularity"] == 60
1011-
response = await async_client.feed("user", "michelle").get(limit=1)
1012+
response = await feed.get(limit=1)
10121013
a = response["results"][0]
10131014
assert child["id"] in a["reaction"]
10141015
assert a["verb"] == "superlike"
@@ -1196,7 +1197,7 @@ async def test_collections_delete(async_client):
11961197
async def test_feed_enrichment_collection(async_client):
11971198
entry = await async_client.collections.add("items", {"name": "time machine"})
11981199
entry.pop("duration")
1199-
f = async_client.feed("user", "mike")
1200+
f = async_client.feed("user", f"mike-{uuid4()}")
12001201
activity_data = {
12011202
"actor": "mike",
12021203
"verb": "buy",
@@ -1213,7 +1214,7 @@ async def test_feed_enrichment_collection(async_client):
12131214
async def test_feed_enrichment_user(async_client):
12141215
user = await async_client.users.add(str(uuid1()), {"name": "Mike"})
12151216
user.pop("duration")
1216-
f = async_client.feed("user", "mike")
1217+
f = async_client.feed("user", f"mike-{uuid4()}")
12171218
activity_data = {
12181219
"actor": async_client.users.create_reference(user),
12191220
"verb": "buy",
@@ -1228,7 +1229,7 @@ async def test_feed_enrichment_user(async_client):
12281229

12291230
@pytest.mark.asyncio
12301231
async def test_feed_enrichment_own_reaction(async_client):
1231-
f = async_client.feed("user", "mike")
1232+
f = async_client.feed("user", f"mike-{uuid4()}")
12321233
activity_data = {"actor": "mike", "verb": "buy", "object": "object"}
12331234
response = await f.add_activity(activity_data)
12341235
reaction = await async_client.reactions.add("like", response["id"], "mike")
@@ -1239,7 +1240,7 @@ async def test_feed_enrichment_own_reaction(async_client):
12391240

12401241
@pytest.mark.asyncio
12411242
async def test_feed_enrichment_recent_reaction(async_client):
1242-
f = async_client.feed("user", "mike")
1243+
f = async_client.feed("user", f"mike-{uuid4()}")
12431244
activity_data = {"actor": "mike", "verb": "buy", "object": "object"}
12441245
response = await f.add_activity(activity_data)
12451246
reaction = await async_client.reactions.add("like", response["id"], "mike")
@@ -1250,7 +1251,7 @@ async def test_feed_enrichment_recent_reaction(async_client):
12501251

12511252
@pytest.mark.asyncio
12521253
async def test_feed_enrichment_reaction_counts(async_client):
1253-
f = async_client.feed("user", "mike")
1254+
f = async_client.feed("user", f"mike-{uuid4()}")
12541255
activity_data = {"actor": "mike", "verb": "buy", "object": "object"}
12551256
response = await f.add_activity(activity_data)
12561257
reaction = await async_client.reactions.add("like", response["id"], "mike")

0 commit comments

Comments
 (0)