test: verify push notifications are sent
authorKyle Fuller <redacted>
Sun, 5 Apr 2020 13:03:46 +0000 (14:03 +0100)
committerKyle Fuller <redacted>
Sun, 5 Apr 2020 13:16:22 +0000 (14:16 +0100)
test/test_palaver.py

index e8be557edc80e9c303bb4361b25950f47bc5faad..a2a9d94a958efead6979c4809078ed111f1b1562 100644 (file)
@@ -50,7 +50,10 @@ async def setUp(event_loop):
 
     return (proc, reader, writer)
 
+
 async def tearDown(proc):
+    await asyncio.sleep(0.2)
+
     proc.kill()
     await proc.wait()
 
@@ -58,6 +61,7 @@ async def tearDown(proc):
     if os.path.exists(config):
         os.remove(config)
 
+
 async def test_registering_device(event_loop):
     (proc, reader, writer) = await setUp(event_loop)
 
@@ -74,10 +78,10 @@ async def test_registering_device(event_loop):
     writer.write(b'PALAVER ADD MENTION-KEYWORD {nick}\r\n')
     writer.write(b'PALAVER END\r\n')
     await writer.drain()
-    time.sleep(1)
 
     await tearDown(proc)
 
+
 async def test_loading_module_new_cap(event_loop):
     await requires_znc_version('1.7.0')
 
@@ -101,10 +105,9 @@ async def test_loading_module_new_cap(event_loop):
     line = await reader.readline()
     assert line == b':*status!znc@znc.in PRIVMSG admin :Loaded module palaver: [test/fixtures/modules/palaver.so]\r\n'
 
-    time.sleep(1)
-
     await tearDown(proc)
 
+
 async def test_unloading_module_del_cap(event_loop):
     await requires_znc_version('1.7.0')
 
@@ -119,6 +122,69 @@ async def test_unloading_module_del_cap(event_loop):
     line = await reader.readline()
     assert line == b':*status!znc@znc.in PRIVMSG admin :Module palaver unloaded.\r\n'
 
-    time.sleep(1)
+    await tearDown(proc)
+
+
+async def test_receiving_notification(event_loop):
+    (proc, reader, writer) = await setUp(event_loop)
+
+    async def connected(reader, writer):
+        line = await reader.readline()
+        assert line == b'POST /push HTTP/1.1\r\n'
+
+        line = await reader.readline()
+        assert line == b'Host: 127.0.0.1\r\n'
+
+        line = await reader.readline()
+        assert line == b'Authorization: Bearer 9167e47b01598af7423e2ecd3d0a3ec4\r\n'
+
+        line = await reader.readline()
+        assert line == b'Connection: close\r\n'
+
+        line = await reader.readline()
+        assert line == b'Content-Length: 109\r\n'
+
+        line = await reader.readline()
+        assert line == b'Content-Type: application/json\r\n'
+
+        line = await reader.readline()
+        assert line == b'User-Agent: ZNC\r\n'
+
+        line = await reader.readline()
+        assert line == b'\r\n'
+
+        line = await reader.readline()
+        assert line == b'{"badge": 1,"message": "Test notification","sender": "palaver","network": "b758eaab1a4611a310642a6e8419fbff"}'
+
+        writer.write(b'HTTP/1.1 204 No Content\r\n\r\n')
+        await writer.drain()
+        writer.close()
+
+        connected.called = True
+
+    server = await asyncio.start_server(connected, host='127.0.0.1', port=0, loop=event_loop)
+    await asyncio.sleep(0.2)
+    addr = server.sockets[0].getsockname()
+    url = f'Serving on http://{addr[0]}:{addr[1]}/push'
+
+    writer.write(b'PALAVER IDENTIFY 9167e47b01598af7423e2ecd3d0a3ec4 611d3a30a3d666fc491cdea0d2e1dd6e b758eaab1a4611a310642a6e8419fbff\r\n')
+    await writer.drain()
+
+    line = await reader.readline()
+    assert line == b'PALAVER REQ *\r\n'
+
+    writer.write(b'PALAVER BEGIN 9167e47b01598af7423e2ecd3d0a3ec4 611d3a30a3d666fc491cdea0d2e1dd6e\r\n')
+    writer.write(f'PALAVER SET PUSH-ENDPOINT {url}\r\n'.encode('utf-8'))
+    writer.write(b'PALAVER END\r\n')
+    await writer.drain()
+
+    writer.write(b'PRIVMSG *palaver :test\r\n')
+    await writer.drain()
+
+    await asyncio.sleep(0.2)
+    server.close()
+    await server.wait_closed()
 
     await tearDown(proc)
+
+    assert connected.called
git clone https://git.99rst.org/PROJECT