public async void If_account_flag_is_not_in_DB_should_add_it_to_the_DB() { // Set up the request var request = new Messages.AccountFlagSetRequest { Flags = new Dictionary <string, bool> { { "flag1", true } } }; // Act await _controller.SetFlags(request); // Assert - the DB should now contain the flag var addedFlags = Db.Mock.AccountFlags.Added; addedFlags.Should().HaveCount(1); var flag1 = addedFlags.First(); flag1.Name.Should().Be("flag1"); flag1.Value.Should().BeTrue(); Db.WasSaveChangesCalled.Should().BeTrue(); }
public async void On_success_should_respond_with_NoContent() { // Set up var request = new Messages.AccountFlagSetRequest { Flags = new Dictionary <string, bool> { { "flag1", true } } }; // Act var result = await _controller.SetFlags(request); // Assert result.Should().BeOfType <NoContentResult>(); }
public async void Should_correctly_set_additional_flag_columns() { // Set up var request = new Messages.AccountFlagSetRequest { Flags = new Dictionary <string, bool> { { "flag1", true } } }; // Act await _controller.SetFlags(request); // Assert - additional columns should be set up correctly var addedFlags = Db.Mock.AccountFlags.Added; var flag1 = addedFlags.First(); flag1.Id.Should().NotBe(default);
public async Task <IActionResult> SetFlags([FromBody] Messages.AccountFlagSetRequest msg) { // Validate that the user is logged in. var account = await GetCurrentAccountAsync(); if (account == null) { return(Unauthorized()); } // Validate the request if (msg.Flags == null || !msg.Flags.Any()) { return(BadRequest("Must provide at least one account flag to set")); } foreach (var flag in msg.Flags) { var name = flag.Key; if (string.IsNullOrWhiteSpace(name)) { return(BadRequest("Account flag name cannot be empty or whitespace. Was: '" + name + "'.")); } if (name.Length > AccountFlag.MaxNameLength) { return(BadRequest("Account flag name cannot be longer than " + AccountFlag.MaxNameLength + " characters. Was: " + name.Length + " characters, '" + name + "'.")); } } // Edit/change the flags as needed. var accountId = account.Id; var flags = await Db.AccountFlags.Where(f => f.AccountId == accountId).ToListAsync(); var flagsByName = flags.ToDictionary(flag => flag.Name); foreach (var flagToSet in msg.Flags) { var found = flagsByName.TryGetValue(flagToSet.Key, out var flagToChange); if (found) { // Update existing flag. flagToChange.Value = flagToSet.Value; flagToChange.LastChangedTimeUtc = _timeService.UtcNow; Db.AccountFlags.Update(flagToChange); } else { // Add a new flag. var newFlag = new AccountFlag { Id = Guid.NewGuid(), AccountId = accountId, Name = flagToSet.Key, Value = flagToSet.Value, LastChangedTimeUtc = _timeService.UtcNow }; Db.AccountFlags.Add(newFlag); } } await Db.SaveChangesAsync(); return(NoContent()); }