Тестирование конечной точки аутентификации django oauth2_provider
Мне нужно протестировать конечную точку o/token провайдера oauth2_provider. Я написал тест в pytest. Вроде бы все учел, но получаю ошибку invalid_client.
Код:
фабрика:
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
Тест:
@pytest.mark.django_db
def test_token_generation(api_client, user):
# secret is 12345
hashed_secret = "pbkdf2_sha256$600000$fB3GLAgzjFjcBAzaHTMyrP$/0qtnsOKi3KRqQgbZ7B0/PcudGTSIgItHXxg9R9Zkf4="
application=UserApplicationFactory(
name='test app',
client_type='confidential',
client_secret=hashed_secret,
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
Я напечатал application.client_secret, результат отличался от hashed_secret:
print(application.client_secret)
#result : md5$20JXardwR2tDHMzpZAGkVy$45cdd904b795af35d7a9f64d15086a59