def test_call_token_endpoint_handles_error(mock_post, rf):
    """
    When we get an error back from the API we should be
    raising an TokenRequestFailed error.
    """
    mock_post.return_value = Mock(ok=True)
    mock_post.return_value.json.return_value = {
        "error": "failure",
        "error_description": "something went wrong",
    }
    endpoint_data = {"grant_type": "authorization_code", "code": "imacode"}

    c = Config()
    MockDiscoveryDocument = MagicMock()

    with patch("okta_oauth2.tokens.DiscoveryDocument",
               MockDiscoveryDocument), pytest.raises(TokenRequestFailed):
        tv = TokenValidator(c, "defaultnonce", rf.get("/"))
        tv.call_token_endpoint(endpoint_data)
Esempio n. 2
0
def test_call_token_endpoint_returns_tokens(mock_post, rf):
    """
    when we call the token endpoint with valid data we expect
    to receive a bunch of tokens. See assertions to understand which.
    """
    mock_post.return_value = Mock(ok=True)
    mock_post.return_value.json.return_value = {
        "access_token": build_access_token(),
        "id_token": build_id_token(),
        "refresh_token": "refresh",
    }
    endpoint_data = {"grant_type": "authorization_code", "code": "imacode"}

    c = Config()
    MockDiscoveryDocument = MagicMock()

    with patch("okta_oauth2.tokens.DiscoveryDocument", MockDiscoveryDocument):
        tv = TokenValidator(c, "defaultnonce", rf.get("/"))
        tokens = tv.call_token_endpoint(endpoint_data)
        assert "access_token" in tokens
        assert "id_token" in tokens
        assert "refresh_token" in tokens