#!/usr/bin/env python
from __future__ import print_function

import pytest
from ticket_parser2 import (
    BlackboxEnv,
    Status,
    UserTicket,
)
from ticket_parser2.low_level import UserContext
from ticket_parser2.exceptions import (
    EmptyTvmKeysException,
    MalformedTvmKeysException,
    TicketParsingException,
)
import ticket_parser2.unittest as tp2u


EMPTY_TVM_KEYS = (
    '1:EpUBCpIBCAYQABqHATCBhAKBgQCoZkFGm9oLTqjeXZAq6j5S6i7K20V0lNdBBLqfmFBIRuTkYxhs4vUYnWjZrKRAd5bp6_p'
    'y0csmFmpl_5Yh0b-2pdo_E5PNP7LGRzKyKSiFddyykKKzVOazH8YYldDAfE8Z5HoS9e48an5JsPg0jr-TPu34DnJq3yv2a6dq'
    'iKL9zSCakQY'
)
EXPIRED_USER_TICKET = (
    '3:user:CA0QABokCgMIyAMKAgh7EMgDGghiYjpzZXNzMRoIYmI6c2VzczIgEigB:D0CmYVwWg91LDYejjeQ2UP8AeiA_mr1q1'
    'CUD_lfJ9zQSEYEOYGDTafg4Um2rwOOvQnsD1JHM4zHyMUJ6Jtp9GAm5pmhbXBBZqaCcJpyxLTEC8a81MhJFCCJRvu_G1FiAgR'
    'gB25gI3HIbkvHFUEqAIC_nANy7NFQnbKk2S-EQPGY'
)
MALFORMED_TVM_KEYS = (
    '1:CpgCCpMCCAEQABqIAjCCAQQCggEAcLEXeH67FQESFUn4_7wnX7wN0PUrBoUsm3QQ4W5vC-qz6sXaEjSwnTV8w1o-z6X9KPL'
    'lhzMQvuS38NCNfK4uvJ4Zvfp3YsXJ25-rYtbnrYJHNvHohD-kPCCw_yZpMp21JdWigzQGuV7CtrxUhF-NNrsnUaJrE5-OpEWN'
    't4X6nCItKIYeVcSK6XJUbEWbrNCRbvkSc4ak2ymFeMuHYJVjxh4eQbk7_ZPzodP0WvF6eUYrYeb42imVEOR8ofVLQWE5DVnb1'
    'z_TqZm4i1XkS7jMwZuBxBRw8DGdYei0lT_sAf7KST2jC0590NySB3vsBgWEVs1OdUUWA6r-Dvx9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
    'Gm9oLTqjeXZAq6j5S6i7K20V0lNdBBLqfmFBIRuTkYxhs4vUYnWjZrKRAd5bp6_py0csmFmpl_5Yh0b-2pdo_E5PNP7LGRzKy'
    'KSiFddyykKKzVOazH8YYldDAfE8Z5HoS9e48an5JsPg0jr-TPu34DnJq3yv2a6dqiKL9zSCakQYSlQEKkgEIEBAAGocBMIGEA'
    'oGBALhrihbf3EpjDQS2sCQHazoFgN0nBbE9eesnnFTfzQELXb2gnJU9enmV_aDqaHKjgtLIPpCgn40lHrn5k6mvH5OdedyI6c'
    'CzE-N-GFp3nAq0NDJyMe0fhtIRD__CbT0ulcvkeow65ubXWfw6dBC2gR_34rdMe_L_TGRLMWjDULbNIJ'
)
MALFORMED_USER_TICKET = (
    '3:user:CA0Q__________9_GiQKAwjIAwoCCHsQyAMaCGJiOnNlc3MxcXn9krYk19LCvlFrhMW-R4q8mKfXJXCd-RBVBgUQzC'
    'OR1Dx2FiOyU-BxUoIsaU0PiwTjbVY5I2onJDilge70Cl5zEPI9pfab2qwklACq_ZBUvD1tzrfNUr88otBGAziHASJWgyVDkhy'
    'Q3p7YbN38qpb0vGQrYNxlk4e2I'
)
SIGN_BROKEN_USER_TICKET = (
    '3:user:CA0Q__________9_GiQKAwjIAwoCCHsQyAMaCGJiOnNlc3MxGghiYjpzZXNzMiASKAE:KJFv5EcXn9krYk19LCvlFr'
    'hMW-R4q8mKfXJXCd-RBVBgUQzCOR1Dx2FiOyU-BxUoIsaU0PiwI2onJDilge70Cl5zEPI9pfab2qwklACq_ZBUvD1tzrfNUr8'
    '8otBGAziHASJWgyVDkhyQ3p7YbN38qpb0vGQrYNxlk4e2'
)
UNSUPPORTED_VERSION_USER_TICKET = (
    '2:user:CA0Q__________9_GiQKAwjIAwoCCHsQyAMaCGJiOnNlc3MxGghiYjpzZXNzMiASKAE:KJFv5EcXn9krYk19LCvlFr'
    'hMW-R4q8mKfXJXCd-RBVBgUQzCOR1Dx2FiOyU-BxUoIsaU0PiwTjbVY5I2onJDilge70Cl5zEPI9pfab2qwklACq_ZBUvD1tz'
    'rfNUr88otBGAziHASJWgyVDkhyQ3p7YbN38qpb0vGQrYNxlk4e2I'
)
VALID_SERVICE_TICKET = (
    '3:serv:CBAQ__________9_IhkI5QEQHBoIYmI6c2VzczEaCGJiOnNlc3My:WUPx1cTf05fjD1exB35T5j2DCHWH1YaLJon_a'
    '4rN-D7JfXHK1Ai4wM4uSfboHD9xmGQH7extqtlEk1tCTCGm5qbRVloJwWzCZBXo3zKX6i1oBYP_89WcjCNPVe1e8jwGdLsnu6'
    'PpxL5cn0xCksiStILH5UmDR6xfkJdnmMG94o8'
)
VALID_USER_TICKET_1 = (
    '3:user:CA0Q__________9_GiQKAwjIAwoCCHsQyAMaCGJiOnNlc3MxGghiYjpzZXNzMiASKAE:KJFv5EcXn9krYk19LCvlFr'
    'hMW-R4q8mKfXJXCd-RBVBgUQzCOR1Dx2FiOyU-BxUoIsaU0PiwTjbVY5I2onJDilge70Cl5zEPI9pfab2qwklACq_ZBUvD1tz'
    'rfNUr88otBGAziHASJWgyVDkhyQ3p7YbN38qpb0vGQrYNxlk4e2I'
)
VALID_USER_TICKET_SIGNLESS_1 = '3:user:CA0Q__________9_GiQKAwjIAwoCCHsQyAMaCGJiOnNlc3MxGghiYjpzZXNzMiASKAE:'
VALID_USER_TICKET_2 = (
    '3:user:CA0Q__________9_GhAKAwjIAwoCCHsQyAMgEigB:KRibGYTJUA2ns0Fn7VYqeMZ1-GdscB1o9pRzELyr7QJrJsfsE'
    '8Y_HoVvB8Npr-oalv6AXOpagSc8HpZjAQz8zKMAVE_tI0tL-9DEsHirpawEbpy7OWV7-k18o1m-RaDaKeTlIB45KHbBul1-9a'
    'eKkortBfbbXtz_Qy9r_mfFPiQ'
)
VALID_USER_TICKET_3 = (
    '3:user:CA0Q__________9_Go8bCgIIAAoCCAEKAggCCgIIAwoCCAQKAggFCgIIBgoCCAcKAggICgIICQoCCAoKAggLCgIIDA'
    'oCCA0KAggOCgII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jka'
    'CGJiOnNlc3M3GgliYjpzZXNzNzAaCWJiOnNlc3M3MRoJYmI6c2VzczcyGgliYjpzZXNzNzMaCWJiOnNlc3M3NBoJYmI6c2Vzc'
    'zc1GgliYjpzZXNzNzYaCWJiOnNlc3M3NxoJYmI6c2Vzczc4GgliYjpzZXNzNzkaCGJiOnNlc3M4GgliYjpzZXNzODAaCWJiOn'
    'Nlc3M4MRoJYmI6c2VzczgyGgliYjpzZXNzODMaCWJiOnNlc3M4NBoJYmI6c2Vzczg1GgliYjpzZXNzODYaCWJiOnNlc3M4Nxo'
    'JYmI6c2Vzczg4GgliYjpzZXNzODkaCGJiOnNlc3M5GgliYjpzZXNzOTAaCWJiOnNlc3M5MRoJYmI6c2VzczkyGgliYjpzZXNz'
    'OTMaCWJiOnNlc3M5NBoJYmI6c2Vzczk1GgliYjpzZXNzOTYaCWJiOnNlc3M5NxoJYmI6c2Vzczk4GgliYjpzZXNzOTkgEigB:'
    'CX8PIOrxJnQqFXl7wAsiHJ_1VGjoI-asNlCXb8SE8jtI2vdh9x6CqbAurSgIlAAEgotVP-nuUR38x_a9YJuXzmG5AvJ458apW'
    'QtODHIDIX6ZaIwMxjS02R7S5LNqXa0gAuU_R6bCWpZdWe2uLMkdpu5KHbDgW08g-uaP_nceDOk'
)


def test_context():
    user_context = UserContext(BlackboxEnv.Test, tp2u.TVMKNIFE_PUBLIC_KEYS)
    user_context.reset_keys(tp2u.TVMKNIFE_PUBLIC_KEYS)


def test_context_exceptions():
    with pytest.raises(MalformedTvmKeysException):
        UserContext(BlackboxEnv.Test, MALFORMED_TVM_KEYS)
    with pytest.raises(EmptyTvmKeysException):
        UserContext(BlackboxEnv.Stress, EMPTY_TVM_KEYS)


def test_ticket():
    user_context = UserContext(BlackboxEnv.Test, tp2u.TVMKNIFE_PUBLIC_KEYS)
    ticket = user_context.check(VALID_USER_TICKET_1)
    assert ticket.scopes == ['bb:sess1', 'bb:sess2']
    assert ticket.has_scope('bb:sess1')
    assert ticket.has_scope('bb:sess2')
    assert not ticket.has_scope('bb:sess3')
    assert ticket.uids == [456, 123]
    assert ticket.default_uid == 456
    assert (
        ticket.debug_info()
        == 'ticket_type=user;expiration_time=9223372036854775807;scope=bb:sess1;scope=bb:sess2;default_uid=456;uid=456;uid=123;env=Test;'
    )
    assert VALID_USER_TICKET_SIGNLESS_1 == UserTicket.remove_signature(VALID_USER_TICKET_1)
    assert (
        repr(ticket)
        == 'ticket_type=user;expiration_time=9223372036854775807;scope=bb:sess1;scope=bb:sess2;default_uid=456;uid=456;uid=123;env=Test;'
    )
    assert (
        str(ticket)
        == 'ticket_type=user;expiration_time=9223372036854775807;scope=bb:sess1;scope=bb:sess2;default_uid=456;uid=456;uid=123;env=Test;'
    )


