Skip to content

Commit

Permalink
chore: use different test emails for auth and storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
onehassan committed Oct 31, 2023
1 parent 827df46 commit 081445b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
32 changes: 19 additions & 13 deletions packages/nhost_dart/test/auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'admin_gql.dart';
import 'setup.dart';
import 'test_helpers.dart';

const testEmail = '[email protected]';
var testEmail = getTestEmail();
const testPassword = 'password-1';

const invalidRefreshToken = '10b27fd6-a606-42f4-9063-d6bd9d7866c8';
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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,
);
});
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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)),
Expand Down
34 changes: 14 additions & 20 deletions packages/nhost_dart/test/storage_test.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
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';
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;
Expand All @@ -31,29 +30,24 @@ 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();

// Create a fresh client
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', () {
Expand Down Expand Up @@ -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');
});
Expand Down Expand Up @@ -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');
});
Expand Down Expand Up @@ -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);
});
});
}
Expand Down
22 changes: 15 additions & 7 deletions packages/nhost_dart/test/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import 'package:nhost_sdk/nhost_sdk.dart';
import 'package:otp/otp.dart';

const defaultTestEmail = '[email protected]';

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);
Expand All @@ -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<void> registerTestUser(NhostAuthClient auth) async {
await auth.signUp(email: defaultTestEmail, password: defaultTestPassword);
Future<void> 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<AuthResponse> registerAndSignInBasicUser(NhostAuthClient auth) async {
return await auth.signUp(
email: defaultTestEmail, password: defaultTestPassword);
Future<AuthResponse> 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<String> registerMfaUser(NhostAuthClient auth,
Future<String> 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();
Expand Down

0 comments on commit 081445b

Please sign in to comment.