Ejemplo n.º 1
0
        public async Task WhenTheCreationTypeIsRefreshToken_ItSouldUpdateAccessTokenTimestampOnly()
        {
            var command = new UpdateLastAccessTokenDateCommand(
                UserId,
                DateTime.UtcNow,
                UpdateLastAccessTokenDateCommand.AccessTokenCreationType.RefreshToken);

            var userRepository = new Mock <IUpdateUserTimeStampsDbStatement>();

            userRepository.Setup(v => v.UpdateAccessTokenAsync(command.UserId, command.Timestamp))
            .Returns(Task.FromResult(0)).Verifiable();

            var target = new UpdateLastAccessTokenDateCommandHandler(userRepository.Object);

            await target.HandleAsync(command);

            userRepository.Verify();
        }
Ejemplo n.º 2
0
        public async Task WhenGrantingRefreshToken_AndItSucceedes_ItShouldUpdateTheAccessTokenTimestamp()
        {
            var context = new OAuthGrantRefreshTokenContext(
                new OwinContext(this.environment),
                new OAuthAuthorizationServerOptions()
            {
                AuthenticationType = AuthenticationType
            },
                new AuthenticationTicket(
                    new ClaimsIdentity(
                        new List <Claim> {
                new Claim(ClaimTypes.NameIdentifier, UserClaimsIdentity.UserId.Value.EncodeGuid())
            },
                        AuthenticationType),
                    new AuthenticationProperties(new Dictionary <string, string> {
                { Constants.TokenClientIdKey, ClientId }
            })),
                ClientId);

            this.getUserClaimsIdentity
            .Setup(v => v.HandleAsync(new GetUserClaimsIdentityQuery(UserClaimsIdentity.UserId, null, null, AuthenticationType)))
            .ReturnsAsync(UserClaimsIdentity);

            UpdateLastAccessTokenDateCommand updateTimestampCommand = null;

            this.updateLastAccessTokenDate
            .Setup(v => v.HandleAsync(It.IsAny <UpdateLastAccessTokenDateCommand>()))
            .Callback <UpdateLastAccessTokenDateCommand>(v => updateTimestampCommand = v)
            .Returns(Task.FromResult(0));

            var before = DateTime.UtcNow;

            await this.target.GrantRefreshTokenAsync(context);

            var after = DateTime.UtcNow;

            Assert.IsNotNull(updateTimestampCommand);
            Assert.AreEqual(UserClaimsIdentity.UserId, updateTimestampCommand.UserId);
            Assert.AreEqual(UpdateLastAccessTokenDateCommand.AccessTokenCreationType.RefreshToken, updateTimestampCommand.CreationType);
            Assert.IsTrue(updateTimestampCommand.Timestamp <= after);
            Assert.IsTrue(updateTimestampCommand.Timestamp >= before);
        }
Ejemplo n.º 3
0
        public async Task WhenGrantingResourceOwnerCredentials_AndValidInformationIsGiven_ItShouldUpdateTheAccessTokenTimestamp()
        {
            var context = new OAuthGrantResourceOwnerCredentialsContext(
                new OwinContext(this.environment),
                new OAuthAuthorizationServerOptions()
            {
                AuthenticationType = AuthenticationType
            },
                ClientId,
                Username,
                Password,
                new List <string>());

            context.OwinContext.Set <string>(Constants.TokenAllowedOriginKey, Constants.DefaultAllowedOrigin);

            this.getUserClaimsIdentity
            .Setup(v => v.HandleAsync(new GetUserClaimsIdentityQuery(null, new Username(Username), new Password(Password), AuthenticationType)))
            .ReturnsAsync(UserClaimsIdentity);

            UpdateLastAccessTokenDateCommand updateTimestampCommand = null;

            this.updateLastAccessTokenDate
            .Setup(v => v.HandleAsync(It.IsAny <UpdateLastAccessTokenDateCommand>()))
            .Callback <UpdateLastAccessTokenDateCommand>(v => updateTimestampCommand = v)
            .Returns(Task.FromResult(0));

            var before = DateTime.UtcNow;

            await this.target.GrantResourceOwnerCredentialsAsync(context);

            var after = DateTime.UtcNow;

            Assert.IsNotNull(updateTimestampCommand);
            Assert.AreEqual(UserClaimsIdentity.UserId, updateTimestampCommand.UserId);
            Assert.AreEqual(UpdateLastAccessTokenDateCommand.AccessTokenCreationType.SignIn, updateTimestampCommand.CreationType);
            Assert.IsTrue(updateTimestampCommand.Timestamp <= after);
            Assert.IsTrue(updateTimestampCommand.Timestamp >= before);
        }