Bird
0
0

Given this FastAPI code snippet, what will happen if a user without the 'editor' role tries to access the endpoint?

medium📝 Predict Output Q4 of 15
FastAPI - Authentication and Security
Given this FastAPI code snippet, what will happen if a user without the 'editor' role tries to access the endpoint?
async def editor_access(user: User = Depends(get_current_user)):
    if 'editor' not in user.roles:
        raise HTTPException(status_code=403, detail='Forbidden')

@app.get('/edit')
async def edit_page(dep=Depends(editor_access)):
    return {'message': 'Welcome editor'}
AThe user receives a 403 Forbidden error
BThe user sees the message 'Welcome editor'
CThe endpoint returns a 404 Not Found error
DThe endpoint ignores the role and returns 200 OK
Step-by-Step Solution
Solution:
  1. Step 1: Analyze role check in dependency

    The dependency raises HTTPException with 403 if 'editor' role is missing.
  2. Step 2: Understand endpoint behavior

    If exception raised, FastAPI returns 403 error, blocking access.
  3. Final Answer:

    The user receives a 403 Forbidden error -> Option A
  4. Quick Check:

    Missing role triggers 403 error [OK]
Quick Trick: Missing role in dependency raises HTTPException 403 [OK]
Common Mistakes:
MISTAKES
  • Assuming user sees success message without role
  • Confusing 403 with 404 error
  • Thinking endpoint ignores role checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes