Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smdirr committed Jun 14, 2024
1 parent a20f6c4 commit ab2d72f
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/.coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
omit =
manage.py
119 changes: 119 additions & 0 deletions src/person/tests/test_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,83 @@ def test_get_person(self):
self.assertEqual(response.json()[0]["first_name"], "John")
self.assertEqual(response.json()[0]["email"], "john@test.com")

def test_create_person(self):

data = {
"first_name": "Jenifer",
"last_name": "Lopez",
"email": "jlo@mail.com",
"birth_date": "1970-03-21",
}
response = self.client.post(
reverse("api:person_create"),
data=data,
content_type="application/json",
headers={"Authorization": f"Bearer {self.access_token}"},
)

self.assertEqual(response.status_code, 200)
self.assertGreater(response.json().get("id"), 0)

def test_update_person(self):
data = {
"first_name": "Jenifer",
"last_name": "Lopez",
"email": "jlo@mail.com",
"birth_date": "1970-03-21",
}
response = self.client.post(
reverse("api:person_create"),
data=data,
content_type="application/json",
headers={"Authorization": f"Bearer {self.access_token}"},
)

self.assertEqual(response.status_code, 200)
id = response.json().get("id")
url = reverse("api:person_edit", kwargs={"person_id": id})
data["last_name"] = "López"
response = self.client.put(
url,
data=data,
content_type="application/json",
headers={"Authorization": f"Bearer {self.access_token}"},
)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json().get("success"), True)
person = self.client.get(
reverse("api:person_get", args=[id]),
headers={"Authorization": f"Bearer {self.access_token}"},
)

self.assertEqual(person.status_code, 200)
self.assertEqual(person.json()["last_name"], "López")

def test_person_delete(self):
data = {
"first_name": "Jenifer",
"last_name": "Lopez",
"email": "jlo@mail.com",
"birth_date": "1970-03-21",
}
response = self.client.post(
reverse("api:person_create"),
data=data,
content_type="application/json",
headers={"Authorization": f"Bearer {self.access_token}"},
)

self.assertEqual(response.status_code, 200)
id = response.json().get("id")
url = reverse("api:person_delete", kwargs={"person_id": id})
response = self.client.delete(
url,
headers={"Authorization": f"Bearer {self.access_token}"},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json().get("success"), True)

def test_get_person_401(self):
response = self.client.get(reverse("api:person_list"))
self.assertEqual(response.status_code, 401)
Expand Down Expand Up @@ -79,3 +156,45 @@ def test_str_person(self):
first_name="John", last_name="Doe", email="john@test.com"
)
self.assertEqual(str(person), "Doe, John")

def test_register_pass_no_match(self):
data = {
"first_name": "Mick",
"last_name": "Jaegger",
"email": "mj@mail.me",
"password": "match",
"confirm_password": "no match",
}
response = self.client.post(
reverse("register"), data=data, content_type="application/json"
)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json().get("error"), "Passwords do not match")

def test_register_username_already_exist(self):
data = {
"first_name": "Mick",
"last_name": "Jaegger",
"email": "tand@mail.me",
"password": "test@1234",
"confirm_password": "test@1234",
}
response = self.client.post(
reverse("register"), data=data, content_type="application/json"
)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json().get("error"), "Username already taken")

def test_register_invalid_body(self):
invalid_json = "{email: 'test@example.com', password: 'pass123', confirm_password: 'pass123'"
response = self.client.post(
reverse("register"), data=invalid_json, content_type="application/json"
)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json().get("error"), "Invalid JSON")

def test_register_invalid_request_method(self):

response = self.client.get(reverse("register"), content_type="application/json")
self.assertEqual(response.status_code, 405)
self.assertEqual(response.json().get("error"), "Invalid request method")
3 changes: 0 additions & 3 deletions src/person/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def register(request):
if User.objects.filter(username=email).exists():
return JsonResponse({"error": "Username already taken"}, status=400)

if User.objects.filter(email=email).exists():
return JsonResponse({"error": "Email already taken"}, status=400)

user = User.objects.create_user(username=email, email=email, password=password)
user.save()

Expand Down
Empty file.
44 changes: 44 additions & 0 deletions src/time_tracker/tests/test_security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.test import TestCase, Client
from django.urls import reverse


class TestPerson(TestCase):

def setUp(self) -> None:
super().setUp()
self.client = Client()
my_pass = "devtestlocal"
data = {
"first_name": "Thomas",
"last_name": "Anderson",
"email": "tand@mail.me",
"password": f"{my_pass}",
"confirm_password": f"{my_pass}",
}
self.client.post(
reverse("register"), data=data, content_type="application/json"
)
token = self.client.post(
reverse("token_obtain_pair"),
{"username": data.get("email"), "password": f"{my_pass}"},
content_type="application/json",
)
self.access_token = token.json().get("access")

def test_request_ok(self):

response = self.client.get(
reverse("api:person_list"),
headers={"Authorization": f"Bearer {self.access_token}"},
)

self.assertEqual(response.status_code, 200)

def test_request_invalid(self):

response = self.client.get(
reverse("api:person_list"),
headers={"Authorization": "Bearer 1Nv4l1d70k3n"},
)

self.assertEqual(response.status_code, 401)

0 comments on commit ab2d72f

Please sign in to comment.