bea87c4d95a25b6e07525f298105ee82d6500320
[releng.git] / utils / test / testapi / opnfv_testapi / ui / auth / base.py
1 import random
2 import string
3
4 from six.moves.urllib import parse
5
6 from opnfv_testapi.resources import handlers
7
8
9 class BaseHandler(handlers.GenericApiHandler):
10     def __init__(self, application, request, **kwargs):
11         super(BaseHandler, self).__init__(application, request, **kwargs)
12         self.table = 'users'
13
14     def set_cookies(self, cookies):
15         for cookie_n, cookie_v in cookies:
16             self.set_secure_cookie(cookie_n, cookie_v)
17
18
19 def get_token(length=30):
20     """Get random token."""
21     return ''.join(random.choice(string.ascii_lowercase)
22                    for i in range(length))
23
24
25 def set_query_params(url, params):
26     """Set params in given query."""
27     url_parts = parse.urlparse(url)
28     url = parse.urlunparse((
29         url_parts.scheme,
30         url_parts.netloc,
31         url_parts.path,
32         url_parts.params,
33         parse.urlencode(params),
34         url_parts.fragment))
35     return url