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"); }
internal static bool CheckSession(string SessionId, out string UserName) { KeyValuePair <DateTime, string> Pair; DateTime TP; DateTime Now; UserName = null; lock (lastAccessBySessionId) { if (!lastAccessBySessionId.TryGetValue(SessionId, out Pair)) { return(false); } TP = Pair.Key; Now = DateTime.Now; if (Now - TP > sessionTimeout) { lastAccessBySessionId.Remove(SessionId); sessionIdByLastAccess.Remove(TP); return(false); } sessionIdByLastAccess.Remove(TP); while (sessionIdByLastAccess.ContainsKey(Now)) { Now = Now.AddTicks(gen.Next(1, 10)); } sessionIdByLastAccess [Now] = SessionId; UserName = Pair.Value; lastAccessBySessionId [SessionId] = new KeyValuePair <DateTime, string> (Now, UserName); } return(true); }
public void ReadWrite(DateTimeKind kind) { var entity = new DateTimeClass { Id = AdditionalDateId, Value = GetTestDate(kind) }; var typeKind = GetTypeKind(); // Now must be acquired before transaction because some db freezes current_timestamp at transaction start, // like PostgreSQL. https://www.postgresql.org/docs/7.2/static/functions-datetime.html#AEN6700 // This then wrecks tests with DbTimestampType if the always out of tran Now is called for fetching // beforeNow only after transaction start. // And account db accuracy var beforeNow = Now.AddTicks(-DateAccuracyInTicks); // Save using (var s = OpenSession()) using (var t = s.BeginTransaction()) { s.Save(entity); if (kind != typeKind && typeKind != DateTimeKind.Unspecified) { Assert.That(() => t.Commit(), Throws.TypeOf <PropertyValueException>()); return; } t.Commit(); } var afterNow = Now.AddTicks(DateAccuracyInTicks); if (RevisionCheck) { Assert.That(entity.Revision, Is.GreaterThan(beforeNow).And.LessThan(afterNow), "Revision not correctly seeded."); if (typeKind != DateTimeKind.Unspecified) { Assert.That(entity.Revision.Kind, Is.EqualTo(typeKind), "Revision kind not correctly seeded."); } Assert.That(entity.NullableValue, Is.Null, "NullableValue unexpectedly seeded."); } // Retrieve, compare then update DateTimeClass retrieved; using (var s = OpenSession()) { using (var t = s.BeginTransaction()) { retrieved = s.Get <DateTimeClass>(AdditionalDateId); Assert.That(retrieved, Is.Not.Null, "Entity not saved or cannot be retrieved by its key."); Assert.That(retrieved.Value, Is.EqualTo(GetExpectedValue(entity.Value)), "Unexpected value."); if (RevisionCheck) { Assert.That(retrieved.Revision, Is.EqualTo(entity.Revision), "Revision should be the same."); } Assert.That(retrieved.NullableValue, Is.EqualTo(entity.NullableValue), "NullableValue should be the same."); if (typeKind != DateTimeKind.Unspecified) { Assert.That(retrieved.Value.Kind, Is.EqualTo(typeKind), "Value kind not correctly retrieved."); if (RevisionCheck) { Assert.That(retrieved.Revision.Kind, Is.EqualTo(typeKind), "Revision kind not correctly retrieved."); } } t.Commit(); } beforeNow = Now.AddTicks(-DateAccuracyInTicks); using (var t = s.BeginTransaction()) { retrieved.NullableValue = GetTestDate(kind); retrieved.Value = GetTestDate(kind).AddMonths(-1); t.Commit(); } afterNow = Now.AddTicks(DateAccuracyInTicks); } if (RevisionCheck) { Assert.That( retrieved.Revision, Is.GreaterThan(beforeNow).And.LessThan(afterNow).And.GreaterThanOrEqualTo(entity.Revision), "Revision not correctly incremented."); if (typeKind != DateTimeKind.Unspecified) { Assert.That(retrieved.Revision.Kind, Is.EqualTo(typeKind), "Revision kind incorrectly changed."); } } // Retrieve and compare again using (var s = OpenSession()) using (var t = s.BeginTransaction()) { var retrievedAgain = s.Get <DateTimeClass>(AdditionalDateId); Assert.That(retrievedAgain, Is.Not.Null, "Entity deleted or cannot be retrieved again by its key."); Assert.That( retrievedAgain.Value, Is.EqualTo(GetExpectedValue(retrieved.Value)), "Unexpected value at second compare."); if (RevisionCheck) { Assert.That(retrievedAgain.Revision, Is.EqualTo(retrieved.Revision), "Revision should be the same again."); } Assert.That( retrievedAgain.NullableValue, Is.EqualTo(GetExpectedValue(retrieved.NullableValue.Value)), "Unexpected NullableValue at second compare."); if (typeKind != DateTimeKind.Unspecified) { Assert.That(retrievedAgain.Value.Kind, Is.EqualTo(typeKind), "Value kind not correctly retrieved again."); if (RevisionCheck) { Assert.That(retrievedAgain.Revision.Kind, Is.EqualTo(typeKind), "Revision kind not correctly retrieved again."); } Assert.That( retrievedAgain.NullableValue.Value.Kind, Is.EqualTo(typeKind), "NullableValue kind not correctly retrieved again."); } t.Commit(); } }
public async Task ReadWriteAsync() { var entity = new DateTimeOffsetClass { Id = AdditionalDateId, Value = GetTestDate() }; // Now must be acquired before transaction because some db freezes current_timestamp at transaction start, // like PostgreSQL. https://www.postgresql.org/docs/7.2/static/functions-datetime.html#AEN6700 // This then wrecks tests with DbTimestampType if the always out of tran Now is called for fetching // beforeNow only after transaction start. // And account db accuracy var beforeNow = Now.AddTicks(-DateAccuracyInTicks); // Save using (var s = OpenSession()) using (var t = s.BeginTransaction()) { await(s.SaveAsync(entity)); await(t.CommitAsync()); } var afterNow = Now.AddTicks(DateAccuracyInTicks); if (RevisionCheck) { Assert.That(entity.Revision, Is.GreaterThan(beforeNow).And.LessThan(afterNow), "Revision not correctly seeded."); Assert.That(entity.NullableValue, Is.Null, "NullableValue unexpectedly seeded."); } // Retrieve, compare then update DateTimeOffsetClass retrieved; using (var s = OpenSession()) { using (var t = s.BeginTransaction()) { retrieved = await(s.GetAsync <DateTimeOffsetClass>(AdditionalDateId)); Assert.That(retrieved, Is.Not.Null, "Entity not saved or cannot be retrieved by its key."); Assert.That(retrieved.Value, Is.EqualTo(entity.Value), "Unexpected value."); if (RevisionCheck) { Assert.That(retrieved.Revision, Is.EqualTo(entity.Revision), "Revision should be the same."); } Assert.That(retrieved.NullableValue, Is.EqualTo(entity.NullableValue), "NullableValue should be the same."); await(t.CommitAsync()); } beforeNow = Now.AddTicks(-DateAccuracyInTicks); using (var t = s.BeginTransaction()) { retrieved.NullableValue = GetTestDate(); retrieved.Value = GetTestDate().AddMonths(-1); await(t.CommitAsync()); } afterNow = Now.AddTicks(DateAccuracyInTicks); } if (RevisionCheck) { Assert.That( retrieved.Revision, Is.GreaterThan(beforeNow).And.LessThan(afterNow).And.GreaterThanOrEqualTo(entity.Revision), "Revision not correctly incremented."); } // Retrieve and compare again using (var s = OpenSession()) using (var t = s.BeginTransaction()) { var retrievedAgain = await(s.GetAsync <DateTimeOffsetClass>(AdditionalDateId)); Assert.That(retrievedAgain, Is.Not.Null, "Entity deleted or cannot be retrieved again by its key."); Assert.That( retrievedAgain.Value, Is.EqualTo(retrieved.Value), "Unexpected value at second compare."); if (RevisionCheck) { Assert.That(retrievedAgain.Revision, Is.EqualTo(retrieved.Revision), "Revision should be the same again."); } Assert.That( retrievedAgain.NullableValue, Is.EqualTo(retrieved.NullableValue.Value), "Unexpected NullableValue at second compare."); await(t.CommitAsync()); } }