def test_put_update_own_profile(user_api_client, profile):
    assert Profile.objects.count() == 1

    user_profile_url = get_user_profile_url(profile)
    phone_number_data = {"phone": "0461234567"}

    put_update(user_api_client, user_profile_url, phone_number_data)

    profile.refresh_from_db()
    assert profile.phone == phone_number_data["phone"]
def test_put_nonexistent_concept_of_interest(user_api_client, profile):
    assert not profile.concepts_of_interest.exists()
    assert not Concept.objects.exists()

    serialized_concept_of_interest = "nonexistent:concept"
    concept_of_interest_data = {
        "concepts_of_interest": [serialized_concept_of_interest]
    }

    user_profile_url = get_user_profile_url(profile)
    put_update(user_api_client,
               user_profile_url,
               concept_of_interest_data,
               status_code=400)
def test_put_concept_of_interest_in_wrong_format(user_api_client, profile,
                                                 concept):
    assert not profile.concepts_of_interest.exists()
    assert Concept.objects.exists()

    badly_serialized_concept_of_interest = "{}-{}".format(
        concept.vocabulary.prefix, concept.code)
    concept_of_interest_data = {
        "concepts_of_interest": [badly_serialized_concept_of_interest]
    }

    user_profile_url = get_user_profile_url(profile)
    put_update(user_api_client,
               user_profile_url,
               concept_of_interest_data,
               status_code=400)
def test_concept_of_interest_to_internal_value(user_api_client, profile,
                                               concept):
    assert not profile.concepts_of_interest.exists()

    serialized_concept_of_interest = "{}:{}".format(concept.vocabulary.prefix,
                                                    concept.code)
    concept_of_interest_data = {
        "concepts_of_interest": [serialized_concept_of_interest]
    }

    user_profile_url = get_user_profile_url(profile)
    put_update(user_api_client, user_profile_url, concept_of_interest_data)

    assert profile.concepts_of_interest.exists()
    coi = profile.concepts_of_interest.first()
    assert coi == concept
def test_put_profile_image(user_api_client, profile, default_image):
    assert not profile.image

    user_profile_url = get_user_profile_url(profile)
    image_data = {"image": default_image}

    put_update(user_api_client, user_profile_url, image_data)

    profile.refresh_from_db()
    assert profile.image

    expected_image_path = os.path.join(
        settings.MEDIA_ROOT, get_user_media_folder(profile,
                                                   default_image.name))
    actual_image_path = os.path.join(settings.MEDIA_ROOT, profile.image.url)
    assert os.path.exists(actual_image_path)
    assert actual_image_path == expected_image_path
def test_override_previous_profile_image(user_api_client, profile_with_image):
    assert profile_with_image.image

    old_image_path = os.path.join(settings.MEDIA_ROOT,
                                  profile_with_image.image.url)
    assert os.path.exists(old_image_path)

    new_image_file = create_in_memory_image_file("new_avatar", "png")
    new_image = SimpleUploadedFile("new_avatar.png", new_image_file.read(),
                                   "image/png")
    user_profile_url = get_user_profile_url(profile_with_image)
    new_image_data = {"image": new_image}

    put_update(user_api_client, user_profile_url, new_image_data)

    profile_with_image.refresh_from_db()

    new_image_path = os.path.join(settings.MEDIA_ROOT,
                                  profile_with_image.image.url)
    assert os.path.exists(new_image_path)
    assert not os.path.exists(old_image_path)