1+ import unittest
2+ from uid2_client import Uid2Client
3+ import datetime as dt
4+ import base64
5+
6+
7+ def _make_dt (timestamp ):
8+ return dt .datetime .fromtimestamp (timestamp , tz = dt .timezone .utc )
9+
10+
11+ class TestKeyParse (unittest .TestCase ):
12+ def test_key_parse (self ):
13+ s = "{ \" body\" : { " + \
14+ "\" caller_site_id\" : 11, " + \
15+ "\" master_keyset_id\" : 1, " + \
16+ "\" default_keyset_id\" : 99999, " + \
17+ "\" token_expiry_seconds\" : 1728000, " + \
18+ "\" keys\" : [ " + \
19+ "{ " + \
20+ "\" id\" : 3, " + \
21+ "\" keyset_id\" : 99999, " + \
22+ "\" created\" : 1609459200, " + \
23+ "\" activates\" : 1609459210, " + \
24+ "\" expires\" : 1893456000, " + \
25+ "\" secret\" : \" o8HsvkwJ5Ulnrd0uui3GpukpwDapj+JLqb7qfN/GJKo=\" " + \
26+ "}, " + \
27+ "{ " + \
28+ "\" id\" : 2, " + \
29+ "\" keyset_id\" : 1, " + \
30+ "\" created\" : 1609458200, " + \
31+ "\" activates\" : 1609459220, " + \
32+ "\" expires\" : 1893457000, " + \
33+ "\" secret\" : \" DD67xF8OFmbJ1/lMPQ6fGRDbJOT4kXErrYWcKdFfCUE=\" " + \
34+ "} " + \
35+ "] " + \
36+ "}, " + \
37+ "\" status\" : \" success\" }"
38+
39+ client = Uid2Client ("ep" , "ak" , "ioG3wKxAokmp+rERx6A4kM/13qhyolUXIu14WN16Spo=" )
40+
41+ now = dt .datetime .now (tz = dt .timezone .utc )
42+
43+ key_containter = client .refresh_json (s )
44+
45+ self .assertEqual (11 , key_containter .get_caller_site_id ())
46+ masterKey = key_containter .get_master_key (now )
47+ self .assertEqual (2 , masterKey .key_id )
48+
49+ default_key = key_containter .get_default_keyset_key (now )
50+ self .assertEqual (3 , default_key .key_id )
51+ self .assertEqual (1728000 , key_containter .get_token_expiry_seconds ())
52+
53+ key = key_containter .get (3 )
54+ self .assertEqual (99999 , key .keyset_id )
55+ self .assertEqual (_make_dt (1609459200 ), key .created )
56+ self .assertEqual (_make_dt (1609459210 ), key .activates )
57+ self .assertEqual (_make_dt (1893456000 ), key .expires )
58+ self .assertEqual ("o8HsvkwJ5Ulnrd0uui3GpukpwDapj+JLqb7qfN/GJKo=" , base64 .b64encode (key .secret ).decode ("ascii" ))
59+
60+ key = key_containter .get (2 )
61+ self .assertEqual (1 , key .keyset_id )
62+ self .assertEqual (_make_dt (1609458200 ), key .created )
63+ self .assertEqual (_make_dt (1609459220 ), key .activates )
64+ self .assertEqual (_make_dt (1893457000 ), key .expires )
65+ self .assertEqual ("DD67xF8OFmbJ1/lMPQ6fGRDbJOT4kXErrYWcKdFfCUE=" , base64 .b64encode (key .secret ).decode ("ascii" ))
66+
67+ def test_parse_key_error (self ):
68+ client = Uid2Client ("ep" , "ak" , "ioG3wKxAokmp+rERx6A4kM/13qhyolUXIu14WN16Spo=" )
69+ self .assertRaises (BaseException , client .refresh_json , "{\" status\" : \" error\" }" )
70+ self .assertRaises (BaseException , client .refresh_json , "{\" body\" : \" error\" }" )
71+ self .assertRaises (BaseException , client .refresh_json , "{\" body\" : [1, 2, 3]}" )
72+ self .assertRaises (BaseException , client .refresh_json , "{\" body\" : [{}]}" )
73+ self .assertRaises (BaseException , client .refresh_json , "{\" body\" : [{\" id\" : \" test\" }]}" )
74+ self .assertRaises (BaseException , client .refresh_json , "{\" body\" : [{\" id\" : 5}]}" )
0 commit comments