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