[misc] Use black to reformat code

This commit is contained in:
Martin Rey 2020-10-12 22:47:06 +02:00 committed by Maike
parent 4146cb89b5
commit 2c058d7e15
16 changed files with 127 additions and 57 deletions

View file

@ -142,16 +142,19 @@ class Censor:
async def __is_appropriate(self, message):
for badword in await BadWord.objects.filter(hood=self.hood).all():
if search(badword.pattern, message.text, IGNORECASE):
logger.debug('Matched bad word - dropped message: {0}'.format(
message.text))
logger.debug(
'Matched bad word - dropped message: {0}'.format(message.text)
)
return False
for trigger in await Trigger.objects.filter(hood=self.hood).all():
if search(trigger.pattern, message.text, IGNORECASE):
logger.debug('Matched trigger - passed message: {0}'.format(
message.text))
logger.debug(
'Matched trigger - passed message: {0}'.format(message.text)
)
return True
logger.debug('Did not match any trigger - dropped message: {0}'.format(
message.text))
logger.debug(
'Did not match any trigger - dropped message: {0}'.format(message.text)
)
return False

View file

@ -36,7 +36,9 @@ class EmailBot(Censor):
while True:
message = await self.receive()
logger.debug(
'Received message from censor ({0}): {1}'.format(self.hood.name, message.text)
'Received message from censor ({0}): {1}'.format(
self.hood.name, message.text
)
)
for subscriber in await EmailSubscribers.objects.filter(
hood=self.hood

View file

@ -66,7 +66,9 @@ class Main:
)
response = post(
'{0}/api/hoods/{1}/email/messages/'.format(config['root_url'], email.hood.pk),
'{0}/api/hoods/{1}/email/messages/'.format(
config['root_url'], email.hood.pk
),
json={'text': text, 'secret': email.secret},
)
if response.status_code == status.HTTP_201_CREATED:

View file

