172 lines
7.8 KiB
Python
172 lines
7.8 KiB
Python
import re
|
|
import tempfile
|
|
import unittest
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from threading import Barrier
|
|
|
|
from .http_support import LiveServer, WebSession, prepare_database
|
|
|
|
|
|
class RealHTTPTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.directory = tempfile.TemporaryDirectory(prefix="fam-http-")
|
|
self.env, self.password, self.manage = prepare_database(self.directory.name)
|
|
self.server = LiveServer(self.env, self.directory.name)
|
|
self.server.start()
|
|
self.addCleanup(self.directory.cleanup)
|
|
self.addCleanup(self.server.stop)
|
|
self.alice = WebSession(self.server.url)
|
|
self.bob = WebSession(self.server.url)
|
|
self.alice.login("synthetic-one", self.password)
|
|
self.bob.login("synthetic-two", self.password)
|
|
|
|
def selection(self, quantity=1):
|
|
# IDs are discovered from actual rendered forms, not guessed from seed ordering.
|
|
html = self.alice.get("/bestand/hinzufuegen/")[1]
|
|
manufacturer = re.search(r'<option value="(\d+)"', html).group(1)
|
|
html = self.alice.post(
|
|
"/bestand/hinzufuegen/", {"manufacturer": manufacturer, "action": "filter"}
|
|
)[1]
|
|
material = re.search(
|
|
r'<select name="material".*?<option value="([^"]+)"', html, re.S
|
|
).group(1)
|
|
filters = {"manufacturer": manufacturer, "material": material}
|
|
html = self.alice.post("/bestand/hinzufuegen/", filters | {"action": "filter"})[1]
|
|
product = re.search(
|
|
r'<select name="product_type".*?<option value="(\d+)"', html, re.S
|
|
).group(1)
|
|
filters["product_type"] = product
|
|
html = self.alice.post(
|
|
"/bestand/hinzufuegen/",
|
|
filters | {"action": "filter"},
|
|
)[1]
|
|
color = re.search(r'<select name="color".*?<option value="(\d+)"', html, re.S).group(1)
|
|
html = self.alice.post(
|
|
"/bestand/hinzufuegen/",
|
|
filters | {"color": color, "action": "filter"},
|
|
)[1]
|
|
spool = re.search(r'<select name="spool_type".*?<option value="([^"]+)"', html, re.S).group(
|
|
1
|
|
)
|
|
return filters | {"color": color, "spool_type": spool, "quantity": quantity}
|
|
|
|
def test_two_accounts_privacy_shared_totals_static_and_server_restart(self):
|
|
payload = self.selection(2)
|
|
self.assertEqual(self.alice.post("/bestand/hinzufuegen/", payload)[0], 200)
|
|
self.alice.post("/bestand/hinzufuegen/", payload | {"quantity": 1})
|
|
html = self.alice.get("/bestand/")[1]
|
|
self.assertIn('class="quantity">3</td>', html)
|
|
edit_url = re.search(r'href="(/bestand/\d+/bearbeiten/)"', html).group(1)
|
|
self.assertEqual(self.bob.get(edit_url)[0], 404)
|
|
self.assertNotIn('class="quantity">3</td>', self.bob.get("/bestand/")[1])
|
|
self.alice.post("/bestellung/hinzufuegen/", payload)
|
|
self.bob.post("/bestellung/hinzufuegen/", payload | {"quantity": 1})
|
|
html = self.bob.get("/bestellung/")[1]
|
|
self.assertIn("Bestellwunsch · synthetic-one", html)
|
|
self.assertIn("Bestellwunsch · synthetic-two", html)
|
|
self.assertIn('class="quantity">2</td>', html)
|
|
self.assertIn('class="quantity">1</td>', html)
|
|
css = re.search(r'href="(/static/[^"]+\.css)"', html).group(1)
|
|
self.assertEqual(self.alice.get(css)[0], 200)
|
|
self.assertIn("text/css", self.alice.get(css)[2]["Content-Type"])
|
|
self.server.stop()
|
|
self.server.start()
|
|
self.assertIn('class="quantity">3</td>', self.alice.get("/bestand/")[1])
|
|
self.assertIn("Bestellwunsch · synthetic-one", self.bob.get("/bestellung/")[1])
|
|
print(
|
|
"HTTP: separate logins, private stock, 2+1 aggregation, CSS and Gunicorn restart verified."
|
|
)
|
|
|
|
def test_parallel_additions_do_not_lose_quantities(self):
|
|
payload = self.selection()
|
|
clients = [WebSession(self.server.url) for _ in range(6)]
|
|
for client in clients:
|
|
client.login("synthetic-one", self.password)
|
|
|
|
def add(client):
|
|
return client.post("/bestand/hinzufuegen/", payload)[0]
|
|
|
|
with ThreadPoolExecutor(max_workers=6) as pool:
|
|
self.assertEqual(list(pool.map(add, clients)), [200] * 6)
|
|
self.assertIn('class="quantity">6</td>', self.alice.get("/bestand/")[1])
|
|
print("HTTP: six simultaneous additions, one position, quantity six verified.")
|
|
|
|
def test_parallel_marketplace_take_only_transfers_once(self):
|
|
payload = self.selection()
|
|
self.alice.post("/bestand/hinzufuegen/", payload)
|
|
html = self.alice.get("/bestand/")[1]
|
|
offer_url = re.search(r'href="(/bestand/\d+/anbieten/)"', html).group(1)
|
|
form = self.alice.get(offer_url)[1]
|
|
revision = re.search(r'name="expected_revision" value="([^"]+)"', form).group(1)
|
|
status, html, _, _ = self.alice.post(
|
|
offer_url, {"quantity": 1, "price": "10,00", "expected_revision": revision}
|
|
)
|
|
self.assertEqual(status, 200)
|
|
self.assertIn("Dein Angebot", html)
|
|
self.manage(
|
|
"shell",
|
|
"-c",
|
|
"import os; from django.contrib.auth import get_user_model; "
|
|
"get_user_model().objects.create_user('synthetic-three', password=os.environ['FAM_TEST_PASSWORD'])",
|
|
)
|
|
third = WebSession(self.server.url)
|
|
third.login("synthetic-three", self.password)
|
|
take_url = re.search(
|
|
r'href="(/marktplatz/\d+/uebernehmen/)"', self.bob.get("/marktplatz/")[1]
|
|
).group(1)
|
|
requests = []
|
|
for session in (self.bob, third):
|
|
html = session.get(take_url)[1]
|
|
self.assertIn("Übernahme bestätigen", html)
|
|
token = re.search(r'name="csrfmiddlewaretoken" value="([^"]+)"', html).group(1)
|
|
requests.append((session, token))
|
|
ready = Barrier(2)
|
|
|
|
def take(request):
|
|
session, token = request
|
|
ready.wait(timeout=5)
|
|
return session.request(take_url, {"csrfmiddlewaretoken": token})[0]
|
|
|
|
with ThreadPoolExecutor(max_workers=2) as pool:
|
|
results = list(pool.map(take, requests))
|
|
self.assertEqual(results, [200, 200])
|
|
quantity_cell = 'class="quantity">1</td>'
|
|
self.assertNotIn(quantity_cell, self.alice.get("/bestand/")[1])
|
|
self.assertEqual(
|
|
sum(quantity_cell in session.get("/bestand/")[1] for session in (self.bob, third)), 1
|
|
)
|
|
self.assertEqual(
|
|
self.manage(
|
|
"shell",
|
|
"-c",
|
|
"from filament.models import MarketplaceOffer, StockMovement; "
|
|
"print(MarketplaceOffer.objects.filter(status='taken').count(), "
|
|
"StockMovement.objects.filter(kind='marketplace_in').count())",
|
|
)
|
|
.strip()
|
|
.splitlines()[-1],
|
|
"1 1",
|
|
)
|
|
|
|
def test_member_can_share_and_revoke_stock_from_profile(self):
|
|
payload = self.selection(2)
|
|
self.alice.post("/bestand/hinzufuegen/", payload)
|
|
shared = "/bestand/geteilt/"
|
|
link = f'href="{shared}"'
|
|
self.assertNotIn(link, self.bob.get("/bestand/")[1])
|
|
self.assertNotIn('class="quantity">2</td>', self.bob.get(shared)[1])
|
|
|
|
self.alice.post("/profil/", {"share_inventory": "on", "first_name": "Private Example"})
|
|
self.assertIn(link, self.bob.get("/bestand/")[1])
|
|
status, html, headers, _ = self.bob.get(shared)
|
|
self.assertEqual(status, 200)
|
|
self.assertIn("synthetic-one", html)
|
|
self.assertIn('class="quantity">2</td>', html)
|
|
self.assertNotIn("Private Example", html)
|
|
self.assertNotIn("data-consume", html)
|
|
self.assertIn("no-store", headers["Cache-Control"])
|
|
|
|
self.alice.post("/profil/", {})
|
|
self.assertNotIn('class="quantity">2</td>', self.bob.get(shared)[1])
|
|
self.assertNotIn(link, self.bob.get("/bestand/")[1])
|