Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve robustness of API key tests #978

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/cli/metrics/metrics.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -551,21 +551,29 @@ Future<User> loginWithRetry(App app, Credentials credentials, {int retryCount =
}

Future<void> waitForCondition(
bool Function() condition, {
FutureOr<bool> Function() condition, {
Duration timeout = const Duration(seconds: 1),
Duration retryDelay = const Duration(milliseconds: 100),
String? message,
}) async {
await Future.any<void>([
Future<void>.delayed(timeout, () => throw TimeoutException('Condition not met within $timeout. Message: ${message != null ? ': $message' : ''}')),
Future.doWhile(() async {
if (condition()) {
return false;
}
await Future<void>.delayed(retryDelay);
return true;
})
]);
}) {
return waitForConditionWithResult<bool>(() => condition(), (value) => value == true);
nirinchev marked this conversation as resolved.
Show resolved Hide resolved
}

Future<T> waitForConditionWithResult<T>(FutureOr<T> Function() getter, FutureOr<bool> Function(T value) condition,
{Duration timeout = const Duration(seconds: 1), Duration retryDelay = const Duration(milliseconds: 100), String? message}) async {
final start = DateTime.now();
while (true) {
final value = await getter();
if (await condition(value)) {
return value;
}

if (DateTime.now().difference(start) > timeout) {
throw TimeoutException('Condition not met within $timeout. Message: ${message != null ? ': $message' : ''}');
}

await Future<void>.delayed(retryDelay);
}
}

extension RealmObjectTest on RealmObject {
Expand Down
73 changes: 48 additions & 25 deletions test/user_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,31 @@ Future<void> main([List<String>? args]) async {
expect(user.profile.email, testUsername);
});

Future<ApiKey> createAndVerifyApiKey(User user, String name) async {
final result = await user.apiKeys.create(name);
await waitForConditionWithResult<ApiKey?>(() => user.apiKeys.fetch(result.id), (fetched) => fetched != null, timeout: Duration(seconds: 15));
return result;
}

Future<void> enableAndVerifyApiKey(User user, ObjectId keyId) async {
await user.apiKeys.enable(keyId);
await waitForConditionWithResult<ApiKey?>(() => user.apiKeys.fetch(keyId), (fetched) => fetched!.isEnabled, timeout: Duration(seconds: 15));
}

Future<void> disableAndVerifyApiKey(User user, ObjectId keyId) async {
await user.apiKeys.disable(keyId);
await waitForConditionWithResult<ApiKey?>(() => user.apiKeys.fetch(keyId), (fetched) => !fetched!.isEnabled, timeout: Duration(seconds: 15));
}

Future<void> deleteAndVerifyApiKey(User user, ObjectId keyId) async {
await user.apiKeys.delete(keyId);
await waitForConditionWithResult<ApiKey?>(() => user.apiKeys.fetch(keyId), (fetched) => fetched == null, timeout: Duration(seconds: 15));
}

baasTest('User.apiKeys.create creates and reveals value', (configuration) async {
final app = App(configuration);
final user = await getIntegrationUser(app);
final apiKey = await user.apiKeys.create('my-api-key');
final apiKey = await createAndVerifyApiKey(user, 'my-api-key');

expect(apiKey.isEnabled, true);
expect(apiKey.name, 'my-api-key');
Expand Down Expand Up @@ -170,7 +191,7 @@ Future<void> main([List<String>? args]) async {
baasTest('User.apiKeys.fetch with existent returns result', (configuration) async {
final app = App(configuration);
final user = await getIntegrationUser(app);
final apiKey = await user.apiKeys.create('my-api-key');
final apiKey = await createAndVerifyApiKey(user, 'my-api-key');

final refetched = await user.apiKeys.fetch(apiKey.id);

Expand All @@ -189,7 +210,7 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final original = await user.apiKeys.create('my-api-key');
final original = await createAndVerifyApiKey(user, 'my-api-key');

final apiKeys = await user.apiKeys.fetchAll();

Expand All @@ -203,7 +224,7 @@ Future<void> main([List<String>? args]) async {

final original = <ApiKey>[];
for (var i = 0; i < 5; i++) {
original.add(await user.apiKeys.create('my-api-key-$i'));
original.add(await createAndVerifyApiKey(user, 'my-api-key-$i'));
}

final fetched = await user.apiKeys.fetchAll();
Expand All @@ -217,7 +238,7 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final key = await user.apiKeys.create('key');
final key = await createAndVerifyApiKey(user, 'key');

await user.apiKeys.delete(ObjectId());

Expand All @@ -230,10 +251,10 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final toDelete = await user.apiKeys.create('to-delete');
final toRemain = await user.apiKeys.create('to-remain');
final toDelete = await createAndVerifyApiKey(user, 'to-delete');
final toRemain = await createAndVerifyApiKey(user, 'to-remain');

await user.apiKeys.delete(toDelete.id);
await deleteAndVerifyApiKey(user, toDelete.id);

final fetched = await user.apiKeys.fetch(toDelete.id);
expect(fetched, isNull);
Expand Down Expand Up @@ -271,7 +292,7 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final key = await user.apiKeys.create('my-key');
final key = await createAndVerifyApiKey(user, 'my-key');

expect(key.isEnabled, true);

Expand All @@ -286,11 +307,11 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final key = await user.apiKeys.create('my-key');
final key = await createAndVerifyApiKey(user, 'my-key');

expect(key.isEnabled, true);

await user.apiKeys.disable(key.id);
await disableAndVerifyApiKey(user, key.id);

final fetched = await user.apiKeys.fetch(key.id);
expect(fetched!.isEnabled, false);
Expand All @@ -305,15 +326,16 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final first = await user.apiKeys.create('first');
final second = await user.apiKeys.create('second');
final first = await createAndVerifyApiKey(user, 'first');
final second = await createAndVerifyApiKey(user, 'second');

expect(first.isEnabled, true);
expect(second.isEnabled, true);

await user.apiKeys.disable(first.id);
await disableAndVerifyApiKey(user, first.id);

final fetched = await user.apiKeys.fetchAll();

expect(fetched[0].id, first.id);
expect(fetched[0].isEnabled, false);

Expand All @@ -325,13 +347,13 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final first = await user.apiKeys.create('first');
final second = await user.apiKeys.create('second');
final first = await createAndVerifyApiKey(user, 'first');
final second = await createAndVerifyApiKey(user, 'second');

expect(first.isEnabled, true);
expect(second.isEnabled, true);

await user.apiKeys.disable(first.id);
await disableAndVerifyApiKey(user, first.id);

final fetched = await user.apiKeys.fetchAll();
expect(fetched[0].id, first.id);
Expand All @@ -340,7 +362,7 @@ Future<void> main([List<String>? args]) async {
expect(fetched[1].id, second.id);
expect(fetched[1].isEnabled, true);

await user.apiKeys.enable(first.id);
await enableAndVerifyApiKey(user, first.id);

final refetched = await user.apiKeys.fetchAll();
expect(refetched[0].id, first.id);
Expand All @@ -354,7 +376,8 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final key = await user.apiKeys.create('my-key');
final key = await createAndVerifyApiKey(user, 'my-key');

final credentials = Credentials.apiKey(key.value!);

final apiKeyUser = await app.logIn(credentials);
Expand All @@ -367,9 +390,9 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final key = await user.apiKeys.create('my-key');
final key = await createAndVerifyApiKey(user, 'my-key');

await user.apiKeys.disable(key.id);
await disableAndVerifyApiKey(user, key.id);

final credentials = Credentials.apiKey(key.value!);

Expand All @@ -380,7 +403,7 @@ Future<void> main([List<String>? args]) async {
.having((e) => e.statusCode, 'statusCode', 401)
.having((e) => e.linkToServerLogs, 'linkToServerLogs', contains('logs?co_id='))));

await user.apiKeys.enable(key.id);
await enableAndVerifyApiKey(user, key.id);

final apiKeyUser = await app.logIn(credentials);
expect(apiKeyUser.provider, AuthProviderType.apiKey);
Expand All @@ -392,9 +415,9 @@ Future<void> main([List<String>? args]) async {
final app = App(configuration);
final user = await getIntegrationUser(app);

final key = await user.apiKeys.create('my-key');
final key = await createAndVerifyApiKey(user, 'my-key');

await user.apiKeys.delete(key.id);
await deleteAndVerifyApiKey(user, key.id);

final credentials = Credentials.apiKey(key.value!);

Expand Down Expand Up @@ -433,7 +456,7 @@ Future<void> main([List<String>? args]) async {
baasTest("Credentials.apiKey user cannot access API keys", (configuration) async {
final app = App(configuration);
final user = await getIntegrationUser(app);
final apiKey = await user.apiKeys.create('my-key');
final apiKey = await createAndVerifyApiKey(user, 'my-key');

final apiKeyUser = await app.logIn(Credentials.apiKey(apiKey.value!));

Expand Down