mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 05:26:47 +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)
|
// Authentication token (initially empty, must be requested)
|
||||||
String _token = "";
|
String _token = "";
|
||||||
|
|
||||||
|
bool get hasToken => _token.isNotEmpty;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 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.
|
||||||
@ -278,7 +280,7 @@ class InvenTreeAPI {
|
|||||||
bool _connecting = false;
|
bool _connecting = false;
|
||||||
|
|
||||||
bool isConnected() {
|
bool isConnected() {
|
||||||
return profile != null && _connected && baseUrl.isNotEmpty && _token.isNotEmpty;
|
return profile != null && _connected && baseUrl.isNotEmpty && hasToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isConnecting() {
|
bool isConnecting() {
|
||||||
|
@ -7,6 +7,7 @@ import "package:flutter/material.dart";
|
|||||||
// Shortcut function to reduce boilerplate!
|
// Shortcut function to reduce boilerplate!
|
||||||
I18N L10()
|
I18N L10()
|
||||||
{
|
{
|
||||||
|
if (OneContext.hasContext) {
|
||||||
BuildContext? _ctx = OneContext().context;
|
BuildContext? _ctx = OneContext().context;
|
||||||
|
|
||||||
if (_ctx != null) {
|
if (_ctx != null) {
|
||||||
@ -16,6 +17,7 @@ I18N L10()
|
|||||||
return i18n;
|
return i18n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Fallback for "null" context
|
// 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:flutter/material.dart";
|
||||||
import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
||||||
import "package:one_context/one_context.dart";
|
import "package:one_context/one_context.dart";
|
||||||
import "package:inventree/l10.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}) {
|
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;
|
BuildContext? context = OneContext().context;
|
||||||
|
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
|
33
pubspec.lock
33
pubspec.lock
@ -7,14 +7,14 @@ packages:
|
|||||||
name: _fe_analyzer_shared
|
name: _fe_analyzer_shared
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "40.0.0"
|
version: "31.0.0"
|
||||||
analyzer:
|
analyzer:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: analyzer
|
name: analyzer
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.1.0"
|
version: "2.8.0"
|
||||||
archive:
|
archive:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -113,6 +113,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.1"
|
version: "1.3.1"
|
||||||
|
cli_util:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: cli_util
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.3.5"
|
||||||
clock:
|
clock:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -140,7 +147,7 @@ packages:
|
|||||||
name: coverage
|
name: coverage
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.2"
|
version: "1.0.3"
|
||||||
cross_file:
|
cross_file:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -218,6 +225,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.6.3"
|
version: "0.6.3"
|
||||||
|
fake_async:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: fake_async
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.2.0"
|
||||||
ffi:
|
ffi:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -284,6 +298,11 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.5"
|
version: "2.0.5"
|
||||||
|
flutter_test:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description: flutter
|
||||||
|
source: sdk
|
||||||
|
version: "0.0.0"
|
||||||
flutter_web_plugins:
|
flutter_web_plugins:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
@ -839,21 +858,21 @@ packages:
|
|||||||
name: test
|
name: test
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.21.1"
|
version: "1.19.5"
|
||||||
test_api:
|
test_api:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: test_api
|
name: test_api
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.4.9"
|
version: "0.4.8"
|
||||||
test_core:
|
test_core:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: test_core
|
name: test_core
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.4.13"
|
version: "0.4.9"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -937,7 +956,7 @@ packages:
|
|||||||
name: vm_service
|
name: vm_service
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "8.3.0"
|
version: "7.5.0"
|
||||||
watcher:
|
watcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
30
pubspec.yaml
30
pubspec.yaml
@ -40,7 +40,9 @@ dependencies:
|
|||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_launcher_icons: ^0.9.0
|
flutter_launcher_icons: ^0.9.0
|
||||||
lint: ^1.8.0
|
lint: ^1.8.0
|
||||||
test: ^1.21.0
|
test: ^1.19.0
|
||||||
|
flutter_test:
|
||||||
|
sdk: flutter
|
||||||
|
|
||||||
flutter_icons:
|
flutter_icons:
|
||||||
android: true
|
android: true
|
||||||
@ -63,29 +65,3 @@ flutter:
|
|||||||
- assets/sounds/barcode_scan.mp3
|
- assets/sounds/barcode_scan.mp3
|
||||||
- assets/sounds/barcode_error.mp3
|
- assets/sounds/barcode_error.mp3
|
||||||
- assets/sounds/server_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