chore: Updates for 2021 (#135)
* chore: Updates for 2021 * drop nose for pytest Closes #124
This commit is contained in:
parent
51f1f9e320
commit
94516459c6
14
.travis.yml
14
.travis.yml
|
@ -1,14 +0,0 @@
|
|||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
- "3.4"
|
||||
- "3.5"
|
||||
- "3.6"
|
||||
install:
|
||||
- pip install -r requirements.txt
|
||||
- pip install -r test-requirements.txt
|
||||
script:
|
||||
- nosetests
|
||||
- flake8 pywebpush
|
||||
after_success:
|
||||
- codecov
|
|
@ -5,7 +5,6 @@ import unittest
|
|||
import time
|
||||
|
||||
from mock import patch, Mock
|
||||
from nose.tools import eq_, ok_, assert_is_not, assert_raises
|
||||
import http_ece
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
|
@ -79,12 +78,11 @@ class WebpushTestCase(unittest.TestCase):
|
|||
"keys": {'p256dh': 'AAA=', 'auth': 'AAA='}})
|
||||
|
||||
push = WebPusher(subscription_info)
|
||||
assert_is_not(push.subscription_info, subscription_info)
|
||||
assert_is_not(push.subscription_info['keys'],
|
||||
subscription_info['keys'])
|
||||
eq_(push.subscription_info['endpoint'], subscription_info['endpoint'])
|
||||
eq_(push.receiver_key, rk_decode)
|
||||
eq_(push.auth_key, b'\x93\xc2U\xea\xc8\xddn\x10"\xd6}\xff,0K\xbc')
|
||||
assert push.subscription_info != subscription_info
|
||||
assert push.subscription_info['keys'] != subscription_info['keys']
|
||||
assert push.subscription_info['endpoint'] == subscription_info['endpoint']
|
||||
assert push.receiver_key == rk_decode
|
||||
assert push.auth_key == b'\x93\xc2U\xea\xc8\xddn\x10"\xd6}\xff,0K\xbc'
|
||||
|
||||
def test_encode(self):
|
||||
for content_encoding in ["aesgcm", "aes128gcm"]:
|
||||
|
@ -118,7 +116,7 @@ class WebpushTestCase(unittest.TestCase):
|
|||
auth_secret=raw_auth,
|
||||
version=content_encoding
|
||||
)
|
||||
eq_(decoded.decode('utf8'), data)
|
||||
assert decoded.decode('utf8') == data
|
||||
|
||||
def test_bad_content_encoding(self):
|
||||
subscription_info = self._gen_subscription_info()
|
||||
|
@ -136,13 +134,13 @@ class WebpushTestCase(unittest.TestCase):
|
|||
"Authentication": "bearer vapid"}
|
||||
data = "Mary had a little lamb"
|
||||
WebPusher(subscription_info).send(data, headers)
|
||||
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
|
||||
assert subscription_info.get('endpoint') == mock_post.call_args[0][0]
|
||||
pheaders = mock_post.call_args[1].get('headers')
|
||||
eq_(pheaders.get('ttl'), '0')
|
||||
eq_(pheaders.get('AUTHENTICATION'), headers.get('Authentication'))
|
||||
assert pheaders.get('ttl') == '0'
|
||||
assert pheaders.get('AUTHENTICATION') == headers.get('Authentication')
|
||||
ckey = pheaders.get('crypto-key')
|
||||
ok_('pre-existing' in ckey)
|
||||
eq_(pheaders.get('content-encoding'), 'aes128gcm')
|
||||
assert 'pre-existing' in ckey
|
||||
assert pheaders.get('content-encoding') == 'aes128gcm'
|
||||
|
||||
@patch("requests.post")
|
||||
def test_send_vapid(self, mock_post):
|
||||
|
@ -157,9 +155,9 @@ class WebpushTestCase(unittest.TestCase):
|
|||
content_encoding="aesgcm",
|
||||
headers={"Test-Header": "test-value"}
|
||||
)
|
||||
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
|
||||
assert subscription_info.get('endpoint') == mock_post.call_args[0][0]
|
||||
pheaders = mock_post.call_args[1].get('headers')
|
||||
eq_(pheaders.get('ttl'), '0')
|
||||
assert pheaders.get('ttl') == '0'
|
||||
|
||||
def repad(str):
|
||||
return str + "===="[:len(str) % 4]
|
||||
|
@ -169,12 +167,12 @@ class WebpushTestCase(unittest.TestCase):
|
|||
repad(pheaders['authorization'].split('.')[1])
|
||||
).decode('utf8')
|
||||
)
|
||||
ok_(subscription_info.get('endpoint').startswith(auth['aud']))
|
||||
ok_('vapid' in pheaders.get('authorization'))
|
||||
assert subscription_info.get('endpoint').startswith(auth['aud'])
|
||||
assert 'vapid' in pheaders.get('authorization')
|
||||
ckey = pheaders.get('crypto-key')
|
||||
ok_('dh=' in ckey)
|
||||
eq_(pheaders.get('content-encoding'), 'aesgcm')
|
||||
eq_(pheaders.get('test-header'), 'test-value')
|
||||
assert 'dh=' in ckey
|
||||
assert pheaders.get('content-encoding') == 'aesgcm'
|
||||
assert pheaders.get('test-header') == 'test-value'
|
||||
|
||||
@patch.object(WebPusher, "send")
|
||||
@patch.object(py_vapid.Vapid, "sign")
|
||||
|
@ -211,7 +209,7 @@ class WebpushTestCase(unittest.TestCase):
|
|||
)
|
||||
vapid_sign.assert_called_once_with(claims)
|
||||
pusher_send.assert_called_once()
|
||||
ok_(claims['exp'] > int(time.time()))
|
||||
assert claims['exp'] > int(time.time())
|
||||
|
||||
@patch("requests.post")
|
||||
def test_send_bad_vapid_no_key(self, mock_post):
|
||||
|
@ -219,15 +217,15 @@ class WebpushTestCase(unittest.TestCase):
|
|||
|
||||
subscription_info = self._gen_subscription_info()
|
||||
data = "Mary had a little lamb"
|
||||
assert_raises(WebPushException,
|
||||
webpush,
|
||||
subscription_info=subscription_info,
|
||||
data=data,
|
||||
vapid_claims={
|
||||
"aud": "https://example.com",
|
||||
"sub": "mailto:ops@example.com"
|
||||
}
|
||||
)
|
||||
self.assertRaises(
|
||||
WebPushException,
|
||||
webpush,
|
||||
subscription_info=subscription_info,
|
||||
data=data,
|
||||
vapid_claims={
|
||||
"aud": "https://example.com",
|
||||
"sub": "mailto:ops@example.com"
|
||||
})
|
||||
|
||||
@patch("requests.post")
|
||||
def test_send_bad_vapid_bad_return(self, mock_post):
|
||||
|
@ -235,16 +233,16 @@ class WebpushTestCase(unittest.TestCase):
|
|||
|
||||
subscription_info = self._gen_subscription_info()
|
||||
data = "Mary had a little lamb"
|
||||
assert_raises(WebPushException,
|
||||
webpush,
|
||||
subscription_info=subscription_info,
|
||||
data=data,
|
||||
vapid_claims={
|
||||
"aud": "https://example.com",
|
||||
"sub": "mailto:ops@example.com"
|
||||
},
|
||||
vapid_private_key=self.vapid_key
|
||||
)
|
||||
self.assertRaises(
|
||||
WebPushException,
|
||||
webpush,
|
||||
subscription_info=subscription_info,
|
||||
data=data,
|
||||
vapid_claims={
|
||||
"aud": "https://example.com",
|
||||
"sub": "mailto:ops@example.com"
|
||||
},
|
||||
vapid_private_key=self.vapid_key)
|
||||
|
||||
@patch("requests.post")
|
||||
def test_send_empty(self, mock_post):
|
||||
|
@ -252,20 +250,20 @@ class WebpushTestCase(unittest.TestCase):
|
|||
headers = {"Crypto-Key": "pre-existing",
|
||||
"Authentication": "bearer vapid"}
|
||||
WebPusher(subscription_info).send('', headers)
|
||||
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
|
||||
assert subscription_info.get('endpoint') == mock_post.call_args[0][0]
|
||||
pheaders = mock_post.call_args[1].get('headers')
|
||||
eq_(pheaders.get('ttl'), '0')
|
||||
ok_('encryption' not in pheaders)
|
||||
eq_(pheaders.get('AUTHENTICATION'), headers.get('Authentication'))
|
||||
assert pheaders.get('ttl') == '0'
|
||||
assert 'encryption' not in pheaders
|
||||
assert pheaders.get('AUTHENTICATION') == headers.get('Authentication')
|
||||
ckey = pheaders.get('crypto-key')
|
||||
ok_('pre-existing' in ckey)
|
||||
assert 'pre-existing' in ckey
|
||||
|
||||
def test_encode_empty(self):
|
||||
subscription_info = self._gen_subscription_info()
|
||||
headers = {"Crypto-Key": "pre-existing",
|
||||
"Authentication": "bearer vapid"}
|
||||
encoded = WebPusher(subscription_info).encode('', headers)
|
||||
eq_(encoded, None)
|
||||
assert encoded is None
|
||||
|
||||
def test_encode_no_crypto(self):
|
||||
subscription_info = self._gen_subscription_info()
|
||||
|
@ -274,20 +272,21 @@ class WebpushTestCase(unittest.TestCase):
|
|||
"Authentication": "bearer vapid"}
|
||||
data = 'Something'
|
||||
pusher = WebPusher(subscription_info)
|
||||
assert_raises(WebPushException,
|
||||
pusher.encode,
|
||||
data,
|
||||
headers)
|
||||
self.assertRaises(
|
||||
WebPushException,
|
||||
pusher.encode,
|
||||
data,
|
||||
headers)
|
||||
|
||||
@patch("requests.post")
|
||||
def test_send_no_headers(self, mock_post):
|
||||
subscription_info = self._gen_subscription_info()
|
||||
data = "Mary had a little lamb"
|
||||
WebPusher(subscription_info).send(data)
|
||||
eq_(subscription_info.get('endpoint'), mock_post.call_args[0][0])
|
||||
assert subscription_info.get('endpoint') == mock_post.call_args[0][0]
|
||||
pheaders = mock_post.call_args[1].get('headers')
|
||||
eq_(pheaders.get('ttl'), '0')
|
||||
eq_(pheaders.get('content-encoding'), 'aes128gcm')
|
||||
assert pheaders.get('ttl') == '0'
|
||||
assert pheaders.get('content-encoding') == 'aes128gcm'
|
||||
|
||||
@patch("pywebpush.open")
|
||||
def test_as_curl(self, opener):
|
||||
|
@ -309,15 +308,15 @@ class WebpushTestCase(unittest.TestCase):
|
|||
"-H \"ttl: 0\"",
|
||||
"-H \"content-length:"
|
||||
]:
|
||||
ok_(s in result, "missing: {}".format(s))
|
||||
assert s in result, "missing: {}".format(s)
|
||||
|
||||
def test_ci_dict(self):
|
||||
ci = CaseInsensitiveDict({"Foo": "apple", "bar": "banana"})
|
||||
eq_('apple', ci["foo"])
|
||||
eq_('apple', ci.get("FOO"))
|
||||
eq_('apple', ci.get("Foo"))
|
||||
assert 'apple' == ci["foo"]
|
||||
assert 'apple' == ci.get("FOO")
|
||||
assert 'apple' == ci.get("Foo")
|
||||
del (ci['FOO'])
|
||||
eq_(None, ci.get('Foo'))
|
||||
assert ci.get('Foo') is None
|
||||
|
||||
@patch("requests.post")
|
||||
def test_gcm(self, mock_post):
|
||||
|
@ -331,18 +330,18 @@ class WebpushTestCase(unittest.TestCase):
|
|||
wp.send(data, headers, gcm_key="gcm_key_value")
|
||||
pdata = json.loads(mock_post.call_args[1].get('data'))
|
||||
pheaders = mock_post.call_args[1].get('headers')
|
||||
eq_(pdata["registration_ids"][0], "regid123")
|
||||
eq_(pheaders.get("authorization"), "key=gcm_key_value")
|
||||
eq_(pheaders.get("content-type"), "application/json")
|
||||
assert pdata["registration_ids"][0] == "regid123"
|
||||
assert pheaders.get("authorization") == "key=gcm_key_value"
|
||||
assert pheaders.get("content-type") == "application/json"
|
||||
|
||||
@patch("requests.post")
|
||||
def test_timeout(self, mock_post):
|
||||
mock_post.return_value.status_code = 200
|
||||
subscription_info = self._gen_subscription_info()
|
||||
WebPusher(subscription_info).send(timeout=5.2)
|
||||
eq_(mock_post.call_args[1].get('timeout'), 5.2)
|
||||
assert mock_post.call_args[1].get('timeout') == 5.2
|
||||
webpush(subscription_info, timeout=10.001)
|
||||
eq_(mock_post.call_args[1].get('timeout'), 10.001)
|
||||
assert mock_post.call_args[1].get('timeout') == 10.001
|
||||
|
||||
@patch("requests.Session")
|
||||
def test_send_using_requests_session(self, mock_session):
|
||||
|
@ -352,14 +351,14 @@ class WebpushTestCase(unittest.TestCase):
|
|||
data = "Mary had a little lamb"
|
||||
WebPusher(subscription_info,
|
||||
requests_session=mock_session).send(data, headers)
|
||||
eq_(subscription_info.get('endpoint'),
|
||||
mock_session.post.call_args[0][0])
|
||||
assert subscription_info.get(
|
||||
'endpoint') == mock_session.post.call_args[0][0]
|
||||
pheaders = mock_session.post.call_args[1].get('headers')
|
||||
eq_(pheaders.get('ttl'), '0')
|
||||
eq_(pheaders.get('AUTHENTICATION'), headers.get('Authentication'))
|
||||
assert pheaders.get('ttl') == '0'
|
||||
assert pheaders.get('AUTHENTICATION') == headers.get('Authentication')
|
||||
ckey = pheaders.get('crypto-key')
|
||||
ok_('pre-existing' in ckey)
|
||||
eq_(pheaders.get('content-encoding'), 'aes128gcm')
|
||||
assert 'pre-existing' in ckey
|
||||
assert pheaders.get('content-encoding') == 'aes128gcm'
|
||||
|
||||
|
||||
class WebpushExceptionTestCase(unittest.TestCase):
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-r requirements.txt
|
||||
nose>=1.3.7
|
||||
pytest
|
||||
coverage>=4.4.1
|
||||
mock>=2.0.0
|
||||
flake8>=3.3.0
|
||||
|
|
Loading…
Reference in New Issue