import pingen2sdk
import pytest


class TestIncomingWebhook(object):
    def test_positive(self):
        webhook = pingen2sdk.IncomingWebhook.construct_event(
            b'{"data":{"type":"webhook_issues","id":"309a31e0-1abe-4034-8e7e-1fd473a802fd","attributes":{"reason":"Page limit exceeded","url":"https:\\/\\/5f2e-5-173-206-46.ngrok-free.app\\/webhook","created_at":"2024-04-26T09:54:38+0200"},"relationships":{"organisation":{"links":{"related":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"},"data":{"type":"organisations","id":"b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"}},"letter":{"links":{"related":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf\\/letters\\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"},"data":{"type":"letters","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}},"event":{"data":{"type":"letters_events","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}}}},"included":[{"type":"organisations","id":"b85c7b52-debb-4b15-b5db-d86b0a4e9bbf","attributes":{"name":"FreshCompany","status":"active","plan":"free","billing_mode":"prepaid","billing_currency":"CHF","billing_balance":569.72,"missing_credits":0,"edition":"pingen","default_country":"DE","default_address_position":"left","default_sender_address":"Test","data_retention_addresses":6,"data_retention_pdf":1,"limits_monthly_letters_count":null,"color":"#8B27F0","flags":["legitimate","letters_submitted"],"created_at":"2022-01-20T09:00:02+0100","updated_at":"2024-02-28T08:11:46+0100"},"links":{"self":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"}},{"type":"letters","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","attributes":{"status":"invalid","file_original_name":"example.pdf","file_pages":1,"address":"Example\\nExampleStreet 8\\n3000 City","address_position":"right","country":"CH","delivery_product":"postag_b","print_mode":"simplex","print_spectrum":"color","price_currency":"CHF","price_value":1.63,"paper_types":["normal"],"fonts":[{"name":"ArialMT","is_embedded":true}],"source":null,"tracking_number":null,"sender_address":null,"submitted_at":null,"created_at":"2024-04-26T09:54:38+0200","updated_at":"2024-04-26T09:54:38+0200"},"links":{"self":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf\\/letters\\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}},{"type":"letters_events","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","attributes":{"code":"file_too_many_pages","name":"Page limit exceeded","producer":"Pingen","location":"","has_image":false,"data":[],"emitted_at":"2024-04-26T09:54:38+0200","created_at":"2024-04-26T09:54:38+0200","updated_at":"2024-04-26T09:54:38+0200"}}]}',
            {
                "Content-Type": "application/vnd.api+json",
                "Signature": "36ce106d7bc8c59c039e57d21c127f6942bc0af33bb94c85594cc05b769354a2",
            },
            "webhook_test_secret123",
        )

        assert webhook.data["data"]["type"] == "webhook_issues"

    def test_missing_signature_header(self):
        with pytest.raises(pingen2sdk.error.WebhookSignatureException):
            pingen2sdk.IncomingWebhook.construct_event(
                b'{"data":{"type":"webhook_issues","id":"309a31e0-1abe-4034-8e7e-1fd473a802fd","attributes":{"reason":"Page limit exceeded","url":"https:\\/\\/5f2e-5-173-206-46.ngrok-free.app\\/webhook","created_at":"2024-04-26T09:54:38+0200"},"relationships":{"organisation":{"links":{"related":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"},"data":{"type":"organisations","id":"b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"}},"letter":{"links":{"related":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf\\/letters\\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"},"data":{"type":"letters","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}},"event":{"data":{"type":"letters_events","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}}}},"included":[{"type":"organisations","id":"b85c7b52-debb-4b15-b5db-d86b0a4e9bbf","attributes":{"name":"FreshCompany","status":"active","plan":"free","billing_mode":"prepaid","billing_currency":"CHF","billing_balance":569.72,"missing_credits":0,"edition":"pingen","default_country":"DE","default_address_position":"left","default_sender_address":"Test","data_retention_addresses":6,"data_retention_pdf":1,"limits_monthly_letters_count":null,"color":"#8B27F0","flags":["legitimate","letters_submitted"],"created_at":"2022-01-20T09:00:02+0100","updated_at":"2024-02-28T08:11:46+0100"},"links":{"self":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"}},{"type":"letters","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","attributes":{"status":"invalid","file_original_name":"example.pdf","file_pages":1,"address":"Example\\nExampleStreet 8\\n3000 City","address_position":"right","country":"CH","delivery_product":"postag_b","print_mode":"simplex","print_spectrum":"color","price_currency":"CHF","price_value":1.63,"paper_types":["normal"],"fonts":[{"name":"ArialMT","is_embedded":true}],"source":null,"tracking_number":null,"sender_address":null,"submitted_at":null,"created_at":"2024-04-26T09:54:38+0200","updated_at":"2024-04-26T09:54:38+0200"},"links":{"self":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf\\/letters\\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}},{"type":"letters_events","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","attributes":{"code":"file_too_many_pages","name":"Page limit exceeded","producer":"Pingen","location":"","has_image":false,"data":[],"emitted_at":"2024-04-26T09:54:38+0200","created_at":"2024-04-26T09:54:38+0200","updated_at":"2024-04-26T09:54:38+0200"}}]}',
                {
                    "Content-Type": "application/vnd.api+json",
                },
                "webhook_test_secret",
            )

    def test_webhook_signature_matching_failed(self):
        with pytest.raises(pingen2sdk.error.WebhookSignatureException):
            pingen2sdk.IncomingWebhook.construct_event(
                b'{"data":{"type":"webhook_issues","id":"309a31e0-1abe-4034-8e7e-1fd473a802fd","attributes":{"reason":"Page limit exceeded","url":"https:\\/\\/5f2e-5-173-206-46.ngrok-free.app\\/webhook","created_at":"2024-04-26T09:54:38+0200"},"relationships":{"organisation":{"links":{"related":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"},"data":{"type":"organisations","id":"b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"}},"letter":{"links":{"related":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf\\/letters\\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"},"data":{"type":"letters","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}},"event":{"data":{"type":"letters_events","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}}}},"included":[{"type":"organisations","id":"b85c7b52-debb-4b15-b5db-d86b0a4e9bbf","attributes":{"name":"FreshCompany","status":"active","plan":"free","billing_mode":"prepaid","billing_currency":"CHF","billing_balance":569.72,"missing_credits":0,"edition":"pingen","default_country":"DE","default_address_position":"left","default_sender_address":"Test","data_retention_addresses":6,"data_retention_pdf":1,"limits_monthly_letters_count":null,"color":"#8B27F0","flags":["legitimate","letters_submitted"],"created_at":"2022-01-20T09:00:02+0100","updated_at":"2024-02-28T08:11:46+0100"},"links":{"self":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf"}},{"type":"letters","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","attributes":{"status":"invalid","file_original_name":"example.pdf","file_pages":1,"address":"Example\\nExampleStreet 8\\n3000 City","address_position":"right","country":"CH","delivery_product":"postag_b","print_mode":"simplex","print_spectrum":"color","price_currency":"CHF","price_value":1.63,"paper_types":["normal"],"fonts":[{"name":"ArialMT","is_embedded":true}],"source":null,"tracking_number":null,"sender_address":null,"submitted_at":null,"created_at":"2024-04-26T09:54:38+0200","updated_at":"2024-04-26T09:54:38+0200"},"links":{"self":"https:\\/\\/api-integration.pingen.com\\/organisations\\/b85c7b52-debb-4b15-b5db-d86b0a4e9bbf\\/letters\\/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}},{"type":"letters_events","id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","attributes":{"code":"file_too_many_pages","name":"Page limit exceeded","producer":"Pingen","location":"","has_image":false,"data":[],"emitted_at":"2024-04-26T09:54:38+0200","created_at":"2024-04-26T09:54:38+0200","updated_at":"2024-04-26T09:54:38+0200"}}]}',
                {
                    "Content-Type": "application/vnd.api+json",
                    "Signature": "wrong99999999999999999999999999999999999999999999999999signature",
                },
                "webhook_test_secret",
            )
