コード例 #1
0
            public async Task WhenContextIsValid_AndRefreshTokenFoundAndNotExpired_ItShouldCreateARefreshToken()
            {
                var context = new AuthenticationTokenCreateContext(
                    new OwinContext(this.environment),
                    this.secureDataFormat.Object,
                    Ticket);

                context.OwinContext.Set <int>(Constants.TokenRefreshTokenLifeTimeKey, RefreshTokenLifetimeMinutes);

                this.tryGetRefreshToken.Setup(v => v.HandleAsync(new TryGetRefreshTokenQuery(new ClientId(ClientId), new Username(Username))))
                .ReturnsAsync(new RefreshToken {
                    ExpiresDate = Now.AddTicks(1), EncryptedId = "encryptedId"
                });

                this.encryptionService.Setup(v => v.DecryptRefreshTokenId(new EncryptedRefreshTokenId("encryptedId"))).Returns(new RefreshTokenId("existingId"));

                SetRefreshTokenCommand command = null;

                this.createRefreshToken.Setup(v => v.HandleAsync(It.IsAny <SetRefreshTokenCommand>()))
                .Callback <SetRefreshTokenCommand>(v => command = v)
                .Returns(Task.FromResult(0));

                await this.target.CreateAsync(context);

                Assert.IsNull(command);

                Assert.AreEqual(context.Token, "existingId");
            }
コード例 #2
0
            public async Task WhenContextIsValid_AndRefreshTokenFoundAndExpired_ItShouldCreateARefreshToken()
            {
                var context = new AuthenticationTokenCreateContext(
                    new OwinContext(this.environment),
                    this.secureDataFormat.Object,
                    Ticket);

                context.OwinContext.Set <int>(Constants.TokenRefreshTokenLifeTimeKey, RefreshTokenLifetimeMinutes);

                this.tryGetRefreshToken.Setup(v => v.HandleAsync(new TryGetRefreshTokenQuery(new ClientId(ClientId), new Username(Username))))
                .ReturnsAsync(new RefreshToken {
                    ExpiresDate = Now
                });

                SetRefreshTokenCommand command = null;

                this.createRefreshToken.Setup(v => v.HandleAsync(It.IsAny <SetRefreshTokenCommand>()))
                .Callback <SetRefreshTokenCommand>(v => command = v)
                .Returns(Task.FromResult(0));

                await this.target.CreateAsync(context);

                Assert.IsNotNull(command);
                Assert.AreEqual(ClientId, command.ClientId.Value);
                Assert.AreEqual(Username, command.Username.Value);
                Assert.AreEqual(ProtectedTicket, command.ProtectedTicket);
                Assert.AreEqual(Now, command.IssuedDate);
                Assert.AreEqual(Now.AddMinutes(RefreshTokenLifetimeMinutes), command.ExpiresDate);
                Assert.IsNotNull(command.RefreshTokenId);

                Assert.AreEqual(context.Token, command.RefreshTokenId.Value);
            }
コード例 #3
0
        public async Task WhenCommandIsValid_ItShouldCallTheDbStatementWithAToken()
        {
            var command = new SetRefreshTokenCommand(
                RefreshTokenId,
                ClientId,
                Username,
                ProtectedTicket,
                IssuedDate,
                ExpiresDate);

            this.encryptionService.Setup(v => v.EncryptRefreshTokenId(RefreshTokenId)).Returns(EncryptedRefreshTokenId);

            this.upsertRefreshToken.Setup(v => v.ExecuteAsync(
                                              new RefreshToken(Username.Value,
                                                               ClientId.Value,
                                                               EncryptedRefreshTokenId.Value,
                                                               IssuedDate,
                                                               ExpiresDate, ProtectedTicket))).Returns(Task.FromResult(0)).Verifiable();

            await this.target.HandleAsync(command);

            this.upsertRefreshToken.Verify();
        }