Replace deprecated variables in csit/libraries (2).
[integration/test.git] / csit / libraries / AAAKeywords.robot
1 *** Settings ***
2 Library           RequestsLibrary
3 Variables         ../variables/Variables.py
4
5 *** Variables ***
6 ${WORKSPACE}      /tmp
7 ${BUNDLEFOLDER}    distribution-karaf-0.3.0-SNAPSHOT
8 ${AUTHN_CFG_FILE}    ${WORKSPACE}/${BUNDLEFOLDER}/etc/org.opendaylight.aaa.authn.cfg
9
10 *** Keywords ***
11 AAA Login
12     [Arguments]    ${controller_ip}    ${auth_data}
13     [Documentation]    Makes a POST REST call to the AUTH_TOKEN_API with the given auth_data and returns the response
14     Create Session    ODL_SESSION    http://${controller_ip}:8181
15     ${headers}=    Create Dictionary    Content-Type=application/x-www-form-urlencoded
16     ${resp}=    RequestsLibrary.POST Request    ODL_SESSION    ${AUTH_TOKEN_API}    data=${auth_data}    headers=${headers}
17     Delete All Sessions
18     [Return]    ${resp}
19
20 Create Auth Data
21     [Arguments]    ${user}=${USER}    ${password}=${PWD}    ${scope}=${SCOPE}    ${client_id}=${EMPTY}    ${client_secret}=${EMPTY}
22     [Documentation]    returns a string in the direct authentacation format (e.g., grant_type=password&username=admin&password=admin).
23     ...    It can also be passed scope, client_id and client_secret arguments for the case of client specific authorization
24     ${data}=    Set Variable    grant_type=password&username=${user}&password=${password}&scope=${scope}
25     ${data}=    Run Keyword If    "${client_id}" != "${EMPTY}"    Set Variable    ${data}&client_id=${client_id}
26     ...    ELSE    Set Variable    ${data}
27     ${data}=    Run Keyword If    "${client_secret}" != "${EMPTY}"    Set Variable    ${data}&client_secret=${client_secret}
28     ...    ELSE    Set Variable    ${data}
29     [Return]    ${data}
30
31 Disable Authentication On Controller
32     [Arguments]    ${controller_ip}
33     [Documentation]    Will disable token based authentication. Currently, that is done with a config file change
34     SSHLibrary.Open Connection    ${controller_ip}
35     Login With Public Key    ${ODL_SYSTEM_USER}    ${USER_HOME}/.ssh/${SSH_KEY}    any
36     ${cmd}=    Set Variable    sed -i 's/^authEnabled=.*$/authEnabled=false/g' ${AUTHN_CFG_FILE}
37     SSHLibrary.Execute Command    ${cmd}
38     SSHLibrary.Close Connection
39
40 Enable Authentication On Controller
41     [Arguments]    ${controller_ip}
42     [Documentation]    Will enable token based authentication. Currently, that is done with a config file change
43     SSHLibrary.Open Connection    ${controller_ip}
44     Login With Public Key    ${ODL_SYSTEM_USER}    ${USER_HOME}/.ssh/${SSH_KEY}    any
45     ${cmd}=    Set Variable    sed -i 's/^authEnabled=.*$/authEnabled=true/g' ${AUTHN_CFG_FILE}
46     SSHLibrary.Execute Command    ${cmd}
47     SSHLibrary.Close Connection
48
49 Get Auth Token
50     [Arguments]    ${user}=${USER}    ${password}=${PWD}    ${scope}=${SCOPE}    ${client_id}=${EMPTY}    ${client_secret}=${EMPTY}
51     [Documentation]    Wrapper used to login to controller and retrieve an auth token. Optional argumented available for client based credentials.
52     ${auth_data}=    Create Auth Data    ${USER}    ${PWD}    ${scope}    ${client_id}    ${client_secret}
53     ${resp}=    AAA Login    ${ODL_SYSTEM_IP}    ${auth_data}
54     Should Be Equal As Strings    ${resp.status_code}    201
55     ${auth_token}=    Extract Value From Content    ${resp.content}    /access_token    strip
56     [Return]    ${auth_token}
57
58 Revoke Auth Token
59     [Arguments]    ${token}
60     [Documentation]    Requests the given token be revoked via POST to ${REVOKE_TOKEN_API}
61     ${headers}=    Create Dictionary    Content-Type=application/x-www-form-urlencoded
62     ${resp}=    RequestsLibrary.POST Request    ODL_SESSION    ${REVOKE_TOKEN_API}    data=${token}    headers=${headers}
63     Should Be Equal As Strings    ${resp.status_code}    204
64
65 Validate Token Format
66     [Arguments]    ${token}
67     [Documentation]    Validates the given string is in the proper "token" format
68     Should Match Regexp    ${token}    [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
69
70 Get User From IDM DB
71     [Arguments]    ${user_id}=${EMPTY}
72     [Documentation]    Will return user information. If no user id is passed, it will retrieve all users in DB
73     Create Session    httpbin    http://${ODL_SYSTEM_IP}:${RESTPORT}
74     ${headers}=    Create Dictionary    Content-Type=application/x-www-form-urlencoded
75     ${resp}=    RequestsLibrary.GET Request    httpbin    ${idmurl}/users/${user_id}    headers=${headers}
76     Should Be Equal As Strings    ${resp.status_code}    200
77     Log    ${resp.content}
78     [Return]    ${resp}
79
80 Create User
81     [Arguments]    ${user_data}
82     [Documentation]    Will return user information. If no user id is passed, it will retrieve all users in DB
83     Create Session    httpbin    http://${ODL_SYSTEM_IP}:${RESTPORT}
84     ${headers}=    Create Dictionary    Content-Type=application/json
85     ${resp}=    RequestsLibrary.POST Request    httpbin    ${idmurl}/users    headers=${headers}    data=${user_data}
86     Should Be Equal As Strings    ${resp.status_code}    201
87     Log    ${resp.content}
88     [Return]    ${resp}