def test_sc_homepage_summary_complete_with_archived_SAs(
        logged_in_client, test_user):
    # Arrange
    active_sa = SupplyChainFactory(
        name="Medical",
        gov_department=test_user.gov_department,
    )
    sc = SupplyChainFactory(
        name="carbon",
        gov_department=test_user.gov_department,
    )

    sa = StrategicActionFactory(supply_chain=active_sa)
    StrategicActionFactory.create_batch(3,
                                        supply_chain=sc,
                                        is_archived=True,
                                        archived_reason="Reason")

    StrategicActionUpdateFactory()
    dummy_qs = QuerySet(model=StrategicActionUpdate)

    # Act
    with mock.patch(
            "supply_chains.models.SupplyChainQuerySet.submitted_since",
            return_value=dummy_qs,
    ):
        resp = logged_in_client.get(reverse("sc-home"))

    # Assert
    assert resp.context["update_complete"]
    assert resp.context["num_in_prog_supply_chains"] == 0
    assert len(resp.context["supply_chains"]) == 2
def test_strat_action_summary_page_pagination(logged_in_client, test_user):
    """Test pagination of strategic actions returned in context."""
    sc = SupplyChainFactory(gov_department=test_user.gov_department)
    StrategicActionFactory.create_batch(14, supply_chain=sc)
    response = logged_in_client.get(
        "%s?page=3" % reverse("strategic-action-summary", args=[sc.slug]))
    assert response.status_code == 200
    assert len(response.context["strategic_actions"]) == 4
def tasklist_stub(test_user):
    sc_name = "Supply Chain 1"
    sa_description = "1234567890qweertyuiodfsfgfgggsf"
    sa_name = "SA 00"
    sc = SupplyChainFactory(name=sc_name,
                            gov_department=test_user.gov_department)
    sa = StrategicActionFactory.create_batch(4,
                                             name=sa_name,
                                             description=sa_description,
                                             supply_chain=sc)
    yield {
        "sc_name":
        sc_name,
        "sa_description":
        sa_description,
        "sa_name":
        sa_name,
        "url":
        reverse("supply-chain-task-list",
                kwargs={"supply_chain_slug": slugify(sc_name)}),
        "sc":
        sc,
        "sa":
        sa,
    }
    def test_pagination(self, logged_in_client, test_user, sc_name, num_sas,
                        url, actions_returned):
        # Arrange
        sc = SupplyChainFactory(name=sc_name,
                                gov_department=test_user.gov_department)
        StrategicActionFactory.create_batch(num_sas,
                                            name=f"{sc_name} 00",
                                            supply_chain=sc)

        # Act
        resp = logged_in_client.get(url)

        # Assert
        p = resp.context["view"].sa_updates

        assert resp.status_code == 200
        assert len(p.object_list) == actions_returned
    def test_archived_actions_dont_appear_in_task_list(self, logged_in_client,
                                                       tasklist_stub):
        # Arrange
        sc = tasklist_stub["sc"]
        StrategicActionFactory.create_batch(5,
                                            is_archived=True,
                                            archived_reason="Reason",
                                            supply_chain=sc)

        # Act
        resp = logged_in_client.get(tasklist_stub["url"])

        # Assert
        num_unarchived_actions = StrategicAction.objects.filter(
            supply_chain=sc, is_archived=False).count()
        v = resp.context["view"]
        assert v.total_sa == num_unarchived_actions
def test_strat_action_summary_page_success(logged_in_client, test_user):
    """Test authenticated request.

    When an authorised request is made to the strategic_action_summary URL,
    from a user linked to the same gov department as the strategic actions,
    the correct supply chain and only un_archived strategic actions are
    returned in context.
    """
    sc = SupplyChainFactory(gov_department=test_user.gov_department)
    StrategicActionFactory.create_batch(5, supply_chain=sc)
    StrategicActionFactory.create_batch(2,
                                        supply_chain=sc,
                                        is_archived=True,
                                        archived_reason="A reason")
    response = logged_in_client.get(
        reverse("strategic-action-summary", args=[sc.slug]))
    assert response.status_code == 200
    assert response.context["supply_chain"] == sc
    assert len(response.context["strategic_actions"]) == 5
def test_sc_homepage_summary_with_archived_SAs(logged_in_client, test_user):
    # Arrange
    active_sa = SupplyChainFactory(
        name="Medical",
        gov_department=test_user.gov_department,
    )
    sc = SupplyChainFactory(
        name="carbon",
        gov_department=test_user.gov_department,
    )

    StrategicActionFactory.create_batch(5, supply_chain=active_sa)
    StrategicActionFactory.create_batch(3,
                                        supply_chain=sc,
                                        is_archived=True,
                                        archived_reason="Reason")

    # Act
    resp = logged_in_client.get(reverse("sc-home"))

    # Assert
    assert resp.context["update_complete"] == False
    assert resp.context["num_in_prog_supply_chains"] == 1
    assert len(resp.context["supply_chains"]) == 2
def test_sc_homepage_filters_out_archived_SAs(logged_in_client, test_user):
    # Arrange
    sc = SupplyChainFactory(
        name="Medical",
        gov_department=test_user.gov_department,
    )
    archived_count = 0
    sas = StrategicActionFactory.create_batch(10, supply_chain=sc)
    for i in range(len(sas)):
        if i % 2 == 0:
            archived_count += 1
            sas[i].is_archived = True
            sas[i].archived_reason = "Reason"
            sas[i].save()

    # Act
    resp = logged_in_client.get(reverse("sc-home"))

    # Assert
    supply_chain = resp.context["supply_chains"].object_list[0]
    assert supply_chain["sa_count"] == archived_count