public async Task when_a_leader_create_a_inStudy_starpoint_the_total_amount_of_starpoint_should_not_change() { SetUser(MemberDotnet); var memberDotnet = DbContext.Users.First(x => x.Id == MemberDotnet.Id); memberDotnet.NumberOfPoints = 101; DbContext.Entry(memberDotnet).Property(x => x.NumberOfPoints).IsModified = true; DbContext.SaveChanges(); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); var createdStarpoint = DbContext.StarpointsItem.First(x => x.TextJustification == newlyCreatedStarpointDescription); createdStarpoint.NumberOfPoints.IsSameOrEqualTo(101); }
private void Authorize(StarpointsItem starpointsItem, bool isLeader = false) { var isLeaderValue = isLeader; var itemsStartech = starpointsItem.Startech; StartechAuthorizationService.Setup(x => x.IsMemberOrLeaderOf(It.IsIn(itemsStartech), It.IsIn(true))) .Returns(Task.Factory.StartNew(() => isLeader)); }
public void A_starpoint_item_with_text_justification_should_be_validated() { var starpoint = new StarpointsItem { TextJustification = "AAAAA" }; ValidateSuccess(starpoint); }
public void A_starpoint_item_with_url_justification_should_be_validated() { var starpoint = new StarpointsItem { UrlJustification = "http://www.test.org" }; ValidateSuccess(starpoint); }
private static void ValidateSuccess(StarpointsItem starpoint) { var urlValidator = new StarpointItemJustficationValidationAttribute(); urlValidator.Validate(starpoint.UrlJustification, new ValidationContext(starpoint)); urlValidator.ErrorMessage.Should().BeNullOrEmpty(); var textValidator = new StarpointItemJustficationValidationAttribute(); textValidator.Validate(starpoint.TextJustification, new ValidationContext(starpoint)); textValidator.ErrorMessage.Should().BeNullOrEmpty(); }
private static void ValidateFailure(StarpointsItem starpoint) { var urlValidator = new StarpointItemJustficationValidationAttribute(); Action urlActionValidation = () => urlValidator.Validate(starpoint.UrlJustification, new ValidationContext(starpoint)); urlActionValidation.Should().Throw <ValidationException>(); var textValidator = new StarpointItemJustficationValidationAttribute(); Action textActionValidation = () => textValidator.Validate(starpoint.TextJustification, new ValidationContext(starpoint)); textActionValidation.Should().Throw <ValidationException>(); }
public async Task CreateStarpoints_only_startech_leader_should_create_starpoint_for_another_user() { SetUser(MemberDotnet); UnauthorizeMember(); var target = Create(); var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet }; ((Action)(() => target.CreateStarpoints(LeaderDotnet.Id, starpoint).GetAwaiter().GetResult())).Should().Throw <UnauthorizedAccessException>(); }
private StarpointsItem CreateInputStarpoint(StarpointsItem itemToUpdate, Action <StarpointsItem> todo = null) { var starpoint = new StarpointsItem { Id = itemToUpdate.Id, ApplicationUserId = itemToUpdate.ApplicationUserId, Startech = itemToUpdate.Startech, Type = itemToUpdate.Type }; todo?.Invoke(starpoint); return(starpoint); }
public async Task CreateStarpoints_if_the_currentuser_is_not_member_of_the_startech_he_can_not_create_starpoint_for_this_startech() { SetUser(MemberDotnet); var target = Create(); var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Agile }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); (result as ObjectResult).StatusCode.Should().IsSameOrEqualTo((int)HttpStatusCode.MethodNotAllowed); }
public async Task CreateStarpoints_only_member_of_the_startech_should_create_starpoints_as_current_user() { SetUser(MemberDotnet); var target = Create(); var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); result.Should().BeOfType <OkResult>(); }
public async Task a_member_can_not_create_starpoints_from_a_non_active_type() { SetUser(MemberDotnet); var target = Create(); var starpoint = new StarpointsItem { Type = NonActiveType, Startech = Startechs.Dotnet, }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); result.Should().BeOfType <BadRequestObjectResult>(); }
public async Task <IActionResult> CreateStarpoints([FromRoute] int userId, [FromBody] StarpointsItem itemToCreate) { if (!ModelState.IsValid) { return(BadRequest($"your model is not valid :: {ModelState.GetNonValidationErrorMessage()}")); } (var userToDealWith, var startechs, var isLeader) = await GetStartechsToStudyForUser(userId); if (!startechs.Contains(itemToCreate.Startech)) { return(StatusCode((int)HttpStatusCode.MethodNotAllowed, $"you or user don't have enought right on startech {itemToCreate.Startech}")); } itemToCreate.ApplicationUserId = userToDealWith.Id; #pragma warning disable CS8604 // Possible null reference argument. StarpointsType?starpointTypeToCreate = await GetStarpointType(itemToCreate.Type); #pragma warning restore CS8604 // Possible null reference argument. itemToCreate.ValidationState = isLeader ? ValidationState.Validated : ValidationState.InStudy; itemToCreate.Date = DateTime.Now; if (!isLeader) { if (starpointTypeToCreate is null) { return(BadRequest("only startech leader can create starpoints from non typed startech")); } itemToCreate.Type = starpointTypeToCreate; itemToCreate.NumberOfPoints = starpointTypeToCreate.NumberOfPoint; } else { if (itemToCreate.Type != null && starpointTypeToCreate is null) { return(BadRequest($"the type {itemToCreate.Type.TypeName} don't exist")); } itemToCreate.Type = starpointTypeToCreate; userToDealWith.NumberOfPoints += itemToCreate.NumberOfPoints; } dbContext.Add(itemToCreate); await dbContext.SaveChangesAsync(); return(Ok()); }
public async Task CreateStarpoints_even_startech_leader_can_not_create_starpoints_for_a_user_which_is_not_member_of_the_startech() { SetUser(LeaderDotnet); AuthorizeLeader(); var target = Create(); var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet }; var result = await target.CreateStarpoints(User.Id, starpoint); (result as ObjectResult).StatusCode.Should().IsSameOrEqualTo((int)HttpStatusCode.MethodNotAllowed); }
private StarpointsItem CreateItemToCancel(ValidationState state = ValidationState.InStudy, Startechs startech = Startechs.Dotnet, int userId = 5) { var starpoint = new StarpointsItem { Id = WorkingStarpointItemId, ApplicationUserId = userId, Startech = startech, NumberOfPoints = 15, ValidationState = state, Date = DateTime.Now }; DbContext.Add(starpoint); DbContext.SaveChanges(); return(starpoint); }
public async Task when_a_startech_member_create_a_starpointsItem_with_a_starpoints_type_the_numberOfPoints_should_be_the_types_number_of_points() { SetUser(MemberDotnet); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { Type = null, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); result.Should().BeOfType <BadRequestObjectResult>(); }
private StarpointsItem CreateItemToUpdateStatus(ValidationState state = ValidationState.InStudy, Startechs startech = Startechs.Dotnet) { var starpoint = new StarpointsItem { Id = WorkingStarpointItemId, ApplicationUserId = MemberDotnet.Id, Startech = startech, NumberOfPoints = NumberOfPointOfWorkingStapointItem, ValidationState = state, Date = DateTime.Now }; DbContext.Add(starpoint); DbContext.SaveChanges(); return(starpoint); }
public async Task CreateStarpoints_save_the_created_starpoints() { SetUser(MemberDotnet); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); DbContext.StarpointsItem.Any(x => x.TextJustification == newlyCreatedStarpointDescription).Should().BeTrue(); }
public async Task if_the_user_is_current_user_the_starpoint_should_be_created_for_him() { SetUser(MemberDotnet); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); var createdStarpoint = DbContext.StarpointsItem.First(x => x.TextJustification == newlyCreatedStarpointDescription); createdStarpoint.ApplicationUserId.IsSameOrEqualTo(MemberDotnet.Id); }
public async Task when_a_member_create_a_starpoint_the_starpoint_state_is_in_study() { SetUser(MemberDotnet); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); var createdStarpoint = DbContext.StarpointsItem.First(x => x.TextJustification == newlyCreatedStarpointDescription); createdStarpoint.ValidationState.IsSameOrEqualTo(ValidationState.InStudy); }
public async Task created_starpoint_date_should_be_now_date() { SetUser(MemberDotnet); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { Type = BlogArticle, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(ThisUser.Id, starpoint); var createdStarpoint = DbContext.StarpointsItem.First(x => x.TextJustification == newlyCreatedStarpointDescription); createdStarpoint.Date.Should().BeCloseTo(DateTime.Now, precision: 20000); }
public async Task CreateStarpoints_only_startech_leader_can_create_starpoint_with_no_starpoints_type() { SetUser(LeaderDotnet); AuthorizeLeader(); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { Type = null, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(MemberDotnet.Id, starpoint); var createdStarpoint = DbContext.StarpointsItem.First(x => x.TextJustification == newlyCreatedStarpointDescription); createdStarpoint.StarpointsTypeId.Should().BeNull(); createdStarpoint.Type.Should().BeNull(); }
public async Task a_startech_leader_can_create_a_starpoint_item_with_a_type_and_a_different_starpoint_number_of_this_type() { SetUser(LeaderDotnet); AuthorizeLeader(); var target = Create(); const string newlyCreatedStarpointDescription = "newly created starpoints"; var starpoint = new StarpointsItem { NumberOfPoints = 999, Type = BlogArticle, Startech = Startechs.Dotnet, TextJustification = newlyCreatedStarpointDescription }; var result = await target.CreateStarpoints(MemberDotnet.Id, starpoint); var createdStarpoint = DbContext.StarpointsItem.First(x => x.TextJustification == newlyCreatedStarpointDescription); createdStarpoint.NumberOfPoints.Should().IsSameOrEqualTo(999); }
public async Task <IActionResult> UpdateStarpoints([FromBody] StarpointsItem starpointToUpdate) { if (!ModelState.IsValid) { return(BadRequest($"starpoint item is invalid: {ModelState.GetNonValidationErrorMessage()}")); } var inDatatabaseStarpointToUpdate = await GetThisItem(starpointToUpdate.Id); if (inDatatabaseStarpointToUpdate is null) { return(BadRequest($"unknow starpoint item {starpointToUpdate.Id}")); } var thisUser = await GetThisUser(returnOnlyStartechWhereUserIsLeader : true); bool isCurrentUser = thisUser.Id != inDatatabaseStarpointToUpdate.ApplicationUserId; bool isStartechLeader = IsStartechLeader(inDatatabaseStarpointToUpdate.Startech); if (isCurrentUser && !isStartechLeader) { return(BadRequest("you don't have the right to do this")); } else if (!isStartechLeader && inDatatabaseStarpointToUpdate.ValidationState != ValidationState.InStudy) { return(BadRequest("current user can only modify InStudy state starpoints item")); } if (isStartechLeader) { if (inDatatabaseStarpointToUpdate.NumberOfPoints != starpointToUpdate.NumberOfPoints && inDatatabaseStarpointToUpdate.ValidationState == ValidationState.Validated) { var userOfUpdatedStarpointsItem = await GetUser(inDatatabaseStarpointToUpdate.ApplicationUserId); userOfUpdatedStarpointsItem.NumberOfPoints += starpointToUpdate.NumberOfPoints - inDatatabaseStarpointToUpdate.NumberOfPoints; inDatatabaseStarpointToUpdate.NumberOfPoints = starpointToUpdate.NumberOfPoints; } var starpointType = await GetStarpointType(starpointToUpdate.Type); if (starpointType?.Id != inDatatabaseStarpointToUpdate.StarpointsTypeId) { inDatatabaseStarpointToUpdate.Type = starpointType; } } // update only what is necessary if (inDatatabaseStarpointToUpdate.TextJustification != starpointToUpdate.TextJustification) { inDatatabaseStarpointToUpdate.TextJustification = starpointToUpdate.TextJustification; } if (inDatatabaseStarpointToUpdate.UrlJustification != starpointToUpdate.UrlJustification) { inDatatabaseStarpointToUpdate.UrlJustification = starpointToUpdate.UrlJustification; } await dbContext.SaveChangesAsync(); return(Ok()); }
public void A_starpoint_item_no_url_justification__nor_text_justification_should_not_be_validated() { var starpoint = new StarpointsItem(); ValidateFailure(starpoint); }