Beispiel #1
0
async def test_smartapp_webhook(hass):
    """Test the smartapp webhook calls the manager."""
    manager = Mock()
    manager.handle_request = CoroutineMock(return_value={})
    hass.data[DOMAIN][DATA_MANAGER] = manager
    request = Mock()
    request.headers = []
    request.json = CoroutineMock(return_value={})
    result = await smartapp.smartapp_webhook(hass, "", request)

    assert result.body == b"{}"
Beispiel #2
0
async def test_handle_sms():
    handler = RequestHandler()
    request = Mock()
    request.query = {'to': '+15055551212', 'token': '2222'}
    request.headers = {'X-API-Key': 'foo'}
    mock_message = Mock()
    mock_message.create = CoroutineMock()
    with patch('telnyx_2fa.app.Message', mock_message):
        r = await handler.handle_sms(request)
    response = json.loads(r.text)
    assert response['status'] == 'ok'
Beispiel #3
0
    async def test_004_patch_rest_configuration(self, bus_stop_mock):
        req = Mock()

        async def json():
            return {'bus': {'dsn': 'mqtt://new@localhost'}, 'new': True}

        req.headers = {'Content-Type': 'application/json'}
        req.json = json
        await self.apiconf.patch(req)
        eq_(self.nyuki._config['new'], True)
        eq_(self.nyuki._config['bus']['dsn'], 'mqtt://new@localhost')
        # finish coroutines
        await exhaust_callbacks(self.loop)
        bus_stop_mock.assert_called_once_with()
Beispiel #4
0
 async def test_004_patch_rest_configuration(self, bus_stop_mock):
     req = Mock()
     async def json():
         return {
             'bus': {'jid': 'updated@localhost'},
             'new': True
         }
     req.headers = {'Content-Type': 'application/json'}
     req.json = json
     await self.apiconf.patch(req)
     eq_(self.nyuki._config['new'], True)
     eq_(self.nyuki._config['bus']['jid'], 'updated@localhost')
     # finish coroutines
     await exhaust_callbacks(self.loop)
     bus_stop_mock.assert_called_once_with()
Beispiel #5
0
def mock_request_response(
    controller,
    json={},
    status=200,
    content_type="application/json",
    headers=None,
):
    mock_response = Mock()
    mock_response.json.return_value = asyncio.Future()
    mock_response.json.return_value.set_result(json)
    mock_response.status = status
    mock_response.content_type = content_type
    mock_response.headers = headers

    controller.session.request.return_value.__aenter__.side_effect = Mock(
        return_value=mock_response)
Beispiel #6
0
def test_aget_dict(mocker):
    from jenkins_epo.github import CustomGitHub

    aiohttp = mocker.patch('jenkins_epo.github.aiohttp')
    session = aiohttp.ClientSession.return_value
    response = Mock(spec=['headers', 'json', 'status'])
    session.get = CoroutineMock(return_value=response)
    response.status = 200
    response.content_type = 'application/json'
    response.headers = {'ETag': 'cafed0d0'}
    response.json = CoroutineMock(return_value={'data': 1})
    GITHUB = CustomGitHub(access_token='cafed0d0')
    res = yield from GITHUB.user.aget(per_page='100')

    assert res._headers
    assert 'data' in res
Beispiel #7
0
def test_apost(mocker):
    from jenkins_epo.github import CustomGitHub, ApiError

    aiohttp = mocker.patch('jenkins_epo.github.aiohttp')
    session = aiohttp.ClientSession.return_value
    response = Mock(spec=['headers', 'json', 'status'])
    session.post = CoroutineMock(return_value=response)
    response.status = 304
    response.content_type = 'application/json'
    response.headers = {'ETag': 'cafed0d0'}
    response.json = CoroutineMock(return_value={'message': 'Not found'})

    GITHUB = CustomGitHub(access_token='cafed0d0')

    with pytest.raises(ApiError):
        yield from GITHUB.user.apost(pouet=True)
Beispiel #8
0
async def test_handle_2fa():
    handler = RequestHandler()
    handler.is_mobile = CoroutineMock(return_value=True)
    request = Mock()
    request.query = {'to': '+15055551212'}
    request.headers = {'X-API-Key': 'foo'}
    r = await handler.handle_2fa(request)
    response = json.loads(r.text)
    sms = response['sms']
    assert len(sms['token']) == MockSettings.SMS_TOKEN_DIGITS
    assert sms[
        'url'] == f'foo/2fa/sms?to=%2B15055551212&token={sms["token"]}&language=en-US'

    voice = response['voice']
    assert len(voice['token']) == MockSettings.VOICE_TOKEN_DIGITS
    assert voice[
        'url'] == f'foo/2fa/voice?to=%2B15055551212&token={voice["token"]}&language=en-US'
Beispiel #9
0
def test_aget_html(mocker):
    from jenkins_epo.github import CustomGitHub

    aiohttp = mocker.patch('jenkins_epo.github.aiohttp')
    session = aiohttp.ClientSession.return_value
    response = Mock(spec=['headers', 'read', 'status'])
    session.get = CoroutineMock(return_value=response)
    response.status = 200
    response.content_type = 'text/html'
    response.headers = {'ETag': 'cafed0d0'}
    response.read = CoroutineMock(return_value='<!DOCTYPE')

    GITHUB = CustomGitHub(access_token='cafed0d0')
    with pytest.raises(Exception):
        yield from GITHUB.user.aget()

    assert response.read.mock_calls
Beispiel #10
0
def test_aget_422(mocker):
    from jenkins_epo.github import CustomGitHub, ApiError

    aiohttp = mocker.patch('jenkins_epo.github.aiohttp')
    session = aiohttp.ClientSession.return_value
    response = Mock(spec=['headers', 'json', 'status'])
    session.get = CoroutineMock(return_value=response)
    response.status = 422
    response.headers = {}
    response.content_type = 'application/json'
    response.json = CoroutineMock(
        return_value={'errors': [{'message': 'Pouet'}]},
    )

    GITHUB = CustomGitHub(access_token='cafed0d0')

    with pytest.raises(ApiError):
        yield from GITHUB.user.aget()
Beispiel #11
0
async def test_handle_voice():
    handler = RequestHandler()
    request = Mock()
    mock_telnyx = Mock()
    mock_telnyx.voice_2fa = CoroutineMock(return_value=True)
    request.app = {'telnyx': mock_telnyx}
    request.query = {'to': '+15055551212', 'token': '22'}
    request.headers = {'X-API-Key': 'foo'}

    mock_web = Mock()
    mock_web.HTTPUnauthorized = aiohttp.web_exceptions.HTTPUnauthorized
    mock_web.HTTPBadRequest = aiohttp.web_exceptions.HTTPBadRequest
    mock_web.HTTPServerError = aiohttp.web_exceptions.HTTPServerError
    mock_response = Mock()
    mock_response.prepare = CoroutineMock()
    mock_response.write = CoroutineMock()
    mock_response.write_eof = CoroutineMock()
    mock_web.StreamResponse.return_value = mock_response
    with patch('telnyx_2fa.app.web', mock_web):
        r = await handler.handle_voice(request)
    writes = mock_response.write.call_args_list
    assert json.loads(writes[0][0][0])['status'] == 'waiting'
    assert json.loads(writes[1][0][0])['status'] == 'success'