Skip to content

Commit

Permalink
[Flutter-Parent][RC-3.2.0] Fix migration for users not on refresh tok…
Browse files Browse the repository at this point in the history
…ens (#753)

Also some misc null fixes
  • Loading branch information
CalvinKern authored Apr 22, 2020
1 parent a08b958 commit ae4ff58
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ object OldAppMigrations {
it.put("domain", "${it.optString("protocol") ?: "https"}://${it.optString("domain")}")
it.remove("protocol")

// Some users may not be on refresh tokens, so pull their old token as the 'accessToken'
val token = it.optString("token")
if (token != null && token.isNotEmpty()) it.put("accessToken", token)

// Add client id/secret if this is the current user
if (refreshToken == it.optString("refreshToken")) {
it.put("clientId", prefs.getString("client_id", null))
Expand Down
4 changes: 3 additions & 1 deletion apps/flutter_parent/lib/network/utils/api_prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ class ApiPrefs {

static bool isLoggedIn() {
_checkInit();
return getAuthToken() != null && getDomain() != null;
final token = getAuthToken() ?? '';
final domain = getDomain() ?? '';
return token.isNotEmpty && domain.isNotEmpty;
}

static Login getCurrentLogin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ class _ManageStudentsState extends State<ManageStudentsScreen> {
void _showAddStudentDialog() async {
locator<Analytics>().logEvent(AnalyticsEventConstants.ADD_STUDENT_MANAGE_STUDENTS);
bool studentPaired = await _addStudentDialog(context);
if (studentPaired) {
// Can be null if popped by dismissing
if (studentPaired == true) {
_refreshKey.currentState.show();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import 'package:flutter/material.dart';
import 'package:flutter_parent/l10n/app_localizations.dart';
import 'package:flutter_parent/network/utils/analytics.dart';
import 'package:flutter_parent/router/panda_router.dart';
import 'package:flutter_parent/screens/qr_login/qr_login_tutorial_screen_interactor.dart';
import 'package:flutter_parent/utils/design/parent_theme.dart';
Expand Down Expand Up @@ -54,12 +55,13 @@ class _QRLoginTutorialScreenState extends State<QRLoginTutorialScreen> {
shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
onPressed: () async {
var barcodeResult = await locator<QRLoginTutorialScreenInteractor>().scan();
if (barcodeResult.isSuccess) {
if (barcodeResult?.isSuccess == true) {
locator<QuickNav>().pushRoute(context, PandaRouter.qrLogin(barcodeResult.result));
} else {
locator<Analytics>().logMessage(barcodeResult?.errorType?.toString() ?? 'No barcode result');
_showSnackBarError(
context,
barcodeResult.errorType == QRError.invalidQR
barcodeResult?.errorType == QRError.invalidQR
? L10n(context).invalidQRCodeError
: L10n(context).qrCodeNoCameraError);
}
Expand Down
4 changes: 2 additions & 2 deletions apps/flutter_parent/lib/utils/crash_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class CrashUtils {

// Set any user info that will help to debug the issue
await Future.wait([
FlutterCrashlytics().setInfo('domain', ApiPrefs.getDomain()),
FlutterCrashlytics().setInfo('user_id', ApiPrefs.getUser()?.id),
FlutterCrashlytics().setInfo('domain', ApiPrefs.getDomain() ?? 'null'),
FlutterCrashlytics().setInfo('user_id', ApiPrefs.getUser()?.id ?? 'null'),
]);

await FlutterCrashlytics().reportCrash(exception, stacktrace, forceCrash: false);
Expand Down
2 changes: 1 addition & 1 deletion apps/flutter_parent/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ description: Canvas Parent
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 3.2.0+29
version: 3.2.0+30

module:
androidX: true
Expand Down
22 changes: 22 additions & 0 deletions apps/flutter_parent/test/network/api_prefs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ void main() {
expect(ApiPrefs.isLoggedIn(), true);
});

test('is logged in returns false with an empty token', () async {
var login = Login((b) => b
..domain = 'domain'
..accessToken = ''
..user = CanvasModelTestUtils.mockUser().toBuilder());
await setupPlatformChannels(config: PlatformConfig(mockApiPrefs: {ApiPrefs.KEY_CURRENT_LOGIN_UUID: login.uuid}));
await ApiPrefs.addLogin(login);

expect(ApiPrefs.isLoggedIn(), false);
});

test('is logged in returns false with an empty domain', () async {
var login = Login((b) => b
..domain = ''
..accessToken = 'token'
..user = CanvasModelTestUtils.mockUser().toBuilder());
await setupPlatformChannels(config: PlatformConfig(mockApiPrefs: {ApiPrefs.KEY_CURRENT_LOGIN_UUID: login.uuid}));
await ApiPrefs.addLogin(login);

expect(ApiPrefs.isLoggedIn(), false);
});

test('getApiUrl returns the domain with the api path added', () async {
var login = Login((b) => b
..domain = 'domain'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void main() {

Login normalLogin = Login((b) => b
..domain = 'domain'
..accessToken = 'token'
..user = CanvasModelTestUtils.mockUser().toBuilder());

Login masqueradeLogin = normalLogin.rebuild((b) => b
Expand Down

0 comments on commit ae4ff58

Please sign in to comment.