Rewrite Facebook unit test to pytest style test function (#41671)

* refactoring - Rewrite Facebook unit test to pytest style test function

* Modifying email author

* modified code formating

* Run CI
This commit is contained in:
tkacikdominik 2020-10-13 13:45:46 +02:00 committed by GitHub
parent d1041efedf
commit b6855816ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,29 +1,26 @@
"""The test for the Facebook notify module."""
import unittest
import pytest
import requests_mock
# import homeassistant.components.facebook as facebook
import homeassistant.components.facebook.notify as facebook
import homeassistant.components.facebook.notify as fb
class TestFacebook(unittest.TestCase):
"""Tests for Facebook notification service."""
@pytest.fixture
def facebook():
"""Fixture for facebook."""
access_token = "page-access-token"
return fb.FacebookNotificationService(access_token)
def setUp(self):
"""Set up test variables."""
access_token = "page-access-token"
self.facebook = facebook.FacebookNotificationService(access_token)
@requests_mock.Mocker()
def test_send_simple_message(self, mock):
"""Test sending a simple message with success."""
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
async def test_send_simple_message(hass, facebook):
"""Test sending a simple message with success."""
with requests_mock.Mocker() as mock:
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
message = "This is just a test"
target = ["+15555551234"]
self.facebook.send_message(message=message, target=target)
facebook.send_message(message=message, target=target)
assert mock.called
assert mock.call_count == 1
@ -38,15 +35,16 @@ class TestFacebook(unittest.TestCase):
expected_params = {"access_token": ["page-access-token"]}
assert mock.last_request.qs == expected_params
@requests_mock.Mocker()
def test_sending_multiple_messages(self, mock):
"""Test sending a message to multiple targets."""
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
async def test_send_multiple_message(hass, facebook):
"""Test sending a message to multiple targets."""
with requests_mock.Mocker() as mock:
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
message = "This is just a test"
targets = ["+15555551234", "+15555551235"]
self.facebook.send_message(message=message, target=targets)
facebook.send_message(message=message, target=targets)
assert mock.called
assert mock.call_count == 2
@ -63,10 +61,11 @@ class TestFacebook(unittest.TestCase):
expected_params = {"access_token": ["page-access-token"]}
assert request.qs == expected_params
@requests_mock.Mocker()
def test_send_message_attachment(self, mock):
"""Test sending a message with a remote attachment."""
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
async def test_send_message_attachment(hass, facebook):
"""Test sending a message with a remote attachment."""
with requests_mock.Mocker() as mock:
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
message = "This will be thrown away."
data = {
@ -77,7 +76,7 @@ class TestFacebook(unittest.TestCase):
}
target = ["+15555551234"]
self.facebook.send_message(message=message, data=data, target=target)
facebook.send_message(message=message, data=data, target=target)
assert mock.called
assert mock.call_count == 1
@ -92,30 +91,30 @@ class TestFacebook(unittest.TestCase):
expected_params = {"access_token": ["page-access-token"]}
assert mock.last_request.qs == expected_params
@requests_mock.Mocker()
def test_send_targetless_message(self, mock):
async def test_send_targetless_message(hass, facebook):
"""Test sending a message without a target."""
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
with requests_mock.Mocker() as mock:
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
self.facebook.send_message(message="going nowhere")
assert not mock.called
facebook.send_message(message="going nowhere")
assert not mock.called
@requests_mock.Mocker()
def test_send_message_with_400(self, mock):
async def test_send_message_with_400(hass, facebook):
"""Test sending a message with a 400 from Facebook."""
mock.register_uri(
requests_mock.POST,
facebook.BASE_URL,
status_code=400,
json={
"error": {
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"code": 190,
"fbtrace_id": "G4Da2pFp2Dp",
}
},
)
self.facebook.send_message(message="nope!", target=["+15555551234"])
assert mock.called
assert mock.call_count == 1
with requests_mock.Mocker() as mock:
mock.register_uri(
requests_mock.POST,
fb.BASE_URL,
status_code=400,
json={
"error": {
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"code": 190,
"fbtrace_id": "G4Da2pFp2Dp",
}
},
)
facebook.send_message(message="nope!", target=["+15555551234"])
assert mock.called
assert mock.call_count == 1