Skip to content

Commit 86be8e9

Browse files
Enhance API Gateway event creation and add certificate fetching tests: Update create_api_gateway_event function to accept query string parameters. Introduce multiple test cases for fetching certificates by order ID, product ID, email, and a combination of email and product ID, improving test coverage for the certificate retrieval functionality.
1 parent a2ec55a commit 86be8e9

File tree

1 file changed

+211
-3
lines changed

1 file changed

+211
-3
lines changed

test_local.py

Lines changed: 211 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from lambda_function import lambda_handler
1010

1111

12-
def create_api_gateway_event(method: str, path: str, body: dict = None) -> dict:
12+
def create_api_gateway_event(method: str, path: str, body: dict = None, query_string_parameters: dict = None) -> dict:
1313
"""
1414
Cria um evento simulado do API Gateway REST.
1515
@@ -25,7 +25,7 @@ def create_api_gateway_event(method: str, path: str, body: dict = None) -> dict:
2525
"httpMethod": method,
2626
"path": path,
2727
"pathParameters": None,
28-
"queryStringParameters": None,
28+
"queryStringParameters": query_string_parameters,
2929
"headers": {
3030
"Content-Type": "application/json",
3131
"Accept": "application/json"
@@ -109,8 +109,216 @@ def test_create_certificate():
109109
print(f"Erro durante o teste: {e}")
110110
return None
111111

112+
def test_fetch_certificate_by_order_id():
113+
"""Testa o endpoint de busca de certificados."""
114+
print("🧪 Testando endpoint de busca de certificados...")
115+
116+
# Dados de teste (query params devem ser strings)
117+
request_data = {
118+
"order_id": "1826"
119+
}
120+
121+
# Cria evento simulado
122+
event = create_api_gateway_event(
123+
method="GET",
124+
path="/api/v1/certificate/fetch",
125+
query_string_parameters=request_data
126+
)
127+
128+
# Contexto simulado
129+
context = MockLambdaContext()
130+
131+
try:
132+
# Chama o handler da Lambda
133+
print(f"Enviando requisição: {json.dumps(request_data, indent=2)}")
134+
response = lambda_handler(event, context)
135+
136+
# Exibe resposta
137+
print("Resposta recebida:")
138+
print(f"Status Code: {response.get('statusCode', 'N/A')}")
139+
140+
# Tenta fazer parse do body se for string
141+
body = response.get('body', {})
142+
if isinstance(body, str):
143+
try:
144+
body = json.loads(body)
145+
except json.JSONDecodeError:
146+
pass
147+
148+
print(f" Body: {json.dumps(body, indent=2, ensure_ascii=False)}")
149+
150+
# Verifica se foi sucesso
151+
status_code = response.get('statusCode', 500)
152+
if 200 <= status_code < 300:
153+
print("Teste executado com sucesso!")
154+
else:
155+
print("Teste falhou!")
156+
157+
return response
158+
159+
except Exception as e:
160+
print(f"Erro durante o teste: {e}")
161+
return None
162+
163+
def test_fetch_certificate_by_product_id():
164+
"""Testa o endpoint de busca de certificados por product_id."""
165+
print("🧪 Testando endpoint de busca de certificados por product_id...")
166+
167+
# Dados de teste (query params devem ser strings)
168+
request_data = {
169+
"product_id": "1678"
170+
}
171+
172+
# Cria evento simulado
173+
event = create_api_gateway_event(
174+
method="GET",
175+
path="/api/v1/certificate/fetch",
176+
query_string_parameters=request_data
177+
)
178+
179+
# Contexto simulado
180+
context = MockLambdaContext()
181+
182+
try:
183+
# Chama o handler da Lambda
184+
print(f"Enviando requisição: {json.dumps(request_data, indent=2)}")
185+
response = lambda_handler(event, context)
186+
187+
# Exibe resposta
188+
print("Resposta recebida:")
189+
print(f"Status Code: {response.get('statusCode', 'N/A')}")
190+
191+
# Tenta fazer parse do body se for string
192+
body = response.get('body', {})
193+
if isinstance(body, str):
194+
try:
195+
body = json.loads(body)
196+
except json.JSONDecodeError:
197+
pass
198+
199+
print(f" Body: {json.dumps(body, indent=2, ensure_ascii=False)}")
200+
201+
# Verifica se foi sucesso
202+
status_code = response.get('statusCode', 500)
203+
if 200 <= status_code < 300:
204+
print("Teste executado com sucesso!")
205+
else:
206+
print("Teste falhou!")
207+
208+
return response
209+
210+
except Exception as e:
211+
print(f"Erro durante o teste: {e}")
212+
return None
213+
214+
def test_fetch_certificate_by_email():
215+
"""Testa o endpoint de busca de certificados por email."""
216+
print("🧪 Testando endpoint de busca de certificados por email...")
217+
218+
# Dados de teste
219+
request_data = {
220+
"email": "[email protected]"
221+
}
222+
223+
# Cria evento simulado
224+
event = create_api_gateway_event(
225+
method="GET",
226+
path="/api/v1/certificate/fetch",
227+
query_string_parameters=request_data
228+
)
229+
230+
# Contexto simulado
231+
context = MockLambdaContext()
232+
233+
try:
234+
# Chama o handler da Lambda
235+
print(f"Enviando requisição: {json.dumps(request_data, indent=2)}")
236+
response = lambda_handler(event, context)
237+
238+
# Exibe resposta
239+
print("Resposta recebida:")
240+
print(f"Status Code: {response.get('statusCode', 'N/A')}")
241+
242+
# Tenta fazer parse do body se for string
243+
body = response.get('body', {})
244+
if isinstance(body, str):
245+
try:
246+
body = json.loads(body)
247+
except json.JSONDecodeError:
248+
pass
249+
250+
print(f" Body: {json.dumps(body, indent=2, ensure_ascii=False)}")
251+
252+
# Verifica se foi sucesso
253+
status_code = response.get('statusCode', 500)
254+
if 200 <= status_code < 300:
255+
print("Teste executado com sucesso!")
256+
else:
257+
print("Teste falhou!")
258+
259+
return response
260+
261+
except Exception as e:
262+
print(f"Erro durante o teste: {e}")
263+
return None
264+
265+
def test_fetch_certificate_by_email_and_product_id():
266+
"""Testa o endpoint de busca de certificados por email e product_id."""
267+
print("🧪 Testando endpoint de busca de certificados por email e product_id...")
268+
269+
# Dados de teste
270+
request_data = {
271+
"email": "[email protected]",
272+
"product_id": "1678"
273+
}
274+
275+
# Cria evento simulado
276+
event = create_api_gateway_event(
277+
method="GET",
278+
path="/api/v1/certificate/fetch",
279+
query_string_parameters=request_data
280+
)
281+
282+
# Contexto simulado
283+
context = MockLambdaContext()
284+
285+
try:
286+
# Chama o handler da Lambda
287+
print(f"Enviando requisição: {json.dumps(request_data, indent=2)}")
288+
response = lambda_handler(event, context)
289+
290+
# Exibe resposta
291+
print("Resposta recebida:")
292+
print(f"Status Code: {response.get('statusCode', 'N/A')}")
293+
294+
# Tenta fazer parse do body se for string
295+
body = response.get('body', {})
296+
if isinstance(body, str):
297+
try:
298+
body = json.loads(body)
299+
except json.JSONDecodeError:
300+
pass
301+
302+
print(f" Body: {json.dumps(body, indent=2, ensure_ascii=False)}")
303+
304+
# Verifica se foi sucesso
305+
status_code = response.get('statusCode', 500)
306+
if 200 <= status_code < 300:
307+
print("Teste executado com sucesso!")
308+
else:
309+
print("Teste falhou!")
310+
311+
return response
312+
313+
except Exception as e:
314+
print(f"Erro durante o teste: {e}")
315+
return None
112316

113317

114318
if __name__ == "__main__":
115319
# Executa os testes
116-
test_create_certificate()
320+
# test_create_certificate()
321+
test_fetch_certificate_by_order_id()
322+
test_fetch_certificate_by_product_id()
323+
test_fetch_certificate_by_email()
324+
test_fetch_certificate_by_email_and_product_id()

0 commit comments

Comments
 (0)