-
Notifications
You must be signed in to change notification settings - Fork 165
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
Added RetryCustomConfirmationAsync #3468
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,7 +137,7 @@ | |
|
||
await DefaultApp.RemoveUserAsync(second); | ||
|
||
// TODO: validate that the refresh token is invalidated. | ||
Check warning on line 140 in Tests/Realm.Tests/Sync/UserManagementTests.cs GitHub Actions / Verify TODOsTests/Realm.Tests/Sync/UserManagementTests.cs#L140
|
||
Assert.That(second.State, Is.EqualTo(UserState.Removed)); | ||
Assert.That(second.AccessToken, Is.Empty); | ||
Assert.That(second.RefreshToken, Is.Empty); | ||
|
@@ -400,6 +400,42 @@ | |
}); | ||
} | ||
|
||
[Test] | ||
public void User_RetryCustomConfirmationAsync_WorksInAllScenarios() | ||
{ | ||
SyncTestHelpers.RunBaasTestAsync(async () => | ||
{ | ||
// Standard case | ||
var unconfirmedMail = SyncTestHelpers.GetUnconfirmedUsername(); | ||
var credentials = Credentials.EmailPassword(unconfirmedMail, SyncTestHelpers.DefaultPassword); | ||
|
||
// The first time the confirmation function is called we return "pending", so the user needs to be confirmed. | ||
// At the same time we save the user email in a collection. | ||
await DefaultApp.EmailPasswordAuth.RegisterUserAsync(unconfirmedMail, SyncTestHelpers.DefaultPassword).Timeout(10_000, detail: "Failed to register user"); | ||
|
||
var ex3 = await TestHelpers.AssertThrows<AppException>(() => DefaultApp.LogInAsync(credentials)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not super important, but why is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because before I had the sections in the opposite order and I forgot to rename the exceptions 😁 |
||
Assert.That(ex3.Message, Does.Contain("confirmation required")); | ||
|
||
// The second time we call the confirmation function we find the email we saved in the collection and return "success", so the user | ||
// gets confirmed and can log in. | ||
await DefaultApp.EmailPasswordAuth.RetryCustomConfirmationAsync(unconfirmedMail); | ||
var user = await DefaultApp.LogInAsync(credentials); | ||
Assert.That(user.State, Is.EqualTo(UserState.LoggedIn)); | ||
|
||
// Logged in user case | ||
var loggedInUser = await GetUserAsync(); | ||
var ex = await TestHelpers.AssertThrows<AppException>(() => DefaultApp.EmailPasswordAuth.RetryCustomConfirmationAsync(loggedInUser.Profile.Email!)); | ||
Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); | ||
Assert.That(ex.Message, Does.Contain("already confirmed")); | ||
|
||
// Unknown user case | ||
var invalidEmail = "test@gmail.com"; | ||
var ex2 = await TestHelpers.AssertThrows<AppException>(() => DefaultApp.EmailPasswordAuth.RetryCustomConfirmationAsync(invalidEmail)); | ||
Assert.That(ex2.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); | ||
Assert.That(ex2.Message, Does.Contain("user not found")); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void User_JWT_LogsInAndReadsDataFromToken() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,12 +88,33 @@ public class FunctionReturn | |
} | ||
|
||
private const string ConfirmFuncSource = | ||
@"exports = ({ token, tokenId, username }) => { | ||
@"exports = async function ({ token, tokenId, username }) { | ||
// process the confirm token, tokenId and username | ||
if (username.includes(""realm_tests_do_autoverify"")) { | ||
return { status: 'success' } | ||
return { status: 'success' }; | ||
} | ||
// do not confirm the user | ||
|
||
if (username.includes(""realm_tests_do_not_confirm"")) { | ||
const mongodb = context.services.get('BackingDB'); | ||
let collection = mongodb.db('test_db').collection('not_confirmed'); | ||
let result = await collection.findOne({'email': username}); | ||
|
||
if(result === null) | ||
{ | ||
let newVal = { | ||
'email': username, | ||
'token': token, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Saving the token and tokenId is not necessary now, but it could be useful if we want to write tests for |
||
'tokenId': tokenId, | ||
} | ||
|
||
await collection.insertOne(newVal); | ||
return { status: 'pending' }; | ||
} | ||
|
||
return { status: 'success' }; | ||
} | ||
|
||
// fail the user confirmation | ||
return { status: 'fail' }; | ||
};"; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RetryCustomConfirmation
will fail if the input doesn't look like a proper email. We could add the same toGetVerifiedUsername
for consistency