From efebe40a44d6ac94ce1268242c1bb6f7c9e53344 Mon Sep 17 00:00:00 2001 From: Cathy Hu Date: Sat, 18 Jul 2020 23:20:46 +0200 Subject: [PATCH] [tests] Add tests for deleting a twitter bot --- tests/conftest.py | 15 ++++++++++++- tests/test_api_twitter_create_bot.py | 7 +++++- tests/test_api_twitter_delete_bot.py | 33 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/test_api_twitter_delete_bot.py diff --git a/tests/conftest.py b/tests/conftest.py index b180119..8693470 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,8 @@ from fastapi import FastAPI, status from fastapi.testclient import TestClient from kibicara import email -from kibicara.model import Mapping +from kibicara.model import Hood, Mapping +from kibicara.platforms.twitter.model import Twitter from kibicara.webapi import router from pytest import fixture @@ -131,3 +132,15 @@ def email_row(client, hood_id, auth_header): email_id = int(response.headers['Location']) yield response.json() client.delete('/api/hoods/%d/email/%d' % (hood_id, email_id), headers=auth_header) + + +@fixture(scope='function') +def twitter(event_loop, hood_id): + hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id)) + return event_loop.run_until_complete( + Twitter.objects.create( + hood=hood, + access_token='access_token123', + access_token_secret='access_token_secret123', + ) + ) diff --git a/tests/test_api_twitter_create_bot.py b/tests/test_api_twitter_create_bot.py index 3a5eaf3..bfa8468 100644 --- a/tests/test_api_twitter_create_bot.py +++ b/tests/test_api_twitter_create_bot.py @@ -120,13 +120,18 @@ def test_twitter_callback_invalid_oauth_token(client): assert response.status_code == status.HTTP_404_NOT_FOUND -def test_twitter_create_twitter_invalid_hood(client, auth_header): +def test_twitter_create_twitter_invalid_id(client, auth_header): response = client.post('/api/hoods/1337/twitter/', headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND response = client.post('/api/hoods/wrong/twitter/', headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY +def test_twitter_create_unauthorized(client, hood_id): + response = client.post('/api/hoods/{hood_id}/twitter/') + assert response.status_code == status.HTTP_401_UNAUTHORIZED + + def test_twitter_create_wrong_consumer_keys(client, monkeypatch, auth_header, hood_id): # No consumer keys response = client.post(f'/api/hoods/{hood_id}/twitter/', headers=auth_header) diff --git a/tests/test_api_twitter_delete_bot.py b/tests/test_api_twitter_delete_bot.py new file mode 100644 index 0000000..0c272b6 --- /dev/null +++ b/tests/test_api_twitter_delete_bot.py @@ -0,0 +1,33 @@ +# Copyright (C) 2020 by Cathy Hu +# +# SPDX-License-Identifier: 0BSD + +from fastapi import status +from kibicara.platforms.twitter.model import Twitter +from ormantic.exceptions import NoMatch +from pytest import raises + + +def test_twitter_delete_bot(client, event_loop, twitter, auth_header, monkeypatch): + response = client.delete( + f'/api/hoods/{twitter.hood.id}/twitter/{twitter.id}', headers=auth_header + ) + assert response.status_code == status.HTTP_204_NO_CONTENT + with raises(NoMatch): + event_loop.run_until_complete(Twitter.objects.get(id=twitter.id)) + + +def test_twitter_delete_bot_invalid_id(client, auth_header, hood_id): + response = client.delete('/api/hoods/1337/twitter/123', headers=auth_header) + 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(f'/api/hoods/{hood_id}/twitter/7331', headers=auth_header) + assert response.status_code == status.HTTP_404_NOT_FOUND + response = client.delete(f'/api/hoods/{hood_id}/twitter/wrong', headers=auth_header) + assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY + + +def test_twitter_delete_bot_unauthorized(client, twitter): + response = client.delete(f'/api/hoods/{twitter.hood.id}/twitter/{twitter.id}') + assert response.status_code == status.HTTP_401_UNAUTHORIZED