Пример #1
0
def test_test_client_close_is_idempotent():
    """
    a test client, called multiple times, should
    not attempt to close the loop again.
    """
    with loop_context() as loop:
        app = _create_example_app(loop)
        client = TestClient(app)
        client.close()
    client.close()
Пример #2
0
def test_test_client_close_is_idempotent():
    """
    a test client, called multiple times, should
    not attempt to close the loop again.
    """
    loop = setup_test_loop()
    app = _create_example_app(loop)
    client = TestClient(app)
    client.close()
    teardown_test_loop(loop)
    client.close()
Пример #3
0
def test_test_client_close_is_idempotent():
    """
    a test client, called multiple times, should
    not attempt to close the loop again.
    """
    loop = setup_test_loop()
    app = _create_example_app(loop)
    client = TestClient(app)
    client.close()
    teardown_test_loop(loop)
    client.close()
Пример #4
0
def test_server_with_create_test_teardown():
    with loop_context() as loop:
        app = _create_example_app(loop)
        client = TestClient(app)

        @asyncio.coroutine
        def test_get_route():
            resp = yield from client.request("GET", "/")
            assert resp.status == 200
            text = yield from resp.text()
            assert "Hello, world" in text

        loop.run_until_complete(test_get_route())
        client.close()
Пример #5
0
def test_server_with_create_test_teardown():
    with loop_context() as loop:
        app = _create_example_app(loop)
        client = TestClient(app)

        @asyncio.coroutine
        def test_get_route():
            resp = yield from client.request("GET", "/")
            assert resp.status == 200
            text = yield from resp.text()
            assert "Hello, world" in text

        loop.run_until_complete(test_get_route())
        client.close()
Пример #6
0
def clients_context(routine, patch_nyms=nyms):
    with patch('websockets_server.core.server.nyms', patch_nyms):
        with loop_context() as loopext:
            app = HXApplication()
            server = TestServer(app)
            client1 = TestClient(server, loop=loopext)
            loopext.run_until_complete(client1.start_server())
            client2 = TestClient(server, loop=loopext)

            try:
                loopext.run_until_complete(routine(client2, client2, app))
            finally:
                loopext.run_until_complete(client1._server.close())
                loopext.run_until_complete(client1.close())
                loopext.run_until_complete(client2.close())
Пример #7
0
def test_checkVersion():
    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_handle_checkVersion():
        resp = await client.get(
            "/1/private/us/en/dev_1234/ios/1/0/0/common/checkVersion")
        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["code"] == bumper.RETURN_API_SUCCESS
        else:
            assert jsonresp

    # Test
    loop.run_until_complete(test_handle_checkVersion())

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #8
0
def test_postLookup():
    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_handle_lookup(postbody=None):
        resp = await client.post("/lookup.do", json=postbody)

        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["result"] == "ok"
        else:
            assert jsonresp

    # Test FindBest
    postbody = {"todo": "FindBest", "service": "EcoMsgNew"}
    # Test
    loop.run_until_complete(test_handle_lookup(postbody))

    # Test EcoUpdate
    postbody = {"todo": "FindBest", "service": "EcoUpdate"}
    # Test
    loop.run_until_complete(test_handle_lookup(postbody))

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #9
0
def test_logout():
    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_handle_logout(token=None):
        resp = await client.get(
            "/1/private/us/en/dev_1234/ios/1/0/0/user/logout?accessToken={}".
            format(token))
        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["code"] == bumper.RETURN_API_SUCCESS
        else:
            assert jsonresp

    # Add a token to user and test
    bumper.user_add("testuser")
    bumper.user_add_device("testuser", "dev_1234")
    bumper.user_add_token("testuser", "token_1234")
    # Test
    loop.run_until_complete(test_handle_logout(token="token_1234"))

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
def client(event_loop, app, cookie_jar):
    from aiohttp.test_utils import TestServer, TestClient

    _server = TestServer(app, loop=event_loop)
    _client = TestClient(_server, loop=event_loop, cookie_jar=cookie_jar)
    event_loop.run_until_complete(_client.start_server())
    yield _client
    event_loop.run_until_complete(_client.close())
