Skip to content

Wykład 3 zadanie 2: Dlaczego aplikacja nie przechodzi testów? #26

@Mcrackers

Description

@Mcrackers

Szanowni,

Zwracam się z prośbą o wyjaśnienie, dlaczego poniższy kod nie przechodzi testów?
Podczas testowania aplikacji za pomocą curl otrzymuję informację:
HTTP/1.1 307 Temporary Redirect
Ale podczas assertów na repl.it mam bład 422.
W przypadku wpisania niepoprawnego loginu czy hasła wymuszam zwrócenie wyjątku 401:
HTTPException(status_code=401)
A mimo to w testach na repl.it mam błąd 422.

Poniżej załączam kod:

from fastapi import FastAPI, Response, HTTPException
from hashlib import sha256
from pydantic import BaseModel
from fastapi.responses import JSONResponse
from starlette.responses import RedirectResponse

app = FastAPI()
app.num = 0
app.count = -1
app.users = {"trudnY": "PaC13Nt", "admin": "admin"}
app.secret = "secret"
app.tokens = []
patlist = []


@app.get("/")
def root():
	return {"message": "Hello World during the coronavirus pandemic!"}


@app.get("/welcome")
def welcome_to_the_jungle():
	return {"message": "Welcome to the jungle! We have funny games!"}


@app.post("/login")
def login_to_app(user: str, passw: str, response: Response):
	if user in app.users and passw == app.users[user]:
		s_token = sha256(bytes(f"{user}{passw}{app.secret}", encoding='utf8')).hexdigest()
		app.tokens += s_token
		response.set_cookie(key="session_token",value=s_token)
		response = RedirectResponse(url='/welcome')
		print('logged in')
		return response
	else:
		raise HTTPException(status_code=401)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions