テスト時にAPI認証の通し方
OAuthで認証を実装したら、ライブラリがうまいこと隠蔽してるのでテスト時に躓いた。
テストする際にサクッと認証を通す関数を作成した。
from rest_framework_simplejwt.tokens import RefreshToken
....
def auth_factory(self, user):
# トークンを直接生成
refresh = RefreshToken.for_user(user)
token = str(refresh.access_token)
client = APIClient()
client.credentials(HTTP_AUTHORIZATION="Bearer " + token)
return client