mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-27 15:59:09 +00:00
API form fixes (#937)
* API form fixes - Await form completion - Improved error code handling - Additional testing * improve error message display in forms * add text
This commit is contained in:
1 parent
e9601116b4
commit
ed9590cddf
8 files changed
+585
-31
No files matched your search
@@ -365,6 +365,64 @@ void main() {
|
||||
expect(logged("Opening login screen"), equals(false));
|
||||
});
|
||||
|
||||
test("Role request failure is reported", () async {
|
||||
final profile = await fakeProfile(server);
|
||||
|
||||
expect((await login(profile)).authenticated, equals(true));
|
||||
|
||||
server.once(
|
||||
"GET",
|
||||
"api/user/me/roles/",
|
||||
(_) => FakeResponse(403, body: {"detail": "Permission denied."}),
|
||||
);
|
||||
|
||||
expect(await api.connectToServer(profile), equals(false));
|
||||
|
||||
// Previously the connection failed without any message (B20)
|
||||
expect(
|
||||
logged("showServerError: '${statusCodeToString(403)}'"),
|
||||
equals(true),
|
||||
);
|
||||
});
|
||||
|
||||
test("Disconnect clears server data", () async {
|
||||
final profile = await fakeProfile(server);
|
||||
|
||||
server.always(
|
||||
"GET",
|
||||
"api/user/me/roles/",
|
||||
(_) => FakeResponse(
|
||||
200,
|
||||
body: {
|
||||
"roles": {
|
||||
"part": ["view"],
|
||||
},
|
||||
"permissions": {
|
||||
"part": ["view"],
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
await loginAndConnect(profile);
|
||||
|
||||
expect(api.roles, isNotEmpty);
|
||||
expect(api.permissions, isNotEmpty);
|
||||
|
||||
api.notification_counter = 3;
|
||||
api.StockStatus.data["OK"] = {"key": 10, "name": "OK", "label": "OK"};
|
||||
|
||||
api.disconnectFromServer();
|
||||
|
||||
// Nothing from the previous server is shown while connecting to another
|
||||
expect(api.roles, isEmpty);
|
||||
expect(api.permissions, isEmpty);
|
||||
expect(api.serverInfo, isEmpty);
|
||||
expect(api.userInfo, isEmpty);
|
||||
expect(api.notification_counter, equals(0));
|
||||
expect(api.StockStatus.data, isEmpty);
|
||||
});
|
||||
|
||||
final Map<String, FakeResponse> failures = {
|
||||
"Service unavailable (503)": FakeResponse(503),
|
||||
"Connection dropped": FakeResponse.drop(),
|
||||
@@ -936,6 +994,40 @@ void main() {
|
||||
expect(request.query, equals({"category": "1", "search": "x y"}));
|
||||
});
|
||||
|
||||
test("Unexpected status code is reported if requested", () async {
|
||||
server.once(
|
||||
"GET",
|
||||
"api/part/1/",
|
||||
(_) => FakeResponse(404, body: {"detail": "Not found."}),
|
||||
);
|
||||
|
||||
clearDebugMessage();
|
||||
|
||||
final response = await api.get("part/1/", expectedStatusCode: 200);
|
||||
|
||||
expect(response.statusCode, equals(404));
|
||||
expect(
|
||||
logged("showServerError: '${statusCodeToString(404)}'"),
|
||||
equals(true),
|
||||
);
|
||||
});
|
||||
|
||||
test("Unexpected status code is not reported by default", () async {
|
||||
// e.g. a 404 when checking whether an object still exists
|
||||
server.once(
|
||||
"GET",
|
||||
"api/part/1/",
|
||||
(_) => FakeResponse(404, body: {"detail": "Not found."}),
|
||||
);
|
||||
|
||||
clearDebugMessage();
|
||||
|
||||
final response = await api.get("part/1/");
|
||||
|
||||
expect(response.statusCode, equals(404));
|
||||
expect(logged("showServerError:"), equals(false));
|
||||
});
|
||||
|
||||
test("No query string without parameters", () async {
|
||||
await api.get("part/");
|
||||
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
*/
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
|
||||
import "package:flutter_test/flutter_test.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
import "package:inventree/widget/dialogs.dart";
|
||||
import "package:inventree/widget/snacks.dart";
|
||||
|
||||
// Display the error content for the given description / response
|
||||
Future<void> pumpErrorContent(
|
||||
@@ -234,4 +236,127 @@ void main() {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group("Response Data:", () {
|
||||
test("JSON data is pretty-printed", () {
|
||||
expect(
|
||||
formatResponseData({
|
||||
"name": ["This field is required."],
|
||||
"quantity": ["Must be positive"],
|
||||
}),
|
||||
equals(
|
||||
[
|
||||
"{",
|
||||
' "name": [',
|
||||
' "This field is required."',
|
||||
" ],",
|
||||
' "quantity": [',
|
||||
' "Must be positive"',
|
||||
" ]",
|
||||
"}",
|
||||
].join("\n"),
|
||||
),
|
||||
);
|
||||
|
||||
expect(formatResponseData(["a"]), equals('[\n "a"\n]'));
|
||||
});
|
||||
|
||||
test("Other data", () {
|
||||
expect(
|
||||
formatResponseData("<html>Bad Gateway</html>"),
|
||||
equals("<html>Bad Gateway</html>"),
|
||||
);
|
||||
expect(formatResponseData(null), isEmpty);
|
||||
});
|
||||
|
||||
testWidgets("Full response is displayed", (tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: buildResponseDataContent(
|
||||
APIResponse(
|
||||
url: "https://server/api/part/",
|
||||
statusCode: 400,
|
||||
data: {
|
||||
"name": ["This field is required."],
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.text("${L10().statusCode}: 400"), findsOneWidget);
|
||||
expect(find.text("https://server/api/part/"), findsOneWidget);
|
||||
expect(
|
||||
find.textContaining('"This field is required."', findRichText: true),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group("Snackbar Actions:", () {
|
||||
Future<void> pumpSnack(
|
||||
WidgetTester tester, {
|
||||
IconData? icon,
|
||||
String? actionText,
|
||||
required void Function() onAction,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Stack(
|
||||
children: [
|
||||
TopSnackBar(
|
||||
text: L10().formError,
|
||||
icon: icon,
|
||||
backgroundColor: Colors.red,
|
||||
duration: const Duration(seconds: 10),
|
||||
onDismiss: () {},
|
||||
onAction: onAction,
|
||||
actionText: actionText,
|
||||
iconAction: icon != null && actionText == null,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 500));
|
||||
}
|
||||
|
||||
testWidgets("Tapping the icon runs the action", (tester) async {
|
||||
int actions = 0;
|
||||
|
||||
await pumpSnack(
|
||||
tester,
|
||||
icon: TablerIcons.info_circle,
|
||||
onAction: () => actions++,
|
||||
);
|
||||
|
||||
// The icon replaces the text action button
|
||||
expect(find.byType(TextButton), findsNothing);
|
||||
|
||||
await tester.tap(find.byIcon(TablerIcons.info_circle));
|
||||
await tester.pump(const Duration(milliseconds: 500));
|
||||
|
||||
expect(actions, equals(1));
|
||||
});
|
||||
|
||||
testWidgets("Text action button", (tester) async {
|
||||
int actions = 0;
|
||||
|
||||
await pumpSnack(
|
||||
tester,
|
||||
actionText: L10().details,
|
||||
onAction: () => actions++,
|
||||
);
|
||||
|
||||
await tester.tap(find.text(L10().details));
|
||||
await tester.pump(const Duration(milliseconds: 500));
|
||||
|
||||
expect(actions, equals(1));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Widget tests for locating a stock item / location (regression tests for B16).
|
||||
*
|
||||
* With more than one "locate" plugin, the user selects a plugin from a form.
|
||||
* Previously the locate request was sent (with an empty plugin) as soon as
|
||||
* the form was opened, as launchApiForm() returned before the form was closed.
|
||||
*
|
||||
* Uses an in-process fake server (see fake_server.dart).
|
||||
*/
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
|
||||
import "package:flutter_test/flutter_test.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/auth/auth_manager.dart";
|
||||
import "package:inventree/auth/auth_method.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
import "package:inventree/user_profile.dart";
|
||||
|
||||
import "../fake_server.dart";
|
||||
import "../setup.dart";
|
||||
import "harness.dart";
|
||||
|
||||
const String profileName = "Locate Widget Test";
|
||||
|
||||
UserProfileDBManager get manager => UserProfileDBManager();
|
||||
|
||||
Map<String, dynamic> locatePlugin(String key) => {
|
||||
"key": key,
|
||||
"active": true,
|
||||
"meta": {"human_name": "Plugin ${key}"},
|
||||
"mixins": {
|
||||
"locate": {"key": "locate"},
|
||||
},
|
||||
};
|
||||
|
||||
void main() {
|
||||
setupTestEnv();
|
||||
mockSharedPreferences();
|
||||
|
||||
late FakeInvenTreeServer server;
|
||||
|
||||
setUp(() async {
|
||||
server = await FakeInvenTreeServer.start();
|
||||
|
||||
server.always(
|
||||
"GET",
|
||||
"api/plugins/",
|
||||
(_) => FakeResponse(
|
||||
200,
|
||||
body: [locatePlugin("first"), locatePlugin("second")],
|
||||
),
|
||||
);
|
||||
|
||||
server.always("POST", "api/locate/", (_) => FakeResponse(200, body: {}));
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await server.close();
|
||||
});
|
||||
|
||||
// Connect to the fake server, and show a screen to launch the form from
|
||||
Future<BuildContext> setupScreen(WidgetTester tester) async {
|
||||
final UserProfile? existing = await settle(
|
||||
tester,
|
||||
manager.getProfileByName(profileName),
|
||||
);
|
||||
|
||||
if (existing != null) {
|
||||
await settle(tester, manager.deleteProfile(existing));
|
||||
}
|
||||
|
||||
await settle(
|
||||
tester,
|
||||
manager.addProfile(UserProfile(name: profileName, server: server.url)),
|
||||
);
|
||||
|
||||
final UserProfile profile = (await settle(
|
||||
tester,
|
||||
manager.getProfileByName(profileName),
|
||||
))!;
|
||||
|
||||
addTearDown(() => manager.deleteProfile(profile));
|
||||
|
||||
final result = await settle(
|
||||
tester,
|
||||
InvenTreeAuth().beginLogin(
|
||||
profile,
|
||||
PasswordAuthMethod("user", "password"),
|
||||
),
|
||||
);
|
||||
|
||||
expect(result.authenticated, isTrue);
|
||||
expect(
|
||||
await settle(tester, InvenTreeAPI().connectToServer(profile)),
|
||||
isTrue,
|
||||
);
|
||||
|
||||
expect(InvenTreeAPI().getPlugins(mixin: "locate"), hasLength(2));
|
||||
|
||||
late BuildContext context;
|
||||
|
||||
await pumpScreen(
|
||||
tester,
|
||||
Scaffold(
|
||||
body: Builder(
|
||||
builder: (ctx) {
|
||||
context = ctx;
|
||||
return const Text("Locate Test");
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
// Start locating an item, and wait for the plugin form to be displayed.
|
||||
// [onComplete] is called when locateItemOrLocation() completes
|
||||
Future<void> startLocate(
|
||||
WidgetTester tester,
|
||||
void Function() onComplete,
|
||||
) async {
|
||||
final BuildContext context = await setupScreen(tester);
|
||||
|
||||
InvenTreeAPI()
|
||||
.locateItemOrLocation(context, item: 5)
|
||||
.then((_) => onComplete());
|
||||
|
||||
await waitUntil(
|
||||
tester,
|
||||
() => find.text(L10().locateLocation).evaluate().isNotEmpty,
|
||||
reason: "plugin selection form displayed",
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets("Locate request waits for plugin selection", (tester) async {
|
||||
bool complete = false;
|
||||
|
||||
await startLocate(tester, () => complete = true);
|
||||
|
||||
// Wait for any (incorrect) request to arrive
|
||||
for (int i = 0; i < 20; i++) {
|
||||
await step(tester);
|
||||
}
|
||||
|
||||
expect(server.received("POST", "api/locate/"), isEmpty);
|
||||
expect(complete, isFalse);
|
||||
|
||||
// Submit the form (with the default plugin selected)
|
||||
await tester.tap(find.byIcon(TablerIcons.location_search));
|
||||
|
||||
await waitUntil(tester, () => complete, reason: "locate complete");
|
||||
|
||||
final requests = server.received("POST", "api/locate/");
|
||||
|
||||
expect(requests, hasLength(1));
|
||||
expect(requests.first.json, equals({"plugin": "first", "item": "5"}));
|
||||
|
||||
await disconnectInWidgetTest(tester);
|
||||
});
|
||||
|
||||
testWidgets("Cancelled plugin selection sends no request", (tester) async {
|
||||
bool complete = false;
|
||||
|
||||
await startLocate(tester, () => complete = true);
|
||||
|
||||
await tester.pageBack();
|
||||
|
||||
await waitUntil(tester, () => complete, reason: "locate complete");
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
await step(tester);
|
||||
}
|
||||
|
||||
expect(server.received("POST", "api/locate/"), isEmpty);
|
||||
|
||||
await disconnectInWidgetTest(tester);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user