|
| 1 | +from uuid import UUID |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from django.urls import reverse |
3 | 5 | from rest_framework import status |
4 | 6 |
|
5 | 7 | from core.api.serializers import ProgramAreaSerializer |
6 | 8 | from core.api.serializers import UserSerializer |
7 | 9 | from core.models import ProgramArea |
| 10 | +from core.models import ProjectStackElementXref |
8 | 11 | from core.models import UserPermission |
9 | 12 |
|
10 | 13 | pytestmark = pytest.mark.django_db |
|
38 | 41 | SOC_MAJOR_URL = reverse("soc-major-list") |
39 | 42 | SOC_MINORS_URL = reverse("soc-minor-list") |
40 | 43 | URL_TYPE_URL = reverse("url-type-list") |
| 44 | +PROJECT_STACK_ELEMENTS_URL = reverse("project-stack-element-list") |
41 | 45 |
|
42 | 46 | CREATE_USER_PAYLOAD = { |
43 | 47 | "username": "TestUserAPI", |
@@ -608,3 +612,113 @@ def test_project_url_url_type_relationship(auth_client, url_type, project_url): |
608 | 612 |
|
609 | 613 | # Verify the url_type relationship was set correctly |
610 | 614 | assert res.data["url_type"] == url_type.pk |
| 615 | + |
| 616 | + |
| 617 | +def test_create_project_stack_element(auth_client, project, stack_element): |
| 618 | + payload = { |
| 619 | + "project": str(project.uuid), |
| 620 | + "stack_element": str(stack_element.uuid), |
| 621 | + } |
| 622 | + res = auth_client.post(PROJECT_STACK_ELEMENTS_URL, payload) |
| 623 | + assert res.status_code == status.HTTP_201_CREATED |
| 624 | + |
| 625 | + assert UUID(str(res.data["project"])) == project.uuid |
| 626 | + assert UUID(str(res.data["stack_element"])) == stack_element.uuid |
| 627 | + |
| 628 | + |
| 629 | +def test_list_project_stack_elements(auth_client, project_stack_element_xref): |
| 630 | + res = auth_client.get(PROJECT_STACK_ELEMENTS_URL) |
| 631 | + assert res.status_code == status.HTTP_200_OK |
| 632 | + |
| 633 | + # One record created via fixture |
| 634 | + assert len(res.data) == 1 |
| 635 | + assert UUID(str(res.data[0]["project"])) == project_stack_element_xref.project.uuid |
| 636 | + assert ( |
| 637 | + UUID(str(res.data[0]["stack_element"])) |
| 638 | + == project_stack_element_xref.stack_element.uuid |
| 639 | + ) |
| 640 | + |
| 641 | + |
| 642 | +def test_retrieve_project_stack_element(auth_client, project_stack_element_xref): |
| 643 | + url = reverse( |
| 644 | + "project-stack-element-detail", args=[project_stack_element_xref.uuid] |
| 645 | + ) |
| 646 | + res = auth_client.get(url) |
| 647 | + |
| 648 | + assert res.status_code == status.HTTP_200_OK |
| 649 | + assert UUID(str(res.data["uuid"])) == project_stack_element_xref.uuid |
| 650 | + assert UUID(str(res.data["project"])) == project_stack_element_xref.project.uuid |
| 651 | + assert ( |
| 652 | + UUID(str(res.data["stack_element"])) |
| 653 | + == project_stack_element_xref.stack_element.uuid |
| 654 | + ) |
| 655 | + |
| 656 | + |
| 657 | +def test_delete_project_stack_element(auth_client, project_stack_element_xref): |
| 658 | + url = reverse( |
| 659 | + "project-stack-element-detail", args=[project_stack_element_xref.uuid] |
| 660 | + ) |
| 661 | + res = auth_client.delete(url) |
| 662 | + |
| 663 | + assert res.status_code == status.HTTP_204_NO_CONTENT |
| 664 | + assert not ProjectStackElementXref.objects.filter( |
| 665 | + uuid=project_stack_element_xref.uuid |
| 666 | + ).exists() |
| 667 | + |
| 668 | + |
| 669 | +def test_prevent_duplicate_project_stack_element(auth_client, project, stack_element): |
| 670 | + payload = {"project": str(project.uuid), "stack_element": str(stack_element.uuid)} |
| 671 | + |
| 672 | + # First creation works |
| 673 | + res1 = auth_client.post(PROJECT_STACK_ELEMENTS_URL, payload) |
| 674 | + assert res1.status_code == status.HTTP_201_CREATED |
| 675 | + |
| 676 | + # Second creation should fail due to unique constraint |
| 677 | + res2 = auth_client.post(PROJECT_STACK_ELEMENTS_URL, payload) |
| 678 | + assert res2.status_code == status.HTTP_400_BAD_REQUEST |
| 679 | + |
| 680 | + # Assert error mentions uniqueness |
| 681 | + assert any("unique" in str(err).lower() for err in res2.data.values()) |
| 682 | + |
| 683 | + |
| 684 | +def test_project_stack_element_workflow(auth_client): |
| 685 | + # Create a StackElementType |
| 686 | + stack_type_payload = {"name": "Language", "description": "Programming language"} |
| 687 | + res_type = auth_client.post(reverse("stack-element-type-list"), stack_type_payload) |
| 688 | + assert res_type.status_code == status.HTTP_201_CREATED |
| 689 | + stack_type_uuid = UUID(res_type.data["uuid"]) |
| 690 | + |
| 691 | + # Create a StackElement "Python" |
| 692 | + stack_element_payload = { |
| 693 | + "name": "Python", |
| 694 | + "description": "A high-level programming language", |
| 695 | + "url": "https://www.python.org/", |
| 696 | + "logo": "https://upload.wikimedia.org/wikipedia/commons/c/c3/Python-logo-notext.svg", |
| 697 | + "active": True, |
| 698 | + "element_type": stack_type_uuid, |
| 699 | + } |
| 700 | + res_element = auth_client.post(reverse("stack-element-list"), stack_element_payload) |
| 701 | + assert res_element.status_code == status.HTTP_201_CREATED |
| 702 | + stack_element_uuid = UUID(res_element.data["uuid"]) |
| 703 | + |
| 704 | + # Create a Project "PeopleDepot" |
| 705 | + project_payload = { |
| 706 | + "name": "PeopleDepot", |
| 707 | + "description": "People management system", |
| 708 | + "hide": False, |
| 709 | + } |
| 710 | + res_project = auth_client.post(reverse("project-list"), project_payload) |
| 711 | + assert res_project.status_code == status.HTTP_201_CREATED |
| 712 | + project_uuid = UUID(res_project.data["uuid"]) |
| 713 | + |
| 714 | + # Link Project + StackElement |
| 715 | + link_payload = {"project": project_uuid, "stack_element": stack_element_uuid} |
| 716 | + res_link = auth_client.post(reverse("project-stack-element-list"), link_payload) |
| 717 | + assert res_link.status_code == status.HTTP_201_CREATED |
| 718 | + |
| 719 | + # Verify link shows up |
| 720 | + res_list = auth_client.get(reverse("project-stack-element-list")) |
| 721 | + assert res_list.status_code == status.HTTP_200_OK |
| 722 | + assert len(res_list.data) == 1 |
| 723 | + assert res_list.data[0]["project"] == project_uuid |
| 724 | + assert res_list.data[0]["stack_element"] == stack_element_uuid |
0 commit comments