Skip to content

Commit 183e0e1

Browse files
committed
New unit test logic
1 parent 3729c62 commit 183e0e1

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
Unit tests for REST/Auth.py module
3+
Author: Valentin Kuznetsov <vkuznet [AT] gmail [DOT] com>
4+
"""
5+
6+
# system modules
7+
import logging
8+
import unittest
9+
10+
# third party modules
11+
import cherrypy
12+
13+
# WMCore modules
14+
from WMCore.REST.Auth import user_info_from_headers
15+
16+
17+
class RESTAuthTest(unittest.TestCase):
18+
"Unit test for RESTAuth module"
19+
20+
def setUp(self):
21+
"""
22+
setup necessary data for unit test usage
23+
"""
24+
self.logger = logging.getLogger('rest_auth')
25+
26+
# REST/Auth.py module calculates checksum based on provided hmac
27+
# for instnace for self.hmac we will expect self.chkSum
28+
# with all CMS headers
29+
30+
# let's create set of different conditions
31+
self.user_groups = []
32+
self.hmacs = []
33+
self.chkSums = []
34+
35+
# for group:users group:admin
36+
user_groups = 'group:users group:admin'
37+
hmac = '169d02b96265caf05894b526f99a22549dcd38ed'
38+
chkSum = 'd357b299194ec4bed4e4fc73fc9ceab10139c16f'
39+
self.user_groups.append(user_groups)
40+
self.hmacs.append(hmac)
41+
self.chkSums.append(chkSum)
42+
43+
# let's reverse order of user groups
44+
user_groups = 'group:admin group:users'
45+
self.user_groups.append(user_groups)
46+
self.hmacs.append(hmac)
47+
self.chkSums.append(chkSum)
48+
49+
# for group:admin group:users iam_group:test site:T1_XX_YYYY
50+
user_groups = 'group:admin group:users iam_group:test site:T1_XX_YYYY'
51+
hmac = '57ea0f58134aa079972da30a8fc2bf81853c949b'
52+
chkSum = 'd357b299194ec4bed4e4fc73fc9ceab1013'
53+
self.user_groups.append(user_groups)
54+
self.hmacs.append(hmac)
55+
self.chkSums.append(chkSum)
56+
57+
def testRESTAuth(self):
58+
"test RESTAuth methods"
59+
for idx in range(len(self.user_groups)):
60+
user_groups = self.user_groups[idx]
61+
hmac = self.hmacs[idx]
62+
chkSum = self.chkSums[idx]
63+
64+
# for testing purposes we need to setup cherrypy headers
65+
# cms-auth-status, cms-authn, cms-authz, cms-authn-hmac, cms-authn-name, cms-authn-dn
66+
cherrypy.request.headers = {
67+
'cms-auth-status': 'OK',
68+
'cms-authn': 'fake',
69+
'cms-authz-user': user_groups,
70+
'cms-authn-hmac': hmac,
71+
'cms-authn-name': 'test',
72+
'cms-authn-dn': 'dn'}
73+
74+
authzKey = bytes(chkSum, 'UTF-8')
75+
user = user_info_from_headers(key=authzKey)
76+
self.logger.info(f"user_groups {user_groups}")
77+
self.logger.info(f"hmac {hmac}")
78+
self.logger.info(f"chkSum {chkSum}")
79+
self.logger.info(f"user {user}")
80+
81+
82+
if __name__ == '__main__':
83+
unittest.main()

0 commit comments

Comments
 (0)