[英]How to use correct oAuth2 redirect url in Django test case?
My Django webapp uses oAuth2 to get authenticated in Strava. 我的Django网络应用使用oAuth2在Strava中进行身份验证。
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
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.