def test_serialize_additional_contact_person():
    acd = AdditionalContactPersonFactory()
    expected = [
        {
            "key": "FIRST_NAME",
            "value": acd.first_name
        },
        {
            "key": "LAST_NAME",
            "value": acd.last_name
        },
        {
            "key": "PHONE",
            "value": acd.phone
        },
        {
            "key": "EMAIL",
            "value": acd.email
        },
    ]

    serialized_acd = acd.serialize()

    assert "key" in serialized_acd
    assert "children" in serialized_acd
    assert serialized_acd["key"] == "ADDITIONALCONTACTPERSON"
    assert len(serialized_acd["children"]) == 4

    for d in expected:
        assert d in serialized_acd["children"]
예제 #2
0
def test_normal_user_can_remove_additional_contact_persons(
    rf, user_gql_client, phone_data
):
    profile = ProfileFactory(user=user_gql_client.user)
    youth_profile = YouthProfileFactory(profile=profile)
    acp = AdditionalContactPersonFactory(youth_profile=youth_profile)
    request = rf.post("/graphql")
    request.user = user_gql_client.user

    variables = {
        "input": {
            "youthProfile": {
                "removeAdditionalContactPersons": [
                    to_global_id(type="AdditionalContactPersonNode", id=acp.pk)
                ]
            }
        }
    }
    executed = user_gql_client.execute(
        UPDATE_MUTATION, context=request, variables=variables
    )

    expected_data = {
        "updateMyYouthProfile": {
            "youthProfile": {"additionalContactPersons": {"edges": []}}
        }
    }
    assert dict(executed["data"]) == expected_data
예제 #3
0
def test_normal_user_can_query_additional_contact_persons(
    rf, user_gql_client, snapshot
):
    profile = ProfileFactory(user=user_gql_client.user)
    youth_profile = YouthProfileFactory(profile=profile)
    acp = AdditionalContactPersonFactory(youth_profile=youth_profile)
    request = rf.post("/graphql")
    request.user = user_gql_client.user

    executed = user_gql_client.execute(
        ADDITIONAL_CONTACT_PERSONS_QUERY, context=request
    )

    expected_data = {
        "youthProfile": {
            "additionalContactPersons": {
                "edges": [
                    {
                        "node": {
                            "id": to_global_id(
                                type="AdditionalContactPersonNode", id=acp.pk
                            ),
                            "firstName": acp.first_name,
                            "lastName": acp.last_name,
                            "phone": acp.phone,
                            "email": acp.email,
                        }
                    }
                ]
            }
        }
    }
    assert dict(executed["data"]) == expected_data
def test_profile_approval_allows_changing_contact_persons(
        rf, anon_user_gql_client, youth_profile, mocker,
        restricted_profile_response):
    mocker.patch.object(
        ProfileAPI,
        "fetch_profile_with_temporary_access_token",
        return_value=restricted_profile_response,
    )
    request = rf.post("/graphql")
    request.user = anon_user_gql_client.user

    acp_new_data = AdditionalContactPersonDictFactory()
    acp_update = AdditionalContactPersonFactory(youth_profile=youth_profile)
    acp_update_values = AdditionalContactPersonDictFactory()
    acp_remove = AdditionalContactPersonFactory(youth_profile=youth_profile)

    variables = {
        "input": {
            "approvalToken": youth_profile.approval_token,
            "approvalData": {
                "addAdditionalContactPersons": [acp_new_data],
                "updateAdditionalContactPersons": [{
                    "id":
                    to_global_id(type="AdditionalContactPersonNode",
                                 id=acp_update.pk),
                    **acp_update_values,
                }],
                "removeAdditionalContactPersons": [
                    to_global_id(type="AdditionalContactPersonNode",
                                 id=acp_remove.pk)
                ],
            },
        }
    }

    anon_user_gql_client.execute(APPROVAL_MUTATION,
                                 context=request,
                                 variables=variables)

    assert not youth_profile.additional_contact_persons.filter(
        pk=acp_remove.pk).exists()
    assert youth_profile.additional_contact_persons.filter(
        pk=acp_update.pk, first_name=acp_update_values["firstName"]).exists()
    assert youth_profile.additional_contact_persons.exclude(
        pk__in=[acp_update.pk, acp_remove.pk]).exists()
def test_profile_approval_allows_changing_contact_persons(
        rf, anon_user_gql_client, youth_profile):
    EmailFactory(primary=True, profile=youth_profile.profile)

    request = rf.post("/graphql")
    request.user = anon_user_gql_client.user

    acp_new_data = AdditionalContactPersonDictFactory()
    acp_update = AdditionalContactPersonFactory(youth_profile=youth_profile)
    acp_update_values = AdditionalContactPersonDictFactory()
    acp_remove = AdditionalContactPersonFactory(youth_profile=youth_profile)

    variables = {
        "input": {
            "approvalToken": youth_profile.approval_token,
            "approvalData": {
                "addAdditionalContactPersons": [acp_new_data],
                "updateAdditionalContactPersons": [{
                    "id":
                    to_global_id(type="AdditionalContactPersonNode",
                                 id=acp_update.pk),
                    **acp_update_values,
                }],
                "removeAdditionalContactPersons": [
                    to_global_id(type="AdditionalContactPersonNode",
                                 id=acp_remove.pk)
                ],
            },
        }
    }

    anon_user_gql_client.execute(APPROVAL_MUTATION,
                                 context=request,
                                 variables=variables)

    assert not youth_profile.additional_contact_persons.filter(
        pk=acp_remove.pk).exists()
    assert youth_profile.additional_contact_persons.filter(
        pk=acp_update.pk, first_name=acp_update_values["firstName"]).exists()
    assert youth_profile.additional_contact_persons.exclude(
        pk__in=[acp_update.pk, acp_remove.pk]).exists()
def test_normal_user_can_update_additional_contact_persons(
        rf, user_gql_client):
    youth_profile = YouthProfileFactory(user=user_gql_client.user)
    acp = AdditionalContactPersonFactory(youth_profile=youth_profile)
    new_values = AdditionalContactPersonDictFactory()
    request = rf.post("/graphql")
    request.user = user_gql_client.user

    variables = {
        "input": {
            "youthProfile": {
                "updateAdditionalContactPersons": [{
                    "id":
                    to_global_id(type="AdditionalContactPersonNode",
                                 id=acp.pk),
                    **new_values,
                }],
            },
            "profileApiToken": "token",
        }
    }
    executed = user_gql_client.execute(UPDATE_MUTATION,
                                       context=request,
                                       variables=variables)

    expected_data = {
        "updateMyYouthProfile": {
            "youthProfile": {
                "additionalContactPersons": {
                    "edges": [{
                        "node": {
                            "id":
                            to_global_id(type="AdditionalContactPersonNode",
                                         id=acp.pk),
                            **new_values,
                        }
                    }]
                }
            }
        }
    }
    assert dict(executed["data"]) == expected_data
def test_additional_contact_person_runs_full_clean_when_saving(youth_profile):
    acp = AdditionalContactPersonFactory()
    with pytest.raises(ValidationError):
        acp.email = "notanemail"
        acp.save()
def test_serialize_youth_profile(youth_profile):
    AdditionalContactPersonFactory(youth_profile=youth_profile)
    serialized_youth_profile = youth_profile.serialize()

    expected = [
        {
            "key": "BIRTH_DATE",
            "value": youth_profile.birth_date.strftime("%Y-%m-%d")
        },
        {
            "key": "EXPIRATION",
            "value": youth_profile.expiration.strftime("%Y-%m-%d %H:%M"),
        },
        {
            "key": "APPROVER_FIRST_NAME",
            "value": youth_profile.approver_first_name
        },
        {
            "key": "APPROVER_LAST_NAME",
            "value": youth_profile.approver_last_name
        },
        {
            "key": "APPROVER_PHONE",
            "value": youth_profile.approver_phone
        },
        {
            "key": "APPROVER_EMAIL",
            "value": youth_profile.approver_email
        },
        {
            "key": "PHOTO_USAGE_APPROVED",
            "value": youth_profile.photo_usage_approved
        },
        {
            "key": "SCHOOL_NAME",
            "value": youth_profile.school_name
        },
        {
            "key": "SCHOOL_CLASS",
            "value": youth_profile.school_class
        },
        {
            "key": "LANGUAGE_AT_HOME",
            "value": youth_profile.language_at_home.value
        },
    ]

    expected_related = ["ADDITIONAL_CONTACT_PERSONS"]

    assert "key" in serialized_youth_profile
    assert "children" in serialized_youth_profile
    assert serialized_youth_profile["key"] == "YOUTHPROFILE"
    assert len(serialized_youth_profile["children"]
               ) == len(expected) + len(expected_related)

    for d in expected:
        assert d in serialized_youth_profile["children"]

    # Check that related objects are included
    for key in expected_related:
        assert any(
            map(lambda x: x["key"] == key,
                serialized_youth_profile["children"]))