Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
test: fix/add remaining tests for 100% test coverage
Browse files Browse the repository at this point in the history
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
bbangert committed Jul 13, 2016
1 parent b4493ac commit c505edf
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 9 deletions.
12 changes: 6 additions & 6 deletions autopush/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ class testX(Exception):

self.endpoint.write_error(999, exc_info=exc_info)
self.status_mock.assert_called_with(999)
self.assertTrue(self.endpoint.log.called)
eq_(self.endpoint.log.failure.called, True)

def test_write_error_no_exc(self):
""" Write error is triggered by sending the app a request
Expand All @@ -1244,7 +1244,7 @@ def test_write_error_no_exc(self):
"""
self.endpoint.write_error(999)
self.status_mock.assert_called_with(999)
self.assertTrue(self.endpoint.log.called)
eq_(self.endpoint.log.failure.called, True)

def _assert_error_response(self, result):
self.status_mock.assert_called_with(500, None)
Expand Down Expand Up @@ -1434,24 +1434,24 @@ def test_load_params_arguments(self, u=None):
type="test",
))
result = self.reg._load_params()
self.assert_(isinstance(result, dict))
ok_(isinstance(result, dict))
eq_(result["channelID"], dummy_chid)

def test_load_params_invalid_body(self):
self.reg.request.body = b'connect={"type":"test"}'
self.assertTrue(not self.reg._load_params())
ok_(not self.reg._load_params())

@patch('uuid.uuid4', return_value=uuid.UUID(dummy_chid))
def test_load_params_prefer_body(self, t):
args = self.reg.request.arguments
args['connect'] = ['{"type":"invalid"}']
self.reg.request.body = b'connect={"type":"test"}'
self.assertTrue(self.reg._load_params())
eq_(self.reg._load_params(), {})

@patch('uuid.uuid4', return_value=uuid.UUID(dummy_chid))
def test_load_params_no_conn(self, t):
self.reg.request.body = b'noconnect={"type":"test"}'
self.assertTrue(not self.reg._load_params())
ok_(not self.reg._load_params())

def test_cors(self):
ch1 = "Access-Control-Allow-Origin"
Expand Down
112 changes: 112 additions & 0 deletions autopush/tests/test_web_webpush.py
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
6 changes: 3 additions & 3 deletions autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from cyclone.web import Application
from mock import Mock, patch
from moto import mock_dynamodb2
from nose.tools import (eq_, ok_)
from nose.tools import eq_, ok_
from txstatsd.metrics.metrics import Metrics
from twisted.internet import reactor
from twisted.internet.defer import Deferred
Expand Down Expand Up @@ -118,8 +118,8 @@ def raise_error(*args, **kwargs):

req.headers.get.side_effect = raise_error

with self.assertRaises(Exception):
self.proto.onConnect(req)
self.proto.onConnect(req)
self.proto.log.failure.assert_called()

@patch("autopush.websocket.reactor")
def test_autoping_no_uaid(self, mock_reactor):
Expand Down

0 comments on commit c505edf

Please sign in to comment.