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

Add reset password to client #11

Merged
merged 1 commit into from
Dec 2, 2021
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
20 changes: 20 additions & 0 deletions Gotrue/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,26 @@ public async Task<bool> DeleteUser(string uid, string jwt)
throw ExceptionHandler.Parse(ex);
}
}

/// <summary>
/// Sends a reset request to an email address.
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<bool> ResetPasswordForEmail(string email)
{
try
{
var result = await api.ResetPasswordForEmail(email);
result.ResponseMessage.EnsureSuccessStatusCode();
return true;
}
catch (RequestException ex)
{
throw ExceptionHandler.Parse(ex);
}
}

/// <summary>
/// Refreshes the currently logged in User's Session.
Expand Down
95 changes: 52 additions & 43 deletions GotrueTests/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ private static string RandomString(int length)
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}

}
private static string GetRandomPhoneNumber()
{
const string chars = "123456789";
var inner = new string(Enumerable.Repeat(chars, 10)
.Select(s => s[random.Next(s.Length)]).ToArray());

return $"+1{inner}";
}

}
private string GenerateServiceRoleToken()
{
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("37c304f8-51aa-419a-a1af-06154e63707a")); // using GOTRUE_JWT_SECRET
Expand All @@ -52,8 +52,8 @@ private string GenerateServiceRoleToken()
"role", "service_role"
}
}
};

};
var tokenHandler = new JwtSecurityTokenHandler();
var securityToken = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(securityToken);
Expand All @@ -68,54 +68,54 @@ public async Task TestInitializer()
[TestMethod("Client: Signs Up User")]
public async Task ClientSignsUpUser()
{
Session session = null;
Session session = null;
var email = $"{RandomString(12)}@supabase.io";
session = await client.SignUp(email, password);

Assert.IsNotNull(session.AccessToken);
Assert.IsNotNull(session.RefreshToken);
Assert.IsInstanceOfType(session.User, typeof(User));


var phone1 = GetRandomPhoneNumber();
session = await client.SignUp(SignUpType.Phone, phone1, password);

var phone1 = GetRandomPhoneNumber();
session = await client.SignUp(SignUpType.Phone, phone1, password);
Assert.IsNotNull(session.AccessToken);
}

[TestMethod("Client: Signs Up the same user twice should throw BadRequestException")]
public async Task ClientSignsUpUserTwiceShouldReturnBadRequest()
{
var email = $"{RandomString(12)}@supabase.io";
var result1 = await client.SignUp(email, password);

await Assert.ThrowsExceptionAsync<BadRequestException>(async () =>
{
await client.SignUp(email, password);
var result1 = await client.SignUp(email, password);
await Assert.ThrowsExceptionAsync<BadRequestException>(async () =>
{
await client.SignUp(email, password);
});
}

[TestMethod("Client: Triggers Token Refreshed Event")]
public async Task ClientTriggersTokenRefreshedEvent()
{
var tsc = new TaskCompletionSource<string>();

public async Task ClientTriggersTokenRefreshedEvent()
{
var tsc = new TaskCompletionSource<string>();
var email = $"{RandomString(12)}@supabase.io";
var user = await client.SignUp(email, password);

client.StateChanged += (sender, args) =>
{
if (args.State == AuthState.TokenRefreshed)
{
tsc.SetResult(client.CurrentSession.AccessToken);
}
};

await client.RefreshSession();

var newToken = await tsc.Task;

Assert.AreNotEqual(user.RefreshToken, client.CurrentSession.RefreshToken);
var user = await client.SignUp(email, password);
client.StateChanged += (sender, args) =>
{
if (args.State == AuthState.TokenRefreshed)
{
tsc.SetResult(client.CurrentSession.AccessToken);
}
};
await client.RefreshSession();
var newToken = await tsc.Task;
Assert.AreNotEqual(user.RefreshToken, client.CurrentSession.RefreshToken);
}

[TestMethod("Client: Signs In User (Email, Phone, Refresh token)")]
Expand Down Expand Up @@ -243,27 +243,36 @@ await Assert.ThrowsExceptionAsync<BadRequestException>(async () =>
var result = await client.SignIn(user, password + "$");
});

}

}
[TestMethod("Client: Sends Invite Email")]
public async Task ClientSendsInviteEmail()
{
var user = $"{RandomString(12)}@supabase.io";
var service_role_key = GenerateServiceRoleToken();
var result = await client.InviteUserByEmail(user, service_role_key);
Assert.IsTrue(result);
}

}
[TestMethod("Client: Deletes User")]
public async Task ClientDeletesUser()
{
var user = $"{RandomString(12)}@supabase.io";
await client.SignUp(user, password);
var uid = client.CurrentUser.Id;

var uid = client.CurrentUser.Id;
var service_role_key = GenerateServiceRoleToken();
var result = await client.DeleteUser(uid, service_role_key);

var result = await client.DeleteUser(uid, service_role_key);

Assert.IsTrue(result);
}

[TestMethod("Client: Sends Reset Password Email")]
public async Task ClientSendsResetPasswordForEmail()
{
var email = $"{RandomString(12)}@supabase.io";
await client.SignUp(email, password);
var result = await client.ResetPasswordForEmail(email);
Assert.IsTrue(result);
}
}
Expand Down