اختبار نقاط النهاية التي تتطلب المصادقة
سجّل الدخول داخل الاختبارات للوصول إلى المسارات المحمية.
اختبار نقاط النهاية التي تتطلب المصادقة درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Challenge of Auth Tests
Protected routes refuse anonymous visitors. To test them, your client must first log in, just like a real user would before reaching guarded pages.
Confirm the Guard Works
Start by proving the gate is closed. Request the route logged out and assert you get a redirect or a 401 response.
resp = client.get('/dashboard')
assert resp.status_code == 302Log In via the Login Route
The realistic way to authenticate is to post credentials to your login endpoint. The client stores the session cookie automatically.
client.post('/login', data={'email': 'a@b.com', 'password': 'pw'})The Client Keeps Cookies
One handy detail: the test client remembers cookies between calls. After login, later requests stay authenticated with no extra work.
Reach a Protected Route
Now that you are logged in, request the guarded page again. This time assert you get a 200 and see the protected content.
resp = client.get('/dashboard')
assert resp.status_code == 200A Helper to Log In
Repeating the login post gets noisy. Wrap it in a small helper function so every auth test reads cleanly in one line.
def login(client):
return client.post('/login', data={'email': 'a@b.com', 'password': 'pw'})An Authenticated Fixture
Even better, make a fixture that returns an already-logged-in client. Tests that need auth just request it and skip the setup.
@pytest.fixture
def auth_client(client):
login(client)
yield clientTest the Logout Flow
Auth is not done until logout works. Hit /logout, then confirm the protected route again rejects the now anonymous client.
client.get('/logout')
assert client.get('/dashboard').status_code == 302Bypass Login for Speed
For Flask-Login apps you can skip the form and set the session directly. It is faster but tests less of the real login path.
with client.session_transaction() as sess:
sess['_user_id'] = '1'Test Token-Protected APIs
For JWT APIs there is no cookie. Send the token in an Authorization header on each request to reach a protected endpoint.
client.get('/api/me', headers={'Authorization': 'Bearer ' + token})Test Both Sides of the Gate
Strong auth tests check both outcomes: anonymous users are blocked, and authenticated users are allowed. Cover the happy and the sad path.
Quick Check
You need to test a login-only dashboard. What makes it work?
Recap: Authenticated Tests
You log in through the client, lean on its cookie memory, and assert both blocked and allowed paths. Your auth is now fully covered. 🔐
الأسئلة الشائعة
هل درس «اختبار نقاط النهاية التي تتطلب المصادقة» مجاني؟
نعم — نص درس «اختبار نقاط النهاية التي تتطلب المصادقة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «اختبار نقاط النهاية التي تتطلب المصادقة»؟
سجّل الدخول داخل الاختبارات للوصول إلى المسارات المحمية. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «اختبار نقاط النهاية التي تتطلب المصادقة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- عميل الاختبار والتركيبات
- التحقق من المسارات وJSON
- عزل الاختبارات باستخدام قاعدة بيانات اختبار
- اختبار نقاط النهاية التي تتطلب المصادقة