Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def setUpClass(cls):
name="Epic", location=cls.centre
)
cls.other_centre = apps.get_model("core", "AndelaCentre").objects.create(
id=80, name="Epic", country=cls.country
name="Epic", country=cls.country
)
cls.office_block_2 = apps.get_model("core", "OfficeBlock").objects.create(
name="ET", location=cls.other_centre
Expand Down Expand Up @@ -248,6 +248,7 @@ def setUpClass(cls):
cls.incident_report_status_url = reverse(
"state-transitions-detail", kwargs={"pk": str(cls.report_status.id)}
)
cls.state_transitions_url = reverse("state-transitions-list")
cls.manage_asset_urls = reverse("manage-assets-list")
cls.office_block_url = reverse("office-blocks-list")
cls.office_workspace_url = reverse("office-workspaces-list")
Expand Down
110 changes: 88 additions & 22 deletions api/tests/test_allocation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,14 @@
client = APIClient()


class AllocationTestCase(APIBaseTestCase):
def test_non_authenticated_user_view_assets(self):
response = client.get(self.allocations_urls)
class Post_AllocationTestCase(APIBaseTestCase):
def test_non_authenticated_post_allocation_of_assets(self):
data = {"asset": self.asset.id, "current_assignee": self.asset_assignee.id}
response = client.post(self.allocations_urls, data)
self.assertEqual(
response.data, {"detail": "Authentication credentials were not provided."}
)

@patch("api.authentication.auth.verify_id_token")
def test_get_allocations(self, mock_verify_id_token):
"""Test get allocations"""

mock_verify_id_token.return_value = {"email": self.other_user.email}
AllocationHistory.objects.create(
asset=self.asset,
current_assignee=self.asset_assignee,
assigner=self.other_user,
)
response = client.get(
self.allocations_urls, HTTP_AUTHORIZATION=f"Token {self.token_other_user}"
)

data = response.data["results"]
self.assertEqual(len(data), AllocationHistory.objects.count())
self.assertEqual(response.status_code, 200)
self.assertEqual(data[0]["assigner"], self.other_user.email)

@patch("api.authentication.auth.verify_id_token")
def test_post_allocation_of_asset_to_a_user(self, mock_verify_id_token):
"""Test post new allocation"""
Expand All @@ -60,6 +42,15 @@ def test_post_allocation_of_asset_to_a_user(self, mock_verify_id_token):
self.assertEqual(response.status_code, 201)
self.assertIn("assigner", response.data)

def test_post_allocation_of_asset_to_a_user_with_invlaid_token(self):
count = AllocationHistory.objects.count()
data = {"asset": self.asset.id, "current_assignee": self.asset_assignee.id}
response = client.post(
self.allocations_urls, data, HTTP_AUTHORIZATION="Token token"
)
self.assertEqual(response.data["detail"], "User not found")
self.assertEqual(response.status_code, 401)

@patch("api.authentication.auth.verify_id_token")
def test_post_re_allocation_of_asset_to_a_user(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.other_user.email}
Expand All @@ -76,6 +67,10 @@ def test_post_re_allocation_of_asset_to_a_user(self, mock_verify_id_token):
data,
HTTP_AUTHORIZATION="Token {}".format(self.token_user),
)
self.assertEqual(
response.data["asset"],
f"{self.asset.serial_number} - {self.asset.asset_code}",
)
self.assertEqual(response.data["previous_assignee"], self.user.email)
self.assertEqual(response.status_code, 201)

Expand Down Expand Up @@ -139,6 +134,27 @@ def test_asset_status_changes_to_allocated(self, mock_verify_id_token):
self.assertEqual(response.data["serial_number"], self.asset.serial_number)
self.assertEqual(response.status_code, 200)


class Get_AllocationTestCase(APIBaseTestCase):
@patch("api.authentication.auth.verify_id_token")
def test_get_allocations(self, mock_verify_id_token):
"""Test get allocations"""

mock_verify_id_token.return_value = {"email": self.other_user.email}
AllocationHistory.objects.create(
asset=self.asset,
current_assignee=self.asset_assignee,
assigner=self.other_user,
)
response = client.get(
self.allocations_urls, HTTP_AUTHORIZATION=f"Token {self.token_other_user}"
)

data = response.data["results"]
self.assertEqual(len(data), AllocationHistory.objects.count())
self.assertEqual(response.status_code, 200)
self.assertEqual(data[0]["assigner"], self.other_user.email)

@patch("api.authentication.auth.verify_id_token")
def test_filter_allocations_by_asset_owner(self, mock_verify_id_token):

Expand Down Expand Up @@ -250,3 +266,53 @@ def test_filter_allocations_by_asset_code(self, mock_verify_id_token):
response.data["results"][0]["asset"],
f"{self.asset.serial_number} - {self.asset.asset_code}",
)

def test_non_authenticated_user_view_assets(self):
response = client.get(self.allocations_urls)
self.assertEqual(
response.data, {"detail": "Authentication credentials were not provided."}
)


class Update_AllocationTestCase(APIBaseTestCase):
@patch("api.authentication.auth.verify_id_token")
def test_cannot_update_allocations(self, mock_verify_id_token):
"""Test cannot update allocations"""

mock_verify_id_token.return_value = {"email": self.other_user.email}
allocation = AllocationHistory.objects.create(
asset=self.asset,
current_assignee=self.asset_assignee,
assigner=self.other_user,
)
allocations_url = (
f"{self.allocations_urls}/?asset_serial_number={allocation.id}"
)
data = {"asset": self.asset.id, "current_assignee": self.asset_assignee.id}
response = client.put(
allocations_url, data, HTTP_AUTHORIZATION="Token {}".format(self.token_user)
)
self.assertEqual(response.status_code, 405)
self.assertEqual(response.data["detail"], 'Method "PUT" not allowed.')


class Delete_AllocationTestCase(APIBaseTestCase):
@patch("api.authentication.auth.verify_id_token")
def test_cannot_delete_allocations(self, mock_verify_id_token):
"""Test cannot delete allocations"""

mock_verify_id_token.return_value = {"email": self.other_user.email}
allocation = AllocationHistory.objects.create(
asset=self.asset,
current_assignee=self.asset_assignee,
assigner=self.other_user,
)
allocations_url = (
f"{self.allocations_urls}/?asset_serial_number={allocation.id}"
)
data = {"asset": self.asset.id, "current_assignee": self.asset_assignee.id}
response = client.delete(
allocations_url, data, HTTP_AUTHORIZATION="Token {}".format(self.token_user)
)
self.assertEqual(response.status_code, 405)
self.assertEqual(response.data["detail"], 'Method "DELETE" not allowed.')
110 changes: 104 additions & 6 deletions api/tests/test_andela_centre_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@
client = APIClient()


class AndelaCentreAPITest(APIBaseTestCase):
""" Tests for the Andela Centre endpoint"""
class Post_AndelaCentreAPITest(APIBaseTestCase):
""" Tests creation of Andela Centre endpoint"""

def test_non_authenticated_user_get_centres(self):
response = client.get(self.centre_url)
def test_non_authenticated_users_cannot_create_centres(self):
data = {"name": "ET", "country": self.country.id}
response = client.post(self.centre_url, data=data)
self.assertEqual(
response.data, {"detail": "Authentication credentials were not provided."}
)

def test_create_centres_with_invlaid_token_fails(self):
data = {"name": "ET", "country": self.country.id}
response = client.post(
self.centre_url, data=data, HTTP_AUTHORIZATION="Token token"
)
self.assertEqual(response.data["detail"], "User not found")
self.assertEqual(response.status_code, 401)

@patch("api.authentication.auth.verify_id_token")
def test_can_post_centre(self, mock_verify_token):
def test_can_create_centre(self, mock_verify_token):
mock_verify_token.return_value = {"email": self.admin_user.email}
data = {"name": "ET", "country": self.country.id}
response = client.post(
Expand All @@ -35,6 +44,7 @@ def test_can_post_centre(self, mock_verify_token):
self.assertIn("name", response.data.keys())
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data.get("country"), self.country.name)
self.assertEqual(response.data.get("name"), "ET")

@patch("api.authentication.auth.verify_id_token")
def test_cannot_post_centre_by_non_superuser(self, mock_verify_token):
Expand Down Expand Up @@ -72,8 +82,63 @@ def test_cant_post_centre_with_same_name(self, mock_verify_token):
self.assertIn("name", response.data.keys())
self.assertEqual(response.status_code, 400)


class Get_AndelaCentreAPITest(APIBaseTestCase):
"Test fetching of andela centres"

def test_non_authenticated_users_cannot_get_centres(self):
response = client.get(self.centre_url)
self.assertEqual(
response.data, {"detail": "Authentication credentials were not provided."}
)

@patch("api.authentication.auth.verify_id_token")
def test_editing_centre(self, mock_verify_id_token):
def test_get_centre_by_id(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
centre_url = reverse("andela-centres-detail", args={self.centre.id})
res = client.get(
centre_url, HTTP_AUTHORIZATION="Token {}".format(self.token_user)
)
self.assertEqual(res.data["name"], self.centre.name)
self.assertEqual(res.data["country"], self.centre.country.name)
self.assertEqual(res.data["id"], self.centre.id)
self.assertEqual(res.status_code, 200)

@patch("api.authentication.auth.verify_id_token")
def test_get_centres(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
res = client.get(
self.centre_url, HTTP_AUTHORIZATION="Token {}".format(self.token_user)
)
self.assertEqual(res.data["results"][0]["name"], self.centre.name)
self.assertEqual(res.data["results"][0]["country"], self.centre.country.name)
self.assertEqual(res.data["results"][0]["id"], self.centre.id)
self.assertEqual(res.status_code, 200)

@patch("api.authentication.auth.verify_id_token")
def test_get_centre_with_invalid_id_fails(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
centre_url = reverse("andela-centres-detail", args={209})
res = client.get(
centre_url, HTTP_AUTHORIZATION="Token {}".format(self.token_user)
)
self.assertEqual(res.data["detail"], "Not found.")
self.assertEqual(res.status_code, 404)


class Edit_AndelaCentreAPITest(APIBaseTestCase):
"Test editing of andela centres"

def test_non_authenticated_users_cannot_update_centres(self):
data = {"name": "Matoke", "country": self.country.id}
centre_url = reverse("andela-centres-detail", args={self.centre.id})
response = client.get(centre_url, data=data)
self.assertEqual(
response.data, {"detail": "Authentication credentials were not provided."}
)

@patch("api.authentication.auth.verify_id_token")
def test_editing_centre_succeeds(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
data = {"name": "Matoke", "country": self.country.id}
res = client.post(
Expand All @@ -89,8 +154,31 @@ def test_editing_centre(self, mock_verify_id_token):
HTTP_AUTHORIZATION="Token {}".format(self.token_user),
)
self.assertEqual(res.data["name"], "Gorilla")
self.assertEqual(res.data["country"], country.name)
self.assertEqual(res.status_code, 200)

@patch("api.authentication.auth.verify_id_token")
def test_edit_centre_with_invalid_id_fails(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
data = {"name": "Matoke", "country": self.country.id}
centre_url = reverse("andela-centres-detail", args={200})
res = client.put(
centre_url, data=data, HTTP_AUTHORIZATION="Token {}".format(self.token_user)
)
self.assertEqual(res.data["detail"], "Not found.")
self.assertEqual(res.status_code, 404)


class Delete_AndelaCentreAPITest(APIBaseTestCase):
"Test deleting of andela centres"

def test_non_authenticated_users_cannot_delete_centres(self):
centre_url = reverse("andela-centres-detail", args={self.centre.id})
response = client.delete(centre_url)
self.assertEqual(
response.data, {"detail": "Authentication credentials were not provided."}
)

@patch("api.authentication.auth.verify_id_token")
def test_can_delete_centre(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
Expand All @@ -110,6 +198,16 @@ def test_can_delete_centre(self, mock_verify_id_token):
self.assertEqual(response.data, {"detail": "Deleted Successfully"})
self.assertEqual(response.status_code, 204)

@patch("api.authentication.auth.verify_id_token")
def test_delete_centre_with_invalid_id_fails(self, mock_verify_id_token):
mock_verify_id_token.return_value = {"email": self.admin_user.email}
centre_url = reverse("andela-centres-detail", args={200})
res = client.delete(
centre_url, HTTP_AUTHORIZATION="Token {}".format(self.token_user)
)
self.assertEqual(res.data["detail"], "Not found.")
self.assertEqual(res.status_code, 404)

@patch("api.authentication.auth.verify_id_token")
def test_country_create(self, mock_verify_token):
mock_verify_token.return_value = {"email": self.admin_user.email}
Expand Down
Loading