From 081445b774d36806918f18db641f4cd0c82b5913 Mon Sep 17 00:00:00 2001 From: Hassan Ben Jobrane Date: Tue, 31 Oct 2023 20:01:52 +0100 Subject: [PATCH] chore: use different test emails for auth and storage tests --- packages/nhost_dart/test/auth_test.dart | 32 +++++++++++--------- packages/nhost_dart/test/storage_test.dart | 34 +++++++++------------- packages/nhost_dart/test/test_helpers.dart | 22 +++++++++----- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/packages/nhost_dart/test/auth_test.dart b/packages/nhost_dart/test/auth_test.dart index 4f257a3d..67ca4540 100644 --- a/packages/nhost_dart/test/auth_test.dart +++ b/packages/nhost_dart/test/auth_test.dart @@ -14,7 +14,7 @@ import 'admin_gql.dart'; import 'setup.dart'; import 'test_helpers.dart'; -const testEmail = 'user-1@nhost.io'; +var testEmail = getTestEmail(); const testPassword = 'password-1'; const invalidRefreshToken = '10b27fd6-a606-42f4-9063-d6bd9d7866c8'; @@ -34,6 +34,10 @@ void main() async { initLogging(); }); + tearDownAll(() async { + await gqlAdmin.clearUsers(); + }); + setUp(() async { // Clear out any data from the previous test await gqlAdmin.clearUsers(); @@ -134,7 +138,8 @@ void main() async { late String refreshToken; // Each tests registers a basic user, and leaves auth in a logged out state setUp(() async { - final res = await registerAndSignInBasicUser(auth); + final res = + await registerAndSignInBasicUser(auth, testEmail, testPassword); refreshToken = res.session!.refreshToken!; // Don't log out, so we can keep a valid refresh token await auth.clearSession(); @@ -258,7 +263,7 @@ void main() async { group('signOut', () { // All signOut tests log a user in first setUp(() async { - await registerAndSignInBasicUser(auth); + await registerAndSignInBasicUser(auth, testEmail, testPassword); assert(auth.currentUser != null); }); @@ -296,9 +301,9 @@ void main() async { }); test('succeeds if the user exists', () async { - await registerTestUser(auth); + await registerTestUser(auth, testEmail, testPassword); expect( - auth.sendVerificationEmail(email: defaultTestEmail), + auth.sendVerificationEmail(email: testEmail), completes, ); }); @@ -316,19 +321,19 @@ void main() async { }); test('should be called on sign in', () async { - await registerTestUser(auth); + await registerTestUser(auth, testEmail, testPassword); await auth.signInEmailPassword(email: testEmail, password: testPassword); expect(authStateVar, AuthenticationState.signedIn); }); test('should be called on sign out', () async { - await registerTestUser(auth); + await registerTestUser(auth, testEmail, testPassword); await auth.signOut(); expect(authStateVar, AuthenticationState.signedOut); }); test('should not be called once unsubscribed', () async { - await registerTestUser(auth); + await registerTestUser(auth, testEmail, testPassword); unsubscribe(); await auth.signInEmailPassword(email: testEmail, password: testPassword); expect(authStateVar, AuthenticationState.signedOut); @@ -470,7 +475,7 @@ void main() async { group('email change', () { setUp(() async { - await registerAndSignInBasicUser(auth); + await registerAndSignInBasicUser(auth, testEmail, testPassword); }); // This should be tested, but requires a server configured with @@ -515,7 +520,7 @@ void main() async { group('password change', () { setUp(() async { - await registerAndSignInBasicUser(auth); + await registerAndSignInBasicUser(auth, testEmail, testPassword); }); test('should be able to change password directly', () async { @@ -557,7 +562,7 @@ void main() async { group('multi-factor authentication', () { test('can be enabled on a user', () async { - await registerAndSignInBasicUser(auth); + await registerAndSignInBasicUser(auth, testEmail, testPassword); // Ask the backend to generate MFA configuration, and from that, generate // a time-based OTP. @@ -571,7 +576,7 @@ void main() async { }); test('should require TOTP for sign in once enabled', () async { - final otpSecret = await registerMfaUser(auth); + final otpSecret = await registerMfaUser(auth, testEmail, testPassword); final firstFactorAuthResult = await auth.signInEmailPassword( email: testEmail, password: testPassword); @@ -589,7 +594,8 @@ void main() async { }); test('can be disabled', () async { - final otpSecret = await registerMfaUser(auth, signOut: false); + final otpSecret = + await registerMfaUser(auth, testEmail, testPassword, signOut: false); expect( auth.disableMfa(totpFromSecret(otpSecret)), diff --git a/packages/nhost_dart/test/storage_test.dart b/packages/nhost_dart/test/storage_test.dart index f166fe51..30b32c7a 100644 --- a/packages/nhost_dart/test/storage_test.dart +++ b/packages/nhost_dart/test/storage_test.dart @@ -1,11 +1,11 @@ import 'dart:math' as math; +import 'package:graphql/client.dart'; import 'package:http/http.dart' as http; import 'package:mockito/mockito.dart'; import 'package:nhost_dart/nhost_dart.dart'; import 'package:nhost_sdk/src/foundation/collection.dart'; import 'package:nhost_storage_dart/nhost_storage_dart.dart'; -import 'package:path/path.dart' show Context, Style; import 'package:test/test.dart'; import 'admin_gql.dart'; @@ -13,16 +13,15 @@ import 'matchers.dart'; import 'setup.dart'; import 'test_helpers.dart'; -/// Used to join URL paths -final pathContext = Context(style: Style.url); +var testEmail = getTestEmail(); +const testPassword = 'password-1'; void main() async { - final unrecordedGqlAdmin = GqlAdminTestHelper( + final gqlAdmin = GqlAdminTestHelper( subdomain: subdomain, region: region, gqlUrl: gqlUrl, ); - GqlAdminTestHelper? recordedGqlAdmin; NhostClient client; late NhostStorageClient storage; @@ -31,12 +30,13 @@ void main() async { initLogging(); }); - setUp(() async { - // Clear out any data from the previous test - await unrecordedGqlAdmin.clearUsers(); + tearDownAll(() async { + await gqlAdmin.clearUsers(); + }); + setUp(() async { // Clear out all user files from previous run - await unrecordedGqlAdmin.clearFiles(); + await gqlAdmin.clearFiles(); var httpClient = http.Client(); @@ -44,16 +44,10 @@ void main() async { client = createApiTestClient(httpClient); // Register the basic user - await registerAndSignInBasicUser(client.auth); + await registerAndSignInBasicUser(client.auth, testEmail, testPassword); // Provide a few values to tests storage = client.storage; - recordedGqlAdmin = GqlAdminTestHelper( - subdomain: subdomain, - region: region, - gqlUrl: gqlUrl, - httpClientOverride: httpClient, - ); }); group('creating files', () { @@ -81,7 +75,7 @@ void main() async { // Verify stored media - final storedFile = await recordedGqlAdmin!.getFileInfo(fileMetadata.id); + final storedFile = await gqlAdmin.getFileInfo(fileMetadata.id); expect(storedFile!.name, filePath); expect(storedFile.mimeType, 'text/plain; charset=utf-8'); }); @@ -120,7 +114,7 @@ void main() async { expect(fileMetadata.mimeType, 'text/html'); // Verify stored media - final storedFile = await recordedGqlAdmin!.getFileInfo(fileMetadata.id); + final storedFile = await gqlAdmin.getFileInfo(fileMetadata.id); expect(storedFile!.name, filePath); expect(storedFile.mimeType, 'text/html'); }); @@ -175,13 +169,13 @@ void main() async { test('can be deleted', () async { // Sanity check - expect(await recordedGqlAdmin!.getFileInfo(fileId), isNotNull); + expect(await gqlAdmin.getFileInfo(fileId), isNotNull); // Now delete the file await storage.delete(fileId); // And ensure it is no longer available - expect(await recordedGqlAdmin!.getFileInfo(fileId), isNull); + expect(await gqlAdmin.getFileInfo(fileId), isNull); }); }); } diff --git a/packages/nhost_dart/test/test_helpers.dart b/packages/nhost_dart/test/test_helpers.dart index 1374adc1..e22c5106 100644 --- a/packages/nhost_dart/test/test_helpers.dart +++ b/packages/nhost_dart/test/test_helpers.dart @@ -3,6 +3,12 @@ import 'package:nhost_sdk/nhost_sdk.dart'; import 'package:otp/otp.dart'; const defaultTestEmail = 'user-1@nhost.io'; + +String getTestEmail() { + var now = DateTime.now().millisecondsSinceEpoch; + return "user-$now@nhost.io"; +} + const defaultTestPhone = '289-289-2899'; const defaultTestPassword = 'password-1'; final defaultCreatedAt = DateTime(1983); @@ -24,23 +30,25 @@ User createTestUser({required String id, required String email}) { // Registers a basic user for test setup. The auth object will be left in a // logged out state. -Future registerTestUser(NhostAuthClient auth) async { - await auth.signUp(email: defaultTestEmail, password: defaultTestPassword); +Future registerTestUser( + NhostAuthClient auth, String email, String password) async { + await auth.signUp(email: email, password: password); await auth.signOut(); } // Register and logs in a basic user for test setup. The auth object will be // left in a logged in state. -Future registerAndSignInBasicUser(NhostAuthClient auth) async { - return await auth.signUp( - email: defaultTestEmail, password: defaultTestPassword); +Future registerAndSignInBasicUser( + NhostAuthClient auth, String email, String password) async { + return await auth.signUp(email: email, password: password); } // Registers an MFA user for test setup, logs them out, and returns the OTP // secret. -Future registerMfaUser(NhostAuthClient auth, +Future registerMfaUser( + NhostAuthClient auth, String email, String password, {bool signOut = true}) async { - await auth.signUp(email: defaultTestEmail, password: defaultTestPassword); + await auth.signUp(email: email, password: password); final mfaDetails = await auth.generateMfa(); await auth.enableMfa(totpFromSecret(mfaDetails.totpSecret)); if (signOut) await auth.signOut();