Add Expiring Share Links
Designers want to share a design with people outside their team through links that expire and can be revoked. The README has the rules, and share_links.py has the data model. Build it with the assistant and decide what to keep.
share_links.py1 of 3
Files
share_links.py
Python · Editablefrom dataclasses import dataclass
@dataclass
class ShareLink:
token: str
design_id: str
role: str # "view" or "edit"
expires_at: float
revoked: bool = False
class ShareLinks:
"""Share links for designs. Anyone holding a token can open the design with the link's role."""
def __init__(self, clock):
self.clock = clock
self.links = {} # token -> ShareLink
def create_link(self, design_id, role, ttl_s):
"""Creates a link and returns its token. Tokens appear in public URLs."""
now = self.clock()
raise NotImplementedError
def resolve(self, token):
"""Returns (design_id, role) for a usable token, or None."""
raise NotImplementedError
def revoke(self, token):
link = self.links.get(token)
if link:
link.revoked = True
import pytest
from share_links import ShareLinks
WEEK = 7 * 24 * 3600
class FakeClock:
def __init__(self):
self.now = 1000.0
def __call__(self):
return self.now
def make():
clock = FakeClock()
return ShareLinks(clock), clock
def test_new_link_resolves():
links, _ = make()
token = links.create_link("d1", "view", WEEK)
assert links.resolve(token) == ("d1", "view")
def test_tokens_are_unique():
links, _ = make()
a = links.create_link("d1", "view", WEEK)
b = links.create_link("d1", "view", WEEK)
assert a != b
def test_link_expires():
links, clock = make()
token = links.create_link("d1", "view", WEEK)
clock.now += WEEK
assert links.resolve(token) is None
def test_revoked_link_stops_working():
links, _ = make()
token = links.create_link("d1", "view", WEEK)
links.revoke(token)
assert links.resolve(token) is None
def test_unknown_token_resolves_to_none():
links, _ = make()
assert links.resolve("not-a-token") is None
def test_rejects_unknown_role():
links, _ = make()
with pytest.raises(ValueError):
links.create_link("d1", "owner", WEEK)
def test_rejects_non_positive_ttl():
links, _ = make()
with pytest.raises(ValueError):
links.create_link("d1", "view", 0)
def test_active_links_lists_only_usable_links():
links, clock = make()
keep = links.create_link("d1", "view", WEEK)
links.create_link("d1", "view", 60)
revoked = links.create_link("d1", "edit", WEEK)
links.create_link("d2", "view", WEEK)
links.revoke(revoked)
clock.now += 61
assert [link.token for link in links.active_links("d1")] == [keep]
# Share Links
Designers want to share a design with people outside their team. Add share links to share_links.py.
- A link has a role, view or edit, and stops working ttl_s seconds after it's created.
- The owner can revoke a link at any time, and a revoked link stops working at once.
- Tokens appear in public URLs, so anyone who can guess one can open the design.
- The share dialog lists a design's usable links.
- Reject unknown roles and a ttl_s that isn't positive.
The tests in test_share_links.py use a fake clock.
Test Output
No runs yet.