예제 #1
0
async def test_devmgr(aiohttp_client):
    remove_existing_db()
    bumper.db = "tests/tmp.db"  # Set db location for testing
    confserver = create_confserver()
    client = await aiohttp_client(create_app)
    bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot("127.0.0.1")

    # Test PollSCResult
    postbody = {"td": "PollSCResult"}
    resp = await client.post("/api/iot/devmanager.do", json=postbody)
    assert resp.status == 200
    text = await resp.text()
    test_resp = json.loads(text)
    assert test_resp["ret"] == "ok"

    # Test HasUnreadMsg
    postbody = {"td": "HasUnreadMsg"}
    resp = await client.post("/api/iot/devmanager.do", json=postbody)
    assert resp.status == 200
    text = await resp.text()
    test_resp = json.loads(text)
    assert test_resp["ret"] == "ok"
    assert test_resp["unRead"] == False

    # Test BotCommand
    bumper.bot_add("sn_1234", "did_1234", "dev_1234", "res_1234", "eco-ng")
    bumper.bot_set_mqtt("did_1234", True)
    postbody = {"toId": "did_1234"}

    # Test return get status
    command_getstatus_resp = {
        "id": "resp_1234",
        "resp": "<ctl ret='ok' status='idle'/>",
        "ret": "ok",
    }
    bumper.mqtt_helperbot.send_command = mock.MagicMock(
        return_value=async_return(command_getstatus_resp))
    resp = await client.post("/api/iot/devmanager.do", json=postbody)
    assert resp.status == 200
    text = await resp.text()
    test_resp = json.loads(text)
    assert test_resp["ret"] == "ok"

    # Test return fail timeout
    command_timeout_resp = {
        "id": "resp_1234",
        "errno": "timeout",
        "ret": "fail"
    }
    bumper.mqtt_helperbot.send_command = mock.MagicMock(
        return_value=async_return(command_timeout_resp))
    resp = await client.post("/api/iot/devmanager.do", json=postbody)
    assert resp.status == 200
    text = await resp.text()
    test_resp = json.loads(text)
    assert test_resp["ret"] == "fail"
예제 #2
0
    async def on_broker_client_disconnected(self, client_id):

        didsplit = str(client_id).split("@")

        bot = bumper.bot_get(didsplit[0])
        if bot:
            bumper.bot_set_mqtt(bot["did"], False)
            return

        clientresource = didsplit[1].split("/")[1]
        client = bumper.client_get(clientresource)
        if client:
            bumper.client_set_mqtt(client["resource"], False)
            return
예제 #3
0
파일: mqttserver.py 프로젝트: erkkah/bumper
    async def on_broker_client_disconnected(self, client_id):
        try:
            didsplit = str(client_id).split("@")

            bot = bumper.bot_get(didsplit[0])
            if bot:
                bumper.bot_set_mqtt(bot["did"], False)

            #clientuserid = didsplit[0]
            clientresource = didsplit[1].split("/")[1]
            client = bumper.client_get(clientresource)
            if client:
                bumper.client_set_mqtt(client["resource"], False)

        except Exception as e:
            mqttserverlog.exception("{}".format(e))
예제 #4
0
def test_bot_db():
    bumper.db = "tests/tmp.db"  # Set db location for testing
    bumper.bot_add("sn_123", "did_123", "dev_123", "res_123", "co_123")
    assert_true(bumper.bot_get("did_123"))  # Test that bot was added to db

    bumper.bot_set_nick("did_123", "nick_123")
    assert_equals(bumper.bot_get("did_123")["nick"],
                  "nick_123")  # Test that nick was added to bot

    bumper.bot_set_mqtt("did_123", True)
    assert_true(bumper.bot_get("did_123")
                ["mqtt_connection"])  # Test that mqtt was set True for bot

    bumper.bot_set_xmpp("did_123", True)
    assert_true(bumper.bot_get("did_123")
                ["xmpp_connection"])  # Test that xmpp was set True for bot

    bumper.bot_remove("did_123")
    assert_false(bumper.bot_get("did_123"))  # Test that bot is no longer in db
예제 #5
0
async def test_lg_logs(aiohttp_client):
    remove_existing_db()
    bumper.db = "tests/tmp.db"  # Set db location for testing
    bumper.bot_add("sn_1234", "did_1234", "ls1ok3", "res_1234", "eco-ng")
    bumper.bot_set_mqtt("did_1234", True)
    confserver = create_confserver()
    client = await aiohttp_client(create_app)
    bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot("127.0.0.1")

    # Test return get status
    command_getstatus_resp = {
        "id": "resp_1234",
        "resp": "<ctl ret='ok' status='idle'/>",
        "ret": "ok",
    }
    bumper.mqtt_helperbot.send_command = mock.MagicMock(
        return_value=async_return(command_getstatus_resp)
    )

    # Test GetGlobalDeviceList
    postbody = {
        "auth": {
            "realm": "ecouser.net",
            "resource": "ECOGLOBLEac5ae987",
            "token": "token_1234",
            "userid": "testuser",
            "with": "users",
        },
        "did": "did_1234",
        "resource": "res_1234",
        "td": "GetCleanLogs",
    }
    resp = await client.post("/api/lg/log.do", json=postbody)
    assert resp.status == 200
    text = await resp.text()
    jsonresp = json.loads(text)
    assert jsonresp["ret"] == "ok"
예제 #6
0
def test_devmgr():
    if os.path.exists("tests/tmp.db"):
        os.remove("tests/tmp.db")  # Remove existing db
    bumper.db = "tests/tmp.db"  # Set db location for testing
    loop = asyncio.get_event_loop()
    client = TestClient(TestServer(app), loop=loop)
    loop.run_until_complete(client.start_server())
    root = "http://{}".format(confserver.address)

    async def test_devmanager(postbody=None, command=False):
        resp = await client.post("/api/iot/devmanager.do", json=postbody)

        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            if not command:
                assert jsonresp["ret"] == "ok"
            else:
                if "ret" in jsonresp:
                    if jsonresp["ret"] == "ok":
                        assert jsonresp["resp"]
                    else:
                        assert jsonresp["errno"]
        else:
            assert jsonresp

    # Test PollSCResult
    postbody = {"td": "PollSCResult"}
    # Test
    loop.run_until_complete(test_devmanager(postbody, command=False))

    # Test BotCommand
    bumper.bot_add("sn_1234", "did_1234", "dev_1234", "res_1234", "eco-ng")
    bumper.bot_set_mqtt("did_1234", True)
    postbody = {"toId": "did_1234"}

    # Test return get status
    command_getstatus_resp = {
        "id": "resp_1234",
        "resp": "<ctl ret='ok' status='idle'/>",
        "ret": "ok"
    }
    confserver.helperbot.send_command = mock.MagicMock(
        return_value=async_return(command_getstatus_resp))
    # Test
    loop.run_until_complete(test_devmanager(postbody, command=True))

    # Test return fail timeout
    command_timeout_resp = {
        "id": "resp_1234",
        "errno": "timeout",
        "ret": "fail"
    }
    confserver.helperbot.send_command = mock.MagicMock(
        return_value=async_return(command_timeout_resp))
    # Test
    loop.run_until_complete(test_devmanager(postbody, command=True))

    # Set bot not on mqtt
    bumper.bot_set_mqtt("did_1234", False)
    confserver.helperbot.send_command = mock.MagicMock(
        return_value=async_return(command_getstatus_resp))
    # Test
    loop.run_until_complete(test_devmanager(postbody, command=True))

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done