summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/identity.py
blob: 9101090e3fd9da3dfa0bffeb078ed296d4388171 (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
import functools
import os
import urllib

import requests
from testapiclient import user


def _authenticate(username, password):
    session = requests.Session()
    hostname = '{}{}{}'.format(
        os.environ.get('testapi_cas_auth_url'),
        urllib.quote(os.environ.get('testapi_url')),
        os.environ.get('testapi_cas_signin_return'))
    data = {'name': username, 'pass': password, 'form_id': 'user_login'}
    response = session.post(hostname, data)
    user.User.session = session
    return response


def authenticate(xstep):
    @functools.wraps(xstep)
    def wrapper(self, parsed_args):
        username = parsed_args.u
        password = parsed_args.p
        if(username and password):
            response = _authenticate(username, password)
            if "login" in response.text:
                print "Authentication has failed."
            else:
                xstep(self, parsed_args)
    return wrapper