Docsity

WGU D385 Pytest & Security Unit Testing Workbook | 48 Practice Tests, Python Security, API, Exams of Software Engineering

Partial preview of the text

Download WGU D385 Pytest & Security Unit Testing Workbook | 48 Practice Tests, Python Security, API and more Exams Software Engineering in PDF only on Docsity!

WGU D385 Pytest & Security Unit Testing

Workbook | 48 Practice Tests, Python Security,

API Testing & Vulnerability Verification | 2026

update

SECTION 1: PYTEST FUNDAMENTALS & TEST

DISCOVERY (Questions 1-12)

Question 1: What naming convention does pytest require for test files and functions? A. Files/functions named check_* B. Files/functions named test_* C. Files/functions named verify_* D. Any naming convention works Correct Answer: B. Files/functions named test_ *

Rationale: pytest requires test files and functions to be named test_. Test

framework files/functions named test_, plain assert statements, run with

python3 -m pytest; -k runs one test by name (how you capture B screenshots); exit green = rubric evidence. Source: WGU D385 Questions and Answers Set A Question 2: What is the primary purpose of pytest in WGU D385 Task 2?

A. To analyze code without executing it B. To verify that remediated vulnerabilities no longer exist through automated testing C. To replace the need for manual code review D. To encrypt sensitive data Correct Answer: B. To verify that remediated vulnerabilities no longer exist through automated testing Rationale: Task 2 requires C1 pytest functions named test_*, C2 assert in every test, C3 all tests pass, C4 descriptive print() lines explaining each outcome for the console screenshot, C5 four passing-test screenshots: 2 general + 2 API. Source: WGU D385 Task 2 Secure Coding and API Security Question 3: How do you run a single test by name with pytest? A. -t flag B. -k flag (e.g., -k test_name) C. -n flag D. -s flag Correct Answer: B. -k flag (e.g., -k test_name) Rationale: The -k flag runs one test by name (how you capture B screenshots); exit green = rubric evidence. Run with python3 -m pytest. Source: WGU D385 Questions and Answers Set A Question 4: What does an "exit green" result in pytest indicate? A. Tests failed B. Tests were skipped C. Tests passed (rubric evidence) D. Tests errored Correct Answer: C. Tests passed (rubric evidence)

Question 7: What are the four scaffold test names in test_run.py for Task 2? A. test_login, test_rent, test_admin, test_search B. test_general_vuln_1_hardcoded_secrets_remediated, test_general_vuln_2_plaintext_passwords_remediated, test_api_vuln_1_authentication_required, test_api_vuln_2_authorization_enforced C. test_sql, test_xss, test_auth, test_rate D. test_1, test_2, test_3, test_ Correct Answer: B. test_general_vuln_1_hardcoded_secrets_remediated, test_general_vuln_2_plaintext_passwords_remediated, test_api_vuln_1_authentication_required, test_api_vuln_2_authorization_enforced Rationale: The four scaffold test names in test_run.py are: test_general_vuln_1_hardcoded_secrets_remediated, test_general_vuln_2_plaintext_passwords_remediated, test_api_vuln_1_authentication_required, test_api_vuln_2_authorization_enforced. Source: WGU D385 Task 2 Secure Coding and API Security Question 8: What are the expected status codes in the two API tests for Task 2? A. 200 for both tests B. 500 for both tests C. 404 for both tests D. Unauthenticated GET /api/v1/rentals should return 401 Unauthorized; a regular user's Bearer key hitting /api/v1/admin/users should return 403 Forbidden Correct Answer: D. Unauthenticated GET /api/v1/rentals should return 401 Unauthorized; a regular user's Bearer key hitting /api/v1/admin/users should return 403 Forbidden

Rationale: Expected status codes in the two API tests are: Unauthenticated GET /api/v1/rentals should return 401 Unauthorized; a regular user's Bearer key hitting /api/v1/admin/users should return 403 Forbidden. Source: WGU D385 Task 2 Secure Coding and API Security Question 9: What does Section C require for C4 in Task 2? A. Descriptive print() lines explaining each outcome for the console screenshot B. Only assert statements C. Only screenshots D. Only report writing Correct Answer: A. Descriptive print() lines explaining each outcome for the console screenshot Rationale: Section C requirement C4 requires descriptive print() lines explaining each outcome for the console screenshot. Source: WGU D385 Task 2 Secure Coding and API Security Question 10: What does Section C require for C5 in Task 2? A. One screenshot only B. Two screenshots only C. Four passing-test screenshots: 2 general + 2 API D. No screenshots required Correct Answer: C. Four passing-test screenshots: 2 general + 2 API Rationale: Section C requirement C5 requires four passing-test screenshots: 2 general + 2 API. Source: WGU D385 Task 2 Secure Coding and API Security

Question 13: What is the general vulnerability in Lines 79-84 of the Task 2 starter code? A. SQL injection B. XSS vulnerability C. Hardcoded secret_key, API key, DB user, and DB password D. Missing rate limiting Correct Answer: C. Hardcoded secret_key, API key, DB user, and DB password Rationale: Lines 79-84 contain hardcoded secret_key, API key, DB user and DB password. Fix (already implemented in your file): os.environ.get for each with secrets.token_hex(32) as the generated fallback, originals commented out. Source: WGU D385 Task 2 Secure Coding and API Security Question 14: What is the remediation for hardcoded credentials? A. Encrypt the credentials in place B. Move secrets to environment variables or a config outside the repo, rotate the exposed values, and store password HASHES not plaintext C. Use shorter credential strings D. Store credentials in a comment Correct Answer: B. Move secrets to environment variables or a config outside the repo, rotate the exposed values, and store password HASHES not plaintext Rationale: The remediation for hardcoded credentials is to move secrets to environment variables or a config outside the repo (os.environ / python- dotenv), rotate the exposed values, and store password HASHES not plaintext. Source: WGU D385 Task 2 Secure Coding and API Security

Question 15: What is the general vulnerability in Lines 114-119 of the Task 2 starter code? A. SQL injection B. XSS vulnerability C. USERS_DB stores plaintext passwords and api_keys (CWE-256) D. Missing rate limiting Correct Answer: C. USERS_DB stores plaintext passwords and api_keys (CWE-256) Rationale: Lines 114-119 contain USERS_DB which stores plaintext passwords and api_keys for admin/alice/bob/charlie (roles admin, user, user, guest) - CWE-256. Fix: store password_hash values using pbkdf2_hmac or bcrypt and compare hashes on login. Source: WGU D385 Task 2 Secure Coding and API Security Question 16: What is the remediation for plaintext passwords in USERS_DB? A. Encrypt passwords with a reversible algorithm B. Store passwords in a separate file C. Store password_hash values using pbkdf2_hmac or bcrypt and compare hashes on login D. Use shorter passwords Correct Answer: C. Store password_hash values using pbkdf2_hmac or bcrypt and compare hashes on login Rationale: The remediation for plaintext passwords is to store password_hash values using pbkdf2_hmac or bcrypt and compare hashes on login. Source: WGU D385 Task 2 Secure Coding and API Security Question 17: How do you verify that hardcoded secrets have been remediated with pytest?

Correct Answer: B. Use of Hard-coded Password Rationale: CWE-259 is Use of Hard-coded Password; the CWE both tasks' credential findings map to. Source: WGU D385 Questions and Answers Set A Question 20: What is CWE-256? A. Plaintext Storage of a Password B. SQL injection C. Cross-site scripting D. Broken access control Correct Answer: A. Plaintext Storage of a Password Rationale: CWE-256 is Plaintext Storage of a Password. USERS_DB stores plaintext passwords and api_keys - CWE-. Source: WGU D385 Task 2 Secure Coding and API Security Question 21: What should the general vulnerability tests verify for C1 in Task 2? A. That the vulnerability is still present B. That the remediation is in place and the vulnerability no longer exists C. Only that the app runs D. Only that the test file exists Correct Answer: B. That the remediation is in place and the vulnerability no longer exists Rationale: Section C requires C1 pytest functions named test_*, C2 assert in every test, C3 all tests pass. The tests should verify that the remediation is in place and the vulnerability no longer exists. Source: WGU D385 Task 2 Secure Coding and API Security

Question 22: How many total tests should be in the test file for Task 2? A. 2 tests only B. 4 tests only C. At least 4 tests (2 general + 2 API) D. No specific number required Correct Answer: C. At least 4 tests (2 general + 2 API) Rationale: The four scaffold test names in test_run.py are: test_general_vuln_1_hardcoded_secrets_remediated, test_general_vuln_2_plaintext_passwords_remediated, test_api_vuln_1_authentication_required, test_api_vuln_2_authorization_enforced. Source: WGU D385 Task 2 Secure Coding and API Security Question 23: What is the purpose of the Security Mitigation Report in Task 2? A. To document the vulnerabilities identified in the starter API, the severity of each, and the remediation steps taken B. To provide a user manual for the API C. To list all features of the API D. To describe the API's architecture Correct Answer: A. To document the vulnerabilities identified in the starter API, the severity of each, and the remediation steps taken Rationale: The Security Mitigation Report documents the vulnerabilities identified in the starter API, the severity of each (using CVSS), and the remediation steps taken. It demonstrates the student's ability to identify and fix security issues. Source: WGU D385 Task 2 Complete Practice Guide

Question 26: What is the remediation for missing authentication (CWE- 306)? A. Encrypt the API endpoint B. Implement rate limiting C. Read the Authorization header, strip the Bearer prefix, validate the key against USERS_DB, return the username or None D. Log all requests Correct Answer: C. Read the Authorization header, strip the Bearer prefix, validate the key against USERS_DB, return the username or None Rationale: The remediation for missing authentication is to read the Authorization header, strip the Bearer prefix, validate the key against USERS_DB, and return the username or None. Source: WGU D385 Task 2 Secure Coding and API Security Question 27: What vulnerability exists in require_role() as shipped in Task 2? A. Returns False, blocking all access B. Returns True, so any user reaches any endpoint including admin functions (CWE-862) C. Properly enforces role hierarchy D. Logs all role checks Correct Answer: B. Returns True, so any user reaches any endpoint including admin functions (CWE-862) Rationale: require_role() as shipped always returns True, so any user reaches any endpoint including admin functions (CWE-862 Missing Authorization). Fix: look up the user's role in USERS_DB and enforce the hierarchy admin over user over guest. Source: WGU D385 Task 2 Secure Coding and API Security

Question 28: What is the remediation for broken authorization (CWE-862)? A. Encrypt the API endpoint B. Implement rate limiting C. Log all requests D. Look up the user's role in USERS_DB and enforce the hierarchy admin over user over guest Correct Answer: D. Look up the user's role in USERS_DB and enforce the hierarchy admin over user over guest Rationale: The remediation for broken authorization is to look up the user's role in USERS_DB and enforce the hierarchy admin over user over guest. Source: WGU D385 Task 2 Secure Coding and API Security Question 29: What vulnerability exists in check_rate_limit() as shipped in Task 2? A. Always returns True - no rate limiting at all (CWE-307) B. Always returns False - blocks all requests C. Properly limits requests D. Logs all rate limit checks **Correct Answer: A. Always returns True - no rate limiting at all (CWE-

Rationale:** check_rate_limit() as shipped always returns True - no rate limiting at all (CWE-307 Improper Restriction of Excessive Authentication Attempts). Fix: track request counts per IP or API key per time window and reject over the cap. Source: WGU D385 Task 2 Secure Coding and API Security Question 30: What is the remediation for missing rate limiting (CWE-307)?

C. Cross-site scripting D. Broken access control Correct Answer: B. Missing Authentication for Critical Function Rationale: CWE-306 is Missing Authentication for Critical Function. require_api_key() returns None, meaning NO endpoint is ever authenticated (CWE-306). Source: WGU D385 Task 2 Secure Coding and API Security Question 33: What is CWE-862? A. SQL injection B. Missing Authorization C. Cross-site scripting D. Broken access control Correct Answer: B. Missing Authorization Rationale: CWE-862 is Missing Authorization. require_role() returns True, so any user reaches any endpoint including admin functions (CWE-862). Source: WGU D385 Task 2 Secure Coding and API Security Question 34: What is CWE-307? A. Improper Restriction of Excessive Authentication Attempts B. SQL injection C. Cross-site scripting D. Broken access control Correct Answer: A. Improper Restriction of Excessive Authentication Attempts Rationale: CWE-307 is Improper Restriction of Excessive Authentication Attempts. check_rate_limit() always returns True - no rate limiting at all (CWE-307).

Source: WGU D385 Task 2 Secure Coding and API Security Question 35: What is Broken Object-Level Authorization (BOLA)? A. An attack that injects SQL code B. An API vulnerability where users can access objects belonging to other users C. An attack that executes scripts in a victim's browser D. A vulnerability in encryption algorithms Correct Answer: B. An API vulnerability where users can access objects belonging to other users Rationale: Broken Object-Level Authorization (BOLA) occurs when an API endpoint does not verify that the authenticated user is authorized to access the specific object they are requesting. The fix is to verify ownership on every object access. Source: WGU D385 Task 2 Complete Practice Guide Question 36: What does Section C require for C2 in Task 2? A. Only print statements B. Assert in every test C. Only screenshots D. Only report writing Correct Answer: B. Assert in every test Rationale: Section C requires C1 pytest functions named test_*, C2 assert in every test, C3 all tests pass. Source: WGU D385 Task 2 Secure Coding and API Security