Skip to content

Commit ec84c18

Browse files
authored
Merge pull request #215 from praekeltfoundation/clinic_code_validation
clinic code validation
2 parents 0a62526 + 04d618f commit ec84c18

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

README.rst

+6-5
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ To set up and run ``rp-sidekick`` locally, do the following::
3333

3434
$ git clone [email protected]:praekeltfoundation/rp-sidekick.git
3535
$ cd rp-sidekick
36-
$ virtualenv ve
37-
$ source ve/bin/activate
38-
$ pip install -e .
39-
$ pip install -r requirements-dev.txt
40-
$ pre-commit install
36+
$ uv sync
4137

4238
RP-Sidekick does not work with SQLite because it uses `JSONFields`_.
4339
This means that you will need to set up PostgreSQL locally. You can spin up a
4440
local db with docker, using the following command::
4541

4642
$ docker run -d -p 5432:5432 --name=sidekick_db -e POSTGRES_DB=rp_sidekick postgres:9.6
4743

44+
-----
45+
To run tests
46+
-----
47+
$ uv run pytest
48+
4849
-----
4950
Tools
5051
-----

momconnect/tests/test_utils.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66
class TestUtils(TestCase):
77
def test_clean_clinic_code(self):
88
"""
9-
Test that the clean_clinic_code function removes spaces from the input string.
9+
Test that the clean_clinic_code function removes spaces
10+
from the input string.
1011
"""
1112
self.assertEqual(clean_clinic_code("123 456"), "123456")
1213
self.assertEqual(clean_clinic_code(" 789 "), "789")
13-
self.assertEqual(clean_clinic_code("abc def"), "abcdef")
14+
self.assertEqual(clean_clinic_code("abc def"), "")
1415
self.assertEqual(clean_clinic_code(""), "")
16+
self.assertEqual(clean_clinic_code("123-456"), "123456")
17+
self.assertEqual(clean_clinic_code('"123456"'), "123456")
18+
self.assertEqual(clean_clinic_code("123456 clinic"), "123456")
1519

1620
def test_validate_clinic_code(self):
1721
"""
18-
Test that the validate_clinic_code function checks if the clinic code is valid.
19-
A valid clinic code must be exactly 6 digits long and contain only numbers.
22+
Test that the validate_clinic_code function checks if the clinic code
23+
is valid.
24+
A valid clinic code must be exactly 6 digits long and contain only
25+
numbers.
2026
"""
2127
self.assertTrue(validate_clinic_code("123456"))
22-
self.assertTrue(validate_clinic_code(" 123 456 "))
2328
self.assertFalse(validate_clinic_code("12345"))
2429
self.assertFalse(validate_clinic_code("1234567"))
2530
self.assertFalse(validate_clinic_code("12345a"))

momconnect/tests/test_views.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def setUp(self):
2222

2323
def test_unauthenticated_client(self):
2424
"""
25-
Test that an unauthenticated client receives a 401 Unauthorized response
26-
when attempting to access the clinic details endpoint.
25+
Test that an unauthenticated client receives a 401 Unauthorized
26+
response when attempting to access the clinic details endpoint.
2727
"""
2828
response = self.client.get(self.url, {"clinic_code": self.clinic.value})
2929
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
@@ -64,3 +64,51 @@ def test_successful_clinic_retrieval(self):
6464
self.assertEqual(response.status_code, status.HTTP_200_OK)
6565
self.assertEqual(response.data["code"], self.clinic.value)
6666
self.assertEqual(response.data["name"], self.clinic.name)
67+
68+
def test_clinic_code_with_space(self):
69+
"""
70+
Test that the endpoint returns a 200 successfully retrieves clinic
71+
details when a valid clinic_code is contains a space.
72+
"""
73+
response = self.api_client.get(self.url, {"clinic_code": "123 456"})
74+
self.assertEqual(response.status_code, status.HTTP_200_OK)
75+
self.assertEqual(response.data["code"], self.clinic.value)
76+
self.assertEqual(response.data["name"], self.clinic.name)
77+
78+
def test_clinic_code_with_clinic_name(self):
79+
"""
80+
Test that the endpoint returns a 200 successfully retrieves clinic
81+
details when a valid clinic_code is contains a clinic name.
82+
"""
83+
response = self.api_client.get(self.url, {"clinic_code": "123456 Test Clinic"})
84+
self.assertEqual(response.status_code, status.HTTP_200_OK)
85+
self.assertEqual(response.data["code"], self.clinic.value)
86+
self.assertEqual(response.data["name"], self.clinic.name)
87+
88+
def test_clinic_code_with_a_dash(self):
89+
"""
90+
Test that the endpoint returns a 200 successfully retrieves clinic
91+
details when a valid clinic_code is contains a dash.
92+
"""
93+
response = self.api_client.get(self.url, {"clinic_code": "123-456"})
94+
self.assertEqual(response.status_code, status.HTTP_200_OK)
95+
self.assertEqual(response.data["code"], self.clinic.value)
96+
self.assertEqual(response.data["name"], self.clinic.name)
97+
98+
def test_clinic_code_with_invalid_code(self):
99+
"""
100+
Test that the endpoint returns a 400 Bad Request response
101+
when the clinic code parameter is invalid.
102+
"""
103+
response = self.api_client.get(self.url, {"clinic_code": "123 clinic"})
104+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
105+
self.assertEqual(response.data, {"error": "invalid"})
106+
107+
def test_clinic_code_not_found(self):
108+
"""
109+
Test that the endpoint returns a 404 Not Found response
110+
when the clinic code provided does not exist.
111+
"""
112+
response = self.api_client.get(self.url, {"clinic_code": "246 810"})
113+
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
114+
self.assertEqual(response.data, {"error": "clinic not found"})

momconnect/utils.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import re
2+
3+
14
def clean_clinic_code(clinic_code):
2-
return str(clinic_code).replace(" ", "")
5+
# Remove non digit and return only digits from clinic code
6+
return re.sub("[^0-9]", "", clinic_code)
37

48

59
def validate_clinic_code(clinic_code):
6-
clinic_code = clean_clinic_code(clinic_code)
710
return clinic_code.isdigit() and len(clinic_code) == 6

momconnect/views.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from rest_framework.response import Response
33

44
from .models import Clinic
5-
from .utils import validate_clinic_code
5+
from .utils import clean_clinic_code, validate_clinic_code
66

77

88
class ClinicDetailsView(generics.RetrieveAPIView):
@@ -14,6 +14,8 @@ def get(self, request):
1414
{"error": "missing clinic code"}, status.HTTP_400_BAD_REQUEST
1515
)
1616

17+
clinic_code = clean_clinic_code(clinic_code)
18+
1719
if not validate_clinic_code(clinic_code):
1820
return Response({"error": "invalid"}, status.HTTP_400_BAD_REQUEST)
1921

0 commit comments

Comments
 (0)