summaryrefslogtreecommitdiff
path: root/tests/test_core.py
blob: 0c71a628c39662db12a18de49494c08bf1fff3c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pytest

from viewstate import *


class TestViewState(object):

    def test_blank(self):
        vs = ViewState()
        assert not vs.is_valid()

    def test_is_valid(self):
        for s in ['ngcs', 'mot']:
            with open(f'tests/samples/{s}.sample', 'r') as f:
                vs = ViewState(f.read())
                assert vs.is_valid() is True

    def test_invalid_base64(self):
        with pytest.raises(ViewStateException):
            vs = ViewState('hello')

    def test_invalid_decode(self):
        with pytest.raises(ViewStateException):
            vs = ViewState(raw=b'\x01\x02')
            vs.decode()

    def test_parse_int_value(self):
        vs = ViewState(raw=b'\xff\x01\x02\x88\x01')
        assert vs.decode() == 136

    def test_parse_const_value(self):
        vs = ViewState(raw=b'\xff\x01\x67')
        assert vs.decode() is True

    def test_parse_string_value(self):
        s = 'abcdefghij'
        vs = ViewState(raw=b'\xff\x01\x05\x0a' + s.encode())
        assert vs.decode() == s

    def test_parse_simple_dict(self):
        vs = ViewState(raw=b'\xff\x01\x18\x02\x05\x01a\x05\x01b\x05\x01c\x05\x01d')
        assert vs.decode() == {'a': 'b', 'c': 'd'}

    def test_parse_simple_list(self):
        vs = ViewState(raw=b'\xff\x01\x16\x05\x05\x01a\x05\x01b\x05\x01c\x05\x01d\x05\x01e')
        assert vs.decode() == ['a', 'b', 'c', 'd', 'e']

    def test_parse_simple_pair(self):
        vs = ViewState(raw=b'\xff\x01\x0f\x67\x68')
        assert vs.decode() == (True, False)

    def test_parse_complex_pair(self):
        vs = ViewState()
        vs.raw = b'\xff\x01\x0f\x67\x0f\x68\x66'
        assert vs.decode() == (True, (False, 0))
        vs.raw = b'\xff\x01\x0f\x0f\x67\x68\x66'
        assert vs.decode() == ((True, False), 0)
        vs.raw = b'\xff\x01\x0f\x0f\x67\x05\x061q2w3e\x66'
        assert vs.decode() == ((True, '1q2w3e'), 0)

    def test_parse_unknown(self):
        with pytest.raises(ViewStateException):
            vs = ViewState(raw=b'\xff\x01\x99\x99\x99')
            assert vs.decode()