Skip to content

Commit

Permalink
Add tests for helper classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Eden committed Nov 28, 2016
1 parent 8e96533 commit 88af2cb
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Quondam.Tests/PasswordRecordTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;
using System;

namespace MartinEden.Quondam.Tests
{
public class PasswordRecordTests
{
[TestCase(0, ExpectedResult = true)]
[TestCase(15, ExpectedResult = true)]
[TestCase(30, ExpectedResult = true)]
[TestCase(30.001, ExpectedResult = false)]
[TestCase(60, ExpectedResult = false)]
public bool RecordsOnlyRemainFreshForThirtySeconds(double elapsed)
{
var noon = DateTime.Today.AddHours(12);
var record = new PasswordRecord("abc", noon);
return record.IsFresh(noon + TimeSpan.FromSeconds(elapsed));
}
}
}
2 changes: 2 additions & 0 deletions Quondam.Tests/Quondam.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<Compile Include="ClockTests.cs" />
<Compile Include="FakeClock.cs" />
<Compile Include="PasswordManagerTests.cs" />
<Compile Include="PasswordRecordTests.cs" />
<Compile Include="UserStoreTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
80 changes: 80 additions & 0 deletions Quondam.Tests/UserStoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using NUnit.Framework;
using System;

namespace MartinEden.Quondam.Tests
{
public class UserStoreTests
{
private UserStore store;
private const string user = "user";

[SetUp]
public void CreateStore()
{
store = new UserStore();
}

[Test]
public void GetPasswordReturnsNullForUnknownUser()
{
Assert.IsNull(store.GetPassword(user));
}

[Test]
public void GetPasswordReturnsStoredPassword()
{
string password = "swordfish";
var time = DateTime.Today;
store.StorePassword(user, password, time);
var record = store.GetPassword(user);
checkRecord(password, time, record);
}

[Test]
public void AfterClearPasswordGetPasswordReturnsNull()
{
string password = "swordfish";
var time = DateTime.Today;
store.StorePassword(user, password, time);
store.ClearPassword(user);
Assert.IsNull(store.GetPassword(user));
}

[Test]
public void CanStorePasswordsForMultipleUsers()
{
string password1 = "cake";
string password2 = "cherry";
string user1 = "alice";
string user2 = "bob";
var time1 = DateTime.Now;
var time2 = time1.AddSeconds(1);

store.StorePassword(user1, password1, time1);
store.StorePassword(user2, password2, time2);
checkRecord(password1, time1, store.GetPassword(user1));
checkRecord(password2, time2, store.GetPassword(user2));
}

[Test]
public void StoringNewPasswordOverridesPrevious()
{
string password1 = "hunter13";
string password2 = "fsdfeergf4";
var time1 = DateTime.Now;
var time2 = time1.AddSeconds(1);

store.StorePassword(user, password1, time1);
store.StorePassword(user, password2, time2);
var record = store.GetPassword(user);
checkRecord(password2, time2, record);
}

private static void checkRecord(string expectedPassword, DateTime expectedTimestamp, PasswordRecord record)
{
Assert.IsNotNull(record);
Assert.AreEqual(expectedPassword, record.Password);
Assert.AreEqual(expectedTimestamp, record.Timestamp);
}
}
}

0 comments on commit 88af2cb

Please sign in to comment.