mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-10-31 21:35:42 +00:00 
			
		
		
		
	More checks
This commit is contained in:
		
							
								
								
									
										10
									
								
								lib/api.dart
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								lib/api.dart
									
									
									
									
									
								
							| @@ -208,7 +208,7 @@ class InvenTreeAPI { | |||||||
|    * Check server connection and display messages if not connected. |    * Check server connection and display messages if not connected. | ||||||
|    * Useful as a precursor check before performing operations. |    * Useful as a precursor check before performing operations. | ||||||
|    */ |    */ | ||||||
|   bool checkConnection(BuildContext context) { |   bool checkConnection() { | ||||||
|     // Firstly, is the server connected? |     // Firstly, is the server connected? | ||||||
|     if (!isConnected()) { |     if (!isConnected()) { | ||||||
|  |  | ||||||
| @@ -292,14 +292,10 @@ class InvenTreeAPI { | |||||||
|   static final InvenTreeAPI _api = InvenTreeAPI._internal(); |   static final InvenTreeAPI _api = InvenTreeAPI._internal(); | ||||||
|  |  | ||||||
|   // API endpoint for receiving purchase order line items was introduced in v12 |   // API endpoint for receiving purchase order line items was introduced in v12 | ||||||
|   bool supportPoReceive() { |   bool get supportsPoReceive => apiVersion >= 12; | ||||||
|     return apiVersion >= 12; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   // "Modern" API transactions were implemented in API v14 |   // "Modern" API transactions were implemented in API v14 | ||||||
|   bool supportModernStockTransactions() { |   bool get supportsModernStockTransactions => apiVersion >= 14; | ||||||
|     return apiVersion >= 14; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   /* |   /* | ||||||
|    * Connect to the remote InvenTree server: |    * Connect to the remote InvenTree server: | ||||||
|   | |||||||
| @@ -533,7 +533,7 @@ class InvenTreeStockItem extends InvenTreeModel { | |||||||
|     Map<String, dynamic> data = {}; |     Map<String, dynamic> data = {}; | ||||||
|  |  | ||||||
|     // Note: Format of adjustment API was updated in API v14 |     // Note: Format of adjustment API was updated in API v14 | ||||||
|     if (api.supportModernStockTransactions()) { |     if (api.supportsModernStockTransactions) { | ||||||
|       // Modern (> 14) API |       // Modern (> 14) API | ||||||
|       data = { |       data = { | ||||||
|         "items": [ |         "items": [ | ||||||
| @@ -560,7 +560,7 @@ class InvenTreeStockItem extends InvenTreeModel { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // Expected API return code depends on server API version |     // Expected API return code depends on server API version | ||||||
|     final int expected_response = api.supportModernStockTransactions() ? 201 : 200; |     final int expected_response = api.supportsModernStockTransactions ? 201 : 200; | ||||||
|  |  | ||||||
|     var response = await api.post( |     var response = await api.post( | ||||||
|       endpoint, |       endpoint, | ||||||
|   | |||||||
| @@ -34,7 +34,7 @@ class InvenTreeDrawer extends StatelessWidget { | |||||||
|  |  | ||||||
|   void _search() { |   void _search() { | ||||||
|  |  | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     _closeDrawer(); |     _closeDrawer(); | ||||||
|  |  | ||||||
| @@ -51,7 +51,7 @@ class InvenTreeDrawer extends StatelessWidget { | |||||||
|    * Upon successful scan, data are passed off to be decoded. |    * Upon successful scan, data are passed off to be decoded. | ||||||
|    */ |    */ | ||||||
|   Future <void> _scan() async { |   Future <void> _scan() async { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     _closeDrawer(); |     _closeDrawer(); | ||||||
|     scanQrCode(context); |     scanQrCode(context); | ||||||
|   | |||||||
| @@ -71,13 +71,13 @@ class _InvenTreeHomePageState extends State<InvenTreeHomePage> { | |||||||
|   UserProfile? _profile; |   UserProfile? _profile; | ||||||
|  |  | ||||||
|   void _scan(BuildContext context) { |   void _scan(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     scanQrCode(context); |     scanQrCode(context); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _showParts(BuildContext context) { |   void _showParts(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     Navigator.push(context, MaterialPageRoute(builder: (context) => CategoryDisplayWidget(null))); |     Navigator.push(context, MaterialPageRoute(builder: (context) => CategoryDisplayWidget(null))); | ||||||
|   } |   } | ||||||
| @@ -87,7 +87,7 @@ class _InvenTreeHomePageState extends State<InvenTreeHomePage> { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _showStarredParts(BuildContext context) { |   void _showStarredParts(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     Navigator.push( |     Navigator.push( | ||||||
|       context, |       context, | ||||||
| @@ -100,13 +100,13 @@ class _InvenTreeHomePageState extends State<InvenTreeHomePage> { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _showStock(BuildContext context) { |   void _showStock(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(null))); |     Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(null))); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _showPurchaseOrders(BuildContext context) { |   void _showPurchaseOrders(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     Navigator.push( |     Navigator.push( | ||||||
|       context, |       context, | ||||||
| @@ -118,19 +118,19 @@ class _InvenTreeHomePageState extends State<InvenTreeHomePage> { | |||||||
|  |  | ||||||
|   /* |   /* | ||||||
|   void _showSuppliers(BuildContext context) { |   void _showSuppliers(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     Navigator.push(context, MaterialPageRoute(builder: (context) => CompanyListWidget(L10().suppliers, {"is_supplier": "true"}))); |     Navigator.push(context, MaterialPageRoute(builder: (context) => CompanyListWidget(L10().suppliers, {"is_supplier": "true"}))); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _showManufacturers(BuildContext context) { |   void _showManufacturers(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     Navigator.push(context, MaterialPageRoute(builder: (context) => CompanyListWidget(L10().manufacturers, {"is_manufacturer": "true"}))); |     Navigator.push(context, MaterialPageRoute(builder: (context) => CompanyListWidget(L10().manufacturers, {"is_manufacturer": "true"}))); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void _showCustomers(BuildContext context) { |   void _showCustomers(BuildContext context) { | ||||||
|     if (!InvenTreeAPI().checkConnection(context)) return; |     if (!InvenTreeAPI().checkConnection()) return; | ||||||
|  |  | ||||||
|     Navigator.push(context, MaterialPageRoute(builder: (context) => CompanyListWidget(L10().customers, {"is_customer": "true"}))); |     Navigator.push(context, MaterialPageRoute(builder: (context) => CompanyListWidget(L10().customers, {"is_customer": "true"}))); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -247,7 +247,7 @@ class _PurchaseOrderDetailState extends RefreshableState<PurchaseOrderDetailWidg | |||||||
|     ); |     ); | ||||||
|      */ |      */ | ||||||
|  |  | ||||||
|     if (order.isPlaced && InvenTreeAPI().supportPoReceive()) { |     if (order.isPlaced && InvenTreeAPI().supportsPoReceive) { | ||||||
|       children.add( |       children.add( | ||||||
|         SimpleDialogOption( |         SimpleDialogOption( | ||||||
|           onPressed: () { |           onPressed: () { | ||||||
|   | |||||||
| @@ -311,7 +311,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> { | |||||||
|   Future <void> _addStockDialog() async { |   Future <void> _addStockDialog() async { | ||||||
|  |  | ||||||
|     // TODO: In future, deprecate support for older API |     // TODO: In future, deprecate support for older API | ||||||
|     if (InvenTreeAPI().supportModernStockTransactions()) { |     if (InvenTreeAPI().supportsModernStockTransactions) { | ||||||
|  |  | ||||||
|       Map<String, dynamic> fields = { |       Map<String, dynamic> fields = { | ||||||
|         "pk": { |         "pk": { | ||||||
| @@ -391,7 +391,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> { | |||||||
|   void _removeStockDialog() { |   void _removeStockDialog() { | ||||||
|  |  | ||||||
|     // TODO: In future, deprecate support for the older API |     // TODO: In future, deprecate support for the older API | ||||||
|     if (InvenTreeAPI().supportModernStockTransactions()) { |     if (InvenTreeAPI().supportsModernStockTransactions) { | ||||||
|       Map<String, dynamic> fields = { |       Map<String, dynamic> fields = { | ||||||
|         "pk": { |         "pk": { | ||||||
|           "parent": "items", |           "parent": "items", | ||||||
| @@ -463,7 +463,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> { | |||||||
|   Future <void> _countStockDialog() async { |   Future <void> _countStockDialog() async { | ||||||
|  |  | ||||||
|     // TODO: In future, deprecate support for older API |     // TODO: In future, deprecate support for older API | ||||||
|     if (InvenTreeAPI().supportModernStockTransactions()) { |     if (InvenTreeAPI().supportsModernStockTransactions) { | ||||||
|  |  | ||||||
|       Map<String, dynamic> fields = { |       Map<String, dynamic> fields = { | ||||||
|         "pk": { |         "pk": { | ||||||
| @@ -566,7 +566,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> { | |||||||
|   Future <void> _transferStockDialog(BuildContext context) async { |   Future <void> _transferStockDialog(BuildContext context) async { | ||||||
|  |  | ||||||
|     // TODO: In future, deprecate support for older API |     // TODO: In future, deprecate support for older API | ||||||
|     if (InvenTreeAPI().supportModernStockTransactions()) { |     if (InvenTreeAPI().supportsModernStockTransactions) { | ||||||
|  |  | ||||||
|       Map<String, dynamic> fields = { |       Map<String, dynamic> fields = { | ||||||
|         "pk": { |         "pk": { | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ | |||||||
| import "package:test/test.dart"; | import "package:test/test.dart"; | ||||||
|  |  | ||||||
| import "package:inventree/api.dart"; | import "package:inventree/api.dart"; | ||||||
|  | import "package:inventree/helpers.dart"; | ||||||
| import "package:inventree/user_profile.dart"; | import "package:inventree/user_profile.dart"; | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -91,12 +92,13 @@ void main() { | |||||||
|         assert(!result); |         assert(!result); | ||||||
|  |  | ||||||
|         // TODO: Test that the connection attempt above throws an authentication error |         // TODO: Test that the connection attempt above throws an authentication error | ||||||
|  |  | ||||||
|  |         assert(!api.checkConnection()); | ||||||
|  |  | ||||||
|       } else { |       } else { | ||||||
|         assert(false); |         assert(false); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     test("Login Success", () async { |     test("Login Success", () async { | ||||||
| @@ -107,12 +109,34 @@ void main() { | |||||||
|       final bool result = await api.connectToServer(); |       final bool result = await api.connectToServer(); | ||||||
|  |  | ||||||
|       // Check expected values |       // Check expected values | ||||||
|       expect(result, equals(true)); |       assert(result); | ||||||
|       expect(api.hasToken, equals(true)); |       assert(api.hasToken); | ||||||
|       expect(api.baseUrl, equals("http://localhost:12345/")); |       expect(api.baseUrl, equals("http://localhost:12345/")); | ||||||
|  |  | ||||||
|       expect(api.isConnected(), equals(true)); |       assert(api.isConnected()); | ||||||
|       expect(api.isConnecting(), equals(false)); |       assert(!api.isConnecting()); | ||||||
|  |       assert(api.checkConnection()); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|  |     test("Version Checks", () async { | ||||||
|  |       // Test server version information | ||||||
|  |       var api = InvenTreeAPI(); | ||||||
|  |  | ||||||
|  |       assert(await api.connectToServer()); | ||||||
|  |  | ||||||
|  |       // Check supported functions | ||||||
|  |       assert(api.apiVersion >= 50); | ||||||
|  |       assert(api.supportsSettings); | ||||||
|  |       assert(api.supportsNotifications); | ||||||
|  |       assert(api.supportsModernStockTransactions); | ||||||
|  |       assert(api.supportsPoReceive); | ||||||
|  |  | ||||||
|  |       // Check available permissions | ||||||
|  |       assert(api.checkPermission("part", "change")); | ||||||
|  |       assert(api.checkPermission("stocklocation", "delete")); | ||||||
|  |       assert(api.checkPermission("part", "weirdpermission")); | ||||||
|  |       assert(api.checkPermission("blah", "bloo")); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user