forked from Shamlan321/OdooSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_access_test.py
More file actions
301 lines (265 loc) · 11.5 KB
/
Copy pathmodule_access_test.py
File metadata and controls
301 lines (265 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""
Odoo Module Access Test
This module provides functionality to test access to various Odoo modules
and their features, ensuring proper installation and configuration.
Author: Shamlan Arshad
License: MIT
"""
import xmlrpc.client
from typing import Dict, List, Any
from tabulate import tabulate
import json
from datetime import datetime, timedelta
import os
from dotenv import load_dotenv
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
# Odoo connection parameters from environment variables
url = os.getenv('ODOO_URL', 'http://localhost:8069')
db = os.getenv('ODOO_DB', 'odoo')
username = os.getenv('ODOO_USERNAME', 'admin')
password = os.getenv('ODOO_PASSWORD', '')
# Connection URLs
common_url = f'{url}/xmlrpc/2/common'
object_url = f'{url}/xmlrpc/2/object'
class OdooModuleTester:
"""Class for testing access to various Odoo modules."""
def __init__(self):
self.url = url
self.db = db
self.username = username
self.password = password
self.uid = None
self.models = None
self.results = {}
self.context = {'lang': os.getenv('DEFAULT_LANGUAGE', 'en_US')}
def connect(self) -> bool:
"""Establish connection with Odoo server"""
try:
common = xmlrpc.client.ServerProxy(common_url)
self.uid = common.authenticate(self.db, self.username, self.password, {})
if self.uid:
self.models = xmlrpc.client.ServerProxy(object_url)
logger.info(f"Successfully connected to Odoo server at {self.url}")
return True
logger.error("Authentication failed")
return False
except Exception as e:
logger.error(f"Connection error: {str(e)}")
return False
def check_module_installed(self, module_name: str) -> bool:
"""Check if a specific module is installed"""
try:
module_domain = [('name', '=', module_name), ('state', '=', 'installed')]
module_count = self.models.execute_kw(self.db, self.uid, self.password,
'ir.module.module', 'search_count', [module_domain]
)
return bool(module_count)
except Exception as e:
logger.error(f"Error checking module {module_name}: {str(e)}")
return False
def test_crm_access(self) -> Dict[str, Any]:
"""Test access to CRM module"""
try:
if not self.check_module_installed('crm'):
return {'status': 'error', 'message': 'CRM module is not installed'}
lead_fields = ['name', 'partner_id', 'email_from', 'phone', 'type', 'stage_id', 'create_date']
leads = self.models.execute_kw(self.db, self.uid, self.password,
'crm.lead', 'search_read',
[[]], {'fields': lead_fields, 'limit': 10}
)
return {
'status': 'success',
'record_count': len(leads),
'sample_data': leads[:5] if leads else []
}
except Exception as e:
logger.error(f"Error testing CRM access: {str(e)}")
return {'status': 'error', 'message': str(e)}
def test_sales_access(self) -> Dict[str, Any]:
"""Test access to Sales module"""
try:
if not self.check_module_installed('sale'):
return {'status': 'error', 'message': 'Sales module is not installed'}
so_fields = ['name', 'partner_id', 'amount_total', 'state', 'date_order']
orders = self.models.execute_kw(self.db, self.uid, self.password,
'sale.order', 'search_read',
[[]], {'fields': so_fields, 'limit': 10}
)
# Get order lines for each order
for order in orders:
order_line_fields = [
'product_id',
'product_uom_qty',
'price_unit',
'price_subtotal',
'tax_id',
'name',
'order_id'
]
order_lines = self.models.execute_kw(self.db, self.uid, self.password,
'sale.order.line', 'search_read',
[[('order_id', '=', order['id'])]],
{'fields': order_line_fields}
)
order['order_lines'] = order_lines
return {
'status': 'success',
'record_count': len(orders),
'sample_data': orders[:5] if orders else []
}
except Exception as e:
logger.error(f"Error testing Sales access: {str(e)}")
return {'status': 'error', 'message': str(e)}
def test_inventory_access(self) -> Dict[str, Any]:
"""Test access to Inventory module"""
try:
if not self.check_module_installed('stock'):
return {'status': 'error', 'message': 'Inventory module is not installed'}
product_fields = ['name', 'qty_available', 'virtual_available', 'incoming_qty', 'outgoing_qty']
products = self.models.execute_kw(self.db, self.uid, self.password,
'product.product', 'search_read',
[[('type', '=', 'product')]], {'fields': product_fields, 'limit': 10}
)
return {
'status': 'success',
'record_count': len(products),
'sample_data': products[:5] if products else []
}
except Exception as e:
logger.error(f"Error testing Inventory access: {str(e)}")
return {'status': 'error', 'message': str(e)}
def test_manufacturing_access(self) -> Dict[str, Any]:
"""Test access to Manufacturing module"""
try:
# Get manufacturing orders
mo_fields = ['name', 'product_id', 'product_qty', 'state', 'date_planned_start']
orders = self.models.execute_kw(self.db, self.uid, self.password,
'mrp.production', 'search_read', [[]], {'fields': mo_fields, 'limit': 5}
)
return {
'status': 'success',
'record_count': len(orders),
'sample_data': orders[:2] if orders else []
}
except Exception as e:
return {'status': 'error', 'message': str(e)}
def test_purchase_access(self) -> Dict[str, Any]:
"""Test access to Purchase module"""
try:
# Get purchase orders
po_fields = ['name', 'partner_id', 'amount_total', 'state', 'date_order']
orders = self.models.execute_kw(self.db, self.uid, self.password,
'purchase.order', 'search_read', [[]], {'fields': po_fields, 'limit': 5}
)
return {
'status': 'success',
'record_count': len(orders),
'sample_data': orders[:2] if orders else []
}
except Exception as e:
return {'status': 'error', 'message': str(e)}
def test_accounting_access(self) -> Dict[str, Any]:
"""Test access to Accounting module"""
try:
# Updated to use type instead of move_type for invoice type filtering
invoice_fields = ['name', 'partner_id', 'amount_total', 'state', 'date']
invoices = self.models.execute_kw(self.db, self.uid, self.password,
'account.move', 'search_read',
[[('type', '=', 'out_invoice')]], {'fields': invoice_fields, 'limit': 5}
)
return {
'status': 'success',
'record_count': len(invoices),
'sample_data': invoices[:2] if invoices else []
}
except Exception as e:
# If the first attempt fails, try without filtering invoice type
try:
invoice_fields = ['name', 'partner_id', 'amount_total', 'state', 'date']
invoices = self.models.execute_kw(self.db, self.uid, self.password,
'account.move', 'search_read',
[[]], {'fields': invoice_fields, 'limit': 5}
)
return {
'status': 'success',
'record_count': len(invoices),
'sample_data': invoices[:2] if invoices else []
}
except Exception as e2:
return {'status': 'error', 'message': f"First attempt: {str(e)}\nSecond attempt: {str(e2)}"}
def test_website_access(self) -> Dict[str, Any]:
"""Test access to Website module"""
try:
# Get website pages
page_fields = ['name', 'url', 'website_published', 'create_date']
pages = self.models.execute_kw(self.db, self.uid, self.password,
'website.page', 'search_read', [[]], {'fields': page_fields, 'limit': 5}
)
return {
'status': 'success',
'record_count': len(pages),
'sample_data': pages[:2] if pages else []
}
except Exception as e:
return {'status': 'error', 'message': str(e)}
def run_all_tests(self):
"""Run all module access tests"""
test_methods = [
('CRM', self.test_crm_access),
('Sales', self.test_sales_access),
('Inventory', self.test_inventory_access),
('Manufacturing', self.test_manufacturing_access),
('Purchase', self.test_purchase_access),
('Accounting', self.test_accounting_access),
('Website', self.test_website_access)
]
logger.info("\nStarting Module Access Tests...")
logger.info(f"Server URL: {self.url}")
logger.info(f"Database: {self.db}")
logger.info(f"User ID: {self.uid}")
for module_name, test_method in test_methods:
print(f"\nTesting {module_name} module access...")
result = test_method()
self.results[module_name] = result
if result['status'] == 'success':
print(f"✓ Success - Found {result['record_count']} records")
if result['sample_data']:
print("\nSample data:")
print(json.dumps(result['sample_data'], indent=2))
else:
print(f"✗ Error: {result['message']}")
# Save results to file
try:
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f'module_access_report_{timestamp}.json'
with open(filename, 'w') as f:
json.dump({
'timestamp': datetime.now().isoformat(),
'server_info': {
'url': self.url,
'database': self.db,
'user_id': self.uid
},
'test_results': self.results
}, f, indent=2)
logger.info(f"\nDetailed results have been saved to '{filename}'")
except Exception as e:
logger.error(f"Error saving results: {str(e)}")
def main():
"""Main function to run the module access tests."""
tester = OdooModuleTester()
logger.info("Connecting to Odoo server...")
if not tester.connect():
logger.error("Failed to connect to Odoo server.")
return
tester.run_all_tests()
if __name__ == "__main__":
main()