iatoolkit 0.7.11__py3-none-any.whl → 0.7.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of iatoolkit might be problematic. Click here for more details.

Files changed (70) hide show
  1. {iatoolkit-0.7.11.dist-info → iatoolkit-0.7.12.dist-info}/METADATA +1 -1
  2. {iatoolkit-0.7.11.dist-info → iatoolkit-0.7.12.dist-info}/RECORD +70 -4
  3. {iatoolkit-0.7.11.dist-info → iatoolkit-0.7.12.dist-info}/top_level.txt +1 -0
  4. tests/__init__.py +5 -0
  5. tests/common/__init__.py +0 -0
  6. tests/common/test_auth.py +279 -0
  7. tests/common/test_routes.py +42 -0
  8. tests/common/test_session_manager.py +59 -0
  9. tests/common/test_util.py +444 -0
  10. tests/companies/__init__.py +5 -0
  11. tests/conftest.py +36 -0
  12. tests/infra/__init__.py +5 -0
  13. tests/infra/connectors/__init__.py +5 -0
  14. tests/infra/connectors/test_google_drive_connector.py +107 -0
  15. tests/infra/connectors/test_local_file_connector.py +85 -0
  16. tests/infra/connectors/test_s3_connector.py +95 -0
  17. tests/infra/test_call_service.py +92 -0
  18. tests/infra/test_database_manager.py +59 -0
  19. tests/infra/test_gemini_adapter.py +137 -0
  20. tests/infra/test_google_chat_app.py +68 -0
  21. tests/infra/test_llm_client.py +165 -0
  22. tests/infra/test_llm_proxy.py +122 -0
  23. tests/infra/test_mail_app.py +94 -0
  24. tests/infra/test_openai_adapter.py +105 -0
  25. tests/infra/test_redis_session_manager_service.py +117 -0
  26. tests/repositories/__init__.py +5 -0
  27. tests/repositories/test_database_manager.py +87 -0
  28. tests/repositories/test_document_repo.py +76 -0
  29. tests/repositories/test_llm_query_repo.py +340 -0
  30. tests/repositories/test_models.py +38 -0
  31. tests/repositories/test_profile_repo.py +142 -0
  32. tests/repositories/test_tasks_repo.py +76 -0
  33. tests/repositories/test_vs_repo.py +107 -0
  34. tests/services/__init__.py +5 -0
  35. tests/services/test_dispatcher_service.py +274 -0
  36. tests/services/test_document_service.py +181 -0
  37. tests/services/test_excel_service.py +208 -0
  38. tests/services/test_file_processor_service.py +121 -0
  39. tests/services/test_history_service.py +164 -0
  40. tests/services/test_jwt_service.py +255 -0
  41. tests/services/test_load_documents_service.py +112 -0
  42. tests/services/test_mail_service.py +70 -0
  43. tests/services/test_profile_service.py +379 -0
  44. tests/services/test_prompt_manager_service.py +190 -0
  45. tests/services/test_query_service.py +243 -0
  46. tests/services/test_search_service.py +39 -0
  47. tests/services/test_sql_service.py +160 -0
  48. tests/services/test_tasks_service.py +252 -0
  49. tests/services/test_user_feedback_service.py +389 -0
  50. tests/services/test_user_session_context_service.py +132 -0
  51. tests/views/__init__.py +5 -0
  52. tests/views/test_change_password_view.py +191 -0
  53. tests/views/test_chat_token_request_view.py +188 -0
  54. tests/views/test_chat_view.py +98 -0
  55. tests/views/test_download_file_view.py +149 -0
  56. tests/views/test_external_chat_login_view.py +120 -0
  57. tests/views/test_external_login_view.py +102 -0
  58. tests/views/test_file_store_view.py +128 -0
  59. tests/views/test_forgot_password_view.py +142 -0
  60. tests/views/test_history_view.py +336 -0
  61. tests/views/test_home_view.py +61 -0
  62. tests/views/test_llm_query_view.py +154 -0
  63. tests/views/test_login_view.py +114 -0
  64. tests/views/test_prompt_view.py +111 -0
  65. tests/views/test_signup_view.py +140 -0
  66. tests/views/test_tasks_review_view.py +104 -0
  67. tests/views/test_tasks_view.py +130 -0
  68. tests/views/test_user_feedback_view.py +214 -0
  69. tests/views/test_verify_user_view.py +110 -0
  70. {iatoolkit-0.7.11.dist-info → iatoolkit-0.7.12.dist-info}/WHEEL +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: iatoolkit
3
- Version: 0.7.11
3
+ Version: 0.7.12
4
4
  Summary: IAToolkit
5
5
  Author: Fernando Libedinsky
6
6
  License-Expression: MIT
