Skip to content

Commit

Permalink
Update tests to match new 401 messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed May 22, 2024
1 parent c3e2c0a commit 86f7680
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
8 changes: 7 additions & 1 deletion packages/realm_dart/test/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ void main() {
await app.deleteUser(user);
expect(user.state, UserState.removed);

await expectLater(() => loginWithRetry(app, Credentials.emailPassword(username, strongPassword)), throws<AppException>("invalid username/password"));
await expectLater(
() => loginWithRetry(app, Credentials.emailPassword(username, strongPassword)),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', contains('unauthorized'))
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))),
);
});

baasTest('Call Atlas function that does not exist', (configuration) async {
Expand Down
34 changes: 28 additions & 6 deletions packages/realm_dart/test/credentials_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ void main() {
await authProvider.registerUser(username, strongPassword);
await authProvider.callResetPasswordFunction(username, newPassword, functionArgs: <dynamic>['success']);
await app.logIn(Credentials.emailPassword(username, newPassword));
await expectLater(() => app.logIn(Credentials.emailPassword(username, strongPassword)), throws<AppException>("invalid username/password"));
await expectLater(
app.logIn(Credentials.emailPassword(username, strongPassword)),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', contains('unauthorized'))
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))),
);
}, appName: AppName.autoConfirm);

baasTest('Email/Password - call reset password function with no additional arguments', (configuration) async {
Expand All @@ -157,12 +163,16 @@ void main() {
const String newPassword = "!@#!DQXQWD!223eda";
final authProvider = EmailPasswordAuthProvider(app);
await authProvider.registerUser(username, strongPassword);
await expectLater(() async {
await expectLater(
// Calling this function with no additional arguments fails for the test
// because of the specific implementation of resetFunc in the cloud.
// resetFunc returns status 'fail' in case no other status is passed.
return await authProvider.callResetPasswordFunction(username, newPassword);
}, throws<AppException>('failed to reset password for user "$username"'));
authProvider.callResetPasswordFunction(username, newPassword),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', contains('failed to reset password for user "$username"'))
.having((e) => e.statusCode, 'statusCode', 400)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))),
);
}, appName: AppName.autoConfirm);

/// JWT Payload data
Expand Down Expand Up @@ -298,14 +308,26 @@ void main() {
var token =
"eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiI2MmYzOTY4ODhhZjg3MjBiMzczZmYwNmEiLCJlbWFpbCI6Indvbmdfc2lnbml0dXJlX2tleUByZWFsbS5pbyIsImlhdCI6MTY2MDE0MjIxNSwiZXhwIjo0ODEzNzQyMjE1LCJhdWQiOiJtb25nb2RiLmNvbSIsImlzcyI6Imh0dHBzOi8vcmVhbG0uaW8ifQ.Af--ZUCL_KC7lAhrD_d1lq91O7qVwu7GqXifwxKojkLCkbjmAER9K2Xa7BPO8xNstFeX8m9uBo4BCD5B6XmngSmyCj5OZWdiG5LTR_uhA3MnpqcV3Vu40K4Yx8XrjPuCL39xVPnEfPKLGz5TjEcMLa8xMPqo51byX0q3mR2eSS4w1A7c5TiTNuQ23_SCO8aK95SyXwuUmU4mH0iR4sHPtf64WyoAXkx8w5twXExzky1_h473CwtAERdMsBhwz1YzFKP0kxU31pg5SRciF5Ly66sK1fSPTMQPuVdS_wKvAYll8_trWnWS83M3_PWs4UxzOdjSpoK0uqhN-_IC38YOGg";
final credentials = Credentials.jwt(token);
await expectLater(() => app.logIn(credentials), throws<AppException>("crypto/rsa: verification error"));
await expectLater(
() => app.logIn(credentials),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', contains('unauthorized'))
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))),
);
});

baasTest('Facebook credentials - invalid or expired token', (configuration) async {
final app = App(configuration);
final accessToken = 'invalid or expired token';
final credentials = Credentials.facebook(accessToken);
await expectLater(() => app.logIn(credentials), throws<AppException>("error fetching info from OAuth2 provider"));
await expectLater(
app.logIn(credentials),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', contains('unauthorized'))
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))),
);
});

baasTest('Function credentials - wrong payload', (configuration) {
Expand Down
6 changes: 3 additions & 3 deletions packages/realm_dart/test/user_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void main() {
await expectLater(
() => app.logIn(credentials),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', contains('invalid API key'))
.having((e) => e.message, 'message', contains('unauthorized'))
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))));

Expand All @@ -405,7 +405,7 @@ void main() {
await expectLater(
() => app.logIn(credentials),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', contains('invalid API key'))
.having((e) => e.message, 'message', contains('unauthorized'))
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))));
});
Expand Down Expand Up @@ -452,7 +452,7 @@ void main() {
await expectLater(
() async => await app.logIn(credentials),
throwsA(isA<AppException>()
.having((e) => e.message, 'message', 'invalid API key')
.having((e) => e.message, 'message', 'unauthorized')
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))));
});
Expand Down

0 comments on commit 86f7680

Please sign in to comment.