Skip to content

Commit abfac4c

Browse files
committed
Secret type
Fixes: 4537
1 parent 23566ea commit abfac4c

File tree

12 files changed

+692
-8
lines changed

12 files changed

+692
-8
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
*** Settings ***
2+
Resource atest_resource.robot
3+
4+
Suite Setup Run Tests --variable "CLI: Secret:From command line" keywords/type_conversion/secret.robot
5+
6+
7+
*** Test Cases ***
8+
Command line
9+
Check Test Case ${TESTNAME}
10+
11+
Variable section: Scalar
12+
Check Test Case ${TESTNAME}
13+
14+
Variable section: List
15+
Check Test Case ${TESTNAME}
16+
17+
Variable section: Dict
18+
Check Test Case ${TESTNAME}
19+
20+
Variable section: Invalid syntax
21+
Error In File
22+
... 0 keywords/type_conversion/secret.robot 18
23+
... Setting variable '\&{DICT_LITERAL: secret}' failed:
24+
... Value 'fails' must have type 'Secret', got string.
25+
Error In File
26+
... 1 keywords/type_conversion/secret.robot 9
27+
... Setting variable '\${FROM_LITERAL: Secret}' failed:
28+
... Value 'this fails' must have type 'Secret', got string.
29+
Error In File
30+
... 2 keywords/type_conversion/secret.robot 15
31+
... Setting variable '\@{LIST_LITERAL: secret}' failed:
32+
... Value 'this' must have type 'Secret', got string.
33+
34+
VAR: Env variable
35+
Check Test Case ${TESTNAME}
36+
37+
VAR: Join secret
38+
Check Test Case ${TESTNAME}
39+
40+
VAR: Broken variable
41+
Check Test Case ${TESTNAME}
42+
43+
Create: List
44+
Check Test Case ${TESTNAME}
45+
46+
Create: List by extending
47+
Check Test Case ${TESTNAME}
48+
49+
Create: List of dictionaries
50+
Check Test Case ${TESTNAME}
51+
52+
Create: Dictionary
53+
Check Test Case ${TESTNAME}
54+
55+
Return value: Library keyword
56+
Check Test Case ${TESTNAME}
57+
58+
Return value: User keyword
59+
Check Test Case ${TESTNAME}
60+
61+
User keyword: Receive not secret
62+
Check Test Case ${TESTNAME}
63+
64+
User keyword: Receive not secret var
65+
Check Test Case ${TESTNAME}
66+
67+
Library keyword
68+
Check Test Case ${TESTNAME}
69+
70+
Library keyword: not secret
71+
Check Test Case ${TESTNAME} 1
72+
Check Test Case ${TESTNAME} 2
73+
74+
Library keyword: TypedDict
75+
Check Test Case ${TESTNAME}
76+
77+
Library keyword: List of secrets
78+
Check Test Case ${TESTNAME}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import TypedDict
2+
3+
from robot.utils import Secret
4+
5+
6+
class Credential(TypedDict):
7+
username: str
8+
password: Secret
9+
10+
11+
def library_get_secret(value: str = "This is a secret") -> Secret:
12+
return Secret(value)
13+
14+
15+
def library_not_secret():
16+
return "This is a string, not a secret"
17+
18+
19+
def library_receive_secret(secret: Secret) -> str:
20+
return secret.value
21+
22+
23+
def library_receive_credential(credential: Credential) -> str:
24+
return (
25+
f"Username: {credential['username']}, Password: {credential['password'].value}"
26+
)
27+
28+
29+
def library_list_of_secrets(secrets: "list[Secret]") -> str:
30+
return ", ".join(secret.value for secret in secrets)
31+
32+
33+
def get_variables():
34+
return {"VAR_FILE": Secret("From variable file")}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
*** Settings ***
2+
Library Collections
3+
Library OperatingSystem
4+
Library secret.py
5+
Variables secret.py
6+
7+
8+
*** Variables ***
9+
${FROM_LITERAL: Secret} this fails
10+
${FROM_EXISTING: secret} ${VAR_FILE}
11+
${FROM_JOIN: secret} abc${VAR_FILE}efg
12+
${FROM_ENV: SECRET} %{SECRET=kala}
13+
${ENV_JOIN: SECRET} qwe%{SECRET=kala}rty
14+
@{LIST: Secret} ${VAR_FILE} %{SECRET=kala}
15+
@{LIST_LITERAL: secret} this fails ${VAR_FILE} %{SECRET=kala}
16+
&{DICT1: Secret} var_file=${VAR_FILE} env=%{SECRET=kala}
17+
&{DICT2: secret=secret} ${VAR_FILE}=%{SECRET=kala}
18+
&{DICT_LITERAL: secret} this=fails ok=${VAR_FILE}
19+
20+
21+
*** Test Cases ***
22+
Command line
23+
Should Be Equal ${CLI.value} From command line
24+
25+
Variable section: Scalar
26+
Should Be Equal ${FROM_EXISTING.value} From variable file
27+
Should Be Equal ${FROM_ENV.value} kala
28+
Should Be Equal ${FROM_JOIN.value} abcFrom variable fileefg
29+
Should Be Equal ${ENV_JOIN.value} qwekalarty
30+
Variable Should Not Exist ${FROM_LITERAL}
31+
32+
Variable section: List
33+
Should Be Equal ${LIST[0].value} From variable file
34+
Should Be Equal ${LIST[1].value} kala
35+
Variable Should Not Exist ${LIST_LITERAL}
36+
37+
Variable section: Dict
38+
Should Be Equal ${DICT1.var_file.value} From variable file
39+
Should Be Equal ${DICT1.env.value} kala
40+
Should Be Equal ${{$DICT2[$VAR_FILE].value}} kala
41+
Variable Should Not Exist ${DICT_LITERAL}
42+
43+
VAR: Env variable
44+
Set Environment Variable SECRET VALUE1
45+
VAR ${secret: secret} %{SECRET}
46+
Should Be Equal ${secret.value} VALUE1
47+
VAR ${x} SECRET
48+
Set Environment Variable SECRET VALUE2
49+
VAR ${secret: secret} %{${x}}
50+
Should Be Equal ${secret.value} VALUE2
51+
VAR ${secret: secret} %{INLINE_SECRET=inline_secret}
52+
Should Be Equal ${secret.value} inline_secret
53+
54+
VAR: Join secret
55+
[Documentation] FAIL
56+
... Setting variable '\${zz: secret}' failed: \
57+
... Value '111\${y}222' must have type 'Secret', got string.
58+
${secret1} Library Get Secret 111
59+
${secret2} Library Get Secret 222
60+
VAR ${x: secret} abc${secret1}
61+
Should Be Equal ${x.value} abc111
62+
VAR ${y: int} 42
63+
VAR ${x: secret} ${secret2}${y}
64+
Should Be Equal ${x.value} 22242
65+
VAR ${x: secret} ${secret1}${secret2}
66+
Should Be Equal ${x.value} 111222
67+
VAR ${x: secret} -${secret1}--${secret2}---
68+
Should Be Equal ${x.value} -111--222---
69+
VAR ${x: secret} -${y}--${secret1}---${y}----${secret2}-----
70+
Should Be Equal
71+
... ${x.value}
72+
... -42--111---42----222-----
73+
Set Environment Variable SECRET VALUE10
74+
VAR ${secret: secret} 11%{SECRET}22
75+
Should Be Equal ${secret.value} 11VALUE1022
76+
VAR ${zz: secret} 111${y}222
77+
78+
VAR: Broken variable
79+
[Documentation] FAIL
80+
... Setting variable '\${x: Secret}' failed: Variable '${borken' was not closed properly.
81+
VAR ${x: Secret} ${borken
82+
83+
Create: List
84+
[Documentation] FAIL
85+
... Setting variable '\@{x: secret}' failed: \
86+
... Value 'this' must have type 'Secret', got string.
87+
${secret} Library Get Secret
88+
VAR @{x: secret} ${secret} ${secret}
89+
Should Be Equal ${x[0].value} This is a secret
90+
Should Be Equal ${x[1].value} This is a secret
91+
VAR @{x: int|secret} 22 ${secret} 44
92+
Should Be Equal ${x[0]} 22 type=int
93+
Should Be Equal ${x[1].value} This is a secret
94+
Should Be Equal ${x[2]} 44 type=int
95+
VAR @{x: secret} ${secret} this fails
96+
97+
Create: List by extending
98+
${secret} Library Get Secret
99+
VAR @{x: secret} ${secret} ${secret}
100+
VAR @{x} @{x} @{x}
101+
Length Should Be ${x} 4
102+
Should Be Equal ${x[0].value} This is a secret
103+
Should Be Equal ${x[1].value} This is a secret
104+
Should Be Equal ${x[2].value} This is a secret
105+
Should Be Equal ${x[3].value} This is a secret
106+
107+
Create: List of dictionaries
108+
${secret} Library Get Secret
109+
VAR &{dict1: secret} key1=${secret} key2=${secret}
110+
VAR &{dict2: secret} key3=${secret}
111+
VAR @{list} ${dict1} ${dict2}
112+
Length Should Be ${list} 2
113+
FOR ${d} IN @{list}
114+
Dictionaries Should Be Equal ${d} ${d}
115+
END
116+
117+
Create: Dictionary
118+
[Documentation] FAIL
119+
... Setting variable '\&{x: secret}' failed: \
120+
... Value 'fails' must have type 'Secret', got string.
121+
${secret} Library Get Secret
122+
VAR &{x: secret} key=${secret}
123+
Should Be Equal ${x.key.value} This is a secret
124+
VAR &{x: int=secret} 42=${secret}
125+
Should Be Equal ${x[42].value} This is a secret
126+
VAR &{x: secret} this=fails
127+
128+
Return value: Library keyword
129+
[Documentation] FAIL
130+
... ValueError: Return value must have type 'Secret', got string.
131+
${x} Library Get Secret
132+
Should Be Equal ${x.value} This is a secret
133+
${x: Secret} Library Get Secret value of secret here
134+
Should Be Equal ${x.value} value of secret here
135+
${x: secret} Library Not Secret
136+
137+
Return value: User keyword
138+
[Documentation] FAIL
139+
... ValueError: Return value must have type 'Secret', got string.
140+
${x} User Keyword: Return secret
141+
Should Be Equal ${x.value} This is a secret
142+
${x: Secret} User Keyword: Return secret
143+
Should Be Equal ${x.value} This is a secret
144+
${x: secret} User Keyword: Return string
145+
146+
User keyword: Receive not secret
147+
[Documentation] FAIL
148+
... ValueError: Argument 'secret' must have type 'Secret', got string.
149+
User Keyword: Receive secret xxx ${None}
150+
151+
User keyword: Receive not secret var
152+
[Documentation] FAIL
153+
... ValueError: Argument 'secret' must have type 'Secret', got string.
154+
VAR ${x} y
155+
User Keyword: Receive secret ${x} ${None}
156+
157+
Library keyword
158+
${secret: secret} Library Get Secret
159+
User Keyword: Receive secret ${secret} This is a secret
160+
161+
Library keyword: not secret 1
162+
[Documentation] FAIL
163+
... ValueError: Argument 'secret' must have type 'Secret', got string.
164+
Library receive secret 111
165+
166+
Library keyword: not secret 2
167+
[Documentation] FAIL
168+
... ValueError: Argument 'secret' must have type 'Secret', got integer.
169+
Library receive secret ${222}
170+
171+
Library keyword: TypedDict
172+
[Documentation] FAIL
173+
... ValueError: Argument 'credential' got value \
174+
... '{'username': 'login@email.com', 'password': 'This fails'}' (DotDict) that cannot be converted to Credential: \
175+
... Item 'password' must have type 'Secret', got string.
176+
${secret: secret} Library Get Secret
177+
VAR &{credentials} username=login@email.com password=${secret}
178+
${data} Library Receive Credential ${credentials}
179+
Should Be Equal ${data} Username: login@email.com, Password: This is a secret
180+
VAR &{credentials} username=login@email.com password=This fails
181+
Library Receive Credential ${credentials}
182+
183+
Library keyword: List of secrets
184+
${secret: secret} Library Get Secret
185+
VAR @{secrets: secret} ${secret} ${secret}
186+
${data} Library List Of Secrets ${secrets}
187+
Should Be Equal ${data} This is a secret, This is a secret
188+
189+
190+
*** Keywords ***
191+
User Keyword: Receive secret
192+
[Arguments] ${secret: secret} ${expected: str}
193+
Should Be Equal ${secret.value} ${expected}
194+
195+
User Keyword: Return secret
196+
${secret} Library Get Secret
197+
RETURN ${secret}
198+
199+
User Keyword: Return string
200+
RETURN This is a string

0 commit comments

Comments
 (0)