Path: redb-identity/tests/redb.Identity.Tests/Routes/MePasswordProcessorTests.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Routes/MePasswordProcessorTests.cs
----
using AwesomeAssertions;
using redb.Identity.Contracts.Users;
using redb.Identity.Core.Routes.Processors;
using redb.Route.Core;
using Xunit;
namespace redb.Identity.Tests.Routes;
///
/// H3 (v1.0 DoD §6) unit tests for . Covers the
/// caller-id and request-body precondition paths which reject before
/// any access, so no DB or service wiring is required.
/// Happy-path change + session-revocation flow lives in the integration suite.
///
public sealed class MePasswordProcessorTests
{
private static Exchange BuildExchange(long? userId, object? body)
{
var ex = new Exchange(new Message { Body = body });
ex.In.Headers["operation"] = "change";
if (userId is not null)
ex.Properties["identity:management-user-id"] = userId.Value;
return ex;
}
[Fact]
public async Task MissingSubject_Returns401()
{
var sut = new MePasswordProcessor(context: null!, redbName: null);
var ex = BuildExchange(
userId: null,
body: new MeChangePasswordRequest { OldPassword = "old-pass-123", NewPassword = "new-pass-456" });
await sut.Process(ex);
ex.HasOut.Should().BeTrue();
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(401);
ex.IsStopped.Should().BeTrue();
var body = (Dictionary)ex.Out.Body!;
body["error"].Should().Be("invalid_token");
}
[Fact]
public async Task InvalidBodyType_Returns400_ValidationError()
{
var sut = new MePasswordProcessor(context: null!, redbName: null);
// Body is a generic dict, not MeChangePasswordRequest — must be rejected.
var ex = BuildExchange(userId: 42, body: new Dictionary());
await sut.Process(ex);
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400);
var body = (Dictionary)ex.Out.Body!;
body["error"].Should().Be("validation_error");
}
[Fact]
public async Task EmptyOldPassword_Returns400()
{
var sut = new MePasswordProcessor(context: null!, redbName: null);
var ex = BuildExchange(
userId: 42,
body: new MeChangePasswordRequest { OldPassword = "", NewPassword = "new-pass-456" });
await sut.Process(ex);
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400);
var body = (Dictionary)ex.Out.Body!;
body["error"].Should().Be("validation_error");
body["error_description"].Should().Be("OldPassword is required");
}
[Fact]
public async Task WeakNewPassword_Returns400()
{
var sut = new MePasswordProcessor(context: null!, redbName: null);
var ex = BuildExchange(
userId: 42,
body: new MeChangePasswordRequest { OldPassword = "old-pass-123", NewPassword = "x" });
await sut.Process(ex);
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400);
var body = (Dictionary)ex.Out.Body!;
body["error"].Should().Be("validation_error");
((string)body["error_description"]!).Should().Contain("NewPassword");
}
[Fact]
public async Task SameOldAndNewPassword_Returns400()
{
var sut = new MePasswordProcessor(context: null!, redbName: null);
var ex = BuildExchange(
userId: 42,
body: new MeChangePasswordRequest { OldPassword = "same-pass-123", NewPassword = "same-pass-123" });
await sut.Process(ex);
ex.Out!.Headers["redbHttp.ResponseCode"].Should().Be(400);
var body = (Dictionary)ex.Out.Body!;
body["error"].Should().Be("validation_error");
((string)body["error_description"]!).Should().Contain("differ");
}
}