2019-01-05 22:44:54 +01:00
|
|
|
import logging
|
2019-01-04 09:29:09 +01:00
|
|
|
from unittest import mock
|
2022-02-20 11:50:44 +01:00
|
|
|
from aiohttp.test_utils import unittest_run_loop
|
|
|
|
from tests.cbpi_config_fixture import CraftBeerPiTestCase
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-05 22:44:54 +01:00
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2022-02-20 11:50:44 +01:00
|
|
|
class ActorTestCase(CraftBeerPiTestCase):
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-02 00:48:36 +01:00
|
|
|
async def test_actor_switch(self):
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2018-12-31 00:22:00 +01:00
|
|
|
resp = await self.client.post(path="/login", data={"username": "cbpi", "password": "123"})
|
2022-02-20 11:50:44 +01:00
|
|
|
assert resp.status == 200, "login should be successful"
|
2018-11-16 20:35:59 +01:00
|
|
|
|
2022-02-20 11:50:44 +01:00
|
|
|
resp = await self.client.request("POST", "/actor/3CUJte4bkxDMFCtLX8eqsX/on")
|
|
|
|
assert resp.status == 204, "switching actor on should work"
|
|
|
|
i = self.cbpi.actor.find_by_id("3CUJte4bkxDMFCtLX8eqsX")
|
2019-01-05 22:44:54 +01:00
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
assert i.instance.state is True
|
|
|
|
|
2022-02-20 11:50:44 +01:00
|
|
|
resp = await self.client.request("POST", "/actor/3CUJte4bkxDMFCtLX8eqsX/off")
|
2018-11-16 20:35:59 +01:00
|
|
|
assert resp.status == 204
|
2022-02-20 11:50:44 +01:00
|
|
|
i = self.cbpi.actor.find_by_id("3CUJte4bkxDMFCtLX8eqsX")
|
2018-11-16 20:35:59 +01:00
|
|
|
assert i.instance.state is False
|
|
|
|
|
2019-01-02 00:48:36 +01:00
|
|
|
async def test_crud(self):
|
|
|
|
data = {
|
2022-02-20 11:50:44 +01:00
|
|
|
"name": "SomeActor",
|
|
|
|
"power": 100,
|
|
|
|
"props": {
|
|
|
|
},
|
|
|
|
"state": False,
|
|
|
|
"type": "DummyActor"
|
2019-01-02 00:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Add new sensor
|
|
|
|
resp = await self.client.post(path="/actor/", json=data)
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
m = await resp.json()
|
|
|
|
sensor_id = m["id"]
|
|
|
|
|
|
|
|
# Get sensor
|
|
|
|
resp = await self.client.get(path="/actor/%s" % sensor_id)
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
m2 = await resp.json()
|
|
|
|
sensor_id = m2["id"]
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-05 22:44:54 +01:00
|
|
|
resp = await self.client.request("POST", "/actor/%s/on" % sensor_id)
|
|
|
|
assert resp.status == 204
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-02 00:48:36 +01:00
|
|
|
# Update Sensor
|
|
|
|
resp = await self.client.put(path="/actor/%s" % sensor_id, json=m)
|
|
|
|
assert resp.status == 200
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-02 00:48:36 +01:00
|
|
|
# # Delete Sensor
|
|
|
|
resp = await self.client.delete(path="/actor/%s" % sensor_id)
|
|
|
|
assert resp.status == 204
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
async def test_crud_negative(self):
|
|
|
|
data = {
|
|
|
|
"name": "CustomActor",
|
|
|
|
"type": "CustomActor",
|
|
|
|
"config": {
|
|
|
|
"interval": 5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get actor which not exists
|
|
|
|
resp = await self.client.get(path="/actor/%s" % 9999)
|
|
|
|
assert resp.status == 500
|
|
|
|
|
|
|
|
# Update not existing actor
|
|
|
|
resp = await self.client.put(path="/actor/%s" % 9999, json=data)
|
|
|
|
assert resp.status == 500
|
2019-01-04 09:29:09 +01:00
|
|
|
|
|
|
|
async def test_actor_action(self):
|
|
|
|
resp = await self.client.post(path="/actor/1/action", json=dict(name="myAction", parameter=dict(name="Manuel")))
|
|
|
|
assert resp.status == 204
|