-
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.
* add a patch endpoint to update a user group of an account * a fix in xml comments * format code * Exposes setting product user groups to API * fix Jonas comments * authorize only board for the patch endpoint * uncomment the board authorization line * format code * Update documentation examples * Remove obsolete call to get user groups of product * change access to endpoint * Test update product user groups * Removes second call to DB to update user groups * Removes redundant database call * Small fixes to decrease unnecessary code * Remove code smells * Fix last code smell * Add productusergroups to get products (#231) --------- Co-authored-by: Hubert <[email protected]> Co-authored-by: A-Guldborg <[email protected]> Co-authored-by: Jonas Anker Rasmussen <[email protected]> Co-authored-by: A-Guldborg <[email protected]> Co-authored-by: Hubert Wójcik <[email protected]>
- Loading branch information
1 parent
fe59ee6
commit baab556
Showing
11 changed files
with
267 additions
and
23 deletions.
There are no files selected for viewing
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
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
24 changes: 24 additions & 0 deletions
24
coffeecard/CoffeeCard.Models/DataTransferObjects/v2/User/UpdateUserGroupRequest.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,24 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using CoffeeCard.Models.Entities; | ||
|
||
namespace CoffeeCard.Models.DataTransferObjects.v2.User | ||
{ | ||
/// <summary> | ||
/// Update the UserGroup property of a user | ||
/// </summary> | ||
/// <example> | ||
/// { | ||
/// "UserGroup": "Barista" | ||
/// } | ||
/// </example> | ||
public class UpdateUserGroupRequest | ||
{ | ||
/// <summary> | ||
/// The UserGroup of a user | ||
/// </summary> | ||
/// <value> UserGroup object </value> | ||
/// <example> UserGroup.Barista </example> | ||
[Required] | ||
public UserGroup UserGroup { get; set; } | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
coffeecard/CoffeeCard.Tests.Unit/Services/v2/ProductServiceTest.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,128 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using CoffeeCard.Common.Configuration; | ||
using CoffeeCard.Library.Persistence; | ||
using CoffeeCard.Library.Services.v2; | ||
using CoffeeCard.Models.DataTransferObjects.v2.Product; | ||
using CoffeeCard.Models.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xunit; | ||
|
||
namespace CoffeeCard.Tests.Unit.Services.v2 | ||
{ | ||
public class ProductServiceTest | ||
{ | ||
[Fact(DisplayName = "UpdateProduct removes ommitted user groups and only adds selected user groups")] | ||
public async Task UpdateProduct_Removes_Omitted_UserGroups() | ||
{ | ||
var builder = new DbContextOptionsBuilder<CoffeeCardContext>() | ||
.UseInMemoryDatabase(nameof(UpdateProduct_Removes_Omitted_UserGroups)); | ||
|
||
var databaseSettings = new DatabaseSettings | ||
{ | ||
SchemaName = "test" | ||
}; | ||
var environmentSettings = new EnvironmentSettings() | ||
{ | ||
EnvironmentType = EnvironmentType.Test | ||
}; | ||
|
||
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings); | ||
var p = new Product | ||
{ | ||
Id = 1, | ||
Name = "Coffee", | ||
Description = "Coffee Clip card", | ||
NumberOfTickets = 10, | ||
Price = 10, | ||
ExperienceWorth = 10, | ||
Visible = true | ||
}; | ||
await context.AddAsync(p); | ||
await context.SaveChangesAsync(); | ||
|
||
await context.AddAsync(new ProductUserGroup | ||
{ | ||
Product = p, | ||
UserGroup = UserGroup.Barista | ||
}); | ||
|
||
await context.AddAsync(new ProductUserGroup | ||
{ | ||
Product = p, | ||
UserGroup = UserGroup.Manager | ||
}); | ||
|
||
await context.SaveChangesAsync(); | ||
|
||
using var productService = new ProductService(context); | ||
|
||
await productService.UpdateProduct(new UpdateProductRequest() | ||
{ | ||
Id = 1, | ||
Visible = true, | ||
Price = 10, | ||
NumberOfTickets = 10, | ||
Name = "Coffee", | ||
Description = "Coffee Clip card", | ||
AllowedUserGroups = new List<UserGroup>() { UserGroup.Customer, UserGroup.Board } | ||
}); | ||
|
||
var expected = new List<UserGroup> | ||
{ | ||
UserGroup.Customer, UserGroup.Board | ||
}; | ||
|
||
var result = await productService.GetProductAsync(1); | ||
|
||
Assert.Collection<UserGroup>(expected, | ||
e => e.Equals(UserGroup.Customer), | ||
e => e.Equals(UserGroup.Board)); | ||
} | ||
|
||
[Fact(DisplayName = "AddProduct adds only selected user groups")] | ||
public async Task AddProduct_Sets_Correct_UserGroups() | ||
{ | ||
var builder = new DbContextOptionsBuilder<CoffeeCardContext>() | ||
.UseInMemoryDatabase(nameof(AddProduct_Sets_Correct_UserGroups)); | ||
|
||
var databaseSettings = new DatabaseSettings | ||
{ | ||
SchemaName = "test" | ||
}; | ||
var environmentSettings = new EnvironmentSettings() | ||
{ | ||
EnvironmentType = EnvironmentType.Test | ||
}; | ||
|
||
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings); | ||
|
||
using var productService = new ProductService(context); | ||
|
||
var p = new AddProductRequest | ||
{ | ||
Name = "Coffee", | ||
Description = "Coffee Clip card", | ||
NumberOfTickets = 10, | ||
Price = 10, | ||
Visible = true, | ||
AllowedUserGroups = new List<UserGroup> { UserGroup.Manager, UserGroup.Board } | ||
}; | ||
|
||
await productService.AddProduct(p); | ||
|
||
var expected = new List<UserGroup> | ||
{ | ||
UserGroup.Manager, UserGroup.Board | ||
}; | ||
|
||
var result = await productService.GetProductAsync(1); | ||
|
||
Assert.Collection<UserGroup>(expected, | ||
e => e.Equals(UserGroup.Customer), | ||
e => e.Equals(UserGroup.Board)); | ||
} | ||
} | ||
} |
Oops, something went wrong.