Add workaround for the AndroidMetadata checks

The metaDataAsMap method doesn't work during tests and throws a null check
error. A better solution should be implemented when possible.
This commit is contained in:
Roland Geider
2021-11-08 10:55:16 +01:00
parent a77a0cc5db
commit e5bd9e901f
5 changed files with 16 additions and 13 deletions

View File

@@ -65,7 +65,7 @@ linter:
empty_statements: true
exhaustive_cases: true
file_names: true
flutter_style_todos: true
flutter_style_todos: false
hash_and_equals: true
implementation_imports: true
iterable_contains_unrelated_type: true

View File

@@ -39,7 +39,7 @@ class AuthProvider with ChangeNotifier {
String? serverUrl;
String? serverVersion;
PackageInfo? applicationVersion;
Map<String, String>? metadata = {};
Map<String, String> metadata = {};
static const MIN_APP_VERSION_URL = 'min-app-version';
static const SERVER_VERSION_URL = 'version';
@@ -48,13 +48,16 @@ class AuthProvider with ChangeNotifier {
late http.Client client;
AuthProvider([http.Client? client]) {
AuthProvider([http.Client? client, bool? checkMetadata]) {
this.client = client ?? http.Client();
try {
AndroidMetadata.metaDataAsMap.then((value) => metadata = value);
} on PlatformException {
throw Exception('An error occurred reading the metadata from AndroidManifest');
// TODO: this is a workaround since AndroidMetadata doesn't work while running tests
if (checkMetadata ?? true) {
try {
AndroidMetadata.metaDataAsMap.then((value) => metadata = value!);
} on PlatformException {
throw Exception('An error occurred reading the metadata from AndroidManifest');
} catch (error) {}
}
}
@@ -80,8 +83,8 @@ class AuthProvider with ChangeNotifier {
/// Checking if there is a new version of the application.
Future<bool> applicationUpdateRequired([String? version]) async {
if (metadata!.containsKey('wger.check_min_app_version') ||
metadata!['wger.check_min_app_version'] == 'false') {
if (metadata.containsKey('wger.check_min_app_version') ||
metadata['wger.check_min_app_version'] == 'false') {
return false;
}
@@ -110,7 +113,7 @@ class AuthProvider with ChangeNotifier {
makeUri(serverUrl, REGISTRATION_URL),
headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
HttpHeaders.authorizationHeader: 'Token ${metadata![MANIFEST_KEY_API]}',
HttpHeaders.authorizationHeader: 'Token ${metadata[MANIFEST_KEY_API]}',
HttpHeaders.userAgentHeader: getAppNameHeader(),
},
body: json.encode(data),

View File

@@ -119,7 +119,7 @@ class _AuthCardState extends State<AuthCard> {
// If not, the user will not be able to register via the app
try {
final metadata = Provider.of<AuthProvider>(context, listen: false).metadata;
if (metadata!.containsKey(MANIFEST_KEY_API) || metadata[MANIFEST_KEY_API] == '') {
if (metadata.containsKey(MANIFEST_KEY_API) || metadata[MANIFEST_KEY_API] == '') {
_canRegister = false;
}
} on PlatformException {

View File

@@ -17,7 +17,7 @@ void main() {
setUp(() {
mockClient = MockClient();
authProvider = AuthProvider(mockClient);
authProvider = AuthProvider(mockClient, false);
authProvider.serverUrl = 'http://localhost';
});

View File

@@ -23,7 +23,7 @@ import '../test_data/exercises.dart';
import 'other/base_provider_test.mocks.dart';
// Test Auth provider
final AuthProvider testAuthProvider = AuthProvider(MockClient())
final AuthProvider testAuthProvider = AuthProvider(MockClient(), false)
..token = 'FooBar'
..serverUrl = 'https://localhost';