@ -48,13 +48,15 @@ class TelegramBot(Censor):
await self.telegram_model.update(username=user.username)
await gather(self.dp.start_polling(), self._push())
except CancelledError:
logger.debug('Bot {0} received Cancellation.'.format(
self.telegram_model.hood.name))
logger.debug(
'Bot {0} received Cancellation.'.format(self.telegram_model.hood.name)
)
self.dp = None
raise
except exceptions.ValidationError:
logger.debug('Bot {0} has invalid auth token.'.format(
self.telegram_model.hood.name))
logger.debug(
'Bot {0} has invalid auth token.'.format(self.telegram_model.hood.name)
)
await self.telegram_model.update(enabled=False)
finally:
logger.debug('Bot {0} stopped.'.format(self.telegram_model.hood.name))
@ -64,7 +66,8 @@ class TelegramBot(Censor):
message = await self.receive()
logger.debug(
'Received message from censor ({0}): {1}'.format(
self.telegram_model.hood.name, message.text)
self.telegram_model.hood.name, message.text
)
)
for user in await TelegramUser.objects.filter(
bot=self.telegram_model
@ -76,13 +79,15 @@ class TelegramBot(Censor):
await self.bot.send_message(user_id, message, disable_notification=False)
except exceptions.BotBlocked:
logger.error(
'Target [ID:{0}] ({1}): blocked by user'.format(user_id,
self.telegram_model.hood.name)
'Target [ID:{0}] ({1}): blocked by user'.format(
user_id, self.telegram_model.hood.name
)
)
except exceptions.ChatNotFound:
logger.error(
'Target [ID:{0}] ({1}): invalid user ID'.format(user_id,
self.telegram_model.hood.name)
'Target [ID:{0}] ({1}): invalid user ID'.format(
user_id, self.telegram_model.hood.name
)
)
except exceptions.RetryAfter as e:
logger.error(
@ -95,12 +100,15 @@ class TelegramBot(Censor):
return await self._send_message(user_id, message)
except exceptions.UserDeactivated:
logger.error(
'Target [ID:{0}] ({1}): user is deactivated'.format(user_id,
self.telegram_model.hood.name)
'Target [ID:{0}] ({1}): user is deactivated'.format(
user_id, self.telegram_model.hood.name
)
)
except exceptions.TelegramAPIError:
logger.exception(
'Target [ID:{0}] ({1}): failed'.format(user_id, self.telegram_model.hood.name)
'Target [ID:{0}] ({1}): failed'.format(
user_id, self.telegram_model.hood.name
)
)
async def _send_welcome(self, message: types.Message):

View file

@ -49,14 +49,18 @@ class TwitterBot(Censor):
user = await self.client.user
if user.screen_name:
await self.twitter_model.update(username=user.screen_name)
logger.debug('Starting Twitter bot: {0}'.format(self.twitter_model.__dict__))
logger.debug(
'Starting Twitter bot: {0}'.format(self.twitter_model.__dict__)
)
await gather(self.poll(), self.push())
except CancelledError:
logger.debug('Bot {0} received Cancellation.'.format(
self.twitter_model.hood.name))
logger.debug(
'Bot {0} received Cancellation.'.format(self.twitter_model.hood.name)
)
except exceptions.Unauthorized:
logger.debug('Bot {0} has invalid auth token.'.format(
self.twitter_model.hood.name))
logger.debug(
'Bot {0} has invalid auth token.'.format(self.twitter_model.hood.name)
)
await self.twitter_model.update(enabled=False)
self.enabled = self.twitter_model.enabled
except (KeyError, ValueError, exceptions.NotAuthenticated):
@ -75,7 +79,8 @@ class TwitterBot(Censor):
mentions = await self._poll_mentions()
logger.debug(
'Polled mentions ({0}): {1}'.format(
self.twitter_model.hood.name, str(mentions))
self.twitter_model.hood.name, str(mentions)
)
)
await self.twitter_model.update(
dms_since_id=self.dms_since_id, mentions_since_id=self.mentions_since_id
@ -141,7 +146,8 @@ class TwitterBot(Censor):
message = await self.receive()
logger.debug(
'Received message from censor ({0}): {1}'.format(
self.twitter_model.hood.name, message.text)
self.twitter_model.hood.name, message.text
)
)
if hasattr(message, 'twitter_mention_id'):
await self._retweet(message.twitter_mention_id)

View file

@ -103,7 +103,9 @@ def trigger_id(client, hood_id, auth_header):
@fixture(scope='function')
def badword_id(client, hood_id, auth_header):
response = client.post(
'/api/hoods/{0}/badwords/'.format(hood_id), json={'pattern': ''}, headers=auth_header
'/api/hoods/{0}/badwords/'.format(hood_id),
json={'pattern': ''},
headers=auth_header,
)
assert response.status_code == status.HTTP_201_CREATED
badword_id = int(response.headers['Location'])
@ -121,4 +123,6 @@ def test_id(client, hood_id, auth_header):
assert response.status_code == status.HTTP_201_CREATED
test_id = int(response.headers['Location'])
yield test_id
client.delete('/api/hoods/{0}/test/{1}'.format(hood_id, test_id), headers=auth_header)
client.delete(
'/api/hoods/{0}/test/{1}'.format(hood_id, test_id), headers=auth_header
)

View file

@ -19,4 +19,6 @@ def email_row(client, hood_id, auth_header):
assert response.status_code == status.HTTP_201_CREATED
email_id = int(response.headers['Location'])
yield response.json()
client.delete('/api/hoods/{0}/email/{1}'.format(hood_id, email_id), headers=auth_header)
client.delete(
'/api/hoods/{0}/email/{1}'.format(hood_id, email_id), headers=auth_header
)

View file

@ -15,7 +15,8 @@ from kibicara.webapi.admin import to_token
def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
response = client.post(
'/api/hoods/{0}/email/subscribe/'.format(hood_id), json={'email': 'test@localhost'}
'/api/hoods/{0}/email/subscribe/'.format(hood_id),
json={'email': 'test@localhost'},
)
assert response.status_code == status.HTTP_202_ACCEPTED
mail = receive_email()
@ -27,17 +28,21 @@ def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
)[0]
start = len('token=')
response = client.post(
'/api/hoods/{0}/email/subscribe/confirm/{1}'
.format(hood_id, urlparse(confirm_url).query[start:])
'/api/hoods/{0}/email/subscribe/confirm/{1}'.format(
hood_id, urlparse(confirm_url).query[start:]
)
)
assert response.status_code == status.HTTP_201_CREATED
response = client.post(
'/api/hoods/{0}/email/subscribe/confirm/{1}'
.format(hood_id, urlparse(confirm_url).query[start:])
'/api/hoods/{0}/email/subscribe/confirm/{1}'.format(
hood_id, urlparse(confirm_url).query[start:]
)
)
assert response.status_code == status.HTTP_409_CONFLICT
token = to_token(email=mail['to'], hood=hood_id)
response = client.delete('/api/hoods/{0}/email/unsubscribe/{1}'.format(hood_id, token))
response = client.delete(
'/api/hoods/{0}/email/unsubscribe/{1}'.format(hood_id, token)
)
assert response.status_code == status.HTTP_204_NO_CONTENT

