From b98f044204b6c6c8546098c21de714a8033c5645 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 22 May 2022 09:56:22 +1000 Subject: [PATCH] More checks --- lib/api.dart | 10 +++----- lib/inventree/stock.dart | 4 +-- lib/widget/drawer.dart | 4 +-- lib/widget/home.dart | 16 ++++++------ lib/widget/purchase_order_detail.dart | 2 +- lib/widget/stock_detail.dart | 8 +++--- test/api_test.dart | 36 ++++++++++++++++++++++----- 7 files changed, 50 insertions(+), 30 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index e098a523..52bd29a1 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -208,7 +208,7 @@ class InvenTreeAPI { * Check server connection and display messages if not connected. * Useful as a precursor check before performing operations. */ - bool checkConnection(BuildContext context) { + bool checkConnection() { // Firstly, is the server connected? if (!isConnected()) { @@ -292,14 +292,10 @@ class InvenTreeAPI { static final InvenTreeAPI _api = InvenTreeAPI._internal(); // API endpoint for receiving purchase order line items was introduced in v12 - bool supportPoReceive() { - return apiVersion >= 12; - } + bool get supportsPoReceive => apiVersion >= 12; // "Modern" API transactions were implemented in API v14 - bool supportModernStockTransactions() { - return apiVersion >= 14; - } + bool get supportsModernStockTransactions => apiVersion >= 14; /* * Connect to the remote InvenTree server: diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index 3dc4f2b1..2bb741f2 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -533,7 +533,7 @@ class InvenTreeStockItem extends InvenTreeModel { Map data = {}; // Note: Format of adjustment API was updated in API v14 - if (api.supportModernStockTransactions()) { + if (api.supportsModernStockTransactions) { // Modern (> 14) API data = { "items": [ @@ -560,7 +560,7 @@ class InvenTreeStockItem extends InvenTreeModel { } // 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( endpoint, diff --git a/lib/widget/drawer.dart b/lib/widget/drawer.dart index 85a30259..cff24de8 100644 --- a/lib/widget/drawer.dart +++ b/lib/widget/drawer.dart @@ -34,7 +34,7 @@ class InvenTreeDrawer extends StatelessWidget { void _search() { - if (!InvenTreeAPI().checkConnection(context)) return; + if (!InvenTreeAPI().checkConnection()) return; _closeDrawer(); @@ -51,7 +51,7 @@ class InvenTreeDrawer extends StatelessWidget { * Upon successful scan, data are passed off to be decoded. */ Future _scan() async { - if (!InvenTreeAPI().checkConnection(context)) return; + if (!InvenTreeAPI().checkConnection()) return; _closeDrawer(); scanQrCode(context); diff --git a/lib/widget/home.dart b/lib/widget/home.dart index 290c8666..19f1c953 100644 --- a/lib/widget/home.dart +++ b/lib/widget/home.dart @@ -71,13 +71,13 @@ class _InvenTreeHomePageState extends State { UserProfile? _profile; void _scan(BuildContext context) { - if (!InvenTreeAPI().checkConnection(context)) return; + if (!InvenTreeAPI().checkConnection()) return; scanQrCode(context); } void _showParts(BuildContext context) { - if (!InvenTreeAPI().checkConnection(context)) return; + if (!InvenTreeAPI().checkConnection()) return; Navigator.push(context, MaterialPageRoute(builder: (context) => CategoryDisplayWidget(null))); } @@ -87,7 +87,7 @@ class _InvenTreeHomePageState extends State { } void _showStarredParts(BuildContext context) { - if (!InvenTreeAPI().checkConnection(context)) return; + if (!InvenTreeAPI().checkConnection()) return; Navigator.push( context, @@ -100,13 +100,13 @@ class _InvenTreeHomePageState extends State { } void _showStock(BuildContext context) { - if (!InvenTreeAPI().checkConnection(context)) return; + if (!InvenTreeAPI().checkConnection()) return; Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(null))); } void _showPurchaseOrders(BuildContext context) { - if (!InvenTreeAPI().checkConnection(context)) return; + if (!InvenTreeAPI().checkConnection()) return; Navigator.push( context, @@ -118,19 +118,19 @@ class _InvenTreeHomePageState extends State { /* 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"}))); } 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"}))); } 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"}))); } diff --git a/lib/widget/purchase_order_detail.dart b/lib/widget/purchase_order_detail.dart index 27486969..2abe56c6 100644 --- a/lib/widget/purchase_order_detail.dart +++ b/lib/widget/purchase_order_detail.dart @@ -247,7 +247,7 @@ class _PurchaseOrderDetailState extends RefreshableState { Future _addStockDialog() async { // TODO: In future, deprecate support for older API - if (InvenTreeAPI().supportModernStockTransactions()) { + if (InvenTreeAPI().supportsModernStockTransactions) { Map fields = { "pk": { @@ -391,7 +391,7 @@ class _StockItemDisplayState extends RefreshableState { void _removeStockDialog() { // TODO: In future, deprecate support for the older API - if (InvenTreeAPI().supportModernStockTransactions()) { + if (InvenTreeAPI().supportsModernStockTransactions) { Map fields = { "pk": { "parent": "items", @@ -463,7 +463,7 @@ class _StockItemDisplayState extends RefreshableState { Future _countStockDialog() async { // TODO: In future, deprecate support for older API - if (InvenTreeAPI().supportModernStockTransactions()) { + if (InvenTreeAPI().supportsModernStockTransactions) { Map fields = { "pk": { @@ -566,7 +566,7 @@ class _StockItemDisplayState extends RefreshableState { Future _transferStockDialog(BuildContext context) async { // TODO: In future, deprecate support for older API - if (InvenTreeAPI().supportModernStockTransactions()) { + if (InvenTreeAPI().supportsModernStockTransactions) { Map fields = { "pk": { diff --git a/test/api_test.dart b/test/api_test.dart index 2ed41619..113ab4d3 100644 --- a/test/api_test.dart +++ b/test/api_test.dart @@ -5,6 +5,7 @@ import "package:test/test.dart"; import "package:inventree/api.dart"; +import "package:inventree/helpers.dart"; import "package:inventree/user_profile.dart"; @@ -91,12 +92,13 @@ void main() { assert(!result); // TODO: Test that the connection attempt above throws an authentication error + + assert(!api.checkConnection()); + } else { assert(false); } - - }); test("Login Success", () async { @@ -107,12 +109,34 @@ void main() { final bool result = await api.connectToServer(); // Check expected values - expect(result, equals(true)); - expect(api.hasToken, equals(true)); + assert(result); + assert(api.hasToken); expect(api.baseUrl, equals("http://localhost:12345/")); - expect(api.isConnected(), equals(true)); - expect(api.isConnecting(), equals(false)); + assert(api.isConnected()); + 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")); + }); + }); } \ No newline at end of file