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