Skip to content

Wykład 3, zadanie 2 - niepoprawny status code #30

@jakubgierasimczyk

Description

@jakubgierasimczyk

Chciałbym spytać dlaczego poniższy kod nie przechodzi pierwszego testu z Zadania 2.
Zdaje się, że wcześniej pojawiło się podobne pytanie, ale nie potrafiłem z niego wysnuć rozwzwiązania dla siebie.

Kiedy sprawdzam w /docs, to w terminalu widzę, że jest przekierowanie na /welcome. Status code ustawiam na 302, ale ostatecznie jest zwracany 200 (zdaje się, że z /welcome) i chyba dlatego nie przechodzi testu, ale nie wiem co z tym zrobić. Podobne problem pojawia się w Zadaniu 3

from starlette.responses import RedirectResponse
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi import FastAPI, Response, status
from fastapi import Depends, Cookie, HTTPException
from hashlib import sha256

import secrets


app = FastAPI()


@app.get("/welcome")
def get_welcome():
	return "Hello!"


app.secret_key = "very constatn and random secret, best 64 characters"
app.tokens_list = []
security = HTTPBasic()


def get_current_username(credentials: HTTPBasicCredentials = Depends(security)):
    correct_username = secrets.compare_digest(credentials.username, "trudnY")
    correct_password = secrets.compare_digest(credentials.password, "PaC13Nt")
    print(f"{credentials.username}")
    print(f"{credentials.password}")
    if not (correct_username and correct_password):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            # detail="Incorrect email or password",
            # headers={"WWW-Authenticate": "Basic"},
        )
    return credentials.username



@app.post("/login")
def login(
    user: str, password: str, response: Response,
    credentials_user = Depends(get_current_username)
    ):
    
    session_token = sha256(bytes(f"{user}{password}{app.secret_key}", encoding='utf8')).hexdigest()
    app.tokens_list.append(session_token)
    
    response.set_cookie(key="session_token", value=session_token)
    
        
    response = RedirectResponse(url = "/welcome")
    response.status_code = status.HTTP_302_FOUND
    
    return response

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