akas 2.3.0

AKAS: API Key Authorization Server
*** Settings ***
Documentation       Akas /load URL plain format test suite

Library             Collections
Library             OperatingSystem
Library             Process
Library             RequestsLibrary
Library             lib/ComplexRequests.py

Test Tags           core    cli


*** Variables ***
${BIN}                      release/akas
${PLAIN_KEY_FILE}           tests/files/plain_keys01.txt
${PLAIN_KEY_FILE_HASH}      5b06fe2ef2d191df07333aec5ea0c351bf5b2900cb6f7cd2f133fba22c34fd88
${PLAIN_KEY_FILE02}         tests/files/plain_keys02.txt
${PLAIN_KEY_FILE02_HASH}    5d007b8bb685862a47c6f438683109959745e64c45ee89425503a9516fc7a12c
${SHA256_KEY_FILE}          tests/files/sha256_keys01.txt
${SHA256_KEY_FILE_HASH}     43fcba0b3a5ab1b302f9d13617ae4eec6ae623a7fd52437dd992d5dca115e68d
${AKAS_WAIT}                1s
${HOST}                     http://localhost:5001
${URL}                      ${HOST}/load


*** Test Cases ***
Test AKAS /load URL: with plain format
    [Documentation]    Load test
    Given Start Process    ${BIN}    serve    --no-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XX-XXX
    And Check Auth Access    key=YY-XXX    status=401
    ${result} =    And Terminate Process    akas
    And Should Be Equal As Integers    ${result.rc}    0
    And Should Contain    ${result.stdout}    Lines count: 5 - Keys count: 5
    And Should Contain    ${result.stdout}    File metadata - hash
    ...    : ${PLAIN_KEY_FILE_HASH}
    And Should Contain    ${result.stdout}    File metadata - date:
    And Should Contain    ${result.stdout}    File metadata - keys count: 5
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with plain format and hash
    [Documentation]    Load test
    Given Start Process    ${BIN}    serve    --no-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain", "hash_input_file": "${PLAIN_KEY_FILE_HASH}"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XX-XXX
    And Check Auth Access    key=YY-XXX    status=401
    ${result} =    And Terminate Process    akas
    And Should Be Equal As Integers    ${result.rc}    0
    And Should Contain    ${result.stdout}    Lines count: 5 - Keys count: 5
    And Should Contain    ${result.stdout}    File metadata - hash
    ...    : ${PLAIN_KEY_FILE_HASH}
    And Should Contain    ${result.stdout}    File metadata - date:
    And Should Contain    ${result.stdout}    File metadata - keys count: 5
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with admin key, plain format and hash
    [Documentation]    Load test
    Given Start Process    ${BIN}    serve    --admin-key    my-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=fake-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain", "hash_input_file": "${PLAIN_KEY_FILE_HASH}"}
    Then Should Be Equal As Integers    ${response.status_code}    401
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain", "hash_input_file": "${PLAIN_KEY_FILE_HASH}"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XX-XXX
    And Check Auth Access    key=YY-XXX    status=401
    ${result} =    And Terminate Process    akas
    And Should Be Equal As Integers    ${result.rc}    0
    And Should Contain    ${result.stdout}    Lines count: 5 - Keys count: 5
    And Should Contain    ${result.stdout}    File metadata - hash
    ...    : ${PLAIN_KEY_FILE_HASH}
    And Should Contain    ${result.stdout}    File metadata - date:
    And Should Contain    ${result.stdout}    File metadata - keys count: 5
    [Teardown]    Kill Akas Server

Test AKAS /load URL: plain load twice
    [Documentation]    Load test
    Given Start Process    ${BIN}    serve    --no-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain", "hash_input_file": "${PLAIN_KEY_FILE_HASH}"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XX-XXX
    And Check Auth Access    key=ZZ-ZZZ    status=401
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE02}
    ...    json={"format": "plain", "hash_input_file": "${PLAIN_KEY_FILE02_HASH}"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Sleep    ${AKAS_WAIT}
    And Check Auth Access    key=XX-XXX    status=401
    And Check Auth Access    key=ZZ-ZZZ
    ${result} =    And Terminate Process    akas
    And Should Be Equal As Integers    ${result.rc}    0
    And Should Contain    ${result.stdout}    Lines count: 2 - Keys count: 2
    And Should Contain    ${result.stdout}    File metadata - hash
    ...    : ${PLAIN_KEY_FILE02_HASH}
    And Should Contain    ${result.stdout}    File metadata - date:
    And Should Contain    ${result.stdout}    File metadata - keys count: 2
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with plain format and wrong hash
    [Documentation]    Load test
    [Tags]    error
    Given Start Process    ${BIN}    serve    --no-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain", "hash_input_file": "${PLAIN_KEY_FILE02_HASH}"}
    Then Should Be Equal As Integers    ${response.status_code}    400
    And Check Auth Access    key=XX-XXX    status=401
    ${result} =    And Terminate Process    akas
    And Should Be Equal As Integers    ${result.rc}    0
    And Should Contain    ${result.stdout}    Hash mismatch - input hash:
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with plain format and wrong hash format
    [Documentation]    Load test
    [Tags]    error
    Given Start Process    ${BIN}    serve    --no-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain", "hash_input_file": "${PLAIN_KEY_FILE_HASH}a"}
    Then Should Be Equal As Integers    ${response.status_code}    400
    And Check Auth Access    key=XX-XXX    status=401
    ${result} =    And Terminate Process    akas
    And Should Be Equal As Integers    ${result.rc}    0
    And Should Contain    ${result.stdout}    is not a valid sha-256 hash
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with plain format and sha256 input file
    [Documentation]    Load test
    [Tags]    error
    Given Start Process    ${BIN}    serve    --no-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${SHA256_KEY_FILE}
    ...    json={"format": "plain", "hash_input_file": "${SHA256_KEY_FILE_HASH}"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XX-XXX    status=401
    ${result} =    And Terminate Process    akas
    And Should Be Equal As Integers    ${result.rc}    0
    And Should Contain    ${result.stdout}    Lines count: 5 - Keys count: 5
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with length check
    [Documentation]    Serve test
    When Start Process    ${BIN}    serve    --no-admin-key    --length    6    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XY-ZZZ
    And Check Auth Access    key=XX-ZZ    status=401
    ${result} =    And Terminate Process    akas
    And Should Contain    ${result.stdout}    Key length: 6
    And Should Contain    ${result.stdout}    Lines count: 5 - Keys count: 4
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with prefix check
    [Documentation]    Serve test
    When Start Process    ${BIN}    serve    --no-admin-key    --prefix    XX-    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XX-ZZ
    And Check Auth Access    key=XY-ZZZ    status=401
    ${result} =    And Terminate Process    akas
    Then Should Contain    ${result.stdout}    Key prefix: XX-
    And Should Contain    ${result.stdout}    Lines count: 5 - Keys count: 4
    [Teardown]    Kill Akas Server

Test AKAS /load URL: with length and prefix check
    [Documentation]    Serve test
    When Start Process    ${BIN}    serve    --no-admin-key    --length    6    --prefix    XX-    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${response} =    Post File Json
    ...    url=${URL}
    ...    key=my-admin-key
    ...    path=${PLAIN_KEY_FILE}
    ...    json={"format": "plain"}
    Then Should Be Equal As Integers    ${response.status_code}    200
    And Check Auth Access    key=XX-XXX
    ${result} =    And Terminate Process    akas
    Then Should Contain    ${result.stdout}    Key length: 6
    And Should Contain    ${result.stdout}    Key prefix: XX-
    And Should Contain    ${result.stdout}    Lines count: 5 - Keys count: 3
    [Teardown]    Kill Akas Server

Test AKAS /load URL: no authorization bearer
    [Documentation]    Load test
    [Tags]    error
    Given Start Process    ${BIN}    serve    --no-admin-key    alias=akas
    And Sleep    ${AKAS_WAIT}
    ${file} =    Get File For Streaming Upload    ${PLAIN_KEY_FILE}
    VAR    &{dict} =    file=${file}
    When POST    ${URL}    files=${dict}    expected_status=401
    ${result} =    And Terminate Process    akas
    Then Should Be Equal As Integers    ${result.rc}    0
    [Teardown]    Kill Akas Server


*** Keywords ***
Kill Akas Server
    [Documentation]    A keyword for killing AKAS process if any
    Log To Console    Terminate All Processes    kill=True

Check Auth Access
    [Documentation]    Check the URI /auth access
    [Arguments]    ${key}    ${status}=200    ${port}=5001
    VAR    &{headers} =    Authorization=Bearer ${key}
    GET    http://localhost:${port}/auth    headers=${headers}    expected_status=${status}