Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
update user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yacobolo committed Nov 9, 2023
1 parent 74876ac commit 4a251e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
5 changes: 3 additions & 2 deletions backend/api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from django.core.management import call_command
from django.test import Client
from ninja_jwt.tokens import RefreshToken
from api.tests.factories import UserFactory

client = Client()


# @pytest.fixture(scope="session")
@pytest.fixture()
def user():
def user(load_specific_fixtures):
load_specific_fixtures(["roles"])
return get_user_model().objects.create_user(
username="testuser", password="testpass", email="[email protected]"
)
Expand Down Expand Up @@ -49,6 +49,7 @@ def test_change_password_data():
"new_password": "updatedtestpass",
}


@pytest.fixture
def test_update_password_data():
return {
Expand Down
45 changes: 19 additions & 26 deletions backend/api/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import pytest

from api.tests.factories import UserFactory,UserCreateFactory

from django.urls import reverse



from api.tests.factories import UserCreateFactory, UserFactory


@pytest.mark.django_db
def test_create_user(api_client, load_specific_fixtures):
load_specific_fixtures(["role"])
data = UserFactory.build(role_id=1).dict()
load_specific_fixtures(["roles"])
data = UserFactory.build(role_id=1, resource_ids=[]).dict()
response = api_client.post(
"/api/users/", json.dumps(data, default=str), content_type="application/json"
)
Expand All @@ -32,21 +27,20 @@ def test_get_user(api_client):
instance = UserCreateFactory.create()
print(vars(instance))
# data = instance.dict()
response = api_client.get(
f'/api/users/{instance.id}'
)
response = api_client.get(f"/api/users/{instance.id}")
print(response.content)
assert response.status_code == 200



@pytest.mark.django_db
def test_update_user(api_client):
instance = UserCreateFactory.create()
print(vars(instance))
data = instance.__dict__
response = api_client.put(
f'/api/users/{instance.id}', json.dumps(data, default=str), content_type="application/json"
f"/api/users/{instance.id}",
json.dumps(data, default=str),
content_type="application/json",
)
print(response.content)
assert response.status_code == 200
Expand All @@ -57,37 +51,36 @@ def test_delete_user(api_client):
instance = UserCreateFactory.create()
print(vars(instance))
data = instance.__dict__
response = api_client.delete(
f'/api/users/{instance.id}'
)
response = api_client.delete(f"/api/users/{instance.id}")
print(response.content)
assert response.status_code == 204


@pytest.mark.django_db
def test_change_user_password(api_client,test_change_password_data):
def test_change_user_password(api_client, test_change_password_data):
response = api_client.put(
'/api/users/change-password/', json.dumps(test_change_password_data, default=str), content_type="application/json"
"/api/users/change-password/",
json.dumps(test_change_password_data, default=str),
content_type="application/json",
)
data = response.json()
assert response.status_code == 200
assert data["message"] == "Password changed successfully"


@pytest.mark.django_db
def test_update_user_password(api_client,test_update_password_data):
def test_update_user_password(api_client, test_update_password_data):
response = api_client.post(
'/api/users/update-password', json.dumps(test_update_password_data, default=str), content_type="application/json"
"/api/users/update-password",
json.dumps(test_update_password_data, default=str),
content_type="application/json",
)

assert response.status_code == 200


@pytest.mark.django_db
def test_get_current_user(api_client):
response = api_client.get(
'/api/users/me/'
)

assert response.status_code == 200
response = api_client.get("/api/users/me/")

assert response.status_code == 200

0 comments on commit 4a251e5

Please sign in to comment.