Пример #11
0
def test_checkLogin():
    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_handle_checkLogin(token=None):
        resp = await client.get(
            "/1/private/us/en/dev_1234/ios/1/0/0/user/checkLogin?accessToken={}"
            .format(token))
        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["code"] == bumper.RETURN_API_SUCCESS
            assert "accessToken" in jsonresp["data"]
            if not token:
                assert jsonresp["data"]["accessToken"] != "token_1234"
            else:
                assert jsonresp["data"]["accessToken"] == "token_1234"

            assert "uid" in jsonresp["data"]
            assert "username" in jsonresp["data"]
        else:
            assert jsonresp

    # Test
    loop.run_until_complete(test_handle_checkLogin())

    # Add a user to db and test with existing users
    bumper.user_add("testuser")
    # Test
    loop.run_until_complete(test_handle_checkLogin())

    # Remove dev from tmpuser
    bumper.user_remove_device("tmpuser", "dev_1234")

    # Add a token to user and test
    bumper.user_add("testuser")
    bumper.user_add_device("testuser", "dev_1234")
    bumper.user_add_token("testuser", "token_1234")
    # Test
    loop.run_until_complete(test_handle_checkLogin(token="token_1234"))

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #12
0
def clients_context_num(routine, num=2, patch_nyms=nyms):
    with patch('websockets_server.core.server.nyms', patch_nyms):
        with loop_context() as loopext:
            app = HXApplication()
            server = TestServer(app)
            clients = []
            for i in range(num):
                client = TestClient(server, loop=loopext)
                if i == 0:
                    loopext.run_until_complete(client.start_server())
                clients.append(client)

            try:
                loopext.run_until_complete(routine(app, *clients))
            finally:
                loopext.run_until_complete(clients[0]._server.close())
                for client in clients:
                    loopext.run_until_complete(client.close())
Пример #13
0
def test_getAuthCode():
    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_handle_getAuthCode(uid=None, token=None):
        resp = await client.get(
            "/1/private/us/en/dev_1234/ios/1/0/0/user/getAuthCode?uid={}&accessToken={}"
            .format(uid, token))
        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            if token:
                assert jsonresp["code"] == bumper.RETURN_API_SUCCESS
                assert "authCode" in jsonresp["data"]
                assert "ecovacsUid" in jsonresp["data"]
            else:
                assert jsonresp["code"] == bumper.ERR_TOKEN_INVALID
        else:
            assert jsonresp

    # Test without user or token
    loop.run_until_complete(test_handle_getAuthCode())

    # Add a token to user and test
    bumper.user_add("testuser")
    bumper.user_add_device("testuser", "dev_1234")
    bumper.user_add_token("testuser", "token_1234")
    # Test
    loop.run_until_complete(
        test_handle_getAuthCode(uid="testuser", token="token_1234"))

    # The above should have added an authcode to token, try again to test with existing authcode
    # Test
    loop.run_until_complete(
        test_handle_getAuthCode(uid="testuser", token="token_1234"))

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #14
0
def test_base():
    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_handle_base():
        resp = await client.get("/")
        assert resp.status == 200
        text = await resp.text()
        assert "Bumper!" in text

    # Test
    loop.run_until_complete(test_handle_base())

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #15
0
def test_login():
    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_handle_login():
        resp = await client.get(
            "/1/private/us/en/dev_1234/ios/1/0/0/user/login")
        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["code"] == bumper.RETURN_API_SUCCESS
            assert "accessToken" in jsonresp["data"]
            assert "uid" in jsonresp["data"]
            assert "username" in jsonresp["data"]
        else:
            assert jsonresp

    # Test
    loop.run_until_complete(test_handle_login())

    # Add a user to db and test with existing users
    bumper.user_add("testuser")
    # Test
    loop.run_until_complete(test_handle_login())

    # Add a bot to db that will be added to user
    bumper.bot_add("sn_123", "did_123", "dev_123", "res_123", "com_123")
    # Test
    loop.run_until_complete(test_handle_login())

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #16
0
def test_getUsersAPI():
    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_handle_getUsersApi():
        resp = await client.get("/api/users/user.do")
        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["result"] == "fail"
        else:
            assert jsonresp

    # Test
    loop.run_until_complete(test_handle_getUsersApi())

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #17
0
def test_getProductIotMap():
    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_handle_getProductIotMap():
        resp = await client.post("/api/pim/product/getProductIotMap")
        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["code"] == bumper.RETURN_API_SUCCESS
        else:
            assert jsonresp

    # Test
    loop.run_until_complete(test_handle_getProductIotMap())

    loop.run_until_complete(
        client.close())  # Close test server after all tests are done