def test_ticket_exceptions():
    user_context = UserContext(BlackboxEnv.Test, tp2u.TVMKNIFE_PUBLIC_KEYS)
    with pytest.raises(TicketParsingException) as ex:
        user_context.check(SIGN_BROKEN_USER_TICKET)
    assert ex.value.status == Status.SignBroken
    assert (
        ex.value.debug_info
        == 'ticket_type=user;expiration_time=9223372036854775807;scope=bb:sess1;scope=bb:sess2;default_uid=456;uid=456;uid=123;env=Test;'
    )

    with pytest.raises(TicketParsingException) as ex:
        user_context.check(MALFORMED_USER_TICKET)
    assert ex.value.status == Status.Malformed
    assert ex.value.debug_info == 'status=malformed;'

    with pytest.raises(TicketParsingException) as ex:
        user_context.check(VALID_SERVICE_TICKET)
    assert ex.value.status == Status.InvalidTicketType
    assert ex.value.debug_info == 'ticket_type=not-user;'

    user_context = UserContext(BlackboxEnv.Prod, tp2u.TVMKNIFE_PUBLIC_KEYS)
    with pytest.raises(TicketParsingException) as ex:
        user_context.check(VALID_USER_TICKET_1)
    assert ex.value.status == Status.InvalidBlackboxEnv


def test_create_ticket_for_tests():
    with pytest.raises(TicketParsingException):
        tp2u.create_user_ticket_for_unittest(Status.Expired, 42, ['ololo', 'abc'])
    u = tp2u.create_user_ticket_for_unittest(Status.Ok, 42, ['ololo', 'abc'], [23, 56])
    assert u
    assert u.default_uid == 42
    assert u.scopes == ['abc', 'ololo']
    assert u.uids == [23, 42, 56]
    assert u.debug_info() == 'ticket_type=user;scope=abc;scope=ololo;default_uid=42;uid=23;uid=42;uid=56;env=Test;'

    with pytest.raises(Exception):
        tp2u.create_user_ticket_for_unittest(Status.Ok, 0)
