pytest #patch var inside route in FastApi - pytest

I try to use #path from unittest.mock to mock (with return value) var inside FastApi route
route is:
from . import superlogic
#router.post('/{item_id}/submit'):
async def submit(item: str):
var_i_need_to_mock = super_object.very_long_complicated_logic()
return True // nevermid
pytest:
#patch('app.router.submit'):
def test_submit(test_client):
var_i_need_to_mock = True
test_client.post('/submit') // But var_i_need_to_mock doesn't overrides
I can patch my super_object instead of var_i_need_to_mock and it works as expected like below
#patch('app.router.submit', mockobject)
What is the best practice directly override var_i_need_to_mock inside route and how to achieve this

In this case, I would try to patch the return value of super_object.very_long_complicated_logic, like so:
#patch('filename.super_object.very_long_complicated_logic', return_value=True):
def test_submit(test_client):
test_client.post('/submit')

Related

How to implement pytest for FastAPI with MongoDB(Motor)

I want to write tests for my FastAPI endpoints
example for my code:
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
#app.get("/todos")
async def get_todo_by_title(title: str,current_user: User = Depends(get_current_user))
document = await collection.find_one({"title": title})
return document
client = TestClient(app)
def test_get_todo_by_title():
response = client.get("/todos")
assert response.status_code == 200
What the best way to test my endpoints?
I want to use fake DB for testing, something like json file
db = {
todos: [...]
}
It is not the best option to use fake data from a JSON file. Instead, you can use a testing DB (based on the env you are running your app through), or any other DB (dev, stg, ..etc), and delete your test data after running all unit tests.
Here is how to simply apply the latter approach in FastAPI;
Assume you have 3 simple tables (or collections) X, Y, and Z
MongoDB is the DB service
PyTest is the test engine
conftest.py
from pytest import fixture
from starlette.config import environ
from starlette.testclient import TestClient
from config.db_connection import database, X_collection, Y_collection, Z_collection
#fixture(scope="session")
def test_X():
return {
"_id": "10",
"name": "test",
"description": "test",
"type": "single value",
"crt_at": "2022-06-27T12:23:15.143Z",
"upd_at": "2022-06-27T12:23:15.143Z"
}
//test_Y and test_Z fixtures should be like test_X
#fixture(scope="session", autouse=True)
def test_client(test_X, test_Y, test_Z):
import main
application = main.app
with TestClient(application) as test_client:
yield test_client
db = database
//Here, delete any objects you have created for your tests
db[X_collection].delete_one({"_id": test_X["_id"]})
db[Y_collection].delete_one({"_id": test_Y["_id"]})
db[Z_collection].delete_one({"_id": test_Z["_id"]})
environ['TESTING'] = 'TRUE'
Unit tests should look like the following sample.
test_X_endpoints.py
def test_create_X(test_client, test_X):
response = test_client.post("/create_X_URI/", json=test_X)
assert response.status_code == 201
//Add assertions as needed
def test_list_X(test_client):
response = test_client.get("/list_X_objects_URI/?page=1&size=1")
assert response.status_code == 200
//Add assertions as needed

How do I return assets with a 404 status code in Play?

Currently I have a play application with Scala, in where I have the frontend in the public folder as a SPA.
I have a /404 route in where I want to return the frontEnd with the status code of 404.
However if I write:
def returnNotFound(): Action[AnyContent] = Action {
NotFound(assets.at("index.html"))
}
Then I get a compilation error because assets returns an Action, not a Writeable.
So how can I return the index.html in the public folder with the status code of 404?
The issue here is that assets.at("index.html") returns Action[AnyContent] which suppose to be return method of Controller, hence in order to change status you need to change result from assets.at like:
def returnNotFound(): Action[AnyContent] = Action.async { implicit request =>
assets.at("index.html")(request).map { result =>
new Result(result.header.copy(status = 404), result.body)
}
}

How to redirect internally in Ktor?

