Skip to content

Secret type #5449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions atest/robot/keywords/type_conversion/secret.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
*** Settings ***
Resource atest_resource.robot

Suite Setup Run Tests --variable "CLI: Secret:From command line" keywords/type_conversion/secret.robot


*** Test Cases ***
Command line
Check Test Case ${TESTNAME}

Variable section: Scalar
Check Test Case ${TESTNAME}

Variable section: List
Check Test Case ${TESTNAME}

Variable section: Dict
Check Test Case ${TESTNAME}

Variable section: Invalid syntax
Error In File
... 0 keywords/type_conversion/secret.robot 18
... Setting variable '\&{DICT_LITERAL: secret}' failed:
... Value 'fails' must have type 'Secret', got string.
Error In File
... 1 keywords/type_conversion/secret.robot 9
... Setting variable '\${FROM_LITERAL: Secret}' failed:
... Value 'this fails' must have type 'Secret', got string.
Error In File
... 2 keywords/type_conversion/secret.robot 15
... Setting variable '\@{LIST_LITERAL: secret}' failed:
... Value 'this' must have type 'Secret', got string.

VAR: Env variable
Check Test Case ${TESTNAME}

VAR: Join secret
Check Test Case ${TESTNAME}

VAR: Broken variable
Check Test Case ${TESTNAME}

Create: List
Check Test Case ${TESTNAME}

Create: List by extending
Check Test Case ${TESTNAME}

Create: List of dictionaries
Check Test Case ${TESTNAME}

Create: Dictionary
Check Test Case ${TESTNAME}

Return value: Library keyword
Check Test Case ${TESTNAME}

Return value: User keyword
Check Test Case ${TESTNAME}

User keyword: Receive not secret
Check Test Case ${TESTNAME}

User keyword: Receive not secret var
Check Test Case ${TESTNAME}

Library keyword
Check Test Case ${TESTNAME}

Library keyword: not secret
Check Test Case ${TESTNAME} 1
Check Test Case ${TESTNAME} 2

Library keyword: TypedDict
Check Test Case ${TESTNAME}

Library keyword: List of secrets
Check Test Case ${TESTNAME}
34 changes: 34 additions & 0 deletions atest/testdata/keywords/type_conversion/secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import TypedDict

from robot.utils import Secret


class Credential(TypedDict):
username: str
password: Secret


def library_get_secret(value: str = "This is a secret") -> Secret:
return Secret(value)


def library_not_secret():
return "This is a string, not a secret"


def library_receive_secret(secret: Secret) -> str:
return secret.value


def library_receive_credential(credential: Credential) -> str:
return (
f"Username: {credential['username']}, Password: {credential['password'].value}"
)


def library_list_of_secrets(secrets: "list[Secret]") -> str:
return ", ".join(secret.value for secret in secrets)


def get_variables():
return {"VAR_FILE": Secret("From variable file")}
200 changes: 200 additions & 0 deletions atest/testdata/keywords/type_conversion/secret.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
*** Settings ***
Library Collections
Library OperatingSystem
Library secret.py
Variables secret.py


*** Variables ***
${FROM_LITERAL: Secret} this fails
${FROM_EXISTING: secret} ${VAR_FILE}
${FROM_JOIN: secret} abc${VAR_FILE}efg
${FROM_ENV: SECRET} %{SECRET=kala}
${ENV_JOIN: SECRET} qwe%{SECRET=kala}rty
@{LIST: Secret} ${VAR_FILE} %{SECRET=kala}
@{LIST_LITERAL: secret} this fails ${VAR_FILE} %{SECRET=kala}
&{DICT1: Secret} var_file=${VAR_FILE} env=%{SECRET=kala}
&{DICT2: secret=secret} ${VAR_FILE}=%{SECRET=kala}
&{DICT_LITERAL: secret} this=fails ok=${VAR_FILE}


*** Test Cases ***
Command line
Should Be Equal ${CLI.value} From command line

Variable section: Scalar
Should Be Equal ${FROM_EXISTING.value} From variable file
Should Be Equal ${FROM_ENV.value} kala
Should Be Equal ${FROM_JOIN.value} abcFrom variable fileefg
Should Be Equal ${ENV_JOIN.value} qwekalarty
Variable Should Not Exist ${FROM_LITERAL}

Variable section: List
Should Be Equal ${LIST[0].value} From variable file
Should Be Equal ${LIST[1].value} kala
Variable Should Not Exist ${LIST_LITERAL}

Variable section: Dict
Should Be Equal ${DICT1.var_file.value} From variable file
Should Be Equal ${DICT1.env.value} kala
Should Be Equal ${{$DICT2[$VAR_FILE].value}} kala
Variable Should Not Exist ${DICT_LITERAL}

VAR: Env variable
Set Environment Variable SECRET VALUE1
VAR ${secret: secret} %{SECRET}
Should Be Equal ${secret.value} VALUE1
VAR ${x} SECRET
Set Environment Variable SECRET VALUE2
VAR ${secret: secret} %{${x}}
Should Be Equal ${secret.value} VALUE2
VAR ${secret: secret} %{INLINE_SECRET=inline_secret}
Should Be Equal ${secret.value} inline_secret

