-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Purchase Type is currently used to distinguish between Free Purchases and MobilePay. However could be extended in the future. PaymentType is nullable as we old purchases has no assumed payment type.
- Loading branch information
1 parent
7165991
commit cbcffde
Showing
8 changed files
with
816 additions
and
8 deletions.
There are no files selected for viewing
599 changes: 599 additions & 0 deletions
599
coffeecard/CoffeeCard.Library/Migrations/20221228163445_Payment Type.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
coffeecard/CoffeeCard.Library/Migrations/20221228163445_Payment Type.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace CoffeeCard.Library.Migrations | ||
{ | ||
public partial class PaymentType : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.AddColumn<string>( | ||
name: "PaymentType", | ||
schema: "dbo", | ||
table: "Purchases", | ||
type: "nvarchar(max)", | ||
nullable: true); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropColumn( | ||
name: "PaymentType", | ||
schema: "dbo", | ||
table: "Purchases"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,14 +247,153 @@ public async Task InitiatePurchaseAddsTicketsToUserWhenFree(Product product) | |
}; | ||
|
||
// Act | ||
var purchaseResponse = await purchaseService.InitiatePurchase(request, user); | ||
await purchaseService.InitiatePurchase(request, user); | ||
|
||
// Assert | ||
var userUpdated = await context.Users.FindAsync(user.Id); | ||
|
||
Assert.Equal(1, userUpdated.Purchases.Count); | ||
Assert.Equal(product.NumberOfTickets, userUpdated.Tickets.Count); | ||
} | ||
|
||
[Fact(DisplayName = "GetPurchase calls MobilePay Api when Payment Type is MobilePay")] | ||
public async Task GetPurchaseCallsMobilePayApiWhenPaymentTypeIsMobilePay() | ||
{ | ||
// Arrange | ||
var builder = new DbContextOptionsBuilder<CoffeeCardContext>() | ||
.UseInMemoryDatabase(nameof(GetPurchaseCallsMobilePayApiWhenPaymentTypeIsMobilePay)); | ||
|
||
var databaseSettings = new DatabaseSettings | ||
{ | ||
SchemaName = "test" | ||
}; | ||
var environmentSettings = new EnvironmentSettings() | ||
{ | ||
EnvironmentType = EnvironmentType.Test | ||
}; | ||
|
||
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings); | ||
|
||
var user = new User | ||
{ | ||
Id = 1, | ||
Name = "User1", | ||
Email = "[email protected]", | ||
Password = "password", | ||
Salt = "salt", | ||
DateCreated = new DateTime(year: 2020, month: 11, day: 11), | ||
IsVerified = true, | ||
PrivacyActivated = false, | ||
UserGroup = UserGroup.Customer, | ||
UserState = UserState.Active | ||
}; | ||
await context.AddAsync(user); | ||
|
||
var purchase = new Purchase() | ||
{ | ||
Id = 1, | ||
ProductName = "Coffee clip card", | ||
ProductId = 1, | ||
Price = 100, | ||
NumberOfTickets = 10, | ||
DateCreated = DateTime.UtcNow, | ||
Completed = true, | ||
OrderId = Guid.NewGuid().ToString(), | ||
TransactionId = Guid.NewGuid().ToString(), | ||
Status = PurchaseStatus.Completed, | ||
PaymentType = PaymentType.MobilePay, | ||
PurchasedBy = user | ||
}; | ||
await context.AddAsync(purchase); | ||
await context.SaveChangesAsync(); | ||
|
||
var mobilePayService = new Mock<IMobilePayPaymentsService>(); | ||
mobilePayService.Setup(mp => mp.GetPayment(Guid.Parse(purchase.TransactionId))).ReturnsAsync( | ||
new MobilePayPaymentDetails(purchase.OrderId, "redirect", purchase.TransactionId, "complete")); | ||
|
||
var mailService = new Mock<Library.Services.IEmailService>(); | ||
var productService = new ProductService(context); | ||
var ticketService = new TicketService(context, new Mock<IStatisticService>().Object); | ||
|
||
var purchaseService = new PurchaseService(context, mobilePayService.Object, ticketService, | ||
mailService.Object, productService); | ||
|
||
// Act | ||
var result = await purchaseService.GetPurchase(purchase.Id, user); | ||
|
||
// Assert | ||
mobilePayService.Verify(mp => mp.GetPayment(Guid.Parse(purchase.TransactionId)), Times.Once); | ||
|
||
Assert.Equal(purchase.PaymentType, result.PaymentDetails.PaymentType); | ||
} | ||
|
||
[Fact(DisplayName = "GetPurchase doesnt calls MobilePay Api when Payment Type is FreePurchase")] | ||
public async Task GetPurchaseDoesntCallsMobilePayApiWhenPaymentTypeIsFreePurchase() | ||
{ | ||
// Arrange | ||
var builder = new DbContextOptionsBuilder<CoffeeCardContext>() | ||
.UseInMemoryDatabase(nameof(GetPurchaseDoesntCallsMobilePayApiWhenPaymentTypeIsFreePurchase)); | ||
|
||
var databaseSettings = new DatabaseSettings | ||
{ | ||
SchemaName = "test" | ||
}; | ||
var environmentSettings = new EnvironmentSettings() | ||
{ | ||
EnvironmentType = EnvironmentType.Test | ||
}; | ||
|
||
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings); | ||
|
||
var user = new User | ||
{ | ||
Id = 1, | ||
Name = "User1", | ||
Email = "[email protected]", | ||
Password = "password", | ||
Salt = "salt", | ||
DateCreated = new DateTime(year: 2020, month: 11, day: 11), | ||
IsVerified = true, | ||
PrivacyActivated = false, | ||
UserGroup = UserGroup.Customer, | ||
UserState = UserState.Active | ||
}; | ||
await context.AddAsync(user); | ||
|
||
var purchase = new Purchase() | ||
{ | ||
Id = 1, | ||
ProductName = "Coffee clip card", | ||
ProductId = 1, | ||
Price = 100, | ||
NumberOfTickets = 10, | ||
DateCreated = DateTime.UtcNow, | ||
Completed = true, | ||
OrderId = Guid.NewGuid().ToString(), | ||
TransactionId = Guid.NewGuid().ToString(), | ||
Status = PurchaseStatus.Completed, | ||
PaymentType = PaymentType.FreePurchase, | ||
PurchasedBy = user | ||
}; | ||
await context.AddAsync(purchase); | ||
await context.SaveChangesAsync(); | ||
|
||
var mobilePayService = new Mock<IMobilePayPaymentsService>(); | ||
var mailService = new Mock<Library.Services.IEmailService>(); | ||
var productService = new ProductService(context); | ||
var ticketService = new TicketService(context, new Mock<IStatisticService>().Object); | ||
|
||
var purchaseService = new PurchaseService(context, mobilePayService.Object, ticketService, | ||
mailService.Object, productService); | ||
|
||
// Act | ||
var result = await purchaseService.GetPurchase(purchase.Id, user); | ||
|
||
// Assert | ||
mobilePayService.Verify(mp => mp.GetPayment(Guid.Parse(purchase.TransactionId)), Times.Never); | ||
|
||
Assert.Equal(purchase.PaymentType, result.PaymentDetails.PaymentType); | ||
} | ||
|
||
public static IEnumerable<object[]> ProductGenerator() | ||
{ | ||
|