public async void SingleConfiguration_Options_PushFailed()
            {
                var config = new PushChannelConfiguration()
                {
                    ChannelType = TestChannelType, Id = "TestId"
                };
                var ex = new PushException("test");

                var providerMock = new Mock <IPushProvider>(MockBehavior.Strict);

                providerMock.Setup(v => v.PushAsync(It.IsAny <string>(), It.IsAny <PushOptions>()))
                .Throws(ex);

                var pushService = new PushService(MockByConfigs(new[] { config }),
                                                  new[] { MockByProvider(new Dictionary <string, IPushProvider> {
                        { config.Id, providerMock.Object }
                    }) });

                var res = await Assert.ThrowsAsync <PushFailedException>(async() =>
                                                                         await pushService.Push("userid", new Dictionary <string, string>(), "payload", null));

                Assert.Single(res.Failures);
                Assert.Same(config, res.Failures[0].Configuration);
                Assert.Same(ex, res.Failures[0].Exception);
                Assert.Same(ex, res.InnerException);
            }
            public async void MultiConfiguration_PartiallyFailed()
            {
                var config1 = new PushChannelConfiguration()
                {
                    ChannelType = TestChannelType, Id = "1"
                };
                var config2 = new PushChannelConfiguration()
                {
                    ChannelType = TestChannelType, Id = "2"
                };
                var config3 = new PushChannelConfiguration()
                {
                    ChannelType = TestChannelType, Id = "3"
                };
                var ex1 = new PushException("test");
                var ex2 = new PushException("test");

                var providerMock1 = new Mock <IPushProvider>(MockBehavior.Strict);

                providerMock1.Setup(v => v.PushAsync(It.IsAny <string>(), It.IsAny <PushOptions>()))
                .Throws(ex1);
                var providerMock2 = new Mock <IPushProvider>(MockBehavior.Strict);

                providerMock2.Setup(v => v.PushAsync(It.IsAny <string>(), It.IsAny <PushOptions>()))
                .Throws(ex2);
                var providerMock3 = new Mock <IPushProvider>(MockBehavior.Strict);

                providerMock3.Setup(v => v.PushAsync(It.IsAny <string>(), It.IsAny <PushOptions>()))
                .Returns(Task.CompletedTask);

                var pushService = new PushService(MockByConfigs(new[] { config1, config2, config3 }),
                                                  new[] { MockByProvider(new Dictionary <string, IPushProvider> {
                        { config1.Id, providerMock1.Object },
                        { config2.Id, providerMock2.Object },
                        { config3.Id, providerMock3.Object }
                    }) });

                var res = await Assert.ThrowsAsync <PushPartiallyFailedException>(async() =>
                                                                                  await pushService.Push("userid", new Dictionary <string, string>(), "payload", null));

                Assert.Equal(2, res.Failures.Length);
                Assert.Collection(res.Failures,
                                  v =>
                {
                    Assert.Same(config1, v.Configuration);
                    Assert.Same(ex1, v.Exception);
                },
                                  v =>
                {
                    Assert.Same(config2, v.Configuration);
                    Assert.Same(ex2, v.Exception);
                });
                Assert.Collection(res.InnerExceptions, v => Assert.Same(ex1, v), v => Assert.Same(ex2, v));
                Assert.Single(res.Succeeded);
                Assert.Collection(res.Succeeded, v =>
                {
                    Assert.Same(config3, v.Configuration);
                    Assert.Null(v.Exception);
                });
            }
        private static IPushConfigurationStore MockByConfig(PushChannelConfiguration config)
        {
            var pushConfigurationMock = new Mock <IPushConfigurationStore>(MockBehavior.Strict);

            pushConfigurationMock.Setup(v => v.GetAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(config));
            return(pushConfigurationMock.Object);
        }
Example #4
0
 public PushFailedException(PushChannelConfiguration c, Exception e)
     : base($"Push to configuration failed.", e)
 {
     Failures = new[] { new PushResult()
                        {
                            Configuration = c, Exception = e
                        } };
 }
 public FirebasePushProvider(IOptions <FirebaseConfig> optionsAccessor,
                             IFirebaseHttpClient firebaseHttpClient,
                             PushChannelConfiguration config,
                             PushEndpoint endpoint)
 {
     options = optionsAccessor.Value;
     this.firebaseHttpClient = firebaseHttpClient;
     this.config             = config;
     this.endpoint           = endpoint;
 }
Example #6
0
        private async Task <IPushProvider> CreateProvider(PushChannelConfiguration config)
        {
            var providerFactory = pushProviderFactories.SingleOrDefault(v => v.PushChannelType == config.ChannelType);

            if (null == providerFactory)
            {
                throw new NotSupportedException($"Provider for push type {config.ChannelType} was not configured.");
            }
            return(await providerFactory.CreateProvider(config));
        }
Example #7
0
        public async Task <IPushProvider> CreateProvider(PushChannelConfiguration config)
        {
            var endpoint = await pushConfigurationStore.GetEndpointAsync(config.Id);

            var subscription = new PushSubscription()
            {
                Auth     = endpoint.EndpointOptions["AuthKey"],
                Endpoint = new Uri(endpoint.Endpoint),
                P256dh   = endpoint.EndpointOptions["P256dhKey"]
            };

            return(new WebPushProvider(subscription, webPushClient));
        }
Example #8
0
 public async Task <IPushProvider> CreateProvider(PushChannelConfiguration config)
 {
     return(new AzureNotificationPushProvider(options, config, await pushConfigurationStore.GetEndpointAsync(config.Id)));
 }
 public AzureNotificationPushProvider(IOptions <AzureNotificationHubConfig> optionsAccessor, PushChannelConfiguration config, PushEndpoint endpoint)
 {
     options       = optionsAccessor.Value;
     this.config   = config;
     this.endpoint = endpoint;
 }
Example #10
0
        private async Task PushWithProvider(PushChannelConfiguration config, string payload, PushOptions options)
        {
            var provider = await CreateProvider(config);

            await provider.PushAsync(payload, options);
        }
 public async Task <IPushProvider> CreateProvider(PushChannelConfiguration config)
 {
     return(new FirebasePushProvider(options, firebaseHttpClient, config, await pushConfigurationStore.GetEndpointAsync(config.Id)));
 }