@@ -122,7 +122,73 @@ iatoolkit/views/tasks_review_view.py,sha256=keLsLCyOTTlcoIapnB_lbuSvLwrPVZVpBiFC
122
122
  iatoolkit/views/tasks_view.py,sha256=a3anTXrJTTvbQuc6PSpOzidLKQFL4hWa7PI2Cppcz8w,4110
123
123
  iatoolkit/views/user_feedback_view.py,sha256=G37zmP8P4LvZrSymNJ5iFXhLZg1A3BEwRfTpH1Iam5w,2652
124
124
  iatoolkit/views/verify_user_view.py,sha256=a3q4wHJ8mKAEmgbNTOcnX4rMikROjOR3mHvCr30qGGA,2351
125
- iatoolkit-0.7.11.dist-info/METADATA,sha256=aW2_npSGL4nUTH8Iq5RnAvKBmT38Xa9RlPO5cngT80Y,9301
126
- iatoolkit-0.7.11.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
127
- iatoolkit-0.7.11.dist-info/top_level.txt,sha256=V_w4QvDx0b1RXiy8zTCrD1Bp7AZkFe3_O0-9fMiwogg,10
128
- iatoolkit-0.7.11.dist-info/RECORD,,
125
+ tests/__init__.py,sha256=5JqK9sZ6jBuK83zDQokUhxQ0wuJJJ9DXB8pYCLkX7X4,102
126
+ tests/conftest.py,sha256=S000Ry-rWczEDsndTtOvsj2zf2EYgjHi78oVn60hoOU,1094
127
+ tests/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
128
+ tests/common/test_auth.py,sha256=WViWPG3E8OldgZyPbl-lp5rosjgPQb9MlADGLr1rq4k,14338
129
+ tests/common/test_routes.py,sha256=_Fz8mYqxp8YZ39CSng0CQOIRs9fOfgC42iU9XiHLMzU,1560
130
+ tests/common/test_session_manager.py,sha256=8FLa8Z7ueLYNnJeJyTgCm8pp1DY6QvC0b0ZxZC2vpKc,2388
131
+ tests/common/test_util.py,sha256=SMh0pSIv8K-bCeMZeYBbCD70o6MpOUXHQH6kZFLLN5c,20653
132
+ tests/companies/__init__.py,sha256=5JqK9sZ6jBuK83zDQokUhxQ0wuJJJ9DXB8pYCLkX7X4,102
133
+ tests/infra/__init__.py,sha256=5JqK9sZ6jBuK83zDQokUhxQ0wuJJJ9DXB8pYCLkX7X4,102
134
+ tests/infra/test_call_service.py,sha256=p5kFqXFojSmHucLTyika2u3wvpQwJRCrAlVOPeohoVE,3747
135
+ tests/infra/test_database_manager.py,sha256=SMU_pcCsW89b6GyykAU2LEO8UaHL9gO5rdeWGFP3A6g,2503
136
+ tests/infra/test_gemini_adapter.py,sha256=gMjSgxh854sC08vYQpH6QSIX9w921U3g_Jrt_HU1i1Y,6292
137
+ tests/infra/test_google_chat_app.py,sha256=D1iUaGy8Z-PHufJuQ607lGEUOFueedoziXR8J9b7qmM,2363
138
+ tests/infra/test_llm_client.py,sha256=i5uRPy3k2csoNOg16dlVAaZq93MDTk5OxLgRPrsyDow,7619
139
+ tests/infra/test_llm_proxy.py,sha256=Y9F8ozVsnPLOiIt_Minr_VvANEz0x0JIPvFaWJBCLgk,5753
140
+ tests/infra/test_mail_app.py,sha256=6Mhj3HbnQ_sVaQc7wgTpIEFb2GAzc4kkFNWgPWBCmp0,3727
141
+ tests/infra/test_openai_adapter.py,sha256=4CLLIOqQLaC_zpc3clyoA9fiMu5uJmfDoPqs7hEw4Qo,4319
142
+ tests/infra/test_redis_session_manager_service.py,sha256=NbrUVxmEmWIZc9TtnFd1IiWwm1N0Ih0y7chP7z5Mx34,4914
143
+ tests/infra/connectors/__init__.py,sha256=5JqK9sZ6jBuK83zDQokUhxQ0wuJJJ9DXB8pYCLkX7X4,102
144
+ tests/infra/connectors/test_google_drive_connector.py,sha256=UxbR64xiDpFyeE18--BHiKgvwEuaWt9b4Cf6bL6LreM,4434
145
+ tests/infra/connectors/test_local_file_connector.py,sha256=-1wpjXt2DOn_NbSUerdGFd-g-NI3rWS5vDoL3PFqxGA,3459
146
+ tests/infra/connectors/test_s3_connector.py,sha256=2hsJvKPyTWQj6kkNjtpRwsLNdvW3O5N5JlhzUM67x5Q,3512
147
+ tests/repositories/__init__.py,sha256=5JqK9sZ6jBuK83zDQokUhxQ0wuJJJ9DXB8pYCLkX7X4,102
148
+ tests/repositories/test_database_manager.py,sha256=C6pqzxPCtk0DcZE7VNaFCT5ahONO7mWFKDo1bxb-9sU,3990
149
+ tests/repositories/test_document_repo.py,sha256=sqNL5yVM_8phPaxcYR3FupuTWN5RNG8yLfeDxR07uIM,2543
150
+ tests/repositories/test_llm_query_repo.py,sha256=Vdu85MI_L7Nh5ksNz9X3d4hORp9hXaXPwH1AUbuN5YE,13228
151
+ tests/repositories/test_models.py,sha256=CJbz3U89BgPKmP61pwGdwDW8vHvPROKwYfqcg4lx-_Q,1627
152
+ tests/repositories/test_profile_repo.py,sha256=Ex9R5JH2uyam8dt8Nl-1yiNYYj72Ma9A3Jlo_JkwktI,4981
153
+ tests/repositories/test_tasks_repo.py,sha256=ZtOOjJTb_j8dKEpirZTQAnR9fhDDO7xQWZU710So9Bk,2769
154
+ tests/repositories/test_vs_repo.py,sha256=PGN5PqQBtlwMC_UFU3qMSqxtoBSNz1YvZK4yYj0O0UU,4595
155
+ tests/services/__init__.py,sha256=5JqK9sZ6jBuK83zDQokUhxQ0wuJJJ9DXB8pYCLkX7X4,102
156
+ tests/services/test_dispatcher_service.py,sha256=iALQx0I94SehG4eXqwvdswyYrPVShXFIlk-GwwbWyzw,13086
157
+ tests/services/test_document_service.py,sha256=Z62VjjIn1EPgDT2_2jEVI6AQoO3yOdskc_FDqlvML1Q,8085
158
+ tests/services/test_excel_service.py,sha256=7Gyy82MFqOVFIGL7RXMIOmal22ne-lsgAfY16azDAMM,8434
159
+ tests/services/test_file_processor_service.py,sha256=XzdA581hoAhevp8atqgI7jxTY1gmHE7rnMllihRS9S0,4794
160
+ tests/services/test_history_service.py,sha256=VF6IgogsgaFe64LNDeXMFcLrWjuGLf3Vj_srnFfkneM,7273
161
+ tests/services/test_jwt_service.py,sha256=3TYtuPk1ps0aa5TKGvd30Q4F7Q1FEsqK6Gr1wjW8-q8,10325
162
+ tests/services/test_load_documents_service.py,sha256=zDcHSXVQAdtAZCVWecTl-pYlkA4Sp8JLxKpu82LB69A,4640
163
+ tests/services/test_mail_service.py,sha256=PPy5XA2lYFFBNdmU1Ea-cfqszwQZrrqZSZU1OQ26lDE,2295
164
+ tests/services/test_profile_service.py,sha256=WzvVxeK5AYUDWJLBRB43vVDLHi64cIkPYPcynMEGZzM,14831
165
+ tests/services/test_prompt_manager_service.py,sha256=B6j12RNjljAKZnd7vy1CNAdZxwKiTWD5l4uxOGKVuvE,9395
166
+ tests/services/test_query_service.py,sha256=vq8dhteJOFiA0eGo1s5eBzasx7noZFrv96XdKFKhmS8,11331
167
+ tests/services/test_search_service.py,sha256=n8C5mjDDCVX4NAJvS3mKgw-WsLWu5qUA8eTOxy-g9NU,1183
168
+ tests/services/test_sql_service.py,sha256=bHDiPzjfM7-mbM4eEXlMVk3vcvefhlGCx3bdIrlriVM,7753
169
+ tests/services/test_tasks_service.py,sha256=V_WcglSX4CAtOkkKhZKl7Q92LsODHLRu66TqydiSQP4,10598
170
+ tests/services/test_user_feedback_service.py,sha256=HMU75j48jT-Chls78QEXoq92Hpq7TXP-m8xXIAL0rK0,15859
171
+ tests/services/test_user_session_context_service.py,sha256=c49RqHuoY6W_u-eR-UR05SNwDfqvRpRQWAJ6Ghyh7sQ,6799
172
+ tests/views/__init__.py,sha256=5JqK9sZ6jBuK83zDQokUhxQ0wuJJJ9DXB8pYCLkX7X4,102
173
+ tests/views/test_change_password_view.py,sha256=5dIjx6_-rNHqj9yrkz00ajMxmZENBoTwN0gNFGUzxRE,9213
174
+ tests/views/test_chat_token_request_view.py,sha256=jwgGaNmBPTK9irKTqsDTTTIZawpYBxfGXBUv0xxvCQ0,9850
175
+ tests/views/test_chat_view.py,sha256=24dySNWlpu7EXkIeFJwUUXwb5xAq3Kdv2IVzPSVSWuY,3798
176
+ tests/views/test_download_file_view.py,sha256=l0o5DnI29ByV7N5kK4UoTKhPmStKuiAOZKFPxCSZszE,5969
177
+ tests/views/test_external_chat_login_view.py,sha256=1PLOPqVJjR-7d1DV24uuA7QOYEeKsIusjw6poBYTFB8,5405
178
+ tests/views/test_external_login_view.py,sha256=Lf4VHxTyddx_RSTvYoxcZr8cG82bvJ0pdVqU1CJpwcg,4394
179
+ tests/views/test_file_store_view.py,sha256=hT2k5-ppGNphKnQwueTvyzHQUGJ1umVXpPNOpTL8GOI,4837
180
+ tests/views/test_forgot_password_view.py,sha256=GeaGMQpY20T9_cM_kBTrwRcx-Z1zsDbytB-KoyM9p2o,6224
181
+ tests/views/test_history_view.py,sha256=AxNNjVEEYVdLDNa6aZaWdSE3FMquiqqDGxvl57UYdD8,12322
182
+ tests/views/test_home_view.py,sha256=uqPCQnDyTm25l6YLrwzINrHMd16oyY3yrqXui1LChjg,2438
183
+ tests/views/test_llm_query_view.py,sha256=3Mf_mfZNW9wTG7BivwEW0OL-QXplV2zRakl5rIUOvu8,5121
184
+ tests/views/test_login_view.py,sha256=P-6n6LGnQ87pC9a48FMz0okVm7eukpSeIKDnMUABs3I,4812
185
+ tests/views/test_prompt_view.py,sha256=y8mIXc0AqOoSe2QJJX3UdiFgA1a2TYFCcIwGiupxR0U,4563
186
+ tests/views/test_signup_view.py,sha256=WVh11rzH2MbJWS9rvYIn9R1aCWJLnpIhIK_k7MQxi0I,6534
187
+ tests/views/test_tasks_review_view.py,sha256=HXbAY99rbcdg-ZoaU3kxp-jNTPgOhgiTyKmV9rUcFyQ,3743
188
+ tests/views/test_tasks_view.py,sha256=TNdV69Vt17uc-vBI37A7qtRNqyPho2MoW7cgGxQKecU,4551
189
+ tests/views/test_user_feedback_view.py,sha256=IfKOpsncmf5ciMDsGuPtro9GdKXkxhRzsysVlEEM_HA,9181
190
+ tests/views/test_verify_user_view.py,sha256=dxQ3ibOETEuP8M50Gmwbmzj2L6UU7PAyC91Wp9Xs0K0,4772
191
+ iatoolkit-0.7.12.dist-info/METADATA,sha256=vhLpVH2zk4dOidIF2--_Z-aSRdqIj530gc-e4zSNCgo,9301
192
+ iatoolkit-0.7.12.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
193
+ iatoolkit-0.7.12.dist-info/top_level.txt,sha256=or0Ar3Su6BhTy86zRrUwMAWtsR8Nk-tFEwdC0CZpKCs,16
194
+ iatoolkit-0.7.12.dist-info/RECORD,,
tests/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ # Copyright (c) 2024 Fernando Libedinsky
2
+ # Product: IAToolkit
3
+ #
4
+ # IAToolkit is open source software.
5
+
File without changes
@@ -0,0 +1,279 @@
1
+ # Copyright (c) 2024 Fernando Libedinsky
2
+ # Product: IAToolkit
3
+ #
4
+ # IAToolkit is open source software.
5
+
6
+ from flask import Flask, Response
7
+ from unittest.mock import patch, MagicMock
8
+ from iatoolkit.common.auth import IAuthentication
9
+ import pytest
10
+ from datetime import datetime, timezone
11
+ from iatoolkit.repositories.profile_repo import ProfileRepo
12
+ from iatoolkit.services.jwt_service import JWTService
13
+ from werkzeug.exceptions import HTTPException
14
+
15
+ CURRENT_TIME = datetime.now(timezone.utc).timestamp()
16
+
17
+
18
+ class TestAuth:
19
+
20
+ def setup_method(self):
21
+ self.app = Flask(__name__)
22
+ self.app.config['TESTING'] = True
23
+
24
+ self.profile_repo = MagicMock(spec=ProfileRepo)
25
+ self.jwt_service = MagicMock(spec=JWTService)
26
+ self.iauth_service = IAuthentication(
27
+ profile_repo=self.profile_repo,
28
+ jwt_service=self.jwt_service
29
+ )
30
+
31
+ @self.app.route('/<company_short_name>/protected')
32
+ def protected(company_short_name):
33
+ with self.app.test_request_context():
34
+ auth_result = self.iauth_service.check_if_user_is_logged_in(company_short_name)
35
+ if isinstance(auth_result, Response):
36
+ return auth_result
37
+ return "Access Granted", 200
38
+
39
+ @self.app.route('/login')
40
+ def login():
41
+ return "Login Page", 200
42
+
43
+ self.client = self.app.test_client()
44
+
45
+ def teardown_method(self):
46
+ patch.stopall()
47
+
48
+ # --- Pruebas para check_if_user_is_logged_in (de TestAuth original) ---
49
+
50
+ def test_allows_access_if_user_authenticated_and_active(self):
51
+ with patch('iatoolkit.common.auth.SessionManager.get') as mock_get, \
52
+ patch('iatoolkit.common.auth.SessionManager.set') as mock_set:
53
+ mock_get.side_effect = lambda key, default=None: {
54
+ 'user': {'id': 1, 'username': 'test_user'},
55
+ 'company_short_name': 'test_company',
56
+ 'last_activity': CURRENT_TIME
57
+ }.get(key, default)
58
+
59
+ response = self.client.get('/test_company/protected')
60
+
61
+ assert response.status_code == 200
62
+ assert response.data == b"Access Granted"
63
+ mock_set.assert_called_with("last_activity", pytest.approx(CURRENT_TIME, 1))
64
+
65
+ def test_redirect_if_user_not_authenticated(self):
66
+ with patch('iatoolkit.common.auth.SessionManager.get') as mock_get:
67
+ mock_get.side_effect = lambda key, default=None: None if key == "user" else default
68
+ response = self.client.get('/test_company/protected')
69
+ assert response.status_code == 302
70
+ assert "/login" in response.headers['Location']
71
+
72
+ def test_redirect_if_last_activity_missing(self):
73
+ with patch('iatoolkit.common.auth.SessionManager.get') as mock_get, \
74
+ patch('iatoolkit.common.auth.SessionManager.clear') as mock_clear:
75
+ mock_get.side_effect = lambda key, default=None: {
76
+ 'user': {'id': 1, 'username': 'test_user'},
77
+ 'company_short_name': 'test_company'
78
+ }.get(key, default)
79
+
80
+ response = self.client.get('/test_company/protected')
81
+
82
+ assert response.status_code == 302
83
+ assert "/login" in response.headers['Location']
84
+ mock_clear.assert_called_once()
85
+
86
+ # --- Pruebas para verify (de TestAuthVerify original) ---
87
+
88
+ def test_verify_with_jwt_success(self):
89
+ with patch.object(self.iauth_service, '_authenticate_via_chat_jwt') as mock_auth_jwt, \
90
+ patch.object(self.iauth_service, '_authenticate_via_api_key') as mock_auth_api_key:
91
+ mock_auth_jwt.return_value = (123, 'ext_user_1', None)
92
+
93
+ with self.app.test_request_context():
94
+ result = self.iauth_service.verify('some_company')
95
+
96
+ assert result['success'] is True
97
+ assert result['company_id'] == 123
98
+ assert result['external_user_id'] == 'ext_user_1'
99
+ mock_auth_jwt.assert_called_once_with('some_company')
100
+ mock_auth_api_key.assert_not_called()
101
+
102
+ def test_verify_with_jwt_failure(self):
103
+ with patch.object(self.iauth_service, '_authenticate_via_chat_jwt') as mock_auth_jwt, \
104
+ patch.object(self.iauth_service, '_authenticate_via_api_key') as mock_auth_api_key:
105
+ mock_auth_jwt.return_value = (None, None, "Error de JWT")
106
+
107
+ with self.app.test_request_context():
108
+ result = self.iauth_service.verify('some_company')
109
+
110
+ assert result == {"error_message": "Fallo de autenticación JWT"}
111
+ mock_auth_api_key.assert_not_called()
112
+
113
+ def test_verify_with_api_key_success(self):
114
+ with patch.object(self.iauth_service, '_authenticate_via_chat_jwt') as mock_auth_jwt, \
115
+ patch.object(self.iauth_service, '_authenticate_via_api_key') as mock_auth_api_key, \
116
+ patch.object(self.iauth_service, 'check_if_user_is_logged_in') as mock_check_if_logged_in:
117
+ mock_auth_jwt.return_value = (None, None, None)
118
+ mock_auth_api_key.return_value = (456, None)
119
+
120
+ with self.app.test_request_context():
121
+ result = self.iauth_service.verify('some_company', body_external_user_id='body_user_2')
122
+
123
+ assert result['success'] is True
124
+ assert result['company_id'] == 456
125
+ assert result['external_user_id'] == 'body_user_2'
126
+ mock_auth_api_key.assert_called_once_with('some_company')
127
+ mock_check_if_logged_in.assert_not_called()
128
+
129
+ def test_verify_with_api_key_failure(self):
130
+ with patch.object(self.iauth_service, '_authenticate_via_chat_jwt') as mock_auth_jwt, \
131
+ patch.object(self.iauth_service, '_authenticate_via_api_key') as mock_auth_api_key:
132
+ mock_auth_jwt.return_value = (None, None, None)
133
+ mock_auth_api_key.return_value = (None, "Error de API Key")
134
+
135
+ with self.app.test_request_context():
136
+ result = self.iauth_service.verify('some_company')
137
+
138
+ assert result == {"error_message": "Fallo de autenticación API Key"}
139
+
140
+ def test_verify_with_session_success(self):
141
+ with patch.object(self.iauth_service, '_authenticate_via_chat_jwt') as mock_auth_jwt, \
142
+ patch.object(self.iauth_service, '_authenticate_via_api_key') as mock_auth_api_key, \
143
+ patch.object(self.iauth_service, 'check_if_user_is_logged_in') as mock_check_if_logged_in, \
144
+ patch('iatoolkit.common.auth.SessionManager.get') as mock_session_get:
145
+ mock_auth_jwt.return_value = (None, None, None)
146
+ mock_auth_api_key.return_value = (None, None)
147
+ mock_check_if_logged_in.return_value = None
148
+ mock_session_get.side_effect = lambda key, default=None: {'user_id': 789, 'company_id': 999}.get(key,
149
+ default)
150
+
151
+ with self.app.test_request_context():
152
+ result = self.iauth_service.verify('some_company')
153
+
154
+ assert result['success'] is True
155
+ assert result['company_id'] == 999
156
+ assert result['local_user_id'] == 789
157
+ mock_check_if_logged_in.assert_called_once_with('some_company')
158
+
159
+ def test_verify_with_session_incomplete_data(self):
160
+ with patch.object(self.iauth_service, '_authenticate_via_chat_jwt') as mock_auth_jwt, \
161
+ patch.object(self.iauth_service, '_authenticate_via_api_key') as mock_auth_api_key, \
162
+ patch.object(self.iauth_service, 'check_if_user_is_logged_in') as mock_check_if_logged_in, \
163
+ patch('iatoolkit.common.auth.SessionManager.get') as mock_session_get:
164
+ mock_auth_jwt.return_value = (None, None, None)
165
+ mock_auth_api_key.return_value = (None, None)
166
+ mock_check_if_logged_in.return_value = None
167
+ mock_session_get.side_effect = lambda key, default=None: {'user_id': 789}.get(key, default)
168
+
169
+ with self.app.test_request_context():
170
+ result = self.iauth_service.verify('some_company')
171
+
172
+ assert result == {"error_message": "Fallo interno en la autenticación o no autenticado"}
173
+
174
+ def test_verify_with_session_check_fails_by_exception(self):
175
+ with patch.object(self.iauth_service, '_authenticate_via_chat_jwt') as mock_auth_jwt, \
176
+ patch.object(self.iauth_service, '_authenticate_via_api_key') as mock_auth_api_key, \
177
+ patch.object(self.iauth_service, 'check_if_user_is_logged_in') as mock_check_if_logged_in:
178
+ mock_auth_jwt.return_value = (None, None, None)
179
+ mock_auth_api_key.return_value = (None, None)
180
+ mock_check_if_logged_in.side_effect = HTTPException
181
+
182
+ with self.app.test_request_context(), pytest.raises(HTTPException):
183
+ self.iauth_service.verify('some_company')
184
+
185
+ # --- Nuevas pruebas para _authenticate_via_api_key ---
186
+
187
+ def test_authenticate_via_api_key_success(self):
188
+ with self.app.test_request_context(headers={'Authorization': 'Bearer valid_key'}):
189
+ mock_api_entry = MagicMock()
190
+ mock_api_entry.company.short_name = 'test_company'
191
+ mock_api_entry.company_id = 123
192
+ self.profile_repo.get_active_api_key_entry.return_value = mock_api_entry
193
+
194
+ company_id, error = self.iauth_service._authenticate_via_api_key('test_company')
195
+
196
+ assert company_id == 123
197
+ assert error is None
198
+ self.profile_repo.get_active_api_key_entry.assert_called_once_with('valid_key')
199
+
200
+ def test_authenticate_via_api_key_no_header(self):
201
+ with self.app.test_request_context(headers={}):
202
+ company_id, error = self.iauth_service._authenticate_via_api_key('test_company')
203
+ assert company_id is None
204
+ assert error is None
205
+ self.profile_repo.get_active_api_key_entry.assert_not_called()
206
+
207
+ def test_authenticate_via_api_key_wrong_scheme(self):
208
+ with self.app.test_request_context(headers={'Authorization': 'Basic some_token'}):
209
+ company_id, error = self.iauth_service._authenticate_via_api_key('test_company')
210
+ assert company_id is None
211
+ assert error is None
212
+ self.profile_repo.get_active_api_key_entry.assert_not_called()
213
+
214
+ def test_authenticate_via_api_key_inactive_key(self):
215
+ with self.app.test_request_context(headers={'Authorization': 'Bearer inactive_key'}):
216
+ self.profile_repo.get_active_api_key_entry.return_value = None
217
+ company_id, error = self.iauth_service._authenticate_via_api_key('test_company')
218
+ assert company_id is None
219
+ assert error == "API Key inválida o inactiva"
220
+
221
+ def test_authenticate_via_api_key_company_mismatch(self):
222
+ with self.app.test_request_context(headers={'Authorization': 'Bearer valid_key'}):
223
+ mock_api_entry = MagicMock()
224
+ mock_api_entry.company.short_name = 'other_company'
225
+ self.profile_repo.get_active_api_key_entry.return_value = mock_api_entry
226
+
227
+ company_id, error = self.iauth_service._authenticate_via_api_key('test_company')
228
+ assert company_id is None
229
+ assert error == "API Key no es válida para la compañía test_company"
230
+
231
+ def test_authenticate_via_api_key_repo_exception(self):
232
+ with self.app.test_request_context(headers={'Authorization': 'Bearer any_key'}):
233
+ self.profile_repo.get_active_api_key_entry.side_effect = Exception("DB error")
234
+ company_id, error = self.iauth_service._authenticate_via_api_key('test_company')
235
+ assert company_id is None
236
+ assert error == "Error interno del servidor al validar API Key"
237
+
238
+ # --- Nuevas pruebas para _authenticate_via_chat_jwt ---
239
+
240
+ def test_authenticate_via_chat_jwt_success(self):
241
+ with self.app.test_request_context(headers={'X-Chat-Token': 'valid_jwt'}):
242
+ self.jwt_service.validate_chat_jwt.return_value = {'company_id': 123, 'external_user_id': 'ext_user'}
243
+ company_id, external_user_id, error = self.iauth_service._authenticate_via_chat_jwt('test_company')
244
+ assert company_id == 123
245
+ assert external_user_id == 'ext_user'
246
+ assert error is None
247
+ self.jwt_service.validate_chat_jwt.assert_called_once_with('valid_jwt', 'test_company')
248
+
249
+ def test_authenticate_via_chat_jwt_no_header(self):
250
+ with self.app.test_request_context(headers={}):
251
+ company_id, external_user_id, error = self.iauth_service._authenticate_via_chat_jwt('test_company')
252
+ assert company_id is None
253
+ assert external_user_id is None
254
+ assert error is None
255
+ self.jwt_service.validate_chat_jwt.assert_not_called()
256
+
257
+ def test_authenticate_via_chat_jwt_validation_fails(self):
258
+ with self.app.test_request_context(headers={'X-Chat-Token': 'invalid_jwt'}):
259
+ self.jwt_service.validate_chat_jwt.return_value = None
260
+ company_id, external_user_id, error = self.iauth_service._authenticate_via_chat_jwt('test_company')
261
+ assert company_id is None
262
+ assert external_user_id is None
263
+ assert error == "Token de chat expirado, debes reingresar al chat"
264
+
265
+ def test_authenticate_via_chat_jwt_incomplete_payload_no_company_id(self):
266
+ with self.app.test_request_context(headers={'X-Chat-Token': 'valid_jwt'}):
267
+ self.jwt_service.validate_chat_jwt.return_value = {'external_user_id': 'ext_user'}
268
+ company_id, external_user_id, error = self.iauth_service._authenticate_via_chat_jwt('test_company')
269
+ assert company_id is None
270
+ assert external_user_id is None
271
+ assert error == "Token de chat con formato interno incorrecto"
272
+
273
+ def test_authenticate_via_chat_jwt_incomplete_payload_no_external_id(self):
274
+ with self.app.test_request_context(headers={'X-Chat-Token': 'valid_jwt'}):
275
+ self.jwt_service.validate_chat_jwt.return_value = {'company_id': 123}
276
+ company_id, external_user_id, error = self.iauth_service._authenticate_via_chat_jwt('test_company')
277
+ assert company_id is None
278
+ assert external_user_id is None
279
+ assert error == "Token de chat con formato interno incorrecto"
@@ -0,0 +1,42 @@
1
+ # Copyright (c) 2024 Fernando Libedinsky
2
+ # Product: IAToolkit
3
+ #
4
+ # IAToolkit is open source software.
5
+
6
+ from flask import Flask
7
+ from unittest.mock import patch
8
+
9
+
10
+ class TestRoutes:
11
+ def setup_method(self):
12
+ # Configurar la aplicación Flask para pruebas
13
+ self.app = Flask(__name__)
14
+ self.app.config['VERSION'] = "1.0.0"
15
+ self.app.secret_key = 'test_secret'
16
+
17
+ # Concentramos todos los mocks y patches aquí:
18
+
19
+ # Patch para el SessionManager (por ejemplo para evitar lecturas de sesión reales)
20
+ self.session_manager_patch = patch("common.routes.SessionManager")
21
+ self.mock_session_manager = self.session_manager_patch.start()
22
+ # Para este ejemplo, no es necesario configurar un return_value, pero se podría definir según se requiera
23
+
24
+ # Patch para la función flash, reemplazándola para que no intente enviar mensajes reales
25
+ self.flash_patch = patch("common.routes.flash")
26
+ self.mock_flash = self.flash_patch.start()
27
+
28
+ # Patch para render_template, que es usado por la ruta "/about" y otros
29
+ self.render_template_patch = patch("common.routes.render_template", return_value="<html>About</html>")
30
+ self.mock_render_template = self.render_template_patch.start()
31
+
32
+ # Registrar las rutas en la aplicación
33
+ from iatoolkit.common.routes import register_routes
34
+ register_routes(self.app)
35
+
36
+ # Crear el cliente de pruebas
37
+ self.client = self.app.test_client()
38
+
39
+ def teardown_method(self, method):
40
+ patch.stopall()
41
+
42
+
@@ -0,0 +1,59 @@
1
+ # Copyright (c) 2024 Fernando Libedinsky
2
+ # Product: IAToolkit
3
+ #
4
+ # IAToolkit is open source software.
5
+
6
+ import unittest
7
+ from unittest.mock import patch
8
+ from iatoolkit.common.session_manager import SessionManager
9
+
10
+
11
+ class TestSessionManager(unittest.TestCase):
12
+
13
+ def setUp(self):
14
+ """
15
+ Configura los patches y mocks comunes para las pruebas.
16
+ """
17
+ # Parchear el objeto 'session' como un diccionario en todos los tests
18
+ self.session_patcher = patch("iatoolkit.common.session_manager.session", new_callable=dict)
19
+ self.mock_session = self.session_patcher.start() # Iniciar el patch y obtener el mock
20
+
21
+ def tearDown(self):
22
+ """
23
+ Detiene todos los patches después de cada prueba.
24
+ """
25
+ patch.stopall() # Detener cualquier parche activo y limpiar el entorno
26
+
27
+ def test_set(self):
28
+ """Prueba que el método set almacena un valor en la sesión."""
29
+ SessionManager.set("key", "value")
30
+ self.assertEqual(self.mock_session["key"], "value")
31
+
32
+ def test_get_existing_key(self):
33
+ """Prueba que el método get devuelve el valor correcto si la clave existe."""
34
+ self.mock_session["key"] = "value"
35
+ result = SessionManager.get("key")
36
+ self.assertEqual(result, "value")
37
+
38
+ def test_get_non_existing_key_with_default(self):
39
+ """Prueba que el método get devuelve el valor predeterminado si la clave no existe."""
40
+ result = SessionManager.get("non_existing_key", default="default_value")
41
+ self.assertEqual(result, "default_value")
42
+
43
+ def test_remove_existing_key(self):
44
+ """Prueba que el método remove elimina correctamente una clave existente."""
45
+ self.mock_session["key"] = "value"
46
+ SessionManager.remove("key")
47
+ self.assertNotIn("key", self.mock_session)
48
+
49
+ def test_remove_non_existing_key(self):
50
+ """Prueba que el método remove no lanza errores si la clave no existe."""
51
+ SessionManager.remove("non_existing_key")
52
+ # No debería hacer nada, y la sesión debe permanecer vacía
53
+ self.assertEqual(len(self.mock_session), 0)
54
+
55
+ def test_clear(self):
56
+ """Prueba que el método clear elimina todos los elementos de la sesión."""
57
+ self.mock_session.update({"key1": "value1", "key2": "value2"})
58
+ SessionManager.clear()
59
+ self.assertEqual(len(self.mock_session), 0)