View file

@ -13,7 +13,9 @@ def test_email_create_unauthorized(client, hood_id):
def test_email_delete_unauthorized(client, hood_id, email_row):
response = client.delete('/api/hoods/{0}/email/{1}'.format(hood_id, email_row['id']))
response = client.delete(
'/api/hoods/{0}/email/{1}'.format(hood_id, email_row['id'])
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -43,7 +43,9 @@ def test_email_message_wrong(client, hood_id, email_row):
def test_email_unsubscribe_wrong_token(client, hood_id):
try:
client.delete(
'/api/hoods/{0}/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf'.format(hood_id)
'/api/hoods/{0}/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf'.format(
hood_id
)
)
except CryptoError:
pass

View file

@ -19,7 +19,8 @@ def test_telegram_delete_bot(client, event_loop, bot, telegram, auth_header):
TelegramUser.objects.create(user_id=5678, bot=telegram.id)
)
response = client.delete(
'/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id), headers=auth_header
'/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id),
headers=auth_header,
)
assert response.status_code == status.HTTP_204_NO_CONTENT
with raises(NoMatch):
@ -33,7 +34,9 @@ def test_telegram_delete_bot_invalid_id(client, auth_header, hood_id):
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete('/api/hoods/wrong/telegram/123', headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
response = client.delete('/api/hoods/{0}/telegram/7331'.format(hood_id), headers=auth_header)
response = client.delete(
'/api/hoods/{0}/telegram/7331'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete(
'/api/hoods/{0}/telegram/wrong'.format(hood_id), headers=auth_header
@ -43,5 +46,7 @@ def test_telegram_delete_bot_invalid_id(client, auth_header, hood_id):
@mark.parametrize('bot', [{'api_token': 'apitoken123', 'welcome_message': 'msg'}])
def test_telegram_delete_bot_unauthorized(client, bot, telegram):
response = client.delete('/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id))
response = client.delete(
'/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -10,7 +10,8 @@ from pytest import mark
@mark.parametrize('bot', [{'api_token': 'apitoken123', 'welcome_message': 'msg'}])
def test_telegram_get_bot(client, auth_header, event_loop, bot, telegram):
response = client.get(
'/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id), headers=auth_header
'/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id),
headers=auth_header,
)
assert response.status_code == status.HTTP_200_OK
assert response.json()['id'] == telegram.id
@ -23,13 +24,19 @@ def test_telegram_get_bot_invalid_id(client, auth_header, hood_id):
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get('/api/hoods/wrong/telegram/123', headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
response = client.get('/api/hoods/{0}/telegram/7331'.format(hood_id), headers=auth_header)
response = client.get(
'/api/hoods/{0}/telegram/7331'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get('/api/hoods/{0}/telegram/wrong'.format(hood_id), headers=auth_header)
response = client.get(
'/api/hoods/{0}/telegram/wrong'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
@mark.parametrize('bot', [{'api_token': 'apitoken456', 'welcome_message': 'msg'}])
def test_telegram_get_bot_unauthorized(client, bot, telegram):
response = client.get('/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id))
response = client.get(
'/api/hoods/{0}/telegram/{1}'.format(telegram.hood.id, telegram.id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -77,7 +77,9 @@ def test_twitter_create_bot(
)
# Twitter create endpoint
response = client.post('/api/hoods/{0}/twitter/'.format(hood_id), headers=auth_header)
response = client.post(
'/api/hoods/{0}/twitter/'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_201_CREATED
bot_id = response.json()['id']
twitter = event_loop.run_until_complete(Twitter.objects.get(id=bot_id))
@ -139,7 +141,9 @@ def test_twitter_create_unauthorized(client, hood_id):
def test_twitter_create_wrong_consumer_keys(client, monkeypatch, auth_header, hood_id):
# No consumer keys
response = client.post('/api/hoods/{0}/twitter/'.format(hood_id), headers=auth_header)
response = client.post(
'/api/hoods/{0}/twitter/'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
# Invalid consumer keys
@ -149,5 +153,7 @@ def test_twitter_create_wrong_consumer_keys(client, monkeypatch, auth_header, ho
{'consumer_key': 'consumer_key123', 'consumer_secret': 'consumer_secret123'},
)
response = client.post('/api/hoods/{0}/twitter/'.format(hood_id), headers=auth_header)
response = client.post(
'/api/hoods/{0}/twitter/'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR

View file

@ -12,7 +12,8 @@ from kibicara.platforms.twitter.model import Twitter
def test_twitter_delete_bot(client, event_loop, twitter, auth_header):
response = client.delete(
'/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id), headers=auth_header
'/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id),
headers=auth_header,
)
assert response.status_code == status.HTTP_204_NO_CONTENT
with raises(NoMatch):
@ -24,12 +25,18 @@ def test_twitter_delete_bot_invalid_id(client, auth_header, hood_id):
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete('/api/hoods/wrong/twitter/123', headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
response = client.delete('/api/hoods/{0}/twitter/7331'.format(hood_id), headers=auth_header)
response = client.delete(
'/api/hoods/{0}/twitter/7331'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete('/api/hoods/{0}/twitter/wrong'.format(hood_id), headers=auth_header)
response = client.delete(
'/api/hoods/{0}/twitter/wrong'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_twitter_delete_bot_unauthorized(client, twitter):
response = client.delete('/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id))
response = client.delete(
'/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -8,7 +8,8 @@ from fastapi import status
def test_twitter_get_bot(client, auth_header, event_loop, twitter):
response = client.get(
'/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id), headers=auth_header
'/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id),
headers=auth_header,
)
assert response.status_code == status.HTTP_200_OK
assert response.json()['id'] == twitter.id
@ -21,12 +22,18 @@ def test_twitter_get_bot_invalid_id(client, auth_header, hood_id):
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get('/api/hoods/wrong/twitter/123', headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
response = client.get('/api/hoods/{0}/twitter/7331'.format(hood_id), headers=auth_header)
response = client.get(
'/api/hoods/{0}/twitter/7331'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get('/api/hoods/{0}/twitter/wrong'.format(hood_id), headers=auth_header)
response = client.get(
'/api/hoods/{0}/twitter/wrong'.format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_twitter_get_bot_unauthorized(client, twitter):
response = client.get('/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id))
response = client.get(
'/api/hoods/{0}/twitter/{1}'.format(twitter.hood.id, twitter.id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -25,7 +25,9 @@ def test_twitter_get_bots(client, auth_header, event_loop, hood_id):
access_token_secret='access_token_secret456',
)
)
response = client.get('/api/hoods/{0}/twitter'.format(twitter0.hood.id), headers=auth_header)
response = client.get(
'/api/hoods/{0}/twitter'.format(twitter0.hood.id), headers=auth_header
)
assert response.status_code == status.HTTP_200_OK
assert response.json()[0]['id'] == twitter0.id
assert response.json()[0]['access_token'] == twitter0.access_token