Skip to content

Commit 85d3529

Browse files
committed
tests: Update, fix tests
1 parent b303752 commit 85d3529

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

tests/unit/manage/test_views.py

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ def test_create_macaroon(self, monkeypatch):
15271527
create_macaroon_obj = pretend.stub(
15281528
validate=lambda: True,
15291529
description=pretend.stub(data=pretend.stub()),
1530-
validated_scope=pretend.stub(),
1530+
validated_scope="foobar",
15311531
)
15321532
create_macaroon_cls = pretend.call_recorder(
15331533
lambda *a, **kw: create_macaroon_obj
@@ -1564,6 +1564,119 @@ def test_create_macaroon(self, monkeypatch):
15641564
"macaroon": macaroon,
15651565
"create_macaroon_form": create_macaroon_obj,
15661566
}
1567+
assert user_service.record_event.calls == [
1568+
pretend.call(
1569+
request.user.id,
1570+
tag="account:api_token:added",
1571+
ip_address=request.remote_addr,
1572+
additional={
1573+
"description": create_macaroon_obj.description.data,
1574+
"caveats": {
1575+
"permissions": create_macaroon_obj.validated_scope,
1576+
"version": 1,
1577+
},
1578+
},
1579+
)
1580+
]
1581+
1582+
def test_create_macaroon_records_events_for_each_project(self, monkeypatch):
1583+
macaroon = pretend.stub()
1584+
macaroon_service = pretend.stub(
1585+
create_macaroon=pretend.call_recorder(
1586+
lambda *a, **kw: ("not a real raw macaroon", macaroon)
1587+
)
1588+
)
1589+
record_event = pretend.call_recorder(lambda *a, **kw: None)
1590+
user_service = pretend.stub(record_event=record_event)
1591+
request = pretend.stub(
1592+
POST={},
1593+
domain=pretend.stub(),
1594+
user=pretend.stub(
1595+
id=pretend.stub(),
1596+
has_primary_verified_email=True,
1597+
username=pretend.stub(),
1598+
projects=[
1599+
pretend.stub(name="foo", record_event=record_event),
1600+
pretend.stub(name="bar", record_event=record_event),
1601+
],
1602+
),
1603+
find_service=lambda interface, **kw: {
1604+
IMacaroonService: macaroon_service,
1605+
IUserService: user_service,
1606+
}[interface],
1607+
remote_addr="0.0.0.0",
1608+
)
1609+
1610+
create_macaroon_obj = pretend.stub(
1611+
validate=lambda: True,
1612+
description=pretend.stub(data=pretend.stub()),
1613+
validated_scope={"projects": ["foo", "bar"]},
1614+
)
1615+
create_macaroon_cls = pretend.call_recorder(
1616+
lambda *a, **kw: create_macaroon_obj
1617+
)
1618+
monkeypatch.setattr(views, "CreateMacaroonForm", create_macaroon_cls)
1619+
1620+
project_names = [pretend.stub()]
1621+
monkeypatch.setattr(
1622+
views.ProvisionMacaroonViews, "project_names", project_names
1623+
)
1624+
1625+
default_response = {"default": "response"}
1626+
monkeypatch.setattr(
1627+
views.ProvisionMacaroonViews, "default_response", default_response
1628+
)
1629+
1630+
view = views.ProvisionMacaroonViews(request)
1631+
result = view.create_macaroon()
1632+
1633+
assert macaroon_service.create_macaroon.calls == [
1634+
pretend.call(
1635+
location=request.domain,
1636+
user_id=request.user.id,
1637+
description=create_macaroon_obj.description.data,
1638+
caveats={
1639+
"permissions": create_macaroon_obj.validated_scope,
1640+
"version": 1,
1641+
},
1642+
)
1643+
]
1644+
assert result == {
1645+
**default_response,
1646+
"serialized_macaroon": "not a real raw macaroon",
1647+
"macaroon": macaroon,
1648+
"create_macaroon_form": create_macaroon_obj,
1649+
}
1650+
assert record_event.calls == [
1651+
pretend.call(
1652+
request.user.id,
1653+
tag="account:api_token:added",
1654+
ip_address=request.remote_addr,
1655+
additional={
1656+
"description": create_macaroon_obj.description.data,
1657+
"caveats": {
1658+
"permissions": create_macaroon_obj.validated_scope,
1659+
"version": 1,
1660+
},
1661+
},
1662+
),
1663+
pretend.call(
1664+
tag="project:api_token:added",
1665+
ip_address=request.remote_addr,
1666+
additional={
1667+
"description": create_macaroon_obj.description.data,
1668+
"user": request.user.username,
1669+
},
1670+
),
1671+
pretend.call(
1672+
tag="project:api_token:added",
1673+
ip_address=request.remote_addr,
1674+
additional={
1675+
"description": create_macaroon_obj.description.data,
1676+
"user": request.user.username,
1677+
},
1678+
),
1679+
]
15671680

15681681
def test_delete_macaroon_invalid_form(self, monkeypatch):
15691682
macaroon_service = pretend.stub(

0 commit comments

Comments
 (0)