VAR: Join secret
[Documentation] FAIL
... Setting variable '\${zz: secret}' failed: \
... Value '111\${y}222' must have type 'Secret', got string.
${secret1} Library Get Secret 111
${secret2} Library Get Secret 222
VAR ${x: secret} abc${secret1}
Should Be Equal ${x.value} abc111
VAR ${y: int} 42
VAR ${x: secret} ${secret2}${y}
Should Be Equal ${x.value} 22242
VAR ${x: secret} ${secret1}${secret2}
Should Be Equal ${x.value} 111222
VAR ${x: secret} -${secret1}--${secret2}---
Should Be Equal ${x.value} -111--222---
VAR ${x: secret} -${y}--${secret1}---${y}----${secret2}-----
Should Be Equal
... ${x.value}
... -42--111---42----222-----
Set Environment Variable SECRET VALUE10
VAR ${secret: secret} 11%{SECRET}22
Should Be Equal ${secret.value} 11VALUE1022
VAR ${zz: secret} 111${y}222

VAR: Broken variable
[Documentation] FAIL
... Setting variable '\${x: Secret}' failed: Variable '${borken' was not closed properly.
VAR ${x: Secret} ${borken

Create: List
[Documentation] FAIL
... Setting variable '\@{x: secret}' failed: \
... Value 'this' must have type 'Secret', got string.
${secret} Library Get Secret
VAR @{x: secret} ${secret} ${secret}
Should Be Equal ${x[0].value} This is a secret
Should Be Equal ${x[1].value} This is a secret
VAR @{x: int|secret} 22 ${secret} 44
Should Be Equal ${x[0]} 22 type=int
Should Be Equal ${x[1].value} This is a secret
Should Be Equal ${x[2]} 44 type=int
VAR @{x: secret} ${secret} this fails

Create: List by extending
${secret} Library Get Secret
VAR @{x: secret} ${secret} ${secret}
VAR @{x} @{x} @{x}
Length Should Be ${x} 4
Should Be Equal ${x[0].value} This is a secret
Should Be Equal ${x[1].value} This is a secret
Should Be Equal ${x[2].value} This is a secret
Should Be Equal ${x[3].value} This is a secret

Create: List of dictionaries
${secret} Library Get Secret
VAR &{dict1: secret} key1=${secret} key2=${secret}
VAR &{dict2: secret} key3=${secret}
VAR @{list} ${dict1} ${dict2}
Length Should Be ${list} 2
FOR ${d} IN @{list}
Dictionaries Should Be Equal ${d} ${d}
END

Create: Dictionary
[Documentation] FAIL
... Setting variable '\&{x: secret}' failed: \
... Value 'fails' must have type 'Secret', got string.
${secret} Library Get Secret
VAR &{x: secret} key=${secret}
Should Be Equal ${x.key.value} This is a secret
VAR &{x: int=secret} 42=${secret}
Should Be Equal ${x[42].value} This is a secret
VAR &{x: secret} this=fails

Return value: Library keyword
[Documentation] FAIL
... ValueError: Return value must have type 'Secret', got string.
${x} Library Get Secret
Should Be Equal ${x.value} This is a secret
${x: Secret} Library Get Secret value of secret here
Should Be Equal ${x.value} value of secret here
${x: secret} Library Not Secret

Return value: User keyword
[Documentation] FAIL
... ValueError: Return value must have type 'Secret', got string.
${x} User Keyword: Return secret
Should Be Equal ${x.value} This is a secret
${x: Secret} User Keyword: Return secret
Should Be Equal ${x.value} This is a secret
${x: secret} User Keyword: Return string

User keyword: Receive not secret
[Documentation] FAIL
... ValueError: Argument 'secret' must have type 'Secret', got string.
User Keyword: Receive secret xxx ${None}

User keyword: Receive not secret var
[Documentation] FAIL
... ValueError: Argument 'secret' must have type 'Secret', got string.
VAR ${x} y
User Keyword: Receive secret ${x} ${None}

Library keyword
${secret: secret} Library Get Secret
User Keyword: Receive secret ${secret} This is a secret

Library keyword: not secret 1
[Documentation] FAIL
... ValueError: Argument 'secret' must have type 'Secret', got string.
Library receive secret 111

Library keyword: not secret 2
[Documentation] FAIL
... ValueError: Argument 'secret' must have type 'Secret', got integer.
Library receive secret ${222}

Library keyword: TypedDict
[Documentation] FAIL
... ValueError: Argument 'credential' got value \
... '{'username': 'login@email.com', 'password': 'This fails'}' (DotDict) that cannot be converted to Credential: \
... Item 'password' must have type 'Secret', got string.
${secret: secret} Library Get Secret
VAR &{credentials} username=login@email.com password=${secret}
${data} Library Receive Credential ${credentials}
Should Be Equal ${data} Username: login@email.com, Password: This is a secret
VAR &{credentials} username=login@email.com password=This fails
Library Receive Credential ${credentials}

Library keyword: List of secrets
${secret: secret} Library Get Secret
VAR @{secrets: secret} ${secret} ${secret}
${data} Library List Of Secrets ${secrets}
Should Be Equal ${data} This is a secret, This is a secret


*** Keywords ***
User Keyword: Receive secret
[Arguments] ${secret: secret} ${expected: str}
Should Be Equal ${secret.value} ${expected}

User Keyword: Return secret
${secret} Library Get Secret
RETURN ${secret}

User Keyword: Return string
RETURN This is a string
Loading