I'm wondering if there's a way to do an internal redirect, re-route, or response forwarding inside Ktor.
call.respondRedirect("relative/url")
sends a HTTP 302 or 301 depending on the permantent: Boolean flag. I'm looking for something that would do the same without using HTTP, just internally in Ktor. Here's some pseudo-routing of what I want to achieve:
get("/foo") {
if (call.parameters["something"] != null) {
call.respondText("Yay, something!")
} else {
call.respondRedirect("/bar") // except without relying on client to handle HTTP redirects.
}
}
get("/bar") {
call.respondText("No thing :(")
}
The goal is that the client shouldn't make 2 requests, and shouldn't be aware of the redirection happening.
NB: I'm aware I can extract a function for /bar's body and invoke it, instead of responsdRedirect. However, I want to make Ktor handle it so that it goes through all the necessary lifecycle and pipeline with all the interceptors. This is to make sure it is handled as if it was an external request, except the network roundtrip.
I'm looking for something like Express.js' req.app.handle(req, res) as shown in the first half of this answer: https://stackoverflow.com/a/48790319/253468. A potential solution I couldn't understand yet is something like TestApplicationEngine.handleRequest (in io.ktor:ktor-server-test-host) is doing with pipeline.execute. I guess I could invoke call.application.execute(), the question is how to construct the ApplicationCall object then. Note this is for production use, so no TestApplicationCall.
You can do similar thing in Ktor by using call.application.execute function with cloned call object. For convenience let's define extension function for doing internal redirects:
suspend fun ApplicationCall.redirectInternally(path: String) {
val cp = object: RequestConnectionPoint by this.request.local {
override val uri: String = path
}
val req = object: ApplicationRequest by this.request {
override val local: RequestConnectionPoint = cp
}
val call = object: ApplicationCall by this {
override val request: ApplicationRequest = req
}
this.application.execute(call)
}
Here it creates a copy of an ApplicationCall object with the replaced path for a request. I use delegates to avoid boilerplate code. You can use redirectInternally function like this:
get("/foo") {
call.redirectInternally("/bar")
}

Play Router: How to add a language-sensitive URL redirect rule?

