-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use different test emails for auth and storage tests
- Loading branch information
Showing
3 changed files
with
48 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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)), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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(); | ||
|