# Path: autopush/metrics.py
# class SinkMetrics(IMetrics):
#     """Exists to ignore metrics when metrics are not active"""
#     def increment(self, name, count=1, **kwargs):
#         pass
# 
#     def gauge(self, name, count, **kwargs):
#         pass
# 
#     def timing(self, name, duration, **kwargs):
#         pass
# 
# Path: autopush/exceptions.py
# class InvalidRequest(AutopushException):
#     """Invalid request exception, may include custom status_code and message
#     to write for the error"""
#     def __init__(self, message, status_code=400, errno=None, headers=None):
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.errno = errno
#         self.headers = {} if headers is None else headers
# 
# class InvalidTokenException(Exception):
#     """Invalid URL token Exception"""
# 
# class ItemNotFound(Exception):
#     """Signals missing DynamoDB Item data"""
# 
# Path: autopush/tests/support.py
# def test_db(metrics=None):
#     """Return a test DatabaseManager: its Storage/Router are mocked"""
#     return DatabaseManager(
#         router_conf=DDBTableConfig(tablename='router'),
#         router=Mock(spec=Router),
#         message_conf=DDBTableConfig(tablename='message'),
#         metrics=SinkMetrics() if metrics is None else metrics,
#         resource=autopush.tests.boto_resource,
#     )


import time
import uuid
import base64
import ecdsa
import pytest
import autopush.utils as utils
from hashlib import sha256
from cryptography.fernet import InvalidToken
from cryptography.exceptions import InvalidSignature
from jose import jws, jwk
from marshmallow import Schema, fields
from mock import Mock, patch
from twisted.internet.defer import inlineCallbacks
from twisted.trial import unittest
from autopush.metrics import SinkMetrics
from autopush.exceptions import (
    InvalidRequest,
    InvalidTokenException,
    ItemNotFound
)
from autopush.tests.support import test_db
        from autopush.web.base import ThreadedValidate
        from cyclone.httpserver import HTTPRequest
        from cyclone.web import RequestHandler
        from autopush.http import EndpointHTTPFactory
        from autopush.web.base import BaseWebHandler, threaded_validate
        from autopush.tests.client import Client
        from autopush.web.webpush import WebPushRequestSchema
        from autopush.config import AutopushConfig
        from autopush.web.webpush import WebPushRequestSchema
        assert cm.value.status_code == 401
        assert cm.value.errno == 109

    def test_missing_vapid_header(self):
        schema = self._make_fut()

        header = {"typ": "JWT", "alg": "ES256"}
        payload = {
            "aud": "https://pusher_origin.example.com",
            "exp": 20,
            "sub": "mailto:admin@example.com"
            }

        token, crypto_key = self._gen_jwt(header, payload)
        self.fernet_mock.decrypt.return_value = ('a'*32) + \
            sha256(utils.base64url_decode(crypto_key)).digest()
        ckey = 'keyid="a1"; dh="foo";p256ecdsa="%s"' % crypto_key
        info = self._make_test_data(
            body="asdfasdfasdfasdf",
            path_kwargs=dict(
                api_ver="v2",
                token="asdfasdf",
            ),
            headers={
                "content-encoding": "aesgcm",
                "encryption": "salt=stuff",
                "crypto-key": ckey
            }
        )


# Path: autopush/metrics.py
# class SinkMetrics(IMetrics):
#     """Exists to ignore metrics when metrics are not active"""
#     def increment(self, name, count=1, **kwargs):
#         pass
# 
#     def gauge(self, name, count, **kwargs):
#         pass
# 
#     def timing(self, name, duration, **kwargs):
#         pass
# 
# Path: autopush/exceptions.py
# class InvalidRequest(AutopushException):
#     """Invalid request exception, may include custom status_code and message
#     to write for the error"""
#     def __init__(self, message, status_code=400, errno=None, headers=None):
#         super(AutopushException, self).__init__(message)
#         self.status_code = status_code
#         self.errno = errno
#         self.headers = {} if headers is None else headers
# 
# class InvalidTokenException(Exception):
#     """Invalid URL token Exception"""
# 
# class ItemNotFound(Exception):
#     """Signals missing DynamoDB Item data"""
# 
# Path: autopush/tests/support.py
# def test_db(metrics=None):
#     """Return a test DatabaseManager: its Storage/Router are mocked"""
#     return DatabaseManager(
#         router_conf=DDBTableConfig(tablename='router'),
#         router=Mock(spec=Router),
#         message_conf=DDBTableConfig(tablename='message'),
#         metrics=SinkMetrics() if metrics is None else metrics,
#         resource=autopush.tests.boto_resource,
#     )


# Path: autopush/tests/test_web_validation.py
import time
import uuid
import base64
import ecdsa
import pytest
import autopush.utils as utils
from hashlib import sha256
from cryptography.fernet import InvalidToken
from cryptography.exceptions import InvalidSignature
from jose import jws, jwk
from marshmallow import Schema, fields
from mock import Mock, patch
from twisted.internet.defer import inlineCallbacks
from twisted.trial import unittest
from autopush.metrics import SinkMetrics
from autopush.exceptions import (
    InvalidRequest,
    InvalidTokenException,
    ItemNotFound
)
from autopush.tests.support import test_db
        from autopush.web.base import ThreadedValidate
        from cyclone.httpserver import HTTPRequest
        from cyclone.web import RequestHandler
        from autopush.http import EndpointHTTPFactory
        from autopush.web.base import BaseWebHandler, threaded_validate
        from autopush.tests.client import Client
        from autopush.web.webpush import WebPushRequestSchema
        from autopush.config import AutopushConfig
        from autopush.web.webpush import WebPushRequestSchema

        assert cm.value.status_code == 401
        assert cm.value.errno == 109

    def test_missing_vapid_header(self):
        schema = self._make_fut()

        header = {"typ": "JWT", "alg": "ES256"}
        payload = {
            "aud": "https://pusher_origin.example.com",
            "exp": 20,
            "sub": "mailto:admin@example.com"
            }

        token, crypto_key = self._gen_jwt(header, payload)
        self.fernet_mock.decrypt.return_value = ('a'*32) + \
            sha256(utils.base64url_decode(crypto_key)).digest()
        ckey = 'keyid="a1"; dh="foo";p256ecdsa="%s"' % crypto_key
        info = self._make_test_data(
            body="asdfasdfasdfasdf",
            path_kwargs=dict(
                api_ver="v2",
                token="asdfasdf",
            ),
            headers={
                "content-encoding": "aesgcm",
                "encryption": "salt=stuff",
                "crypto-key": ckey
            }
        )


        with pytest.raises(InvalidRequest) as cm: