@@ -38,3 +38,56 @@ async def test_system_usage(aiohttp_client):
38
38
resp = await response .json ()
39
39
assert "cpu" in resp
40
40
assert resp ["cpu" ]["count" ] > 0
41
+
42
+
43
+ @pytest .mark .asyncio
44
+ async def test_allocation_invalid_auth_token (aiohttp_client ):
45
+ """Test that the allocation endpoint fails when an invalid auth token is provided."""
46
+ settings .ALLOCATION_TOKEN_HASH = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # = "test"
47
+ app = setup_webapp ()
48
+ client = await aiohttp_client (app )
49
+ response = await client .post (
50
+ "/control/allocations" ,
51
+ json = {"persistent_vms" : []},
52
+ headers = {"X-Auth-Signature" : "notTest" },
53
+ )
54
+ assert response .status == 401
55
+ assert await response .text () == "Authentication token received is invalid"
56
+
57
+
58
+ @pytest .mark .asyncio
59
+ async def test_allocation_missing_auth_token (aiohttp_client ):
60
+ """Test that the allocation endpoint fails when auth token is not provided."""
61
+ app = setup_webapp ()
62
+ client = await aiohttp_client (app )
63
+ response : web .Response = await client .post (
64
+ "/control/allocations" ,
65
+ json = {"persistent_vms" : []},
66
+ )
67
+ assert response .status == 401
68
+ assert await response .text () == "Authentication token is missing"
69
+
70
+
71
+ @pytest .mark .asyncio
72
+ async def test_allocation_valid_token (aiohttp_client ):
73
+ """Test that the allocation endpoint fails when an invalid auth is provided.
74
+
75
+ This is a very simple test that don't start or stop any VM so the mock is minimal"""
76
+
77
+ class FakeVmPool :
78
+ def get_persistent_executions (self ):
79
+ return []
80
+
81
+ settings .ALLOCATION_TOKEN_HASH = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # = "test"
82
+ app = setup_webapp ()
83
+ app ["vm_pool" ] = FakeVmPool ()
84
+ app ["pubsub" ] = FakeVmPool ()
85
+ client = await aiohttp_client (app )
86
+
87
+ response : web .Response = await client .post (
88
+ "/control/allocations" ,
89
+ json = {"persistent_vms" : []},
90
+ headers = {"X-Auth-Signature" : "test" },
91
+ )
92
+ assert response .status == 200
93
+ assert await response .json () == {"success" : True , "successful" : [], "failing" : [], "errors" : {}}
0 commit comments