简体   繁体   English

如何在Django测试案例中使用正确的oAuth2重定向网址?

[英]How to use correct oAuth2 redirect url in Django test case?

My Django webapp uses oAuth2 to get authenticated in Strava. 我的Django网络应用使用oAuth2在Strava中进行身份验证。

  1. I have a link in my webpage that sends a request to Strava indicating a redirect url amongst other arguments. 我的网页中有一个链接,该链接向Strava发送请求,指示除其他参数外的重定向URL。 In this example case http://127.0.0.1:8000/token_exchange 在此示例中, http://127.0.0.1:8000/token_exchange

  2. Strava then makes a request to that url with the code to exchange for the access token. 然后Strava使用该代码向该网址发出请求,以交换访问令牌。 I have a view that does that exchange in that url. 我有一个视图,可以在该URL中进行交换。

Everything works fine. 一切正常。

My problem is that I am trying to build an automated test. 我的问题是我试图建立一个自动化测试。 But when I launch the tests (with python manage.py test) the server does not run in port 8000 but in a different port. 但是,当我启动测试(使用python manage.py test)时,服务器不在端口8000中运行,而是在另一个端口中运行。

Is there a best practice way to manage that redirect url so that it has production domain in production but uses the test server address when running the tests? 是否有最佳实践方法来管理该重定向URL,以使其在生产环境中具有生产域,但在运行测试时使用测试服务器地址?

Thanks!!! 谢谢!!!

The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. 单元测试的目的是隔离程序的每个部分,并表明各个部分是正确的。

https://en.wikipedia.org/wiki/Unit_testing#Advantages https://zh.wikipedia.org/wiki/Unit_testing#Advantages

If you're writing unit tests and not complex integration tests, the best practice would be just mocking this behavior. 如果您是在编写单元测试而不是复杂的集成测试,那么最佳实践就是模拟这种行为。

There is a wonderful guide at Python docs about unittest.mock library on how to monkey patch all the behavior you're not testing in the particular case: https://docs.python.org/3/library/unittest.mock-examples.html Python文档上有一个很棒的指南,关于unittest.mock库,该指南指导如何修补在特定情况下未测试的所有行为: https : //docs.python.org/3/library/unittest.mock-examples .html

Also, you should definitely check out responses library, it's really useful when you're trying to test parts which rely on third-party services: https://github.com/getsentry/responses 另外,您绝对应该签出responses库,当您尝试测试依赖第三方服务的零件时,它真的很有用: https//github.com/getsentry/responses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用OAUTH2 Django - Unable to use OAUTH2 Django Django中的单元测试看不到成功登录的正确重定向URL? - Unit test in Django dont see correct redirect url for successfull login? 将Django OAuth2提供程序与JupyterHub一起使用 - Use Django OAuth2 provider with JupyterHub 如何在facebook Oauth2中使用django-rest-framework-social-oauth2? - How to use the django-rest-framework-social-oauth2 with facebook Oauth2? 尝试从 Django 视图将 OAuth2 与 Google 表格一起使用时出现错误 400:redirect_uri_mismatch - Getting a Error 400: redirect_uri_mismatch when trying to use OAuth2 with Google Sheets from a Django view 使用 Django REST 和 OAuth2 在测试脚本中获取 403 响应 - Getting 403 response in test script using Django REST and OAuth2 Django别名将错误的URL重定向到正确的URL - Django alias redirect on wrong url to the correct url 本地Django上的Salesforce沙盒API Oauth2回调URL - Salesforce Sandbox API Oauth2 Callback URL over localhost Django 如何使用 oauth2 访问 StackExchange API? - How to use oauth2 to access StackExchange API? Facebook Oauth 有效,但 Google Oauth2 错误:Django 中的 redirect_uri_mismatch - Facebook Oauth works but Google Oauth2 error : redirect_uri_mismatch in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM