Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit 9c523a4

Browse files
author
philippe VESSIERE
committed
Fix #130 - Validate IP format in session
. Add a new IPAddressField type. . Update IP type from CharField to IPAddressField. . Add a new Session factory test to know if a session can be instanciated. . Add 2 session tests (IP valid & IP not valid test).
1 parent 10fffaf commit 9c523a4

File tree

5 files changed

+80
-32
lines changed

5 files changed

+80
-32
lines changed

ban/auth/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Session(db.Model):
102102
"""
103103
user = db.ForeignKeyField(User, null=True)
104104
client = db.ForeignKeyField(Client, null=True)
105-
ip = db.CharField(null=True) # TODO IPField
105+
ip = db.IPAddressField(null=True)
106106
email = db.CharField(null=True) # TODO EmailField
107107

108108
@property

ban/db/fields.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import re
33

4+
import ipaddress
45
import peewee
56

67
from playhouse import postgres_ext, fields
@@ -11,7 +12,8 @@
1112
__all__ = ['PointField', 'ForeignKeyField', 'CharField', 'IntegerField',
1213
'HStoreField', 'UUIDField', 'ArrayField', 'DateTimeField',
1314
'BooleanField', 'BinaryJSONField', 'PostCodeField', 'FantoirField',
14-
'ManyToManyField', 'PasswordField', 'DateRangeField', 'TextField']
15+
'IPAddressField', 'ManyToManyField', 'PasswordField',
16+
'DateRangeField', 'TextField']
1517

1618

1719
lonlat_pattern = re.compile('^[\[\(]{1}(?P<lon>-?\d{,3}(:?\.\d*)?), ?(?P<lat>-?\d{,3}(\.\d*)?)[\]\)]{1}$') # noqa
@@ -199,6 +201,21 @@ def coerce(self, value):
199201
return value
200202

201203

204+
class IPAddressField(CharField):
205+
206+
max_length = 100
207+
208+
def coerce(self, value=None):
209+
if value:
210+
value = str(value)
211+
try:
212+
ipaddress.ip_address(value)
213+
except ipaddress.AddressValueError:
214+
raise ValueError('Invalid IP Address "{}"'.format(value))
215+
216+
return value
217+
218+
202219
class ResourceListQueryResultWrapper(peewee.ModelQueryResultWrapper):
203220

204221
def process_row(self, row):

ban/tests/auth/test_models.py

+1-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import pytest
2-
31
from ban.auth import models
4-
from ban.tests.factories import UserFactory, ClientFactory
2+
from ban.tests.factories import UserFactory
53

64

75
def test_user_password_is_hashed():
@@ -12,30 +10,3 @@ def test_user_password_is_hashed():
1210
user = models.User.get(models.User.id == user.id)
1311
assert user.password != password
1412
assert user.check_password(password)
15-
16-
17-
def test_session_can_be_created_with_a_user():
18-
user = UserFactory()
19-
session = models.Session.create(user=user)
20-
assert session.user == user
21-
assert session.as_relation == {
22-
'id': session.pk,
23-
'user': user.username,
24-
'client': None
25-
}
26-
27-
28-
def test_session_can_be_created_with_a_client():
29-
client = ClientFactory()
30-
session = models.Session.create(client=client)
31-
assert session.client == client
32-
assert session.as_relation == {
33-
'id': session.pk,
34-
'user': None,
35-
'client': client.name
36-
}
37-
38-
39-
def test_session_should_have_either_a_client_or_a_user():
40-
with pytest.raises(ValueError):
41-
models.Session.create()

ban/tests/auth/test_session.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
3+
from ban.auth import models
4+
from ban.tests.factories import UserFactory, ClientFactory
5+
6+
7+
def test_session_can_be_created_with_a_user():
8+
user = UserFactory()
9+
session = models.Session.create(user=user)
10+
assert session.user == user
11+
assert session.as_relation == {
12+
'id': session.pk,
13+
'user': user.username,
14+
'client': None
15+
}
16+
17+
18+
def test_session_can_be_created_with_a_client():
19+
client = ClientFactory()
20+
session = models.Session.create(client=client)
21+
assert session.client == client
22+
assert session.as_relation == {
23+
'id': session.pk,
24+
'user': None,
25+
'client': client.name
26+
}
27+
28+
29+
def test_session_should_have_either_a_client_or_a_user():
30+
with pytest.raises(ValueError):
31+
models.Session.create()
32+
33+
34+
def test_session_can_be_created_with_a_client_and_an_valid_IPAddress():
35+
client = ClientFactory()
36+
session = models.Session.create(client=client, ip='192.168.0.1')
37+
assert session.ip == '192.168.0.1'
38+
39+
40+
def test_session_can_not_be_created_with_a_client_and_an_unvalid_IPAddress():
41+
client = ClientFactory()
42+
with pytest.raises(ValueError):
43+
models.Session.create(client=client, ip='999.999.999.999')
44+
45+
46+
def test_session_can_be_created_with_a_user_and_an_valid_IPAddress():
47+
user = UserFactory()
48+
session = models.Session.create(user=user, ip='192.168.0.1')
49+
assert session.ip == '192.168.0.1'
50+
51+
52+
def test_session_can_not_be_created_with_a_user_and_an_unvalid_IPAddress():
53+
user = UserFactory()
54+
with pytest.raises(ValueError):
55+
models.Session.create(user=user, ip='999.999.999.999')

ban/tests/test_factories.py

+5
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ def test_housenumber_can_be_instanciated():
4242
def test_position_can_be_instanciated():
4343
position = factories.PositionFactory()
4444
assert position.center
45+
46+
47+
def test_session_can_be_instanciated():
48+
session = factories.SessionFactory()
49+
assert session.ip == '127.0.0.1'

0 commit comments

Comments
 (0)