[tests] Restructure testing files

This commit is contained in:
Cathy Hu 2020-07-19 14:02:43 +02:00
parent 17c3b4523f
commit 2af4b27e0b
14 changed files with 96 additions and 5 deletions

View file

@ -8,6 +8,7 @@ from fastapi.testclient import TestClient
from kibicara import email from kibicara import email
from kibicara.model import Hood, Mapping from kibicara.model import Hood, Mapping
from kibicara.platforms.twitter.model import Twitter from kibicara.platforms.twitter.model import Twitter
from kibicara.platforms.telegram.model import Telegram
from kibicara.webapi import router from kibicara.webapi import router
from pytest import fixture from pytest import fixture
from urllib.parse import urlparse from urllib.parse import urlparse
@ -145,3 +146,15 @@ def twitter(event_loop, hood_id):
access_token_secret='access_token_secret123', 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'],
)
)

View file

@ -0,0 +1,45 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
#
# 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

View file

@ -0,0 +1,34 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
#
# 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

View file

@ -6,13 +6,12 @@ from fastapi import status
from kibicara import config from kibicara import config
from kibicara.platforms import twitter from kibicara.platforms import twitter
from kibicara.platforms.twitter.model import Twitter from kibicara.platforms.twitter.model import Twitter
from pytest import fixture from pytest import fixture, mark
import pytest
@fixture(scope='function') @fixture(scope='function')
def receive_oauth_request_token(monkeypatch, twitter_request_response): def receive_oauth_request_token(monkeypatch, twitter_request_response):
@pytest.mark.asyncio @mark.asyncio
async def mock_get_oauth_request_token( async def mock_get_oauth_request_token(
consumer_key, consumer_secret, callback_uri='' consumer_key, consumer_secret, callback_uri=''
): ):
@ -23,7 +22,7 @@ def receive_oauth_request_token(monkeypatch, twitter_request_response):
@fixture(scope='function') @fixture(scope='function')
def receive_oauth_access_token(monkeypatch, twitter_access_response): def receive_oauth_access_token(monkeypatch, twitter_access_response):
@pytest.mark.asyncio @mark.asyncio
async def mock_get_oauth_access_token( async def mock_get_oauth_access_token(
consumer_key, consumer_secret, access_token, access_token_secret, oauth_verifier 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()) monkeypatch.setattr(twitter.webapi, 'spawner', DoNothing())
@pytest.mark.parametrize( @mark.parametrize(
'twitter_request_response, twitter_access_response', 'twitter_request_response, twitter_access_response',
[ [
( (