0

I need to test o/token endpoint of the oauth2_provider. I wrote the test in pytest. I think I considered every thing but I get invalid_client error.

The code:

factory:

class UserApplicationFactory(factory.django.DjangoModelFactory):
    name = factory.Faker('pystr', min_chars=5, max_chars=255)
    client_id = factory.LazyAttribute(lambda _: generate_client_id())
    client_secret = factory.LazyAttribute(lambda _: generate_client_secret())
    client_type = factory.Iterator(['confidential', 'public'])
    authorization_grant_type =  factory.Iterator(['client-credentials', 'password', 'openid-hybrid', 'authorization-code']) 
    class Meta:
        model = UserApplication

The test:

@pytest.mark.django_db
def test_token_generation(api_client, user):
    # secret is 12345
    
    application=UserApplicationFactory(
        name='test app',
        client_type='confidential',
        client_secret="12345",
        authorization_grant_type='client_credentials',
        user=user
    )
    token_url = reverse('oauth2_provider:token')
    authorization = base64.b64encode(
        bytes(application.client_id + ":12345" , "ISO-8859-1")
    ).decode("ascii")
    bearer = f"Basic {authorization}"
    data = {
        "grant_type": "client_credentials",
    }
    headers = {
        "Authorization": bearer,
        "Cache-Control": "no-cache",
        "Content-Type": "application/x-www-form-urlencoded",
    }

    response = api_client.post(token_url, data=data, headers=headers)
    response_data = json.loads(response.content)
    access_token = response_data['access_token']
    AccessToken = get_access_token_model()
    token = AccessToken.objects.get(token=access_token)

    assert response.status_code == 200    

0

Browse other questions tagged or ask your own question.