Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit c3a10ad

Browse files
committed
Expose enable_state id in v3 API
1 parent 091bc5f commit c3a10ad

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

normandy/base/tests/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ def contains(cls, *values):
2929
@classmethod
3030
def iso8601(cls):
3131
def is_iso8601_date(s):
32-
# Will throw is it does not match
33-
datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ")
34-
return True
32+
if not isinstance(s, str):
33+
return False
34+
try:
35+
datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ")
36+
return True
37+
except ValueError:
38+
return False
3539

3640
return cls(is_iso8601_date, name="datetime")
3741

normandy/recipes/api/v3/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class EnabledStateSerializer(CustomizableSerializerMixin, serializers.ModelSeria
5252

5353
class Meta:
5454
model = EnabledState
55-
fields = ["revision_id", "created", "creator", "enabled", "carryover_from"]
55+
fields = ["id", "revision_id", "created", "creator", "enabled", "carryover_from"]
5656

5757

5858
class RecipeRevisionSerializer(serializers.ModelSerializer):

normandy/recipes/models.py

100755100644
File mode changed.

normandy/recipes/tests/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import factory
66

7-
from normandy.base.tests import FuzzyUnicode
7+
from normandy.base.tests import FuzzyUnicode, UserFactory
88
from normandy.base.utils import sri_hash
99
from normandy.recipes.models import (
1010
Action,
@@ -169,6 +169,7 @@ class EnabledStateFactory(factory.DjangoModelFactory):
169169
class Meta:
170170
model = EnabledState
171171

172+
creator = factory.SubFactory(UserFactory)
172173
revision = factory.SubFactory(RecipeRevisionFactory)
173174

174175

normandy/recipes/tests/api/v3/test_serializers.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from rest_framework import serializers
33

4+
from normandy.base.tests import UserFactory, Whatever
45
from normandy.recipes.tests import (
56
ARGUMENTS_SCHEMA,
67
ActionFactory,
@@ -9,6 +10,7 @@
910
)
1011
from normandy.recipes.api.v3.serializers import (
1112
ActionSerializer,
13+
EnabledStateSerializer,
1214
RecipeRevisionSerializer,
1315
RecipeSerializer,
1416
)
@@ -181,3 +183,55 @@ def test_it_uses_cdn_url(self, rf, settings):
181183
action = ActionFactory()
182184
serializer = ActionSerializer(action, context={"request": rf.get("/")})
183185
assert serializer.data["implementation_url"].startswith(settings.CDN_URL)
186+
187+
188+
@pytest.mark.django_db()
189+
class TestEnabledStateSerializer:
190+
def test_it_works(self):
191+
author = UserFactory()
192+
reviewer = UserFactory()
193+
194+
recipe = RecipeFactory(name="first", user=author)
195+
revision1 = recipe.latest_revision
196+
approval_request = revision1.request_approval(author)
197+
approval_request.approve(reviewer, "r+")
198+
revision1.enable(author)
199+
state1 = revision1.enabled_state
200+
assert state1 is not None
201+
202+
recipe.revise("second", user=author)
203+
revision2 = recipe.latest_revision
204+
approval_request = revision2.request_approval(author)
205+
approval_request.approve(reviewer, "r+")
206+
state2 = revision2.enabled_state
207+
assert state2 is not None
208+
209+
serializer = EnabledStateSerializer(state1)
210+
assert serializer.data == {
211+
"id": state1.id,
212+
"carryover_from": None,
213+
"created": Whatever.iso8601(),
214+
"creator": {
215+
"id": author.id,
216+
"first_name": author.first_name,
217+
"last_name": author.last_name,
218+
"email": author.email,
219+
},
220+
"enabled": True,
221+
"revision_id": revision1.id,
222+
}
223+
224+
serializer = EnabledStateSerializer(state2)
225+
assert serializer.data == {
226+
"id": state2.id,
227+
"carryover_from": state1.id,
228+
"created": Whatever.iso8601(),
229+
"creator": {
230+
"id": reviewer.id,
231+
"first_name": reviewer.first_name,
232+
"last_name": reviewer.last_name,
233+
"email": reviewer.email,
234+
},
235+
"enabled": True,
236+
"revision_id": revision2.id,
237+
}

0 commit comments

Comments
 (0)