Skip to content

Commit d7b266f

Browse files
authored
Serialize a DisclosureOrigin to/from dict for task (#17276)
1 parent 87e5366 commit d7b266f

File tree

5 files changed

+81
-14
lines changed

5 files changed

+81
-14
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
import pytest
14+
15+
from warehouse.integrations.secrets import utils
16+
17+
18+
@pytest.fixture
19+
def someorigin():
20+
return utils.DisclosureOrigin(
21+
name="SomeOrigin",
22+
key_id_header="SOME_KEY_ID_HEADER",
23+
signature_header="SOME_SIGNATURE_HEADER",
24+
verification_url="https://some.verification.url",
25+
)

tests/unit/integration/secrets/test_tasks.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@
1515
from warehouse.integrations.secrets import tasks, utils
1616

1717

18-
def test_analyze_disclosure_task(monkeypatch):
18+
def test_analyze_disclosure_task(monkeypatch, someorigin):
1919
analyze_disclosure = pretend.call_recorder(lambda *a, **k: None)
2020
monkeypatch.setattr(utils, "analyze_disclosure", analyze_disclosure)
2121

2222
request = pretend.stub()
2323
disclosure_record = pretend.stub()
24-
origin = pretend.stub()
2524

2625
tasks.analyze_disclosure_task(
2726
request=request,
2827
disclosure_record=disclosure_record,
29-
origin=origin,
28+
origin=someorigin.to_dict(),
3029
)
3130

3231
assert analyze_disclosure.calls == [
3332
pretend.call(
3433
request=request,
3534
disclosure_record=disclosure_record,
36-
origin=origin,
35+
origin=someorigin,
3736
)
3837
]

tests/unit/integration/secrets/test_utils.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,31 @@
2323
from warehouse.integrations.secrets import tasks, utils
2424

2525

26-
@pytest.fixture
27-
def someorigin():
28-
return utils.DisclosureOrigin(
29-
name="SomeOrigin",
26+
def test_disclosure_origin_serialization(someorigin):
27+
assert (
28+
someorigin.to_dict()
29+
== utils.DisclosureOrigin.from_dict(someorigin.to_dict()).to_dict()
30+
== {
31+
"name": "SomeOrigin",
32+
"key_id_header": "SOME_KEY_ID_HEADER",
33+
"signature_header": "SOME_SIGNATURE_HEADER",
34+
"verification_url": "https://some.verification.url",
35+
"api_token": None,
36+
}
37+
)
38+
39+
40+
def test_disclosure_origin_equivalence(someorigin):
41+
assert someorigin == someorigin
42+
someotherorigin = utils.DisclosureOrigin(
43+
name="SomeOtherOrigin",
3044
key_id_header="SOME_KEY_ID_HEADER",
3145
signature_header="SOME_SIGNATURE_HEADER",
3246
verification_url="https://some.verification.url",
47+
api_token=None,
3348
)
49+
assert someorigin != someotherorigin
50+
assert someorigin != "wu-tang"
3451

3552

3653
def test_token_leak_matcher_extract():
@@ -726,7 +743,7 @@ def test_analyze_disclosures_wrong_type(metrics, someorigin):
726743
assert exc.value.reason == "format"
727744

728745

729-
def test_analyze_disclosures_raise(metrics, monkeypatch):
746+
def test_analyze_disclosures_raise(metrics, monkeypatch, someorigin):
730747
task = pretend.stub(delay=pretend.call_recorder(lambda *a, **k: None))
731748
request = pretend.stub(task=lambda x: task)
732749

@@ -735,12 +752,12 @@ def test_analyze_disclosures_raise(metrics, monkeypatch):
735752
utils.analyze_disclosures(
736753
request=request,
737754
disclosure_records=[1, 2, 3],
738-
origin="yay",
755+
origin=someorigin,
739756
metrics=metrics,
740757
)
741758

742759
assert task.delay.calls == [
743-
pretend.call(disclosure_record=1, origin="yay"),
744-
pretend.call(disclosure_record=2, origin="yay"),
745-
pretend.call(disclosure_record=3, origin="yay"),
760+
pretend.call(disclosure_record=1, origin=someorigin.to_dict()),
761+
pretend.call(disclosure_record=2, origin=someorigin.to_dict()),
762+
pretend.call(disclosure_record=3, origin=someorigin.to_dict()),
746763
]

warehouse/integrations/secrets/tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
@tasks.task(ignore_result=True, acks_late=True)
1919
def analyze_disclosure_task(request, disclosure_record, origin):
20+
origin = utils.DisclosureOrigin.from_dict(origin)
2021
utils.analyze_disclosure(
2122
request=request,
2223
disclosure_record=disclosure_record,

warehouse/integrations/secrets/utils.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ def headers(self):
5050
"""Set of all headers that must be present"""
5151
return {self.key_id_header, self.signature_header}
5252

53+
def to_dict(self):
54+
return {
55+
"name": self.name,
56+
"key_id_header": self.key_id_header,
57+
"signature_header": self.signature_header,
58+
"verification_url": self.verification_url,
59+
"api_token": self.api_token,
60+
}
61+
62+
@classmethod
63+
def from_dict(cls, data):
64+
return cls(**data)
65+
66+
def __eq__(self, other):
67+
if not isinstance(other, DisclosureOrigin):
68+
return False
69+
70+
return (
71+
self.name == other.name
72+
and self.key_id_header == other.key_id_header
73+
and self.signature_header == other.signature_header
74+
and self.verification_url == other.verification_url
75+
and self.api_token == other.api_token
76+
)
77+
5378

5479
class ExtractionFailedError(Exception):
5580
pass
@@ -314,5 +339,5 @@ def analyze_disclosures(request, disclosure_records, origin, metrics):
314339

315340
for disclosure_record in disclosure_records:
316341
request.task(tasks.analyze_disclosure_task).delay(
317-
disclosure_record=disclosure_record, origin=origin
342+
disclosure_record=disclosure_record, origin=origin.to_dict()
318343
)

0 commit comments

Comments
 (0)