This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix/add remaining tests for 100% test coverage
Add test_web_webpush tests which were accidentally missed in an earlier commit. Update self.assert tests to use nose test utils which actually trigger errors.
- Loading branch information
Showing
3 changed files
with
121 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import uuid | ||
|
||
from cryptography.fernet import Fernet | ||
from cyclone.web import Application | ||
from mock import Mock, patch | ||
from moto import mock_dynamodb2 | ||
from nose.tools import eq_ | ||
from twisted.internet.defer import Deferred | ||
from twisted.logger import Logger | ||
from twisted.trial import unittest | ||
|
||
from autopush.db import ( | ||
Router, | ||
create_rotating_message_table, | ||
) | ||
from autopush.router.interface import IRouter, RouterResponse | ||
from autopush.settings import AutopushSettings | ||
|
||
dummy_request_id = "11111111-1234-1234-1234-567812345678" | ||
dummy_uaid = str(uuid.UUID("abad1dea00000000aabbccdd00000000")) | ||
dummy_chid = str(uuid.UUID("deadbeef00000000decafbad00000000")) | ||
dummy_token = dummy_uaid + ":" + dummy_chid | ||
mock_dynamodb2 = mock_dynamodb2() | ||
|
||
|
||
def setUp(): | ||
mock_dynamodb2.start() | ||
create_rotating_message_table() | ||
|
||
|
||
def tearDown(): | ||
mock_dynamodb2.stop() | ||
|
||
|
||
class TestWebpushHandler(unittest.TestCase): | ||
@patch('uuid.uuid4', return_value=uuid.UUID(dummy_request_id)) | ||
def setUp(self, t): | ||
from autopush.web.webpush import WebPushHandler | ||
|
||
settings = AutopushSettings( | ||
hostname="localhost", | ||
statsd_host=None, | ||
) | ||
self.fernet_mock = settings.fernet = Mock(spec=Fernet) | ||
self.ap_settings = settings | ||
|
||
self.router_mock = settings.router = Mock(spec=Router) | ||
self.request_mock = Mock(body=b'', arguments={}, | ||
headers={"ttl": "0"}, | ||
host='example.com:8080') | ||
|
||
self.wp = WebPushHandler(Application(), | ||
self.request_mock, | ||
ap_settings=settings) | ||
self.wp.path_kwargs = {} | ||
self.status_mock = self.wp.set_status = Mock() | ||
self.write_mock = self.wp.write = Mock() | ||
self.wp.log = Mock(spec=Logger) | ||
d = self.finish_deferred = Deferred() | ||
self.wp.finish = lambda: d.callback(True) | ||
settings.routers["webpush"] = Mock(spec=IRouter) | ||
self.wp_router_mock = settings.routers["webpush"] | ||
|
||
def test_router_needs_update(self): | ||
self.ap_settings.parse_endpoint = Mock(return_value=dict( | ||
uaid=dummy_uaid, | ||
chid=dummy_chid, | ||
public_key="asdfasdf", | ||
)) | ||
self.fernet_mock.decrypt.return_value = dummy_token | ||
self.router_mock.get_uaid.return_value = dict( | ||
router_type="webpush", | ||
router_data=dict(), | ||
) | ||
self.wp_router_mock.route_notification.return_value = RouterResponse( | ||
status_code=503, | ||
router_data=dict(token="new_connect"), | ||
) | ||
|
||
def handle_finish(result): | ||
eq_(result, True) | ||
self.wp.set_status.assert_called_with(503) | ||
assert(self.router_mock.register_user.called) | ||
self.finish_deferred.addCallback(handle_finish) | ||
|
||
self.wp.post("v1", dummy_token) | ||
return self.finish_deferred | ||
|
||
def test_router_returns_data_without_detail(self): | ||
self.ap_settings.parse_endpoint = Mock(return_value=dict( | ||
uaid=dummy_uaid, | ||
chid=dummy_chid, | ||
public_key="asdfasdf", | ||
)) | ||
self.fernet_mock.decrypt.return_value = dummy_token | ||
self.router_mock.get_uaid.return_value = dict( | ||
router_type="webpush", | ||
router_data=dict(), | ||
) | ||
self.wp_router_mock.route_notification.return_value = RouterResponse( | ||
status_code=503, | ||
router_data=dict(), | ||
) | ||
|
||
def handle_finish(result): | ||
eq_(result, True) | ||
self.wp.set_status.assert_called_with(503) | ||
assert(self.router_mock.register_user.called) | ||
self.finish_deferred.addCallback(handle_finish) | ||
|
||
self.wp.post("v1", dummy_token) | ||
return self.finish_deferred |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters