# Path: src/cybersource/signature.py
# class SecureAcceptanceSigner(object):
#     def __init__(self, secret_key):
#         self.secret_key = secret_key
# 
#     def sign(self, data, signed_fields):
#         key = self.secret_key.encode("utf-8")
#         msg_raw = self._build_message(data, signed_fields).encode("utf-8")
#         msg_hmac = hmac.new(key, msg_raw, hashlib.sha256)
#         return base64.b64encode(msg_hmac.digest())
# 
#     def verify_request(self, request):
#         # Ensure the signature is valid and that this request can be trusted
#         signed_field_names = request.POST.get("signed_field_names")
#         if not signed_field_names:
#             raise SuspiciousOperation("Request has no fields to verify")
#         signed_field_names = signed_field_names.split(",")
#         signature_given = request.POST["signature"].encode("utf-8")
#         signature_calc = self.sign(request.POST, signed_field_names)
#         return signature_given == signature_calc
# 
#     def _build_message(self, data, signed_fields):
#         parts = []
#         for field in signed_fields:
#             parts.append("%s=%s" % (field, data.get(field, "")))
#         return ",".join(parts)
# 
# Path: src/cybersource/tests/factories.py
# def get_sa_profile():
#     return SecureAcceptanceProfile.get_profile("testserver")


from django.test import TestCase
from django.test.client import RequestFactory
from ..signature import SecureAcceptanceSigner
from .factories import get_sa_profile


class SignerTest(TestCase):
    fixtures = ["cybersource-test.yaml"]

    def test_sign(self):
        profile = get_sa_profile()
        signer = SecureAcceptanceSigner(profile.secret_key)

        # Baseline
        signer.secret_key = "FOO"
        signature = signer.sign({"foo": "bar", "baz": "bat"}, ("foo", "baz"))
        self.assertEqual(signature, b"IVMC7Aj8pDKwLx+0eNfIfoQAHvViiLeavLyYatCtB+c=")

        # Change field order
        signature = signer.sign({"foo": "bar", "baz": "bat"}, ("baz", "foo"))
        self.assertEqual(signature, b"5Gw1ffUlVU9Cm0tTa/nzhQ81Bc6/SDqz/tEP5VyMzkk=")

        # Change key
        signer.secret_key = "SECRET"
        signature = signer.sign({"foo": "bar", "baz": "bat"}, ("foo", "baz"))
        self.assertEqual(signature, b"FvjC1PIhxuaLipTbRDw9UXL6F58t9Hyj12HLHiYoOD0=")

    def test_verify(self):

# Path: src/cybersource/signature.py
# class SecureAcceptanceSigner(object):
#     def __init__(self, secret_key):
#         self.secret_key = secret_key
# 
#     def sign(self, data, signed_fields):
#         key = self.secret_key.encode("utf-8")
#         msg_raw = self._build_message(data, signed_fields).encode("utf-8")
#         msg_hmac = hmac.new(key, msg_raw, hashlib.sha256)
#         return base64.b64encode(msg_hmac.digest())
# 
#     def verify_request(self, request):
#         # Ensure the signature is valid and that this request can be trusted
#         signed_field_names = request.POST.get("signed_field_names")
#         if not signed_field_names:
#             raise SuspiciousOperation("Request has no fields to verify")
#         signed_field_names = signed_field_names.split(",")
#         signature_given = request.POST["signature"].encode("utf-8")
#         signature_calc = self.sign(request.POST, signed_field_names)
#         return signature_given == signature_calc
# 
#     def _build_message(self, data, signed_fields):
#         parts = []
#         for field in signed_fields:
#             parts.append("%s=%s" % (field, data.get(field, "")))
#         return ",".join(parts)
# 
# Path: src/cybersource/tests/factories.py
# def get_sa_profile():
#     return SecureAcceptanceProfile.get_profile("testserver")


# Path: src/cybersource/tests/test_signer.py
from django.test import TestCase
from django.test.client import RequestFactory
from ..signature import SecureAcceptanceSigner
from .factories import get_sa_profile



class SignerTest(TestCase):
    fixtures = ["cybersource-test.yaml"]

    def test_sign(self):
        profile = get_sa_profile()
        signer = SecureAcceptanceSigner(profile.secret_key)

        # Baseline
        signer.secret_key = "FOO"
        signature = signer.sign({"foo": "bar", "baz": "bat"}, ("foo", "baz"))
        self.assertEqual(signature, b"IVMC7Aj8pDKwLx+0eNfIfoQAHvViiLeavLyYatCtB+c=")

        # Change field order
        signature = signer.sign({"foo": "bar", "baz": "bat"}, ("baz", "foo"))
        self.assertEqual(signature, b"5Gw1ffUlVU9Cm0tTa/nzhQ81Bc6/SDqz/tEP5VyMzkk=")

        # Change key
        signer.secret_key = "SECRET"
        signature = signer.sign({"foo": "bar", "baz": "bat"}, ("foo", "baz"))
        self.assertEqual(signature, b"FvjC1PIhxuaLipTbRDw9UXL6F58t9Hyj12HLHiYoOD0=")

    def test_verify(self):

        profile = get_sa_profile()