forked from ArchipelagoMW/Archipelago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tracker.py
95 lines (81 loc) · 3.33 KB
/
test_tracker.py
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
import os
import pickle
from pathlib import Path
from typing import ClassVar
from uuid import UUID, uuid4
from flask import url_for
from . import TestBase
class TestTracker(TestBase):
room_id: UUID
tracker_uuid: UUID
log_filename: str
data: ClassVar[bytes]
@classmethod
def setUpClass(cls) -> None:
super().setUpClass()
with (Path(__file__).parent / "data" / "One_Archipelago.archipelago").open("rb") as f:
cls.data = f.read()
def setUp(self) -> None:
from pony.orm import db_session
from MultiServer import Context as MultiServerContext
from Utils import user_path
from WebHostLib.models import GameDataPackage, Room, Seed
super().setUp()
multidata = MultiServerContext.decompress(self.data)
with self.client.session_transaction() as session:
session["_id"] = uuid4()
self.tracker_uuid = uuid4()
with db_session:
# store game datapackage(s)
for game, game_data in multidata["datapackage"].items():
if not GameDataPackage.get(checksum=game_data["checksum"]):
GameDataPackage(checksum=game_data["checksum"],
data=pickle.dumps(game_data))
# create an empty seed and a room from it
seed = Seed(multidata=self.data, owner=session["_id"])
room = Room(seed=seed, owner=session["_id"], tracker=self.tracker_uuid)
self.room_id = room.id
self.log_filename = user_path("logs", f"{self.room_id}.txt")
def tearDown(self) -> None:
from pony.orm import db_session, select
from WebHostLib.models import Command, Room
with db_session:
for command in select(command for command in Command if command.room.id == self.room_id): # type: ignore
command.delete()
room: Room = Room.get(id=self.room_id)
room.seed.delete()
room.delete()
try:
os.unlink(self.log_filename)
except FileNotFoundError:
pass
def test_valid_if_modified_since(self) -> None:
"""
Verify that we get a 200 response for valid If-Modified-Since
"""
with self.app.app_context(), self.app.test_request_context():
response = self.client.get(
url_for(
"get_player_tracker",
tracker=self.tracker_uuid,
tracked_team=0,
tracked_player=1,
),
headers={"If-Modified-Since": "Wed, 21 Oct 2015 07:28:00 GMT"},
)
self.assertEqual(response.status_code, 200)
def test_invalid_if_modified_since(self) -> None:
"""
Verify that we get a 400 response for invalid If-Modified-Since
"""
with self.app.app_context(), self.app.test_request_context():
response = self.client.get(
url_for(
"get_player_tracker",
tracker=self.tracker_uuid,
tracked_team=1,
tracked_player=0,
),
headers={"If-Modified-Since": "Wed, 21 Oct 2015 07:28:00"}, # missing timezone
)
self.assertEqual(response.status_code, 400)