async def test_signal_keywords(): cb1 = CoroutineMock() cb2 = CoroutineMock() ts = Signal() ts.connect(cb1) ts.connect(cb2) await ts.emit(test_msg="test msg") cb1.assert_called_with(test_msg="test msg") cb2.assert_called_with(test_msg="test msg")
async def test_signal_cb_exception(): cb_ok = CoroutineMock() async def cb_exception(*args, **kwargs): raise Exception ts = Signal() ts.connect(cb_ok) ts.connect(cb_exception) await ts.emit("test msg") cb_ok.assert_called_with("test msg")
async def test_test_twilio_cloud_hook(self, run_query, tenant_id, flow_id, flow_run_id, monkeypatch, state_type): post_mock = CoroutineMock() client = MagicMock(post=post_mock) monkeypatch.setattr( "prefect_server.api.cloud_hooks.cloud_hook_httpx_client.post", post_mock) hook_id = await api.cloud_hooks.create_cloud_hook( tenant_id=tenant_id, type="TWILIO", states=[state_type], config=dict( account_sid="test_sid", auth_token="test_auth_token", messaging_service_sid="test_messaging_service_sid", to=["+15555555555"], ), ) result = await run_query( query=self.mutation, variables=dict(input=dict( cloud_hook_id=hook_id, flow_run_id=flow_run_id, state_type=state_type, )), ) # sleep to give the async webhook a chance to fire await asyncio.sleep(1) tenant = await models.Tenant.where(id=tenant_id).first({"slug"}) flow = await models.Flow.where(id=flow_id).first({"name"}) flow_run = await models.FlowRun.where(id=flow_run_id ).first({"id", "name"}) post_mock.assert_called_with( "https://api.twilio.com/2010-04-01/Accounts/test_sid/Messages.json", auth=("test_sid", "test_auth_token"), data={ "To": "+15555555555", "From": "test_messaging_service_sid", "Body": f"Run {flow_run.name} of flow {flow.name} entered a new state: {state_type.title()}. \n Link: {config.api.url}/{tenant.slug}/flow-run/{flow_run.id}", }, timeout=2, )
async def test_can_handle_message(self): frame = Frame("MESSAGE", { "subscription": "123", "message-id": "321" }, "blah") handler = CoroutineMock() subscription = Subscription("123", 1, "auto", {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body)
async def test_can_handle_message(self): frame = Frame('MESSAGE', { 'subscription': '123', 'message-id': '321' }, 'blah') handler = CoroutineMock() subscription = Subscription('123', 1, 'auto', {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body)
async def test_test_twilio(self, tenant_id, flow_id, flow_run_id, state, monkeypatch): post_mock = CoroutineMock() client = MagicMock(post=post_mock) monkeypatch.setattr( "prefect_server.api.cloud_hooks.cloud_hook_httpx_client.post", post_mock) cloud_hook_id = await api.cloud_hooks.create_cloud_hook( tenant_id=tenant_id, type="TWILIO", config=dict( account_sid="test_sid", auth_token="test_auth_token", messaging_service_sid="test_messaging_service_sid", to=["+15555555555"], ), states=["RUNNING", "SUCCESS"], ) tenant = await models.Tenant.where(id=tenant_id).first({"slug"}) flow = await models.Flow.where(id=flow_id).first({"name"}) flow_run = await models.FlowRun.where(id=flow_run_id ).first({"id", "name"}) await api.cloud_hooks.test_cloud_hook(cloud_hook_id=cloud_hook_id, flow_run_id=flow_run_id, state=state) post_mock.assert_called_with( "https://api.twilio.com/2010-04-01/Accounts/test_sid/Messages.json", auth=("test_sid", "test_auth_token"), data={ "To": "+15555555555", "From": "test_messaging_service_sid", "Body": f"Run {flow_run.name} of flow {flow.name} entered a new state: {type(state).__name__}. \n Link: {config.api.url}/{tenant.slug}/flow-run/{flow_run.id}", }, timeout=2, )
async def test_can_handle_message_can_nack(self, send_frame_mock): frame = Frame("MESSAGE", { "subscription": "123", "message-id": "321" }, "blah") handler = CoroutineMock() handler.return_value = False subscription = Subscription("123", 1, "client-individual", {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body) send_frame_mock.assert_called_with("NACK", { "subscription": "123", "message-id": "321" })
async def test_can_handle_message_can_nack(self, send_frame_mock): frame = Frame('MESSAGE', { 'subscription': '123', 'message-id': '321' }, 'blah') handler = CoroutineMock() handler.return_value = False subscription = Subscription('123', 1, 'client-individual', {}, handler) frame_handler = Mock() frame_handler.get.return_value = subscription stomp = StompReader(frame_handler, self.loop) await stomp._handle_message(frame) handler.assert_called_with(frame, frame.body) send_frame_mock.assert_called_with('NACK', { 'subscription': '123', 'message-id': '321' })
class TestTokenManager(AsyncTestCase): async def setUpAsync(self): self.end_point = 'http://endpoint/token' self.client_id = 'client_id' self.client_secret = 'client_secret' self.manager = TokenManager(self.end_point, self.client_id, self.client_secret) self._fake_fetch = CoroutineMock() self.manager._fetch = self._fake_fetch def test_should_start_with_no_token(self): self.assertFalse(self.manager._has_token()) def test_should_detect_expired_token(self): self.manager._token = Token('', expires_in=0) self.assertFalse(self.manager._has_token()) def test_should_respect_valid_token(self): self.manager._token = Token('', expires_in=10) self.assertTrue(self.manager._has_token()) @unittest_run_loop async def test_should_reset_token(self): self._fake_fetch.return_value = { 'access_token': 'accesstoken', 'expires_in': 10, } await self.manager.reset_token() self.assertEqual(self.manager._token.access_token, 'accesstoken') self.assertEqual(self.manager._token._expires_in, 10) @unittest_run_loop async def test_should_be_able_to_request_a_new_token(self): self._fake_fetch.return_value = { 'access_token': 'accesstoken', 'expires_in': 10, } await self.manager._request_token() self._fake_fetch.assert_called_with( url=self.end_point, method="POST", auth=(self.client_id, self.client_secret), data={'grant_type': 'client_credentials'}, ) @unittest_run_loop async def test_should_raise_token_error_for_bad_token(self): self._fake_fetch.side_effect = TokenHTTPError('error', 500, 'fail') with self.assertRaises(TokenError) as context: await self.manager._request_token() self.assertEqual(context.exception.response_status, 500) @unittest_run_loop async def test_get_token_data_should_obtain_new_token(self): _request_token = CoroutineMock() _request_token.return_value = { 'access_token': 'new_access_token', 'expires_in': 10, } self.manager._request_token = _request_token await self.manager._get_token_data() self.assertTrue(_request_token.called) @unittest_run_loop async def test_update_token_should_set_a_token_with_data_retrieved(self): _request_token = CoroutineMock() _request_token.return_value = { 'access_token': 'new_access_token', 'expires_in': 10, } self.manager._request_token = _request_token self.manager._token = Token('access_token', expires_in=100) await self.manager._update_token() self.assertTrue(_request_token.called) self.assertEqual(self.manager._token.access_token, 'new_access_token') self.assertEqual(self.manager._token._expires_in, 10) @unittest_run_loop async def test_should_return_token_value(self): self.manager._token = Token('access_token', expires_in=10) token = await self.manager.get_token() self.assertEqual(token, 'access_token') @unittest_run_loop @patch('aioalf.manager.TokenManager._update_token') @patch('aioalf.manager.TokenManager._has_token') async def test_get_token_should_request_a_new_token_if_do_not_have_a_token( self, _has_token, _update_token): _has_token.return_value = False def set_token(): self.manager._token = Token('access_token', expires_in=100) _update_token.side_effect = set_token token = await self.manager.get_token() self.assertEqual(token, 'access_token') self.assertTrue(_update_token.called)