Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Refactoring/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class Authenticator

public Authenticator(List<User> users)
{
if(users == null)
{
throw new NullUserListException();
}
this.users = users;
}

Expand All @@ -24,7 +28,7 @@ private User FindUserByCredentials(string username, string password)
{
if (IsValidUsername(username))
{
return users.FirstOrDefault(user => user.Name.Equals(username) && (password == null || user.Password.Equals(password)));
return users.FirstOrDefault(user => user.Name.Equals(username) && user.Password.Equals(password));
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions Refactoring/NullUserListException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Refactoring
{
[Serializable]
public class NullUserListException : Exception
{
}
}
2 changes: 1 addition & 1 deletion Refactoring/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Purchase(string productId, int quantity)
throw new OutOfStockException();
}

product.Quantity = product.Quantity - quantity+1;
product.Quantity = product.Quantity - quantity;
user.Balance = user.Balance - product.Price * quantity;

dataManager.SaveUser(user);
Expand Down
1 change: 1 addition & 0 deletions Refactoring/Tusc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Authenticator.cs" />
<Compile Include="NullUserListException.cs" />
<Compile Include="DataManager.cs" />
<Compile Include="EmptyUsernameException.cs" />
<Compile Include="InsufficientFundsException.cs" />
Expand Down
105 changes: 105 additions & 0 deletions UnitTestProject/AuthenticatorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.IO;
using System.Collections.Generic;
using Refactoring;
using NUnit.Framework;
using Newtonsoft.Json;

namespace UnitTestProject
{
[TestFixture]
class AuthenticatorTests
{
private Authenticator authenticator;
private List<User> users;
[SetUp]
public void Test_Initialize ()
{
users = new List<User>();
authenticator = new Authenticator( users );
}

[TearDown]
public void Test_CleanUp ()
{
users = null;
authenticator =null;
}

[Test]
[ExpectedException( typeof( NullUserListException ) )]
public void Test_UserListCannotbeNull ()
{
new Authenticator( null );
Assert.Fail();
}

[Test]
public void Test_UserCannotLoginIfNotExist ()
{
string password = "NOTEXIST";
string name = "NOTEXIST";
Assert.IsNull(authenticator.Authenticate( name, password ));
}

[Test]
public void Test_UserCanLogin ()
{
string password = "TEST";
string name = "TESTNAME";
User user = new User();
user.Password = password;
user.Name = name;
users.Add( user );
User authenticated = authenticator.Authenticate( name, password );

Assert.IsNotNull( authenticated );
Assert.IsTrue( name == authenticated.Name );
Assert.IsTrue( password == authenticated.Password );
}

[Test]
public void Test_UserCanNotLoginIfPasswordIsNotCorrect ()
{
string password = "TEST";
string name = "TESTNAME";
User user = new User();
user.Password = password;
user.Name = name;
users.Add( user );
User authenticated = authenticator.Authenticate( name, "wrongPass" );

Assert.Null( authenticated );
}

[Test]
public void Test_UserCanNotLoginIfPasswordIsNull ()
{
string password = "TEST";
string name = "TESTNAME";
User user = new User();
user.Password = password;
user.Name = name;
users.Add( user );
User authenticated = authenticator.Authenticate( name, null );

Assert.IsNull( authenticated );
}

[Test]
public void Test_IncorrectUserNameCanNotLogin ()
{
string password = "TEST";
string name = "TESTNAME";
User user = new User();
user.Password = password;
user.Name = name;
users.Add( user );
User authenticated = authenticator.Authenticate( "NotAName", password );

Assert.IsNull( authenticated );
}


}
}

78 changes: 57 additions & 21 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,26 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
private Store arrangeSingleProductSingleUserTest(
string productId, string testUserName="Test User", int fund=100,
string productName="Product", int price=1, int availableToPurchase=10,
List<Product> productList = null )
{
//Arrange
const string TEST_PRODUCT_ID = "1";

var users = new List<User>();
users.Add(createTestUser("Test User", "", 99.99));

var products = new List<Product>();
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));

users.Add( createTestUser( testUserName, "", fund ) );
var products = (productList==null)? new List<Product>():productList;
products.Add( createTestProduct( productId, "Product", price, availableToPurchase ) );
var dataManager = new DataManager(users, products);
var store = new Store(users[0], dataManager);
return store;
}

[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var store = arrangeSingleProductSingleUserTest(TEST_PRODUCT_ID);
//Act
store.Purchase(TEST_PRODUCT_ID, 10);

Expand All @@ -60,37 +65,68 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
var products = new List<Product>();
var store = arrangeSingleProductSingleUserTest(
TEST_PRODUCT_ID,
availableToPurchase:10,
productList:products);

//Act
store.Purchase( TEST_PRODUCT_ID, 9 );

//Assert
//(choose the appropriate statement(s))
//Assert.AreEqual(1, products[0].Quantity);
//Assert.AreSame(1, products[0].Quantity);
//Assert.IsTrue(products[0].Quantity == 1);
Assert.IsTrue(products[0].Quantity == 1);
}

[Test]
[ExpectedException( typeof( InsufficientFundsException ) )]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
//act
var store = arrangeSingleProductSingleUserTest(
TEST_PRODUCT_ID,
fund:1,
price:10);
store.Purchase( TEST_PRODUCT_ID, 1 );
}

//Act
[Test]
[ExpectedException( typeof( InsufficientFundsException ) )]
public void Test_PurchaseMultipleItemThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
const int QUANTITY_TO_PURCHASE = 11;
const int price = 1;

//Assert
var store = arrangeSingleProductSingleUserTest(
TEST_PRODUCT_ID,
price:price,
fund:(QUANTITY_TO_PURCHASE-1)*price,
availableToPurchase:QUANTITY_TO_PURCHASE );

//Act
store.Purchase( TEST_PRODUCT_ID, QUANTITY_TO_PURCHASE );
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
[ExpectedException( typeof( OutOfStockException ) )]
public void Test_PurchaseThrowsExceptionWhenNotEnoughQuantity ()
{
//Arrange
const string TEST_PRODUCT_ID = "1";
const int QUANTITY_TO_PURCHASE = 11;

//Act
var store = arrangeSingleProductSingleUserTest(
TEST_PRODUCT_ID,
availableToPurchase:QUANTITY_TO_PURCHASE-1);

//Assert
//Act
store.Purchase( TEST_PRODUCT_ID, QUANTITY_TO_PURCHASE );
}


// THE BELOW CODE IS REQUIRED TO PREVENT THE TESTS FROM MODIFYING THE USERS/PRODUCTS ON FILE
// This is not a good unit testing pattern - the unit test dependency on the file system should
// actually be broken ... training on how to do this will be coming.
Expand Down
4 changes: 4 additions & 0 deletions UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -73,6 +76,7 @@
<Compile Include="StoreTests.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AuthenticatorTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Refactoring\Tusc.csproj">
Expand Down