Bird
0
0

What will be the output if a user with roles ['user', 'admin'] accesses this FastAPI endpoint?

medium📝 Predict Output Q5 of 15
FastAPI - Authentication and Security
What will be the output if a user with roles ['user', 'admin'] accesses this FastAPI endpoint?
async def admin_only(user: User = Depends(get_current_user)):
    if 'admin' not in user.roles:
        raise HTTPException(status_code=403)

@app.get('/admin')
async def admin_panel(dep=Depends(admin_only)):
    return {'status': 'Access granted'}
AHTTP 403 Forbidden error
BHTTP 404 Not Found error
C{'status': 'Access granted'}
DEmpty response with status 200
Step-by-Step Solution
Solution:
  1. Step 1: Check user roles against condition

    User has 'admin' role, so condition to raise exception is false.
  2. Step 2: Endpoint returns success message

    Since no exception, endpoint returns {'status': 'Access granted'}.
  3. Final Answer:

    {'status': 'Access granted'} -> Option C
  4. Quick Check:

    User with admin role gets access granted [OK]
Quick Trick: User with required role passes dependency and gets success [OK]
Common Mistakes:
MISTAKES
  • Assuming 403 error despite having admin role
  • Confusing 404 with 403 errors
  • Expecting empty response instead of JSON

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes