File size: 6,155 Bytes
697c967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Final Comprehensive API Test

This test verifies the complete workflow of the Todo List API.
"""

import requests
import json
from datetime import datetime

BASE_URL = "http://localhost:8000"

def test_complete_workflow():
    print("Testing Complete API Workflow")
    print("=" * 50)

    # 1. Register a new user
    print("1. Registering new user...")
    timestamp = str(int(datetime.now().timestamp()))
    user_data = {
        "email": f"workflow_test_{timestamp}@example.com",
        "password": "securepassword123",
        "name": f"Workflow Test {timestamp}"
    }

    response = requests.post(f"{BASE_URL}/api/auth/register",
                           json=user_data,
                           headers={"Content-Type": "application/json"})

    if response.status_code != 201:
        print(f"   FAILED: Registration failed with status {response.status_code}")
        return False

    result = response.json()
    token = result['token']
    user_id = result['user']['id']
    print(f"   SUCCESS: User {result['user']['email']} registered with ID {user_id}")

    # 2. Login with the same user
    print("2. Logging in user...")
    login_data = {
        "email": user_data['email'],
        "password": user_data['password']
    }

    response = requests.post(f"{BASE_URL}/api/auth/login",
                           json=login_data,
                           headers={"Content-Type": "application/json"})

    if response.status_code != 200:
        print(f"   FAILED: Login failed with status {response.status_code}")
        return False

    result = response.json()
    new_token = result['token']
    print(f"   SUCCESS: User logged in successfully")

    # 3. Create multiple tasks
    print("3. Creating tasks...")
    headers = {
        "Authorization": f"Bearer {new_token}",
        "Content-Type": "application/json"
    }

    tasks_to_create = [
        {"title": "First Task", "description": "This is the first task", "completed": False},
        {"title": "Second Task", "description": "This is the second task", "completed": True},
        {"title": "Third Task", "description": "This is the third task", "completed": False}
    ]

    created_tasks = []
    for i, task_data in enumerate(tasks_to_create):
        response = requests.post(f"{BASE_URL}/api/{user_id}/tasks",
                               json=task_data,
                               headers=headers)

        if response.status_code != 201:
            print(f"   FAILED: Creating task {i+1} failed with status {response.status_code}")
            return False

        task = response.json()
        created_tasks.append(task)
        print(f"   SUCCESS: Task '{task['title']}' created with ID {task['id']}")

    # 4. Get all tasks
    print("4. Retrieving all tasks...")
    response = requests.get(f"{BASE_URL}/api/{user_id}/tasks", headers=headers)

    if response.status_code != 200:
        print(f"   FAILED: Getting tasks failed with status {response.status_code}")
        return False

    tasks = response.json()
    print(f"   SUCCESS: Retrieved {len(tasks)} tasks")

    # 5. Get specific task
    print("5. Retrieving specific task...")
    first_task_id = created_tasks[0]['id']
    response = requests.get(f"{BASE_URL}/api/{user_id}/tasks/{first_task_id}", headers=headers)

    if response.status_code != 200:
        print(f"   FAILED: Getting specific task failed with status {response.status_code}")
        return False

    task = response.json()
    print(f"   SUCCESS: Retrieved task '{task['title']}'")

    # 6. Update a task
    print("6. Updating a task...")
    update_data = {
        "title": "Updated First Task",
        "description": "This is the updated first task",
        "completed": True
    }

    response = requests.put(f"{BASE_URL}/api/{user_id}/tasks/{first_task_id}",
                          json=update_data,
                          headers=headers)

    if response.status_code != 200:
        print(f"   FAILED: Updating task failed with status {response.status_code}")
        return False

    updated_task = response.json()
    print(f"   SUCCESS: Task updated to '{updated_task['title']}'")

    # 7. Update task completion status
    print("7. Updating task completion status...")
    completion_data = {"completed": False}

    response = requests.patch(f"{BASE_URL}/api/{user_id}/tasks/{first_task_id}/complete",
                            json=completion_data,
                            headers=headers)

    if response.status_code != 200:
        print(f"   FAILED: Updating completion status failed with status {response.status_code}")
        return False

    completed_task = response.json()
    print(f"   SUCCESS: Task completion updated to {completed_task['completed']}")

    # 8. Delete a task
    print("8. Deleting a task...")
    response = requests.delete(f"{BASE_URL}/api/{user_id}/tasks/{first_task_id}", headers=headers)

    if response.status_code != 204:
        print(f"   FAILED: Deleting task failed with status {response.status_code}")
        return False

    print(f"   SUCCESS: Task deleted")

    # 9. Logout
    print("9. Logging out user...")
    response = requests.post(f"{BASE_URL}/api/auth/logout", headers=headers)

    if response.status_code != 200:
        print(f"   FAILED: Logout failed with status {response.status_code}")
        return False

    print(f"   SUCCESS: User logged out")

    # 10. Test unauthorized access
    print("10. Testing unauthorized access...")
    response = requests.get(f"{BASE_URL}/api/{user_id}/tasks")

    if response.status_code != 401:
        print(f"   FAILED: Unauthorized access should return 401, got {response.status_code}")
        return False

    print(f"   SUCCESS: Unauthorized access properly blocked ({response.status_code})")

    print("\n" + "=" * 50)
    print("✅ ALL WORKFLOW TESTS PASSED!")
    print("✅ API is fully functional!")
    print("=" * 50)

    return True

if __name__ == "__main__":
    success = test_complete_workflow()
    if success:
        print("\n🎉 The Todo List API is working perfectly! 🎉")
    else:
        print("\n❌ Some tests failed")