From 2af4b27e0b0765b5853b4ed2edf294f8581e33d5 Mon Sep 17 00:00:00 2001 From: Cathy Hu Date: Sun, 19 Jul 2020 14:02:43 +0200 Subject: [PATCH] [tests] Restructure testing files --- tests/conftest.py | 13 ++++++ tests/{ => tests_core}/test_api_admin.py | 0 tests/{ => tests_core}/test_api_hoods.py | 0 tests/{ => tests_core}/test_api_test.py | 0 .../test_api_email_happy_path.py | 0 .../test_api_email_unauthorized.py | 0 .../{ => tests_email}/test_api_email_wrong.py | 0 .../test_api_telegram_create_bot.py | 0 .../test_api_telegram_delete_bot.py | 45 +++++++++++++++++++ .../test_api_telegram_get_bot.py | 34 ++++++++++++++ .../test_api_twitter_create_bot.py | 9 ++-- .../test_api_twitter_delete_bot.py | 0 .../test_api_twitter_get_bot.py | 0 .../test_api_twitter_get_bots.py | 0 14 files changed, 96 insertions(+), 5 deletions(-) rename tests/{ => tests_core}/test_api_admin.py (100%) rename tests/{ => tests_core}/test_api_hoods.py (100%) rename tests/{ => tests_core}/test_api_test.py (100%) rename tests/{ => tests_email}/test_api_email_happy_path.py (100%) rename tests/{ => tests_email}/test_api_email_unauthorized.py (100%) rename tests/{ => tests_email}/test_api_email_wrong.py (100%) rename tests/{ => tests_telegram}/test_api_telegram_create_bot.py (100%) create mode 100644 tests/tests_telegram/test_api_telegram_delete_bot.py create mode 100644 tests/tests_telegram/test_api_telegram_get_bot.py rename tests/{ => tests_twitter}/test_api_twitter_create_bot.py (97%) rename tests/{ => tests_twitter}/test_api_twitter_delete_bot.py (100%) rename tests/{ => tests_twitter}/test_api_twitter_get_bot.py (100%) rename tests/{ => tests_twitter}/test_api_twitter_get_bots.py (100%) diff --git a/tests/conftest.py b/tests/conftest.py index 316daae..317f158 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,7 @@ from fastapi.testclient import TestClient from kibicara import email from kibicara.model import Hood, Mapping from kibicara.platforms.twitter.model import Twitter +from kibicara.platforms.telegram.model import Telegram from kibicara.webapi import router from pytest import fixture from urllib.parse import urlparse @@ -145,3 +146,15 @@ def twitter(event_loop, hood_id): access_token_secret='access_token_secret123', ) ) + + +@fixture(scope='function') +def telegram(event_loop, hood_id, bot): + hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id)) + return event_loop.run_until_complete( + Telegram.objects.create( + hood=hood, + api_token=bot['api_token'], + welcome_message=bot['welcome_message'], + ) + ) diff --git a/tests/test_api_admin.py b/tests/tests_core/test_api_admin.py similarity index 100% rename from tests/test_api_admin.py rename to tests/tests_core/test_api_admin.py diff --git a/tests/test_api_hoods.py b/tests/tests_core/test_api_hoods.py similarity index 100% rename from tests/test_api_hoods.py rename to tests/tests_core/test_api_hoods.py diff --git a/tests/test_api_test.py b/tests/tests_core/test_api_test.py similarity index 100% rename from tests/test_api_test.py rename to tests/tests_core/test_api_test.py diff --git a/tests/test_api_email_happy_path.py b/tests/tests_email/test_api_email_happy_path.py similarity index 100% rename from tests/test_api_email_happy_path.py rename to tests/tests_email/test_api_email_happy_path.py diff --git a/tests/test_api_email_unauthorized.py b/tests/tests_email/test_api_email_unauthorized.py similarity index 100% rename from tests/test_api_email_unauthorized.py rename to tests/tests_email/test_api_email_unauthorized.py diff --git a/tests/test_api_email_wrong.py b/tests/tests_email/test_api_email_wrong.py similarity index 100% rename from tests/test_api_email_wrong.py rename to tests/tests_email/test_api_email_wrong.py diff --git a/tests/test_api_telegram_create_bot.py b/tests/tests_telegram/test_api_telegram_create_bot.py similarity index 100% rename from tests/test_api_telegram_create_bot.py rename to tests/tests_telegram/test_api_telegram_create_bot.py diff --git a/tests/tests_telegram/test_api_telegram_delete_bot.py b/tests/tests_telegram/test_api_telegram_delete_bot.py new file mode 100644 index 0000000..42f668b --- /dev/null +++ b/tests/tests_telegram/test_api_telegram_delete_bot.py @@ -0,0 +1,45 @@ +# Copyright (C) 2020 by Cathy Hu +# +# SPDX-License-Identifier: 0BSD + +from fastapi import status +from kibicara.platforms.telegram.model import Telegram, TelegramUser +from ormantic.exceptions import NoMatch +from pytest import mark, raises + + +@mark.parametrize('bot', [{'api_token': 'apitoken123', 'welcome_message': 'msg'}]) +def test_telegram_delete_bot(client, event_loop, bot, telegram, auth_header): + event_loop.run_until_complete( + TelegramUser.objects.create(user_id=1234, bot=telegram.id) + ) + event_loop.run_until_complete( + TelegramUser.objects.create(user_id=5678, bot=telegram.id) + ) + response = client.delete( + f'/api/hoods/{telegram.hood.id}/telegram/{telegram.id}', headers=auth_header + ) + assert response.status_code == status.HTTP_204_NO_CONTENT + with raises(NoMatch): + event_loop.run_until_complete(Telegram.objects.get(id=telegram.id)) + with raises(NoMatch): + event_loop.run_until_complete(TelegramUser.objects.get(id=telegram.id)) + + +def test_telegram_delete_bot_invalid_id(client, auth_header, hood_id): + response = client.delete('/api/hoods/1337/telegram/123', headers=auth_header) + 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(f'/api/hoods/{hood_id}/telegram/7331', headers=auth_header) + assert response.status_code == status.HTTP_404_NOT_FOUND + response = client.delete( + f'/api/hoods/{hood_id}/telegram/wrong', headers=auth_header + ) + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY + + +@mark.parametrize('bot', [{'api_token': 'apitoken123', 'welcome_message': 'msg'}]) +def test_telegram_delete_bot_unauthorized(client, bot, telegram): + response = client.delete(f'/api/hoods/{telegram.hood.id}/telegram/{telegram.id}') + assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/tests/tests_telegram/test_api_telegram_get_bot.py b/tests/tests_telegram/test_api_telegram_get_bot.py new file mode 100644 index 0000000..5fb7302 --- /dev/null +++ b/tests/tests_telegram/test_api_telegram_get_bot.py @@ -0,0 +1,34 @@ +# Copyright (C) 2020 by Cathy Hu +# +# SPDX-License-Identifier: 0BSD + +from fastapi import status +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( + f'/api/hoods/{telegram.hood.id}/telegram/{telegram.id}', headers=auth_header + ) + assert response.status_code == status.HTTP_200_OK + assert response.json()['id'] == telegram.id + assert response.json()['api_token'] == telegram.api_token + assert response.json()['welcome_message'] == telegram.welcome_message + + +def test_telegram_get_bot_invalid_id(client, auth_header, hood_id): + response = client.get('/api/hoods/1337/telegram/123', headers=auth_header) + 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(f'/api/hoods/{hood_id}/telegram/7331', headers=auth_header) + assert response.status_code == status.HTTP_404_NOT_FOUND + response = client.get(f'/api/hoods/{hood_id}/telegram/wrong', 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(f'/api/hoods/{telegram.hood.id}/telegram/{telegram.id}') + assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/tests/test_api_twitter_create_bot.py b/tests/tests_twitter/test_api_twitter_create_bot.py similarity index 97% rename from tests/test_api_twitter_create_bot.py rename to tests/tests_twitter/test_api_twitter_create_bot.py index c49b124..c517f94 100644 --- a/tests/test_api_twitter_create_bot.py +++ b/tests/tests_twitter/test_api_twitter_create_bot.py @@ -6,13 +6,12 @@ from fastapi import status from kibicara import config from kibicara.platforms import twitter from kibicara.platforms.twitter.model import Twitter -from pytest import fixture -import pytest +from pytest import fixture, mark @fixture(scope='function') def receive_oauth_request_token(monkeypatch, twitter_request_response): - @pytest.mark.asyncio + @mark.asyncio async def mock_get_oauth_request_token( consumer_key, consumer_secret, callback_uri='' ): @@ -23,7 +22,7 @@ def receive_oauth_request_token(monkeypatch, twitter_request_response): @fixture(scope='function') def receive_oauth_access_token(monkeypatch, twitter_access_response): - @pytest.mark.asyncio + @mark.asyncio async def mock_get_oauth_access_token( consumer_key, consumer_secret, access_token, access_token_secret, oauth_verifier ): @@ -41,7 +40,7 @@ def disable_spawner(monkeypatch): monkeypatch.setattr(twitter.webapi, 'spawner', DoNothing()) -@pytest.mark.parametrize( +@mark.parametrize( 'twitter_request_response, twitter_access_response', [ ( diff --git a/tests/test_api_twitter_delete_bot.py b/tests/tests_twitter/test_api_twitter_delete_bot.py similarity index 100% rename from tests/test_api_twitter_delete_bot.py rename to tests/tests_twitter/test_api_twitter_delete_bot.py diff --git a/tests/test_api_twitter_get_bot.py b/tests/tests_twitter/test_api_twitter_get_bot.py similarity index 100% rename from tests/test_api_twitter_get_bot.py rename to tests/tests_twitter/test_api_twitter_get_bot.py diff --git a/tests/test_api_twitter_get_bots.py b/tests/tests_twitter/test_api_twitter_get_bots.py similarity index 100% rename from tests/test_api_twitter_get_bots.py rename to tests/tests_twitter/test_api_twitter_get_bots.py