public async Task UpdateBrother_ChangesArePersisted() { ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(JwtClaimTypes.Scope, Constants.Scopes.Administrator), new Claim(JwtClaimTypes.Subject, "some-subject"), new Claim(JwtClaimTypes.Scope, "directory") })); int id = _dbContext.Brother.Add(new Brother()).Entity.Id; await _dbContext.SaveChangesAsync(); BrotherController controller = new BrotherController(_dbContext, principal, Mock.Of <ILogger <BrotherController> >()); Brother brother = new Brother { Id = id, FirstName = "firstname1", LastName = "lastname1" }; OkResult result = await controller.UpdateBrother(id, brother) as OkResult; Assert.Multiple((() => { Assert.That(result, Is.Not.Null); Brother changed = _dbContext.Brother.FirstOrDefault(b => b.Id == id); Assert.That(changed, Is.Not.Null); Assert.That(changed.FirstName, Is.EqualTo("firstname1")); Assert.That(changed.LastName, Is.EqualTo("lastname1")); })); }
public async Task UpdateBrother_WithNonexistentId_Returns404() { BrotherController controller = new BrotherController(_dbContext, null, null); Assert.That(await controller.UpdateBrother(-1, new Brother { Id = 1 }), Is.TypeOf <NotFoundResult>()); }
public async Task UpdateBrother_SamePrincipalNonAdministrator_IsSuccessful() { int id = _dbContext.Brother.Add(new Brother()).Entity.Id; await _dbContext.SaveChangesAsync(); ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(JwtClaimTypes.Subject, id.ToString()), new Claim(JwtClaimTypes.Scope, "directory") })); BrotherController controller = new BrotherController(_dbContext, principal, Mock.Of <ILogger <BrotherController> >()); IActionResult result = await controller.UpdateBrother(id, new Brother { Id = id }); Assert.Multiple(() => { Assert.That(result, Is.TypeOf <OkResult>()); }); }
public async Task UpdateBrother_DifferentPrincipalAdministrator_IsSuccessful() { int id = _dbContext.Brother.Add(new Brother()).Entity.Id; await _dbContext.SaveChangesAsync(); ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(JwtClaimTypes.Subject, "some-subject"), new Claim(JwtClaimTypes.Scope, Constants.Scopes.Administrator) })); BrotherController controller = new BrotherController(_dbContext, principal, Mock.Of <ILogger <BrotherController> >()); Assert.That(await controller.UpdateBrother(id, new Brother { Id = id }), Is.TypeOf <OkResult>()); }
public async Task UpdateBrother_WithoutSubject_IsUnauthorized() { int id = _dbContext.Brother.Add(new Brother()).Entity.Id; _dbContext.SaveChanges(); ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(JwtClaimTypes.Scope, "some-scope") })); BrotherController controller = new BrotherController(_dbContext, principal, Mock.Of <ILogger <BrotherController> >()); Assert.That(await controller.UpdateBrother(id, new Brother { Id = id }), Is.TypeOf <UnauthorizedResult>()); }
public async Task UpdateBrother_WithMismatchingIds_ReturnsBadRequest() { int id = _dbContext.Brother.Add(new Brother()).Entity.Id; _dbContext.SaveChanges(); ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(JwtClaimTypes.Scope, Constants.Scopes.Administrator), new Claim(JwtClaimTypes.Subject, "some-subject"), new Claim(JwtClaimTypes.Scope, "directory") })); BrotherController controller = new BrotherController(_dbContext, principal, Mock.Of <ILogger <BrotherController> >()); Assert.That(await controller.UpdateBrother(id, new Brother { Id = -1 }), Is.TypeOf <BadRequestResult>()); }
public async Task UpdateBrother_DbConcurrencyExceptionIsThrownOnSave_ReturnsConflictAndRecordIsUnchanged() { ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(JwtClaimTypes.Scope, Constants.Scopes.Administrator), new Claim(JwtClaimTypes.Subject, "some-subject"), new Claim(JwtClaimTypes.Scope, "directory") })); int id = _dbContext.Brother.Add(new Brother { FirstName = "ShouldNot", LastName = "BeModified" }).Entity.Id; await _dbContext.SaveChangesAsync(); Mock <DirectoryContext> mockedContext = new Mock <DirectoryContext>(); mockedContext.SetupGet(m => m.Brother).Returns(_dbContext.Brother); List <IUpdateEntry> entries = new List <IUpdateEntry>(new[] { Mock.Of <IUpdateEntry>() }); mockedContext.Setup(m => m.Entry(It.IsAny <Brother>())).Throws(new DbUpdateConcurrencyException(string.Empty, entries)); BrotherController controller = new BrotherController(mockedContext.Object, principal, Mock.Of <ILogger <BrotherController> >()); Brother brother = new Brother { Id = id, FirstName = "firstname1", LastName = "lastname1" }; ConflictResult result = await controller.UpdateBrother(id, brother) as ConflictResult; Assert.Multiple((() => { Assert.That(result, Is.Not.Null); Brother changed = _dbContext.Brother.FirstOrDefault(b => b.Id == id); Assert.That(changed, Is.Not.Null); Assert.That(changed.FirstName, Is.EqualTo("ShouldNot")); Assert.That(changed.LastName, Is.EqualTo("BeModified")); })); }