/// <summary> /// Should return code 200 if called with ids of two existing tags that have a relation /// </summary> // TODO [Test] public void PutTagRelationTest() { var expected = new AnnotationTagRelationFormModel() { FirstTagId = _relation12.FirstTag.Id, SecondTagId = _relation12.SecondTag.Id, Name = "changedName" }; var expectedColor = "oldColor"; _relation12.ArrowStyle = expectedColor; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "1")) .WithDbContext(dbContext => dbContext .WithSet <User>(db => db.Add(_admin)) .WithSet <AnnotationTag>(db => db.AddRange(_tag1, _tag2)) .WithSet <AnnotationTagRelation>(db => db.Add(_relation12)) ) .Calling(c => c.PutTagRelation(_relation12.FirstTagId, _relation12.SecondTagId, expected)) .ShouldHave() .DbContext(db => db.WithSet <AnnotationTagRelation>(relations => relations.Any(actual => actual.FirstTagId == expected.FirstTagId && actual.SecondTagId == expected.SecondTagId && actual.Name == expected.Name && actual.Color == expectedColor // color should not change as it was not set in the model ) )) .AndAlso() .ShouldReturn() .Ok(); }
public void PostTagRelationTest403() { var expected = new AnnotationTagRelationFormModel() { FirstTagId = _tag1.Id, SecondTagId = _tag2.Id, Name = "relation-with-nonexisting-tags" }; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "2")) .WithDbContext(dbContext => dbContext.WithSet <User>(db => db.Add(_student))) .Calling(c => c.PostTagRelation(expected)) .ShouldHave() .DbContext(db => db.WithSet <AnnotationTagRelation>(relations => !relations.Any(actual => // negated --> NO relation like this exists actual.FirstTagId == expected.FirstTagId && actual.SecondTagId == expected.SecondTagId && actual.Name == expected.Name ) )) .AndAlso() .ShouldReturn() .Forbid(); }
/// <summary> /// Should return 400 for duplicate tag relations /// </summary> // TODO [Test] public void PostTagRelationTest_NoDuplicateRelations() { var expected = new AnnotationTagRelationFormModel() { FirstTagId = _relation12.FirstTag.Id, SecondTagId = _relation12.SecondTag.Id, Name = "duplcate-relation" }; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "1")) .WithDbContext(dbContext => dbContext .WithSet <User>(db => db.Add(_admin)) .WithSet <AnnotationTag>(db => db.AddRange(_tag1, _tag2)) .WithSet <AnnotationTagRelation>(db => db.Add(_relation12)) ) .Calling(c => c.PostTagRelation(expected)) .ShouldHave() .DbContext(db => db.WithSet <AnnotationTagRelation>(relations => relations.Count(actual => actual.FirstTagId == expected.FirstTagId && actual.SecondTagId == expected.SecondTagId && actual.Name == expected.Name ) == 1 // should only contain 1 entry, not two )) .AndAlso() .ShouldReturn() .BadRequest(); }
/// <summary> /// Should return code 200 if called with ids of two existing tags that do not have a relation yet /// </summary> // TODO [Test] public void PostTagRelationTest() { var expected = new AnnotationTagRelationFormModel() { FirstTagId = _tag1.Id, SecondTagId = _tag2.Id, Name = "relationName", Color = "schwarzgelb", ArrowStyle = "dotted" }; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "1")) .WithDbContext(dbContext => dbContext .WithSet <User>(db => db.Add(_admin)) .WithSet <AnnotationTag>(db => db.AddRange(_tag1, _tag2)) ) .Calling(c => c.PostTagRelation(expected)) .ShouldHave() .DbContext(db => db.WithSet <AnnotationTagRelation>(relations => relations.Any(actual => actual.FirstTagId == expected.FirstTagId && actual.SecondTagId == expected.SecondTagId && actual.Name == expected.Name && actual.Color == expected.Color && actual.ArrowStyle == expected.ArrowStyle ) )) .AndAlso() .ShouldReturn() .Ok(); }
public AnnotationTagRelation(AnnotationTagRelationFormModel model) { FirstTagId = model.FirstTagId; SecondTagId = model.SecondTagId; Name = model.Name; ArrowStyle = model.ArrowStyle; Color = model.Color; }
/// <summary> /// Should return 400 for relations that are not allowed (child tag to top-level tag) /// </summary> // TODO [Test] public void PostTagRelationTest_NoChildToFirstLevelRelation() { var expected = new AnnotationTagRelationFormModel() { FirstTagId = _tag3.Id, SecondTagId = _tag2.Id, Name = "child-to-toplevel-relation" }; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "1")) .WithDbContext(dbContext => dbContext .WithSet <User>(db => db.Add(_admin)) .WithSet <AnnotationTag>(db => db.AddRange(_tag1, _tag2, _tag3)) ) .Calling(c => c.PostTagRelation(expected)) .ShouldHave() .DbContext(db => db.WithSet <AnnotationTagRelation>(relations => !relations.Any(actual => // negated --> NO relation like this exists actual.FirstTagId == expected.FirstTagId && actual.SecondTagId == expected.SecondTagId && actual.Name == expected.Name ) )) .AndAlso() .ShouldReturn() .BadRequest(); // other way around is also not allowed: expected = new AnnotationTagRelationFormModel() { FirstTagId = _tag2.Id, SecondTagId = _tag3.Id, Name = "toplevel-to-child-relation" }; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "1")) .WithDbContext(dbContext => dbContext .WithSet <User>(db => db.Add(_admin)) .WithSet <AnnotationTag>(db => db.AddRange(_tag1, _tag2, _tag3)) ) .Calling(c => c.PostTagRelation(expected)) .ShouldHave() .DbContext(db => db.WithSet <AnnotationTagRelation>(relations => !relations.Any(actual => // negated --> NO relation like this exists actual.FirstTagId == expected.FirstTagId && actual.SecondTagId == expected.SecondTagId && actual.Name == expected.Name ) )) .AndAlso() .ShouldReturn() .BadRequest(); }
/// <summary> /// Should return 403 for users with the student role /// </summary> // TODO [Test] public void PutTagRelationTest403() { var model = new AnnotationTagRelationFormModel() { FirstTagId = _relation12.FirstTag.Id, SecondTagId = _relation12.SecondTag.Id, Name = "changedName" }; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "2")) .WithDbContext(dbContext => dbContext.WithSet <User>(db => db.Add(_student))) .Calling(c => c.PutTagRelation(_relation12.FirstTagId, _relation12.SecondTagId, model)) .ShouldReturn() .Forbid(); }
public IActionResult PostTagRelation([FromBody] AnnotationTagRelationFormModel model) { if (!_annotationPermissions.IsAllowedToEditTags(User.Identity.GetUserId())) { return(Forbid()); } try { if (_tagManager.AddTagRelation(model)) { return(Ok()); } return(BadRequest()); } catch (InvalidOperationException) { return(BadRequest()); } }
public void DeleteTagRelationTest400() { var model = new AnnotationTagRelationFormModel() { FirstTagId = _relation12.FirstTag.Id, SecondTagId = _relation12.SecondTag.Id }; MyMvc .Controller <AnnotationController>() .WithAuthenticatedUser(user => user.WithClaim("Id", "1")) .WithDbContext(dbContext => dbContext .WithSet <User>(db => db.Add(_admin)) .WithSet <AnnotationTag>(db => db.AddRange(_tag1, _tag2)) ) // --> no TagRelation objects were added to the database .Calling(c => c.DeleteTagRelation(_relation12.FirstTag.Id, _relation12.SecondTag.Id)) .ShouldReturn() .BadRequest(); }
internal bool AddTagRelation(AnnotationTagRelationFormModel model) { var tag1 = DbContext.AnnotationTags.Single(tag => tag.Id == model.FirstTagId); var tag2 = DbContext.AnnotationTags.Single(tag => tag.Id == model.SecondTagId); if (TagRelationExists(tag1, tag2)) { return(false); } else if (tag1 != null && tag2 != null) { var forwardRelation = new AnnotationTagRelation(tag1, tag2, model.Name, model.ArrowStyle, model.Color); DbContext.AnnotationTagRelations.Add(forwardRelation); DbContext.SaveChanges(); return(true); } else { return(false); } }
public IActionResult PutTagRelation([FromQueryAttribute] int sourceId, [FromQueryAttribute] int targetId, [FromBody] AnnotationTagRelationFormModel model) { return(ServiceUnavailable()); }