Skip to content

Commit 3b45465

Browse files
author
Gabriel Monroy
committed
validate that App.id only contains [a-z0-9-] and is a valid domain name, with tests
1 parent 4190cef commit 3b45465

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

api/serializers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from __future__ import unicode_literals
77

8+
import re
9+
810
from django.contrib.auth.models import User
911
from rest_framework import serializers
1012

@@ -165,6 +167,16 @@ class Meta:
165167
model = models.App
166168
read_only_fields = ('created', 'updated')
167169

170+
def validate_id(self, attrs, source):
171+
"""
172+
Check that the ID is all lowercase
173+
"""
174+
value = attrs[source]
175+
match = re.match(r'^[a-z0-9-]+$', value)
176+
if not match:
177+
raise serializers.ValidationError("App IDs can only contain [a-z0-9-]")
178+
return attrs
179+
168180

169181
class ContainerSerializer(serializers.ModelSerializer):
170182
"""Serialize a :class:`~api.models.Container` model."""

api/tests/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ def test_app_actions(self):
185185
def test_app_errors(self):
186186
formation_id, app_id = 'autotest', 'autotest-errors'
187187
url = '/api/apps'
188+
body = {'formation': formation_id, 'id': 'camelCase'}
189+
response = self.client.post(url, json.dumps(body), content_type='application/json')
190+
self.assertContains(response, 'App IDs can only contain [a-z0-9-]', status_code=400)
188191
body = {'formation': formation_id, 'id': app_id}
189192
response = self.client.post(url, json.dumps(body), content_type='application/json')
190193
self.assertEqual(response.status_code, 201)

0 commit comments

Comments
 (0)