mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-27 21:16:48 +00:00
Start of unit tests for the actual API code
This commit is contained in:
parent
62b0fcbec5
commit
e424a3cf7b
@ -201,6 +201,8 @@ class InvenTreeAPI {
|
||||
// Authentication token (initially empty, must be requested)
|
||||
String _token = "";
|
||||
|
||||
bool get hasToken => _token.isNotEmpty;
|
||||
|
||||
/*
|
||||
* Check server connection and display messages if not connected.
|
||||
* Useful as a precursor check before performing operations.
|
||||
@ -278,7 +280,7 @@ class InvenTreeAPI {
|
||||
bool _connecting = false;
|
||||
|
||||
bool isConnected() {
|
||||
return profile != null && _connected && baseUrl.isNotEmpty && _token.isNotEmpty;
|
||||
return profile != null && _connected && baseUrl.isNotEmpty && hasToken;
|
||||
}
|
||||
|
||||
bool isConnecting() {
|
||||
|
14
lib/l10.dart
14
lib/l10.dart
@ -7,16 +7,18 @@ import "package:flutter/material.dart";
|
||||
// Shortcut function to reduce boilerplate!
|
||||
I18N L10()
|
||||
{
|
||||
BuildContext? _ctx = OneContext().context;
|
||||
if (OneContext.hasContext) {
|
||||
BuildContext? _ctx = OneContext().context;
|
||||
|
||||
if (_ctx != null) {
|
||||
I18N? i18n = I18N.of(_ctx);
|
||||
if (_ctx != null) {
|
||||
I18N? i18n = I18N.of(_ctx);
|
||||
|
||||
if (i18n != null) {
|
||||
return i18n;
|
||||
if (i18n != null) {
|
||||
return i18n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback for "null" context
|
||||
return I18NEn();
|
||||
return I18NEn();
|
||||
}
|
@ -1,21 +1,19 @@
|
||||
|
||||
/*
|
||||
* Display a snackbar with:
|
||||
*
|
||||
* a) Text on the left
|
||||
* b) Icon on the right
|
||||
*
|
||||
* | Text <icon> |
|
||||
*/
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
||||
import "package:one_context/one_context.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
|
||||
|
||||
/*
|
||||
* Display a configurable 'snackbar' at the bottom of the screen
|
||||
*/
|
||||
void showSnackIcon(String text, {IconData? icon, Function()? onAction, bool? success, String? actionText}) {
|
||||
|
||||
// Escape quickly if we do not have context
|
||||
if (!OneContext.hasContext) {
|
||||
return;
|
||||
}
|
||||
|
||||
BuildContext? context = OneContext().context;
|
||||
|
||||
if (context != null) {
|
||||
|
33
pubspec.lock
33
pubspec.lock
@ -7,14 +7,14 @@ packages:
|
||||
name: _fe_analyzer_shared
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "40.0.0"
|
||||
version: "31.0.0"
|
||||
analyzer:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: analyzer
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.1.0"
|
||||
version: "2.8.0"
|
||||
archive:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -113,6 +113,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.1"
|
||||
cli_util:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: cli_util
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.3.5"
|
||||
clock:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -140,7 +147,7 @@ packages:
|
||||
name: coverage
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.2"
|
||||
version: "1.0.3"
|
||||
cross_file:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -218,6 +225,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.6.3"
|
||||
fake_async:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: fake_async
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0"
|
||||
ffi:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -284,6 +298,11 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.5"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
flutter_web_plugins:
|
||||
dependency: transitive
|
||||
description: flutter
|
||||
@ -839,21 +858,21 @@ packages:
|
||||
name: test
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.21.1"
|
||||
version: "1.19.5"
|
||||
test_api:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: test_api
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.4.9"
|
||||
version: "0.4.8"
|
||||
test_core:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: test_core
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.4.13"
|
||||
version: "0.4.9"
|
||||
typed_data:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -937,7 +956,7 @@ packages:
|
||||
name: vm_service
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "8.3.0"
|
||||
version: "7.5.0"
|
||||
watcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
30
pubspec.yaml
30
pubspec.yaml
@ -40,7 +40,9 @@ dependencies:
|
||||
dev_dependencies:
|
||||
flutter_launcher_icons: ^0.9.0
|
||||
lint: ^1.8.0
|
||||
test: ^1.21.0
|
||||
test: ^1.19.0
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
flutter_icons:
|
||||
android: true
|
||||
@ -63,29 +65,3 @@ flutter:
|
||||
- assets/sounds/barcode_scan.mp3
|
||||
- assets/sounds/barcode_error.mp3
|
||||
- assets/sounds/server_error.mp3
|
||||
|
||||
# An image asset can refer to one or more resolution-specific "variants", see
|
||||
# https://flutter.dev/assets-and-images/#resolution-aware.
|
||||
|
||||
# For details regarding adding assets from package dependencies, see
|
||||
# https://flutter.dev/assets-and-images/#from-packages
|
||||
|
||||
# To add custom fonts to your application, add a fonts section here,
|
||||
# in this "flutter" section. Each entry in this list should have a
|
||||
# "family" key with the font family name, and a "fonts" key with a
|
||||
# list giving the asset and other descriptors for the font. For
|
||||
# example:
|
||||
# fonts:
|
||||
# - family: Schyler
|
||||
# fonts:
|
||||
# - asset: fonts/Schyler-Regular.ttf
|
||||
# - asset: fonts/Schyler-Italic.ttf
|
||||
# style: italic
|
||||
# - family: Trajan Pro
|
||||
# fonts:
|
||||
# - asset: fonts/TrajanPro.ttf
|
||||
# - asset: fonts/TrajanPro_Bold.ttf
|
||||
# weight: 700
|
||||
#
|
||||
# For details regarding fonts from package dependencies,
|
||||
# see https://flutter.dev/custom-fonts/#from-packages
|
||||
|
55
test/api_test.dart
Normal file
55
test/api_test.dart
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Unit tests for the InvenTree API code
|
||||
*/
|
||||
|
||||
import "package:test/test.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/user_profile.dart";
|
||||
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
setUp(() async {
|
||||
|
||||
// Create and select a profile to user
|
||||
await UserProfileDBManager().addProfile(UserProfile(
|
||||
name: "Test Profile",
|
||||
server: "http://localhost:12345",
|
||||
username: "testuser",
|
||||
password: "testpassword",
|
||||
selected: true,
|
||||
));
|
||||
|
||||
});
|
||||
|
||||
group("Login Tests:", () {
|
||||
|
||||
test("Disconnected", () async {
|
||||
// Test that calling disconnect() does the right thing
|
||||
var api = InvenTreeAPI();
|
||||
|
||||
api.disconnectFromServer();
|
||||
|
||||
// Check expected values
|
||||
expect(api.isConnected(), equals(false));
|
||||
expect(api.isConnecting(), equals(false));
|
||||
expect(api.hasToken, equals(false));
|
||||
|
||||
});
|
||||
|
||||
test("Login Success", () async {
|
||||
// Test that we can login to the server successfully
|
||||
var api = InvenTreeAPI();
|
||||
|
||||
// Attempt to connect
|
||||
final bool result = await api.connectToServer();
|
||||
|
||||
expect(result, equals(true));
|
||||
expect(api.hasToken, equals(true));
|
||||
|
||||
expect(api.baseUrl, equals("http://localhost:12345/"));
|
||||
});
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user