-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ecommerce.py
More file actions
112 lines (88 loc) · 2.61 KB
/
Copy pathtest_ecommerce.py
File metadata and controls
112 lines (88 loc) · 2.61 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
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
import pytest
from main import app, db, Users, Products
@pytest.fixture
def client():
app.config["TESTING"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
with app.test_client() as client:
with app.app_context():
db.create_all()
yield client
def register_user(client):
return client.post("/register", json={
"username": "testuser",
"password": "password123",
"email": "test@example.com"
})
def login_user(client):
response = client.post("/login", json={
"username": "testuser",
"password": "password123"
})
return response.get_json()["token"]
def test_user_registration(client):
response = register_user(client)
assert response.status_code == 200
def test_user_login(client):
register_user(client)
response = client.post("/login", json={
"username": "testuser",
"password": "password123"
})
assert response.status_code == 200
assert "token" in response.get_json()
def test_product_listing(client):
with app.app_context():
product = Products(
product_name="Test Product",
amount=10,
price=9.99,
currency="USD"
)
db.session.add(product)
db.session.commit()
response = client.get("/products")
assert response.status_code == 200
assert len(response.get_json()) == 1
def test_add_to_cart(client):
register_user(client)
token = login_user(client)
with app.app_context():
product = Products(
product_name="Cart Product",
amount=5,
price=4.99,
currency="USD"
)
db.session.add(product)
db.session.commit()
product_id = product.id
response = client.post(
"/cart",
headers={"Authorization": f"Bearer {token}"},
json={"product_id": product_id, "quantity": 2}
)
assert response.status_code == 200
def test_cart_final(client):
register_user(client)
token = login_user(client)
with app.app_context():
product = Products(
product_name="Final Cart Product",
amount=5,
price=10.00,
currency="USD"
)
db.session.add(product)
db.session.commit()
client.post(
"/cart",
headers={"Authorization": f"Bearer {token}"},
json={"product_id": 1, "quantity": 1}
)
response = client.get(
"/carts/final",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
assert "total" in response.get_json()