Skip to content

Commit 1bc2a6d

Browse files
committed
New unit test logic
1 parent 44b9505 commit 1bc2a6d

1 file changed

Lines changed: 85 additions & 2 deletions

File tree

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

0 commit comments

Comments
 (0)