Example #1
0
 public static AuthResponseModel AsResponse(this AuthAccessModel auth) =>
 new AuthResponseModel
 {
     UserId  = auth.Id,
     Token   = auth.Token,
     Email   = auth.Email,
     Name    = auth.Name,
     Expires = auth.ExpiresAt.ToString()
 };
Example #2
0
        public void AuthenticateCommand_AuthUser_SuccessAuth()
        {
            // ---- Arrange ----

            const string email        = "*****@*****.**";
            const string name         = "Test user";
            const string password     = "******";
            var          passwordHash = AuthUtils.GetMd5Hash(password);

            var clock    = new FakeClock(SystemClock.Instance.GetCurrentInstant());
            var testUser = new UserIdentityModel(
                Guid.NewGuid(),
                email,
                name,
                "user",
                passwordHash,
                clock.GetCurrentInstant());

            _authRepositoryMock.Setup(r => r.GetUserIdentity(email))
            .ReturnsAsync(() => testUser);

            var command = new AuthenticateUserCommand(email, password);
            var handler = new AuthenticateUserCommandHandler(_repositoryProviderMock.Object, _configuration)
            {
                Clock = clock
            };

            AuthAccessModel result = null;

            var lifetime = DurationUtils.FromString(
                _configuration[$"{AuthUtils.Jwt.ConfigKeys.Section}:{AuthUtils.Jwt.ConfigKeys.LifetimeKey}"]);

            // ---- Act ----

            Assert.DoesNotThrowAsync(async() => {
                result = await handler.Handle(command, CancellationToken.None);
            });

            clock.Advance(lifetime); // for check expires token instant

            // ---- Assert ----

            Assert.IsNotNull(result);
            Assert.AreEqual(testUser.Id, result.Id);
            Assert.AreEqual(testUser.Email, result.Email);
            Assert.AreEqual(clock.GetCurrentInstant(), result.ExpiresAt);
            Assert.AreEqual(clock.GetCurrentInstant(), result.ExpiresAt);
            Assert.AreEqual("user", testUser.Role);
            Assert.IsNotEmpty(result.Token);

            _authRepositoryMock.Verify(r => r.GetUserIdentity(email), Times.Once);
        }