Terarea  2
The automation project
Loading...
Searching...
No Matches
test_boilerplate_responses.py
Go to the documentation of this file.
1"""_summary_
2 File in charge of testing the boilerplate Boilerplate non http class.
3"""
4import os
5import sys
6
7from fastapi import FastAPI
8import constants as TCONST
9
10sys.path.append(os.getcwd())
11
12try:
13 from src.lib.sql.sql_manager import SQL
14 from src.lib.components.http_codes import HCI
15 from src.lib.components import constants as CONST
16 from src.lib.components.runtime_data import RuntimeData
17 from src.lib.components.endpoints_routes import Endpoints
18 from src.lib.boilerplates.non_web import BoilerplateNonHTTP
19 from src.lib.boilerplates.responses import BoilerplateResponses
20 from src.lib.components.password_handling import PasswordHandling
21except ImportError as e:
22 raise ImportError("Failed to import the src module") from e
23
24ERROR = TCONST.ERROR
25SUCCESS = TCONST.SUCCESS
26DEBUG = TCONST.DEBUG
27
28
29RDI = RuntimeData(TCONST.SERVER_HOST, TCONST.PORT, "Area", ERROR, SUCCESS)
30RDI.app = FastAPI()
31RDI.endpoints_initialised = Endpoints(
32 runtime_data=RDI,
33 success=SUCCESS,
34 error=ERROR,
35 debug=DEBUG
36)
38 runtime_data=RDI,
39 debug=DEBUG
40)
41RDI.boilerplate_responses_initialised = BRI
42RDI.boilerplate_non_http_initialised = BoilerplateNonHTTP(
43 runtime_data_initialised=RDI,
44 success=SUCCESS,
45 error=ERROR,
46 debug=DEBUG
47)
48
49RDI.database_link = SQL(
50 url=CONST.DB_HOST,
51 port=CONST.DB_PORT,
52 username=CONST.DB_USER,
53 password=CONST.DB_PASSWORD,
54 db_name=CONST.DB_DATABASE,
55 debug=DEBUG
56)
57
58RDI.boilerplate_responses_initialised = BRI
59
61 error=ERROR,
62 success=SUCCESS,
63 debug=DEBUG
64)
65
66
67def _register_fake_user() -> None:
68 """_summary_
69 Function in charge of registering a fake user.
70 """
71 input_data = [
72 TCONST.USER_DATA_USERNAME,
73 TCONST.USER_DATA_EMAIL,
74 PHI.hash_password(str(TCONST.USER_DATA_PASSWORD)),
75 TCONST.USER_DATA_METHOD,
76 TCONST.USER_DATA_FAVICON,
77 TCONST.USER_DATA_ADMIN
78 ]
79 column_names = RDI.database_link.get_table_column_names(CONST.TAB_ACCOUNTS)
80 column_names.pop(0)
81 RDI.database_link.insert_or_update_data_into_table(
82 table=CONST.TAB_ACCOUNTS,
83 data=input_data,
84 columns=column_names
85 )
86
87
89 """_summary_
90 Function in charge of deregistering a fake user.
91 """
92 RDI.database_link.remove_data_from_table(
93 table=CONST.TAB_ACCOUNTS,
94 where=f"email='{TCONST.USER_DATA_EMAIL}'"
95 )
96
97
98def _sing_fake_user_in() -> None:
99 """_summary_
100 Function in charge of signing in a fake user.
101 """
103 user_id = RDI.database_link.get_data_from_table(
104 table=CONST.TAB_ACCOUNTS,
105 column="id",
106 where=f"email='{TCONST.USER_DATA_EMAIL}'",
107 beautify=False
108 )
109 table_columns = RDI.database_link.get_table_column_names(
110 CONST.TAB_CONNECTIONS
111 )
112 table_columns.pop(0)
113 input_data = [
114 TCONST.USER_DATA_TOKEN,
115 str(user_id[0][0]),
116 RDI.database_link.datetime_to_string(
117 RDI.boilerplate_non_http_initialised.set_lifespan(
118 TCONST.USER_DATA_TOKEN_LIFESPAN
119 )
120 )
121 ]
122 RDI.database_link.insert_or_update_data_into_table(
123 table=CONST.TAB_CONNECTIONS,
124 data=input_data,
125 columns=table_columns
126 )
127
128
130 """_summary_
131 Function in charge of signing out a fake user.
132 """
133 RDI.database_link.remove_data_from_table(
134 table=CONST.TAB_CONNECTIONS,
135 where=f"token='{TCONST.USER_DATA_TOKEN}'"
136 )
138
139
141 """_summary_
142 Function in charge of testing the build response body function.
143 """
144 title = "Hello World"
145 message = "Some hello world !"
146 response = "hw"
147 token = None
148 error = False
149 data = BRI.build_response_body(
150 title=title,
151 message=message,
152 resp=response,
153 token=token,
154 error=error
155 )
156 resp = {}
157 resp[CONST.JSON_TITLE] = title
158 resp[CONST.JSON_MESSAGE] = message
159 resp[CONST.JSON_RESP] = response
160 resp[CONST.JSON_LOGGED_IN] = False
161 assert data == resp
162
163
165 """_summary_
166 Function in charge of testing the build response body function.
167 """
168 title = "Hello World"
169 message = "Some hello world !"
170 response = "hw"
171 token = None
172 error = True
173 data = BRI.build_response_body(
174 title=title,
175 message=message,
176 resp=response,
177 token=token,
178 error=error
179 )
180 resp = {}
181 resp[CONST.JSON_TITLE] = title
182 resp[CONST.JSON_MESSAGE] = message
183 resp[CONST.JSON_ERROR] = response
184 resp[CONST.JSON_LOGGED_IN] = False
185 assert data == resp
186
187
189 """_summary_
190 Function in charge of testing the build response body function.
191 """
193 title = "Hello World"
194 message = "Some hello world !"
195 response = "hw"
196 error = False
197 data = BRI.build_response_body(
198 title=title,
199 message=message,
200 resp=response,
201 token=TCONST.USER_DATA_TOKEN,
202 error=error
203 )
204 resp = {}
205 resp[CONST.JSON_TITLE] = title
206 resp[CONST.JSON_MESSAGE] = message
207 resp[CONST.JSON_RESP] = response
208 resp[CONST.JSON_LOGGED_IN] = True
210 assert data == resp
211
212
214 """_summary_
215 Function in charge of testing the build response body function.
216 """
218 title = "Hello World"
219 message = "Some hello world !"
220 response = "hw"
221 error = True
222 data = BRI.build_response_body(
223 title=title,
224 message=message,
225 resp=response,
226 token=TCONST.USER_DATA_TOKEN,
227 error=error
228 )
229 resp = {}
230 resp[CONST.JSON_TITLE] = title
231 resp[CONST.JSON_MESSAGE] = message
232 resp[CONST.JSON_ERROR] = response
233 resp[CONST.JSON_LOGGED_IN] = True
235 assert data == resp
236
237
238def test_invalid_token() -> None:
239 """_summary_
240 Function in charge of testing the invalid token function.
241 """
242 title = "Hello World"
243 data = BRI.invalid_token(title)
244 resp = {}
245 resp[CONST.JSON_TITLE] = title
246 resp[CONST.JSON_MESSAGE] = "The token you entered is invalid."
247 resp[CONST.JSON_ERROR] = "Invalid token"
248 resp[CONST.JSON_LOGGED_IN] = False
249 compiled_response = HCI.invalid_token(
250 content=resp,
251 content_type=CONST.CONTENT_TYPE,
252 headers=RDI.json_header
253 )
254 assert data.status_code == compiled_response.status_code
255 assert data.headers == compiled_response.headers
256 assert data.body == compiled_response.body
257
258
259def test_not_logged_in() -> None:
260 """_summary_
261 Function in charge of testing the not logged in function.
262 """
263 title = "Hello World"
264 data = BRI.not_logged_in(title)
265 resp = {}
266 resp[CONST.JSON_TITLE] = title
267 resp[CONST.JSON_MESSAGE] = "You need to be logged in to be able to run this endpoint."
268 resp[CONST.JSON_ERROR] = "User not logged in"
269 resp[CONST.JSON_LOGGED_IN] = False
270 compiled_response = HCI.unauthorized(
271 content=resp,
272 content_type=CONST.CONTENT_TYPE,
273 headers=RDI.json_header
274 )
275 assert data.status_code == compiled_response.status_code
276 assert data.headers == compiled_response.headers
277 assert data.body == compiled_response.body
278
279
280def test_login_failed() -> None:
281 """_summary_
282 Function in charge of testing the login failed function.
283 """
284 title = "Hello World"
285 data = BRI.login_failed(title)
286 resp = {}
287 resp[CONST.JSON_TITLE] = title
288 resp[CONST.JSON_MESSAGE] = "Login failed, invalid credentials or username."
289 resp[CONST.JSON_ERROR] = "Invalid credentials or username."
290 resp[CONST.JSON_LOGGED_IN] = False
291 compiled_response = HCI.unauthorized(
292 content=resp,
293 content_type=CONST.CONTENT_TYPE,
294 headers=RDI.json_header
295 )
296 assert data.status_code == compiled_response.status_code
297 assert data.headers == compiled_response.headers
298 assert data.body == compiled_response.body
299
300
302 """_summary_
303 Function in charge of testing the insuffisant rights function.
304 """
305 title = "Hello World"
306 data = BRI.insuffisant_rights(title, "not_a_token")
307 resp = {}
308 resp[CONST.JSON_TITLE] = title
309 resp[CONST.JSON_MESSAGE] = "You do not have enough permissions to execute this endpoint."
310 resp[CONST.JSON_ERROR] = "Insufficient rights for given account."
311 resp[CONST.JSON_LOGGED_IN] = False
312 compiled_response = HCI.forbidden(
313 content=resp,
314 content_type=CONST.CONTENT_TYPE,
315 headers=RDI.json_header
316 )
317 assert data.status_code == compiled_response.status_code
318 assert data.headers == compiled_response.headers
319 assert data.body == compiled_response.body
320
321
323 """_summary_
324 Function in charge of testing the insuffisant rights function.
325 """
327 title = "Hello World"
328 data = BRI.insuffisant_rights(title, TCONST.USER_DATA_TOKEN)
329 resp = {}
330 resp[CONST.JSON_TITLE] = title
331 resp[CONST.JSON_MESSAGE] = "You do not have enough permissions to execute this endpoint."
332 resp[CONST.JSON_ERROR] = "Insufficient rights for given account."
333 resp[CONST.JSON_LOGGED_IN] = True
334 compiled_response = HCI.forbidden(
335 content=resp,
336 content_type=CONST.CONTENT_TYPE,
337 headers=RDI.json_header
338 )
340 assert data.status_code == compiled_response.status_code
341 assert data.headers == compiled_response.headers
342 assert data.body == compiled_response.body
343
344
346 """_summary_
347 Function in charge of testing the insuffisant rights function.
348 """
349 title = "Hello World"
350 data = BRI.bad_request(title, "not_a_token")
351 resp = {}
352 resp[CONST.JSON_TITLE] = title
353 resp[CONST.JSON_MESSAGE] = "The request was not formatted correctly."
354 resp[CONST.JSON_ERROR] = "Bad request"
355 resp[CONST.JSON_LOGGED_IN] = False
356 compiled_response = HCI.bad_request(
357 content=resp,
358 content_type=CONST.CONTENT_TYPE,
359 headers=RDI.json_header
360 )
361 assert data.status_code == compiled_response.status_code
362 assert data.headers == compiled_response.headers
363 assert data.body == compiled_response.body
364
365
367 """_summary_
368 Function in charge of testing the insuffisant rights function.
369 """
371 title = "Hello World"
372 data = BRI.bad_request(title, TCONST.USER_DATA_TOKEN)
373 resp = {}
374 resp[CONST.JSON_TITLE] = title
375 resp[CONST.JSON_MESSAGE] = "The request was not formatted correctly."
376 resp[CONST.JSON_ERROR] = "Bad request"
377 resp[CONST.JSON_LOGGED_IN] = True
378 compiled_response = HCI.bad_request(
379 content=resp,
380 content_type=CONST.CONTENT_TYPE,
381 headers=RDI.json_header
382 )
384 assert data.status_code == compiled_response.status_code
385 assert data.headers == compiled_response.headers
386 assert data.body == compiled_response.body
387
388
390 """_summary_
391 Function in charge of testing the insuffisant rights function.
392 """
393 title = "Hello World"
394 data = BRI.internal_server_error(title, "not_a_token")
395 resp = {}
396 resp[CONST.JSON_TITLE] = title
397 resp[CONST.JSON_MESSAGE] = "The server has encountered an error."
398 resp[CONST.JSON_ERROR] = "Internal server error"
399 resp[CONST.JSON_LOGGED_IN] = False
400 compiled_response = HCI.internal_server_error(
401 content=resp,
402 content_type=CONST.CONTENT_TYPE,
403 headers=RDI.json_header
404 )
405 assert data.status_code == compiled_response.status_code
406 assert data.headers == compiled_response.headers
407 assert data.body == compiled_response.body
408
409
411 """_summary_
412 Function in charge of testing the insuffisant rights function.
413 """
415 title = "Hello World"
416 data = BRI.internal_server_error(title, TCONST.USER_DATA_TOKEN)
417 resp = {}
418 resp[CONST.JSON_TITLE] = title
419 resp[CONST.JSON_MESSAGE] = "The server has encountered an error."
420 resp[CONST.JSON_ERROR] = "Internal server error"
421 resp[CONST.JSON_LOGGED_IN] = True
422 compiled_response = HCI.internal_server_error(
423 content=resp,
424 content_type=CONST.CONTENT_TYPE,
425 headers=RDI.json_header
426 )
428 assert data.status_code == compiled_response.status_code
429 assert data.headers == compiled_response.headers
430 assert data.body == compiled_response.body
431
432
434 """_summary_
435 Function in charge of testing the insuffisant rights function.
436 """
437 title = "Hello World"
438 data = BRI.unauthorized(title, "not_a_token")
439 resp = {}
440 resp[CONST.JSON_TITLE] = title
441 resp[CONST.JSON_MESSAGE] = "You do not have permission to run this endpoint."
442 resp[CONST.JSON_ERROR] = "Access denied"
443 resp[CONST.JSON_LOGGED_IN] = False
444 compiled_response = HCI.unauthorized(
445 content=resp,
446 content_type=CONST.CONTENT_TYPE,
447 headers=RDI.json_header
448 )
449 assert data.status_code == compiled_response.status_code
450 assert data.headers == compiled_response.headers
451 assert data.body == compiled_response.body
452
453
455 """_summary_
456 Function in charge of testing the insuffisant rights function.
457 """
459 title = "Hello World"
460 data = BRI.unauthorized(title, TCONST.USER_DATA_TOKEN)
461 resp = {}
462 resp[CONST.JSON_TITLE] = title
463 resp[CONST.JSON_MESSAGE] = "You do not have permission to run this endpoint."
464 resp[CONST.JSON_ERROR] = "Access denied"
465 resp[CONST.JSON_LOGGED_IN] = True
466 compiled_response = HCI.unauthorized(
467 content=resp,
468 content_type=CONST.CONTENT_TYPE,
469 headers=RDI.json_header
470 )
472 assert data.status_code == compiled_response.status_code
473 assert data.headers == compiled_response.headers
474 assert data.body == compiled_response.body
475
476
478 """_summary_
479 Function in charge of testing the insuffisant rights function.
480 """
481 title = "Hello World"
482 data = BRI.invalid_verification_code(title, "not_a_token")
483 resp = {}
484 resp[CONST.JSON_TITLE] = title
485 resp[CONST.JSON_MESSAGE] = "The verification code you have entered is incorrect."
486 resp[CONST.JSON_ERROR] = "Invalid verification code"
487 resp[CONST.JSON_LOGGED_IN] = False
488 compiled_response = HCI.bad_request(
489 content=resp,
490 content_type=CONST.CONTENT_TYPE,
491 headers=RDI.json_header
492 )
493 assert data.status_code == compiled_response.status_code
494 assert data.headers == compiled_response.headers
495 assert data.body == compiled_response.body
496
497
499 """_summary_
500 Function in charge of testing the insuffisant rights function.
501 """
503 title = "Hello World"
504 data = BRI.invalid_verification_code(title, TCONST.USER_DATA_TOKEN)
505 resp = {}
506 resp[CONST.JSON_TITLE] = title
507 resp[CONST.JSON_MESSAGE] = "The verification code you have entered is incorrect."
508 resp[CONST.JSON_ERROR] = "Invalid verification code"
509 resp[CONST.JSON_LOGGED_IN] = True
510 compiled_response = HCI.bad_request(
511 content=resp,
512 content_type=CONST.CONTENT_TYPE,
513 headers=RDI.json_header
514 )
516 assert data.status_code == compiled_response.status_code
517 assert data.headers == compiled_response.headers
518 assert data.body == compiled_response.body
519
520
522 """_summary_
523 Function in charge of testing the insuffisant rights function.
524 """
525 title = "Hello World"
526 data = BRI.user_not_found(title, "not_a_token")
527 resp = {}
528 resp[CONST.JSON_TITLE] = title
529 resp[CONST.JSON_MESSAGE] = "The current user was not found."
530 resp[CONST.JSON_ERROR] = "Not found"
531 resp[CONST.JSON_LOGGED_IN] = False
532 compiled_response = HCI.not_found(
533 content=resp,
534 content_type=CONST.CONTENT_TYPE,
535 headers=RDI.json_header
536 )
537 assert data.status_code == compiled_response.status_code
538 assert data.headers == compiled_response.headers
539 assert data.body == compiled_response.body
540
541
543 """_summary_
544 Function in charge of testing the insuffisant rights function.
545 """
547 title = "Hello World"
548 data = BRI.user_not_found(title, TCONST.USER_DATA_TOKEN)
549 resp = {}
550 resp[CONST.JSON_TITLE] = title
551 resp[CONST.JSON_MESSAGE] = "The current user was not found."
552 resp[CONST.JSON_ERROR] = "Not found"
553 resp[CONST.JSON_LOGGED_IN] = True
554 compiled_response = HCI.not_found(
555 content=resp,
556 content_type=CONST.CONTENT_TYPE,
557 headers=RDI.json_header
558 )
560 assert data.status_code == compiled_response.status_code
561 assert data.headers == compiled_response.headers
562 assert data.body == compiled_response.body
563
564
566 """_summary_
567 Function in charge of testing the insuffisant rights function.
568 """
569 title = "Hello World"
570 data = BRI.no_access_token(title, "not_a_token")
571 resp = {}
572 resp[CONST.JSON_TITLE] = title
573 resp[CONST.JSON_MESSAGE] = "Access token not found."
574 resp[CONST.JSON_ERROR] = "No access token"
575 resp[CONST.JSON_LOGGED_IN] = False
576 compiled_response = HCI.bad_request(
577 content=resp,
578 content_type=CONST.CONTENT_TYPE,
579 headers=RDI.json_header
580 )
581 assert data.status_code == compiled_response.status_code
582 assert data.headers == compiled_response.headers
583 assert data.body == compiled_response.body
584
585
587 """_summary_
588 Function in charge of testing the insuffisant rights function.
589 """
591 title = "Hello World"
592 data = BRI.no_access_token(title, TCONST.USER_DATA_TOKEN)
593 resp = {}
594 resp[CONST.JSON_TITLE] = title
595 resp[CONST.JSON_MESSAGE] = "Access token not found."
596 resp[CONST.JSON_ERROR] = "No access token"
597 resp[CONST.JSON_LOGGED_IN] = True
598 compiled_response = HCI.bad_request(
599 content=resp,
600 content_type=CONST.CONTENT_TYPE,
601 headers=RDI.json_header
602 )
604 assert data.status_code == compiled_response.status_code
605 assert data.headers == compiled_response.headers
606 assert data.body == compiled_response.body
607
608
610 """_summary_
611 Function in charge of testing the insuffisant rights function.
612 """
613 title = "Hello World"
614 data = BRI.provider_not_found(title, "not_a_token")
615 resp = {}
616 resp[CONST.JSON_TITLE] = title
617 resp[CONST.JSON_MESSAGE] = "The provider you are looking for was not found."
618 resp[CONST.JSON_ERROR] = "Provider not found"
619 resp[CONST.JSON_LOGGED_IN] = False
620 compiled_response = HCI.not_found(
621 content=resp,
622 content_type=CONST.CONTENT_TYPE,
623 headers=RDI.json_header
624 )
625 assert data.status_code == compiled_response.status_code
626 assert data.headers == compiled_response.headers
627 assert data.body == compiled_response.body
628
629
631 """_summary_
632 Function in charge of testing the insuffisant rights function.
633 """
635 title = "Hello World"
636 data = BRI.provider_not_found(title, TCONST.USER_DATA_TOKEN)
637 resp = {}
638 resp[CONST.JSON_TITLE] = title
639 resp[CONST.JSON_MESSAGE] = "The provider you are looking for was not found."
640 resp[CONST.JSON_ERROR] = "Provider not found"
641 resp[CONST.JSON_LOGGED_IN] = True
642 compiled_response = HCI.not_found(
643 content=resp,
644 content_type=CONST.CONTENT_TYPE,
645 headers=RDI.json_header
646 )
648 assert data.status_code == compiled_response.status_code
649 assert data.headers == compiled_response.headers
650 assert data.body == compiled_response.body
651
652
654 """_summary_
655 Function in charge of testing the insuffisant rights function.
656 """
657 title = "Hello World"
658 data = BRI.provider_not_given(title, "not_a_token")
659 resp = {}
660 resp[CONST.JSON_TITLE] = title
661 resp[CONST.JSON_MESSAGE] = "You have not given a provider."
662 resp[CONST.JSON_ERROR] = "Provider missing"
663 resp[CONST.JSON_LOGGED_IN] = False
664 compiled_response = HCI.bad_request(
665 content=resp,
666 content_type=CONST.CONTENT_TYPE,
667 headers=RDI.json_header
668 )
669 assert data.status_code == compiled_response.status_code
670 assert data.headers == compiled_response.headers
671 assert data.body == compiled_response.body
672
673
675 """_summary_
676 Function in charge of testing the insuffisant rights function.
677 """
679 title = "Hello World"
680 data = BRI.provider_not_given(title, TCONST.USER_DATA_TOKEN)
681 resp = {}
682 resp[CONST.JSON_TITLE] = title
683 resp[CONST.JSON_MESSAGE] = "You have not given a provider."
684 resp[CONST.JSON_ERROR] = "Provider missing"
685 resp[CONST.JSON_LOGGED_IN] = True
686 compiled_response = HCI.bad_request(
687 content=resp,
688 content_type=CONST.CONTENT_TYPE,
689 headers=RDI.json_header
690 )
692 assert data.status_code == compiled_response.status_code
693 assert data.headers == compiled_response.headers
694 assert data.body == compiled_response.body
695
696
698 """_summary_
699 Function in charge of testing the insuffisant rights function.
700 """
701 title = "Hello World"
702 data = BRI.missing_variable_in_body(title, "not_a_token")
703 resp = {}
704 resp[CONST.JSON_TITLE] = title
705 resp[CONST.JSON_MESSAGE] = "A variable is missing in the body of the request."
706 resp[CONST.JSON_ERROR] = "Missing variable"
707 resp[CONST.JSON_LOGGED_IN] = False
708 compiled_response = HCI.bad_request(
709 content=resp,
710 content_type=CONST.CONTENT_TYPE,
711 headers=RDI.json_header
712 )
713 assert data.status_code == compiled_response.status_code
714 assert data.headers == compiled_response.headers
715 assert data.body == compiled_response.body
716
717
719 """_summary_
720 Function in charge of testing the insuffisant rights function.
721 """
723 title = "Hello World"
724 data = BRI.missing_variable_in_body(title, TCONST.USER_DATA_TOKEN)
725 resp = {}
726 resp[CONST.JSON_TITLE] = title
727 resp[CONST.JSON_MESSAGE] = "A variable is missing in the body of the request."
728 resp[CONST.JSON_ERROR] = "Missing variable"
729 resp[CONST.JSON_LOGGED_IN] = True
730 compiled_response = HCI.bad_request(
731 content=resp,
732 content_type=CONST.CONTENT_TYPE,
733 headers=RDI.json_header
734 )
736 assert data.status_code == compiled_response.status_code
737 assert data.headers == compiled_response.headers
738 assert data.body == compiled_response.body