public async Task <IActionResult> UpdateNotificationSettingsAsync([Required][FromBody] UpdateNotificationSettingsRequest request) { request.UserId = GetCurrentUserId(); var res = await _service.UpdateNotificationSettingsAsync(request); return(Ok(res)); }
public async Task UpdateNotificationSettingsTest() { var client = GetAuthorizedUserClient(_userId); await EnsureSettingsExist(_userId); var request = new UpdateNotificationSettingsRequest { Settings = new List <NotificationSettingModel> { new NotificationSettingModel { Type = NotificationType.Email, Event = NotificationEvent.PackageArrived, Enabled = false } } }; var payload = new ObjectContent <UpdateNotificationSettingsRequest>(request, new JsonMediaTypeFormatter(), "application/json"); var response = await client.PutAsync("api/settings/notifications", payload); var content = await response.Content.ReadAsAsync <NotificationSettingsResponse>(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(4, content.Settings.Count()); Assert.False(content.Settings.First(x => x.Type == NotificationType.Email && x.Event == NotificationEvent.PackageArrived).Enabled); Assert.True(content.Settings.Where(x => x.Type != NotificationType.Email && x.Event != NotificationEvent.PackageArrived).All(x => x.Enabled)); }
public async Task WhenUnsupportedNotificationSettingsProvidedThenValidationErrorRaisedTest() { var userId = Guid.NewGuid().ToString(); var currentSettings = new UserSettings { UserId = userId, Settings = new List <NotificationSetting> { new NotificationSetting(NotificationType.Push, NotificationEvent.ArticleCreated, true), new NotificationSetting(NotificationType.Email, NotificationEvent.PackageArrived, true) } }; var request = new UpdateNotificationSettingsRequest { UserId = userId, Settings = new List <NotificationSettingModel> { new NotificationSettingModel { Type = NotificationType.Push, Event = NotificationEvent.ResetPassword, Enabled = false }, new NotificationSettingModel { Type = NotificationType.SMS, Event = NotificationEvent.CommentLiked, Enabled = true }, new NotificationSettingModel { Type = NotificationType.Email, Event = NotificationEvent.ResetPassword, Enabled = true } } }; var store = new Mock <ISettingsDataStore>(); store.Setup(x => x.FindAsync(It.Is <string>(s => s == userId))).ReturnsAsync(currentSettings); var service = new SettingsService(store.Object, DefaultMapper); var exception = await Assert.ThrowsAsync <ValidationException>(() => service.UpdateNotificationSettingsAsync(request)); store.Verify(x => x.UpdateAsync(It.IsAny <UserSettings>()), Times.Never); Assert.Equal(3, exception.Errors.Count); }
public async Task UpdateNotificationSettingsTest() { var service = new Mock <ISettingsService>(); var request = new UpdateNotificationSettingsRequest { Settings = new List <NotificationSettingModel> { new NotificationSettingModel { Type = NotificationType.Push, Event = NotificationEvent.ArticleCreated, Enabled = true } } }; var response = new NotificationSettingsResponse { UserId = UserId, Settings = new List <NotificationSettingModel> { new NotificationSettingModel { Type = NotificationType.Push, Event = NotificationEvent.ArticleCreated, Enabled = true } } }; UpdateNotificationSettingsRequest req = null; service.Setup(x => x.UpdateNotificationSettingsAsync(It.Is <UpdateNotificationSettingsRequest>(settingsRequest => settingsRequest == request && settingsRequest.UserId == UserId))) .Callback <UpdateNotificationSettingsRequest>(r => req = r) .ReturnsAsync(response) .Verifiable(); var controller = new SettingsController(_logger, service.Object).WithUser(); var result = await controller.UpdateNotificationSettingsAsync(request); service.Verify(); Assert.NotNull(req); Assert.Equal(UserId, req.UserId); var res = Assert.IsType <OkObjectResult>(result); Assert.Equal(response, res.Value); Assert.IsType <NotificationSettingsResponse>(res.Value); }
public BaseResponse <GetAccountResponse, MundipaggErrorsResponse> UpdateNotificationSettings(string accountId, UpdateNotificationSettingsRequest request) { var method = new HttpMethod("patch"); var endpoint = $"/accounts/{accountId}/notification-settings"; return(this.SendRequest <GetAccountResponse>(method, endpoint, request, authMode: "amk")); }
public async Task SuccessfulNotificationSettingsUpdateTest() { var userId = Guid.NewGuid().ToString(); var currentSettings = new UserSettings { UserId = userId, Settings = new List <NotificationSetting> { new NotificationSetting(NotificationType.Push, NotificationEvent.ArticleCreated, true), new NotificationSetting(NotificationType.Email, NotificationEvent.PackageArrived, true), new NotificationSetting(NotificationType.SMS, NotificationEvent.SmsSent, true), } }; var missingSettings = new List <NotificationSetting> { new NotificationSetting(NotificationType.SMS, NotificationEvent.SmsSent, true) }; var request = new UpdateNotificationSettingsRequest { UserId = userId, Settings = new List <NotificationSettingModel> { new NotificationSettingModel { Type = NotificationType.Push, Event = NotificationEvent.CommentLiked, Enabled = true }, new NotificationSettingModel { Type = NotificationType.Email, Event = NotificationEvent.PackageArrived, Enabled = false }, new NotificationSettingModel { Type = NotificationType.Push, Event = NotificationEvent.ArticleCreated, Enabled = false }, new NotificationSettingModel { Type = NotificationType.SMS, Event = NotificationEvent.SmsSent, Enabled = true } } }; var store = new Mock <ISettingsDataStore>(); UserSettings newSettings = null; store.Setup(x => x.FindAsync(It.Is <string>(s => s == userId))).ReturnsAsync(currentSettings); store.Setup(x => x.UpdateAsync(It.IsAny <UserSettings>())) .Callback <UserSettings>(settings => newSettings = settings) .ReturnsAsync(() => newSettings); var service = new SettingsService(store.Object, DefaultMapper); var response = await service.UpdateNotificationSettingsAsync(request); Assert.NotNull(response); Assert.Equal(userId, response.UserId); Assert.Equal(4, response.Settings.Count()); foreach (var setting in request.Settings) { Assert.NotNull(response.Settings.FirstOrDefault(x => x.Enabled == setting.Enabled && x.Type == setting.Type && x.Event == setting.Event)); } foreach (var setting in missingSettings) { Assert.NotNull(response.Settings.FirstOrDefault(x => x.Enabled == setting.Enabled && x.Type == setting.Type && x.Event == setting.Event)); } }
public async Task <NotificationSettingsResponse> UpdateNotificationSettingsAsync(UpdateNotificationSettingsRequest request) { Ensure.That(request, nameof(request)).IsNotNull(); var settings = await FindSettingsAsync(request.UserId); var newSettings = request.Settings.Select(Mapper.Map <NotificationSettingModel, NotificationSetting>).ToList(); settings.Settings = PopulateSettings(newSettings); var updatedSettings = await _store.UpdateAsync(settings); return(new NotificationSettingsResponse { UserId = request.UserId, Settings = updatedSettings.Settings.Select(Mapper.Map <NotificationSetting, NotificationSettingModel>).ToList() }); }