0
0
FastAPIframework~10 mins

Testing authentication in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the FastAPI testing client.

FastAPI
from fastapi.testclient import [1]
Drag options to blanks, or click blank then click option'
AClientTest
BFastClient
CTestClient
DTestFastClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name like ClientTest or FastClient.
Trying to import from fastapi.client instead of fastapi.testclient.
2fill in blank
medium

Complete the code to create a test client for the FastAPI app named 'app'.

FastAPI
client = [1](app)
Drag options to blanks, or click blank then click option'
ATestClient
BFastClient
CClientTest
DAppClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name that does not exist like FastClient or ClientTest.
Trying to call the app instance instead of the test client class.
3fill in blank
hard

Fix the error in the test function to send a GET request to '/login'.

FastAPI
def test_login():
    response = client.[1]('/login')
    assert response.status_code == 200
Drag options to blanks, or click blank then click option'
Aget
Bsend
Cpost
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get for a GET route.
Trying to use a method named send or request which do not exist.
4fill in blank
hard

Fill both blanks to send a POST request with JSON data to '/token'.

FastAPI
def test_token():
    response = client.[1]('/token', json=[2])
    assert response.status_code == 200
Drag options to blanks, or click blank then click option'
Apost
B{'username': 'user', 'password': 'pass'}
C{'user': 'user', 'pass': 'pass'}
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET method instead of POST.
Using wrong keys in the JSON data like 'user' or 'pass'.
5fill in blank
hard

Fill all three blanks to check the token response contains 'access_token' and is a string.

FastAPI
def test_token_response():
    response = client.post('/token', json={'username': 'user', 'password': 'pass'})
    data = response.json()
    assert '[1]' in data
    assert isinstance(data['[2]'], [3])
Drag options to blanks, or click blank then click option'
Aaccess_token
Btoken
Cstr
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for wrong key names like 'token'.
Using int instead of str for the token type.