-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_delete.py
More file actions
64 lines (52 loc) · 1.77 KB
/
debug_delete.py
File metadata and controls
64 lines (52 loc) · 1.77 KB
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
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'automax.settings')
django.setup()
from django.test import Client
from django.contrib.auth.models import User
from main.models import Listing
def run():
# 1. Setup Users and Listings
user1 = User.objects.first()
user2 = User.objects.last()
if user1 == user2:
print("Need at least 2 users to test permissions properly.")
user2 = User.objects.create_user(username='test_user_2', password='password')
print(f"U1:{user1} U2:{user2}")
# Create a listing for User1
listing = Listing.objects.create(
seller=user1.profile,
brand='bmw',
model='Test Delete Model',
vin='123456789',
mileage=1000,
color='red',
description='To be deleted',
engine='V8',
transmission='automatic'
)
c = Client()
# Check URL resolution
from django.urls import resolve
try:
res = resolve(f'/listing/{listing.id}/delete/')
print(f"URL Resolve: {res.func.__name__}")
except Exception as e:
print(f"URL Fail: {e}")
# Test
c.force_login(user2)
r1 = c.get(f'/listing/{listing.id}/delete/', follow=True, HTTP_HOST='127.0.0.1')
print(f"R1: {r1.status_code}")
if Listing.objects.filter(id=listing.id).exists():
print("PASS: Unauth delete prevented.")
else:
print("FAIL: Unauth delete happened!")
c.force_login(user1)
r2 = c.get(f'/listing/{listing.id}/delete/', follow=True, HTTP_HOST='127.0.0.1')
print(f"R2: {r2.status_code}")
if not Listing.objects.filter(id=listing.id).exists():
print("PASS: Auth delete success.")
else:
print("FAIL: Auth delete failed!")
if __name__ == '__main__':
run()