mirror of
https://github.com/inventree/inventree-app.git
synced 2025-09-13 14:31:22 +00:00
Token auth (#434)
* Embed device platform information into token request * Remove username and password from userProfile * Display icon to show if profile has associated user token * Remove username / password from login settings screen * Refactor login procedure around token auth * Refactoring * Add profile login screen - Username / password values are not stored - Just to fetch api token * Login with basic auth * Pass profile to API when connecting * Remove _BASE_URL accessor - Fixes URL caching bug * Add more context to login screen * Add helper functions for unit tests - Change default port to 8000 (makes testing easier with local inventree instance) * api.dart handles basic auth now * fix api_test.dart * Further test improvements * linting fixes * Provide feedback when login fails * More linting * Record user details on login, and display in "about" widget * Fix string lookup * Add extra debug * Fix auth values * Fix user profile test
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
|
||||
import "package:flutter/services.dart";
|
||||
import "package:flutter_test/flutter_test.dart";
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/user_profile.dart";
|
||||
|
||||
// This is the same as the following issue except it keeps the http client
|
||||
// TestWidgetsFlutterBinding.ensureInitialized();
|
||||
@@ -19,4 +21,78 @@ void setupTestEnv() {
|
||||
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
||||
return ".";
|
||||
});
|
||||
}
|
||||
|
||||
// Accessors for default testing values
|
||||
const String testServerAddress = "http://localhost:8000/";
|
||||
const String testServerName = "Test Server";
|
||||
const String testUsername = "testuser";
|
||||
const String testPassword = "testpassword";
|
||||
|
||||
|
||||
/*
|
||||
* Request an API token for the given profile
|
||||
*/
|
||||
Future<bool> fetchProfileToken({
|
||||
UserProfile? profile,
|
||||
String username = testUsername,
|
||||
String password = testPassword
|
||||
}) async {
|
||||
|
||||
profile ??= await UserProfileDBManager().getProfileByName(testServerName);
|
||||
|
||||
assert(profile != null);
|
||||
|
||||
final response = await InvenTreeAPI().fetchToken(profile!, username, password);
|
||||
return response.successful();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Setup a valid profile, and return it
|
||||
*/
|
||||
Future<UserProfile> setupServerProfile({bool select = true, bool fetchToken = false}) async {
|
||||
// Setup a valid server profile
|
||||
|
||||
UserProfile? profile = await UserProfileDBManager().getProfileByName(testServerName);
|
||||
|
||||
if (profile == null) {
|
||||
// Profile does not already exist - create it!
|
||||
bool result = await UserProfileDBManager().addProfile(
|
||||
UserProfile(
|
||||
server: testServerAddress,
|
||||
name: testServerName
|
||||
)
|
||||
);
|
||||
|
||||
assert(result);
|
||||
}
|
||||
|
||||
profile = await UserProfileDBManager().getProfileByName(testServerName);
|
||||
assert(profile != null);
|
||||
|
||||
if (select) {
|
||||
assert(await UserProfileDBManager().selectProfileByName(testServerName));
|
||||
}
|
||||
|
||||
if (fetchToken && !profile!.hasToken) {
|
||||
final bool result = await fetchProfileToken(profile: profile);
|
||||
assert(result);
|
||||
assert(profile.hasToken);
|
||||
}
|
||||
|
||||
return profile!;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Complete all steps necessary to login to the server
|
||||
*/
|
||||
Future<void> connectToTestServer() async {
|
||||
|
||||
// Setup profile, and fetch user token as necessary
|
||||
final profile = await setupServerProfile(fetchToken: true);
|
||||
|
||||
// Connect to the server
|
||||
assert(await InvenTreeAPI().connectToServer(profile));
|
||||
}
|
Reference in New Issue
Block a user