# Path: messagebird/signed_request.py
# class SignedRequest:
# 
#     def __init__(self, requestSignature, requestTimestamp, requestBody, requestParameters):
#         self._requestSignature = requestSignature
#         self._requestTimestamp = str(requestTimestamp)
#         self._requestBody = requestBody
#         self._requestParameters = requestParameters
# 
#     def verify(self, signing_key):
#         payload = self._build_payload()
#         expected_signature = base64.b64decode(self._requestSignature)
#         calculated_signature = hmac.new(signing_key.encode('latin-1'), payload.encode('latin-1'),
#                                         hashlib.sha256).digest()
#         return expected_signature == calculated_signature
# 
#     def is_recent(self, offset=10):
#         return int(time.time()) - int(self._requestTimestamp) < offset
# 
#     def _build_payload(self):
#         checksum_body = hashlib.sha256(self._requestBody.encode('latin-1')).digest()
#         str_checksum_body = checksum_body.decode('latin-1')
#         parts = [self._requestTimestamp, urlencode(self._sort_dict(self._requestParameters), True), str_checksum_body]
#         return "\n".join(parts)
# 
#     def _sort_dict(self, dict):
#         sorted_dict = OrderedDict()
# 
#         for key in sorted(dict):
#             sorted_dict[key] = dict[key]
# 
#         return sorted_dict


import time
import unittest
from messagebird.signed_request import SignedRequest
            'reference': 'BAR',
            'statusDatetime': '2019-01-11T09:17:11+00:00',
            'id': 'eef0ab57a9e049be946f3821568c2b2e',
            'status': 'delivered',
            'mccmnc': '20408',
            'ported': '1',
        }

        signature = 'KVBdcVdz2lYMwcBLZCRITgxUfA/WkwSi+T3Wxl2HL6w='
        timestamp = 1547198231
        body = ''

        signed_request = SignedRequest(signature, timestamp, body, query)
        self.assertFalse(signed_request.verify(SIGNING_KEY))

    def test_recent_signed_request(self):
        query = {}
        signature = 'KVBdcVdz2lYMwcBLZCRITgxUfA/WkwSi+T3Wxl2HL6w='
        timestamp = int(time.time()) - 1
        body = ''

        signed_request = SignedRequest(signature, timestamp, body, query)
        self.assertTrue(signed_request.is_recent())

    def test_not_recent_signed_request(self):
        query = {}
        signature = 'KVBdcVdz2lYMwcBLZCRITgxUfA/WkwSi+T3Wxl2HL6w='
        timestamp = int(time.time()) - 100
        body = ''


# Path: messagebird/signed_request.py
# class SignedRequest:
# 
#     def __init__(self, requestSignature, requestTimestamp, requestBody, requestParameters):
#         self._requestSignature = requestSignature
#         self._requestTimestamp = str(requestTimestamp)
#         self._requestBody = requestBody
#         self._requestParameters = requestParameters
# 
#     def verify(self, signing_key):
#         payload = self._build_payload()
#         expected_signature = base64.b64decode(self._requestSignature)
#         calculated_signature = hmac.new(signing_key.encode('latin-1'), payload.encode('latin-1'),
#                                         hashlib.sha256).digest()
#         return expected_signature == calculated_signature
# 
#     def is_recent(self, offset=10):
#         return int(time.time()) - int(self._requestTimestamp) < offset
# 
#     def _build_payload(self):
#         checksum_body = hashlib.sha256(self._requestBody.encode('latin-1')).digest()
#         str_checksum_body = checksum_body.decode('latin-1')
#         parts = [self._requestTimestamp, urlencode(self._sort_dict(self._requestParameters), True), str_checksum_body]
#         return "\n".join(parts)
# 
#     def _sort_dict(self, dict):
#         sorted_dict = OrderedDict()
# 
#         for key in sorted(dict):
#             sorted_dict[key] = dict[key]
# 
#         return sorted_dict


# Path: tests/test_signed_request.py
import time
import unittest
from messagebird.signed_request import SignedRequest

            'reference': 'BAR',
            'statusDatetime': '2019-01-11T09:17:11+00:00',
            'id': 'eef0ab57a9e049be946f3821568c2b2e',
            'status': 'delivered',
            'mccmnc': '20408',
            'ported': '1',
        }

        signature = 'KVBdcVdz2lYMwcBLZCRITgxUfA/WkwSi+T3Wxl2HL6w='
        timestamp = 1547198231
        body = ''

        signed_request = SignedRequest(signature, timestamp, body, query)
        self.assertFalse(signed_request.verify(SIGNING_KEY))

    def test_recent_signed_request(self):
        query = {}
        signature = 'KVBdcVdz2lYMwcBLZCRITgxUfA/WkwSi+T3Wxl2HL6w='
        timestamp = int(time.time()) - 1
        body = ''

        signed_request = SignedRequest(signature, timestamp, body, query)
        self.assertTrue(signed_request.is_recent())

    def test_not_recent_signed_request(self):
        query = {}
        signature = 'KVBdcVdz2lYMwcBLZCRITgxUfA/WkwSi+T3Wxl2HL6w='
        timestamp = int(time.time()) - 100
        body = ''


        signed_request = SignedRequest(signature, timestamp, body, query)