Spaces:
Sleeping
Sleeping
File size: 7,671 Bytes
ba52031 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | """
Task 7: Code Review Workflow Seed Data
Collaborative scenario: Junior developer submits fix that needs code review.
Demonstrates multi-user workflow with approval gates.
"""
from typing import Any, Dict
def get_task7_seed_data() -> Dict[str, Any]:
"""
Generate seed data for task7 (Code Review Workflow).
Scenario:
Bob (Junior Dev) found and fixed a bug in authentication logic.
Alice (Senior Dev) needs to review the fix.
Dave (Tech Lead) oversees the process.
Workflow:
1. Bob reads failing test and understands the bug
2. Bob edits the authentication code to fix the issue
3. Bob runs tests to verify the fix works
4. Alice reviews the code changes
5. Alice checks test coverage and style
6. Alice approves the fix
Issues to address:
1. Code style doesn't match team standards
2. Missing edge case test coverage
3. Missing docstring on modified function
4. Incomplete error handling
"""
return {
"files": {
"auth.py": get_auth_py_content(),
"test_auth.py": get_test_auth_py_content(),
},
"test_file": "test_auth.py",
"participants": [
{
"user_id": "bob_junior",
"role": "assignee",
"responsibility": "Fix the bug, ensure tests pass",
"status": "in_progress",
},
{
"user_id": "alice_senior",
"role": "reviewer",
"responsibility": "Review code, check quality, approve",
"status": "pending",
},
{
"user_id": "dave_lead",
"role": "observer",
"responsibility": "Oversee, escalate if needed",
"status": "pending",
},
],
"initial_state": "Bug reported: authenticate() returns True for invalid tokens",
"expected_issues": [
"Code style inconsistency (spacing, naming)",
"Missing docstring on authenticate_token()",
"Missing test for None token",
"Incomplete error message in exception",
"Missing type hints on helper function",
],
"review_checklist": [
"β Code style matches team guidelines",
"β Test coverage is adequate",
"β Docstrings present and clear",
"β Error handling is complete",
"β No security vulnerabilities introduced",
],
"tools_to_use": [
"read_file",
"edit_file",
"run_tests",
"search_code",
"run_type_check",
"get_test_coverage",
],
"difficulty": "medium",
}
def get_auth_py_content() -> str:
"""Get the auth.py file with the bug to fix."""
return '''"""
Authentication module with token validation.
"""
import re
from typing import Optional
def validate_token_format(token: str) -> bool:
"""Validate token format (basic pattern check)."""
if not token or not isinstance(token, str):
return False
# Simple pattern: alphanumeric, at least 20 chars
return bool(re.match(r"^[a-zA-Z0-9]{20,}$", token))
def check_token_expiry(token: str) -> bool:
"""Check if token is expired."""
# Simplified: tokens with "expired" in them are expired
return "expired" not in token.lower()
# Bug: This function has incorrect logic
def authenticate_token(token: str) -> bool:
"""
Authenticate a user token.
Args:
token: The authentication token to validate
Returns:
True if token is valid, False otherwise
"""
# BUG: Returns True for None/empty tokens
if not validate_token_format(token):
return True # WRONG: Should return False
# BUG: Doesn't check expiry
return True # WRONG: Should check expiry too
def authenticate(username: str, password: str) -> bool:
"""
Authenticate a user with username and password.
Args:
username: The username
password: The password (should be hashed)
Returns:
True if authenticated, False otherwise
"""
# For this exercise, accept specific credentials
if username == "admin" and password == "secret123":
return True
return False
def authenticate_with_token(token: str) -> bool:
"""Authenticate using a token."""
# Uses the buggy function
return authenticate_token(token)
def get_user_info(token: Optional[str]) -> dict:
"""Get user info if token is valid."""
if token and authenticate_with_token(token):
return {"user_id": "123", "username": "admin"}
return {}
'''
def get_test_auth_py_content() -> str:
"""Get the test_auth.py file."""
return '''"""
Tests for authentication module.
"""
import pytest
from auth import (
validate_token_format,
check_token_expiry,
authenticate_token,
authenticate,
authenticate_with_token,
get_user_info,
)
def test_validate_token_format_valid():
"""Test validate_token_format with valid tokens."""
assert validate_token_format("abcd1234567890abcdef")
assert validate_token_format("ABCDEF1234567890WXYZ")
assert validate_token_format("a" * 50) # Very long token
def test_validate_token_format_invalid():
"""Test validate_token_format with invalid tokens."""
assert not validate_token_format("short")
assert not validate_token_format("")
assert not validate_token_format("token-with-dashes")
assert not validate_token_format("token_with_underscores")
def test_validate_token_format_none():
"""Test validate_token_format with None."""
assert not validate_token_format(None)
def test_check_token_expiry():
"""Test check_token_expiry function."""
assert check_token_expiry("valid_token_abc123")
assert not check_token_expiry("expired_token_xyz")
assert not check_token_expiry("EXPIRED")
def test_authenticate_token_valid():
"""Test authenticate_token with valid token."""
assert authenticate_token("validtoken12345678901")
def test_authenticate_token_invalid_format():
"""Test authenticate_token rejects invalid format."""
assert not authenticate_token("short")
assert not authenticate_token("")
def test_authenticate_token_expired():
"""Test authenticate_token rejects expired tokens."""
assert not authenticate_token("expiredtoken12345678901")
def test_authenticate_token_none():
"""Test authenticate_token with None token."""
assert not authenticate_token(None)
def test_authenticate_valid_credentials():
"""Test authenticate with valid credentials."""
assert authenticate("admin", "secret123")
def test_authenticate_invalid_credentials():
"""Test authenticate with invalid credentials."""
assert not authenticate("admin", "wrongpassword")
assert not authenticate("unknownuser", "secret123")
def test_authenticate_with_token():
"""Test authenticate_with_token."""
assert authenticate_with_token("validtoken12345678901")
assert not authenticate_with_token("invalidtoken")
assert not authenticate_with_token("expiredtoken12345678901")
def test_get_user_info_valid_token():
"""Test get_user_info with valid token."""
info = get_user_info("validtoken12345678901")
assert info.get("user_id") == "123"
assert info.get("username") == "admin"
def test_get_user_info_invalid_token():
"""Test get_user_info with invalid token."""
assert get_user_info("invalid") == {}
assert get_user_info(None) == {}
if __name__ == "__main__":
pytest.main([__file__, "-v"])
''' |