Example #1
0
def test_get_user_profile_by_email_in_cache(mock_slack):
    # create cache entry for user
    slack_user = ExternalUser(
        external_id="U12345678",
        display_name="spengler",
        full_name="Egon Spengler",
        email="*****@*****.**",
        deleted=True,
    )
    slack_user.save()
    assert len(ExternalUser.objects.all()) == 1

    # request a user from cache
    user = get_user_profile_by_email("*****@*****.**")

    # check we get back the user from cache (i.e. not call to Slack API)
    mock_slack.get_user_profile_by_email.assert_not_called()
    assert user["id"] == "U12345678"
    assert user["name"] == "spengler"
    assert user["fullname"] == "Egon Spengler"
    assert user["email"] == "*****@*****.**"
    assert user["deleted"]

    # check cache is unchanged
    assert len(ExternalUser.objects.all()) == 1
Example #2
0
def test_get_user_profile_by_email_not_in_cache(mock_slack):
    mock_slack.get_user_profile_by_email.return_value = {
        "id": "U12345678",
        "name": "spengler",
        "fullname": "Egon Spengler",
        "email": "*****@*****.**",
        "deleted": True,
    }

    # check cache is empty at start
    assert len(ExternalUser.objects.all()) == 0

    # request a user from cache
    user = get_user_profile_by_email("*****@*****.**")

    # check we get back the user from slack
    mock_slack.get_user_profile_by_email.assert_called()
    assert user["id"] == "U12345678"

    # check user is now in cache
    assert len(ExternalUser.objects.all()) == 1

    # and that it has the right details populated
    cache_user = ExternalUser.objects.get(
        email="*****@*****.**")
    assert cache_user.display_name == "spengler"
    assert cache_user.full_name == "Egon Spengler"
    assert cache_user.email == "*****@*****.**"
    assert cache_user.deleted