Пример #18
0
def test_client(loop, app):
    client = TestClient(app)
    yield client
    client.close()
Пример #19
0
def test_client(loop, app):
    client = TestClient(app)
    yield client
    client.close()
Пример #20
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
Пример #21
0
        got_news = []
        got_comments = []
        for x in news['news']:
            got_news.append(x['id'])
            got_comments.append(x['comments_count'])
        assert len(got_news) <= len(checkup_list), 'Got more news than needed'
        assert len(got_news) >= len(checkup_list), 'Got not all the news'
        assert got_news == checkup_list, 'News are not in right order'
        assert got_comments == comments_counts, 'Not right comments'
        print('All the test are passed for "all_news"')

    async def test_news_by_id():
        resp = await client.get("/news/2")
        assert resp.status == 200
        news = await resp.json(content_type='text/plain')
        # id коментариев
        checkup_list = [2, 4]
        got_comments = [x['id'] for x in news['comments']]

        assert news['id'] == 2, 'Got not right news'
        assert len(news['comments']) <= len(
            checkup_list), 'Got more comments than needed'
        assert len(
            news['comments']) >= len(checkup_list), 'Got not all the comments'
        assert got_comments == checkup_list, 'Comments are not in right order'
        print('All the test are passed for "news_by_id"')

    loop.run_until_complete(test_all_news())
    loop.run_until_complete(test_news_by_id())
    loop.run_until_complete(client.close())
Пример #22
0
def test_postUsersAPI():
    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_handle_postUsersApi(postbody=None):
        resp = await client.post("/api/users/user.do", json=postbody)

        assert resp.status == 200
        text = await resp.text()
        jsonresp = json.loads(text)
        if jsonresp:
            assert jsonresp["result"] == "ok"
        else:
            assert jsonresp

    # Test FindBest
    postbody = {"todo": "FindBest", "service": "EcoMsgNew"}
    # Test
    loop.run_until_complete(test_handle_postUsersApi(postbody))

    # Test EcoUpdate
    postbody = {"todo": "FindBest", "service": "EcoUpdate"}
    # Test
    loop.run_until_complete(test_handle_postUsersApi(postbody))

    # Test loginByItToken - Uses the authcode
    bumper.user_add("testuser")
    bumper.user_add_device("testuser", "dev_1234")
    bumper.user_add_token("testuser", "token_1234")
    bumper.user_add_authcode("testuser", "token_1234", "auth_1234")
    bumper.user_add_bot("testuser", "did_1234")
    bumper.bot_add("sn_1234", "did_1234", "class_1234", "res_1234", "com_1234")
    # Test
    postbody = {
        "country": "US",
        "last": "",
        "realm": "ecouser.net",
        "resource": "dev_1234",
        "todo": "loginByItToken",
        "token": "auth_1234",
        "userId": "testuser",
    }
    loop.run_until_complete(test_handle_postUsersApi(postbody))

    # Test GetDeviceList
    postbody = {
        "auth": {
            "realm": "ecouser.net",
            "resource": "dev_1234",
            "token": "token_1234",
            "userid": "testuser",
            "with": "users",
        },
        "todo": "GetDeviceList",
        "userid": "testuser",
    }
    loop.run_until_complete(test_handle_postUsersApi(postbody))

    # Test SetDeviceNick
    postbody = {
        "auth": {
            "realm": "ecouser.net",
            "resource": "dev_1234",
            "token": "token_1234",
            "userid": "testuser",
            "with": "users",
        },
        "todo": "SetDeviceNick",
        "nick": "botnick",
        "did": "did_1234",
    }
    loop.run_until_complete(test_handle_postUsersApi(postbody))

    # Test AddOneDevice - Same as set nick for some bots
    postbody = {
        "auth": {
            "realm": "ecouser.net",
            "resource": "dev_1234",
            "token": "token_1234",
            "userid": "testuser",
            "with": "users",
        },
        "todo": "AddOneDevice",
        "nick": "botnick",
        "did": "did_1234",
    }
    loop.run_until_complete(test_handle_postUsersApi(postbody))

    # Test DeleteOneDevice - remove bot
    postbody = {
        "auth": {
            "realm": "ecouser.net",
            "resource": "dev_1234",
            "token": "token_1234",
            "userid": "testuser",
            "with": "users",
        },
        "todo": "DeleteOneDevice",
        "did": "did_1234",
    }
    loop.run_until_complete(test_handle_postUsersApi(postbody))

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