async def test_get_comment(opsdroid): """Test a comment create event creates a message and parses it.""" opsdroid = amock.CoroutineMock() connector = ConnectorGitHub({ "name": "github", "token": "test" }, opsdroid=opsdroid) with open( os.path.join(os.path.dirname(__file__), "responses", "github_comment_payload.json"), "r", ) as f: mock_request = amock.CoroutineMock() mock_request.post = amock.CoroutineMock( return_value={"payload": f.read()}) connector.opsdroid = amock.CoroutineMock() connector.opsdroid.parse = amock.CoroutineMock() await connector.github_message_handler(mock_request) message = connector.opsdroid.parse.call_args[0][0] assert message.connector.name == "github" assert message.text == "hello" assert message.target == "opsdroid/opsdroid#237" assert connector.opsdroid.parse.called
def setUp(self): opsdroid = amock.CoroutineMock() configure_lang({}) self.connector = ConnectorGitHub({ "name": "github", "token": "test" }, opsdroid=opsdroid)
def test_init(self): """Test that the connector is initialised properly.""" connector = ConnectorGitHub({ 'name': 'github', 'token': 'test' }) self.assertEqual(None, connector.default_room) self.assertEqual("github", connector.name)
async def test_get_no_action(opsdroid): """Test a status event doesn't create a message and parse it.""" opsdroid = amock.CoroutineMock() connector = ConnectorGitHub({"name": "github", "token": "test"}, opsdroid=opsdroid) with open( os.path.join( os.path.dirname(__file__), "responses", "github_status_payload.json" ), "r", ) as f: mock_request = amock.CoroutineMock() mock_request.post = amock.CoroutineMock(return_value={"payload": f.read()}) connector.opsdroid = amock.CoroutineMock() connector.opsdroid.parse = amock.CoroutineMock() await connector.github_message_handler(mock_request) assert not connector.opsdroid.parse.called
def setUp(self): opsdroid = amock.CoroutineMock() configure_lang({}) self.connector = ConnectorGitHub({ 'name': 'github', 'token': 'test' }, opsdroid=opsdroid)
def test_init(opsdroid): """Test that the connector is initialised properly.""" connector = ConnectorGitHub({ "name": "github", "token": "test" }, opsdroid=opsdroid) assert connector.default_target is None assert connector.name == "github"
async def test_disconnect(opsdroid): opsdroid = amock.CoroutineMock() connector = ConnectorGitHub({ "name": "github", "token": "test" }, opsdroid=opsdroid) assert await connector.disconnect() is None
async def test_listen(opsdroid): """Test the listen method. The GitHub connector listens using an API endoint and so the listen method should just pass and do nothing. We just need to test that it does not block. """ opsdroid = amock.CoroutineMock() connector = ConnectorGitHub({"name": "github", "token": "test"}, opsdroid=opsdroid) assert await connector.listen() is None
def test_app_init(): """Test that the connector is initialised properly when using a Github app.""" connector = ConnectorGitHub( { "name": "github", "private_key_file": "./test-private-key.pem", "app_id": 1234567, } ) assert connector.default_target is None assert connector.name == "github"
async def test_respond_bot_short(opsdroid): opsdroid = amock.CoroutineMock() connector = ConnectorGitHub({"name": "github", "token": "test"}, opsdroid=opsdroid) with amock.patch("aiohttp.ClientSession.post") as patched_request: mockresponse = amock.CoroutineMock() mockresponse.status = 201 patched_request.return_value = asyncio.Future() patched_request.return_value.set_result(mockresponse) connector.github_username = "******" resp = await connector.send( Message( text="test", user="******", target="opsdroid/opsdroid#1", connector=connector, ) ) assert not patched_request.called assert resp
async def test_connect(opsdroid): opsdroid = amock.CoroutineMock() connector = ConnectorGitHub({"name": "github", "token": "test"}, opsdroid=opsdroid) with amock.patch("aiohttp.ClientSession.get") as patched_request: mockresponse = amock.CoroutineMock() mockresponse.status = 200 mockresponse.json = amock.CoroutineMock(return_value={"login": "******"}) patched_request.return_value = asyncio.Future() patched_request.return_value.set_result(mockresponse) await connector.connect() assert connector.github_username == "opsdroid" assert connector.opsdroid.web_server.web_app.router.add_post.called
async def test_validate_request(opsdroid): connector_config = {"secret": "client-secret", "token": "test"} connector = ConnectorGitHub(connector_config, opsdroid=opsdroid) request = amock.CoroutineMock() request.headers = { "X-Hub-Signature-256": "sha256=fcfa24b327e3467f1586cc1ace043c016cabfe9c15dabc0020aca45440338be9" } request.read = amock.CoroutineMock() request.read.return_value = b'{"test": "test"}' validation = await connector.validate_request(request, "test") assert validation
async def test_connect_failure(opsdroid, caplog): opsdroid = amock.CoroutineMock() connector = ConnectorGitHub({"name": "github", "token": "test"}, opsdroid=opsdroid) with amock.patch("aiohttp.ClientSession.get") as patched_request: mockresponse = amock.CoroutineMock() mockresponse.status = 401 mockresponse.text = amock.CoroutineMock( return_value='{"message": "Bad credentials", "documentation_url": "https://developer.github.com/v3"}' ) patched_request.return_value = asyncio.Future() patched_request.return_value.set_result(mockresponse) await connector.connect() assert "Error connecting to GitHub" in caplog.text
def test_token_init(): """Test that the connector is initialised properly when using a personal api token.""" connector = ConnectorGitHub({"name": "github", "token": "test"}) assert connector.default_target is None assert connector.name == "github"
def test_missing_token(opsdroid, caplog): """Test that attempt to connect without info raises an error.""" ConnectorGitHub({}) assert "Missing auth token!" in caplog.text
def test_missing_secret(caplog): """Test that missing secret will log a message.""" ConnectorGitHub({}) assert "You should use it to improve security" in caplog.text
def test_missing_token_and_app_settings(caplog): """Test that attempt to connect without info raises an error.""" ConnectorGitHub({}) assert "Missing auth token or app settings!" in caplog.text
def setUp(self): configure_lang({}) self.connector = ConnectorGitHub({ 'name': 'github', 'token': 'test' })
def test_missing_token(self): """Test that attempt to connect without info raises an error.""" ConnectorGitHub({}) self.assertLogs("_LOGGER", "error")
def test_init(self): """Test that the connector is initialised properly.""" connector = ConnectorGitHub({"name": "github", "token": "test"}) self.assertEqual(None, connector.default_target) self.assertEqual("github", connector.name)