I have an internationalized Scala Play 2.7.x WebApp and have the usual routes e.g.
GET / controllers.ApplicationController.index
GET /page/somePage/ controllers.SomeController.somePage
GET /contact controllers.ContactController.view
Now I'd like to add a new route that will basically change-language-redirect to any target route. I implement this use-case by adding an additional route on top of routes like this:
GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")
The idea is that every time you do e.g.
http://localhost:9000/en => will go to home page in english
http://localhost:9000/en/contact => will go to contact page in english
http://localhost:9000/es => will go to home page in spanish
http://localhost:9000/es/contact => will go to contact page in spanish
and so on. Unfortunately it doesn't always work e.g. the one included before /en/page/somePage/ it will not match it correctly to the first rule:
GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")
presumably because of the intermediate / ... how can I fix that?
For completeness here is my ApplicationController.langRedirect(...) implementation:
def langRedirect(lang: String, target: String = "") = silhouette.UserAwareAction.async { implicit request =>
Future.successful(Redirect("/" + target).withLang(Lang(lang)))
}
Using Router.withPrefix, you can add langage code prefix to all your routes.
Here is an example.
package handlers
import javax.inject.Inject
import play.api.http._
import play.api.i18n.{ Langs, Lang }
import play.api.mvc.{ Handler, RequestHeader }
class I18nRequestHandler #Inject()(
webCommands: play.core.WebCommands,
optDevContext: play.api.OptionalDevContext,
router: play.api.routing.Router,
errorHandler: HttpErrorHandler,
configuration: HttpConfiguration,
filters: HttpFilters,
langs: Langs)
extends DefaultHttpRequestHandler(
webCommands, optDevContext, router, errorHandler, configuration, filters) {
def getLang(request: RequestHeader): Lang = {
// Get the first path
request.path.tail.split('/').headOption
.flatMap(path => Lang.get(path))
// language from the fist path, if it is in "play.i18n.langs (application.conf)"
.filter(lang => langs.availables.exists(_ == lang))
// Or preferred language, refereeing "Accept-Languages"
.getOrElse(langs.preferred(request.acceptLanguages))
}
override def handlerForRequest(request: RequestHeader): (RequestHeader, Handler) = {
// To use the language code from the path with MessagesApi,
// Replace "Accept-Languages" to the language from the path.
val requestWithLang = request.withHeaders(
request.headers.replace(HeaderNames.ACCEPT_LANGUAGE -> getLang(request).code))
super.handlerForRequest(requestWithLang)
}
override def routeRequest(request: RequestHeader): Option[Handler] = {
val lang = getLang(request)
request.path.tail.split('/').headOption
// If the first path is right language code (if not, Not Found)
.filter(_ == lang.code)
// Route this request with language code prefix
.flatMap(_ => router.withPrefix("/" + lang.code).handlerFor(request))
}
}
To enable I18nRequestHandler, you have to add it to "application.conf".
play.http.requestHandler = "handlers.I18nRequestHandler"
Also add supported languages to "application.conf".
play.i18n.langs = [ "en", "es" ]
This code forces all routes to have the language code prefix. If you need a exceptional routes such as "/" to let users choose its language, create custom routes and add it on routeRequest method.
Hope this is what you want ;)
OK found a possible solution that's to add a second top route that will take any possible target including /, the top of my routes file now look like this:
GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")
GET /$lang<(en|es)>/*target controllers.ApplicationController.langRedirect(lang: String, target: String = "")
GET / controllers.ApplicationController.index
GET /page/somePage/ controllers.SomeController.somePage
GET /contact controllers.ContactController.view
Why I need two? because of the home page can only be http://localhost:9000/en and can't be http://localhost:9000/en/
However, I will be happy to learn (and accept) a better/simpler solution.

Custom spray.io directive to validate request header value

I am new to spray and I am trying to write a custom directive. I would like the directive to reject the request if the header value is not valid otherwise leave the request alone.
I've tried to absorb this page:
http://spray.io/documentation/1.1.2/spray-routing/key-concepts/directives/
Specifically, the part about the responder chain. I'm trying to create something at the level of the bar Directive in the illustration. I'm just not getting how to pass the context unchanged to the inner route.
My else block below is not correct but expresses what I am trying to do. I just can't figure out how to implement it.
Any help would be greatly appreciated.
trait ApiKeyDirective {
import spray.routing.directives.HeaderDirectives._
import spray.routing.directives.BasicDirectives._
def validateApiKey(): Directive1 = {
headerValueByName("api-key") {key =>
val valid = key == "123"
if (!valid) reject() else pass
}
}
}
object ApiKeyDirective extends ApiKeyDirective
You can combine
headerValueByName:
def headerValueByName(headerName: String): Directive1[String]
with validate:
def validate(check: ⇒ Boolean, errorMsg: String): Directive0
For example:
def validateApiKey(route: Route) =
headerValueByName("api-key") { key =>
validate(key == "123", "Invalid API key") {
route
}
}
or without validate:
def validateApiKey(route: Route) =
headerValueByName("api-key") { key =>
if (key == "123")
route
else
reject(ValidationRejection("Invalid API key"))
}
Usage:
lazy val route = ...
... ~
pathPrefix("test_directive") {
get {
validateApiKey {
complete("ok")
}
}
} ~
...
Test from cmd/shell:
# curl http://localhost:8080/test_directive
Request is missing required HTTP header 'api-key'
# curl http://localhost:8080/test_directive -H 'api-key: bad'
Invalid API key
# curl http://localhost:8080/test_directive -H 'api-key: 123'
"ok"
I'm just not getting how to pass the context unchanged to the inner
route.
Spray does that for you!
Your code is mostly correct, there are just 2 simple problems to fix!
Firstly, you need to flatMap headerValueByName("api-key") directive.
Secondly, the return type will be Directive0 because the directive won't provide any value.
So final code would look like this:
object ApiKeyDirective {
import spray.routing.Directives._
val validateApiKey: Directive0 =
headerValueByName("api-key").flatMap { key =>
val valid = key == "123"
if (!valid) reject() else pass
}
}
Also, I recommend you to add a custom rejection to reject() block so that API users will be informed when their api key is invalid.