Skip to content

Commit

Permalink
fix(notifications): allow offline configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Nika Hassani authored and NikaHsn committed May 22, 2024
1 parent b5aca75 commit 4dc0df5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,22 @@ void main() {
),
);
});

test(
'updateEndpoint throws NetworkException for AWSHttpException exceptions',
() async {
when(() => pinpointClient.updateEndpoint(any())).thenThrow(
AWSHttpException(
AWSHttpRequest(method: AWSHttpMethod.post, uri: Uri()),
),
);

expect(
endpointClient.updateEndpoint(),
throwsA(
isA<NetworkException>(),
),
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,25 @@ class PinpointProvider implements ServiceProviderClient {

@override
Future<void> registerDevice(String deviceToken) async {
if (!_isInitialized) {
_logger.error(
'Pinpoint provider not configured.',
);
return;
}
_analyticsClient.endpointClient.address = deviceToken;
final channelType = _getChannelType();
_analyticsClient.endpointClient.channelType = channelType;
_analyticsClient.endpointClient.optOut = 'NONE';
try {
if (!_isInitialized) {
_logger.error(
'Pinpoint provider not configured.',
);
return;
}
_analyticsClient.endpointClient.address = deviceToken;
final channelType = _getChannelType();
_analyticsClient.endpointClient.channelType = channelType;
_analyticsClient.endpointClient.optOut = 'NONE';
await _withUserAgent(
() async => _analyticsClient.endpointClient.updateEndpoint(),
);
} on AWSHttpException catch (e) {
// This is allow offline configuration.
// `EndpointClient.updateEndpoint()` catches all exception and convert
// them to `AnalyticsException`.
// `AnalyticsException` converts `AWSHttpException` to `NetworkException`.
} on NetworkException catch (e) {
_logger.error('Network problem when registering device: ', e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,48 @@ void main() {
verify(mockEndpointClient.updateEndpoint);
});

test('registerDevice should run successfully when device is offline',
() async {
when(
() => mockAmplifyAuthProviderRepository.getAuthProvider(
APIAuthorizationType.iam.authProviderToken,
),
).thenReturn(awsIamAmplifyAuthProvider);
when(
() => mockAnalyticsClient.init(
pinpointAppId: any(named: 'pinpointAppId'),
region: any(named: 'region'),
authProvider: any(named: 'authProvider'),
),
).thenAnswer((realInvocation) async {});

final mockEndpointClient = MockEndpointClient();

when(mockEndpointClient.updateEndpoint)
.thenThrow(const NetworkException('message'));

when(
() => mockAnalyticsClient.endpointClient,
).thenReturn(mockEndpointClient);

await expectLater(
pinpointProvider.init(
config: notificationsPinpointConfig,
authProviderRepo: mockAmplifyAuthProviderRepository,
analyticsClient: mockAnalyticsClient,
),
completes,
);

expect(
pinpointProvider.registerDevice(
'',
),
completes,
);
verify(mockEndpointClient.updateEndpoint);
});

test('recordEvent should run successfully', () async {
when(
() => mockAmplifyAuthProviderRepository.getAuthProvider(
Expand Down

0 comments on commit 4dc0df5

Please sign in to comment.