Ejemplo n.º 1
0
        public async Task Should_send_self_formatted_notifications()
        {
            // STEP 0: Create user.
            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto()
                }
            };

            var users_0 = await _.Client.Users.PostUsersAsync(_.AppId, userRequest);

            var user_0 = users_0.First();


            // STEP 1: Send Notification
            var subjectId = Guid.NewGuid().ToString();

            var publishRequest = new PublishManyDto
            {
                Requests = new List <PublishDto>
                {
                    new PublishDto
                    {
                        Topic        = $"users/{user_0.Id}",
                        Preformatted = new NotificationFormattingDto
                        {
                            Subject = new LocalizedText
                            {
                                ["en"] = subjectId
                            }
                        }
                    }
                }
            };

            await _.Client.Events.PostEventsAsync(_.AppId, publishRequest);


            // Test that notification has been created.
            var notifications = await _.Client.Notifications.WaitForNotificationsAsync(_.AppId, user_0.Id, null, TimeSpan.FromSeconds(30));

            Assert.Contains(notifications, x => x.Subject == subjectId);


            // Test that user notifications have been created.
            var userNotifications = await _.Notifo.CreateUserClient(user_0).Notifications.WaitForMyNotificationsAsyn(null, TimeSpan.FromSeconds(30));

            Assert.Contains(userNotifications, x => x.Subject == subjectId);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PostEvents(string appId, [FromBody] PublishManyDto request)
        {
            if (request.Requests?.Length > 100)
            {
                throw new ValidationException($"Only 100 users can be update in one request, found: {request.Requests.Length}");
            }

            foreach (var dto in request.Requests.OrEmpty().NotNull())
            {
                var @event = dto.ToEvent(appId);

                await eventPublisher.PublishAsync(@event, HttpContext.RequestAborted);
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task Should_send_email(string kind)
        {
            var appName = Guid.NewGuid().ToString();

            // STEP 0: Create app
            var createRequest = new UpsertAppDto
            {
                Name = appName
            };

            var app_0 = await _.Client.Apps.PostAppAsync(createRequest);


            // STEP 1: Create email template.
            var emailTemplateRequest = new CreateChannelTemplateDto
            {
                Kind = kind
            };

            await _.Client.EmailTemplates.PostTemplateAsync(app_0.Id, emailTemplateRequest);


            // STEP 2: Create integration
            var emailIntegrationRequest = new CreateIntegrationDto
            {
                Type       = "SMTP",
                Properties = new Dictionary <string, string>
                {
                    ["host"]      = mailcatcher.SmtpHost,
                    ["fromEmail"] = "*****@*****.**",
                    ["fromName"]  = "Hello Notifo",
                    ["port"]      = mailcatcher.SmtpPort.ToString(CultureInfo.InvariantCulture)
                },
                Enabled = true
            };

            await _.Client.Apps.PostIntegrationAsync(app_0.Id, emailIntegrationRequest);


            // STEP 3: Create user
            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto
                    {
                        EmailAddress = "*****@*****.**"
                    }
                }
            };

            var users_0 = await _.Client.Users.PostUsersAsync(app_0.Id, userRequest);

            var user_0 = users_0.First();


            // STEP 4: Send email
            var subjectId = Guid.NewGuid().ToString();

            var publishRequest = new PublishManyDto
            {
                Requests = new List <PublishDto>
                {
                    new PublishDto
                    {
                        Topic        = $"users/{user_0.Id}",
                        Preformatted = new NotificationFormattingDto
                        {
                            Subject = new LocalizedText
                            {
                                ["en"] = subjectId
                            }
                        },
                        Settings = new Dictionary <string, ChannelSettingDto>
                        {
                            [Providers.Email] = new ChannelSettingDto
                            {
                                Send = ChannelSend.Send
                            }
                        }
                    }
                }
            };

            await _.Client.Events.PostEventsAsync(app_0.Id, publishRequest);


            // Get email status
            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
            {
                while (!cts.IsCancellationRequested)
                {
                    var messages = await mailcatcher.GetMessagesAsync(cts.Token);

                    var message = messages.FirstOrDefault(x => x.Subject.Contains(appName, StringComparison.OrdinalIgnoreCase));

                    if (message != null)
                    {
                        var body = await mailcatcher.GetBodyAsync(message.Id, cts.Token);

                        Assert.Contains(subjectId, body.Plain, StringComparison.OrdinalIgnoreCase);
                        Assert.Contains(subjectId, body.Html, StringComparison.OrdinalIgnoreCase);
                        return;
                    }

                    await Task.Delay(50);
                }
            }

            Assert.False(true, "Email not received.");
        }
Ejemplo n.º 4
0
        public async Task Should_not_send_email_when_template_missing()
        {
            var appName = Guid.NewGuid().ToString();

            // STEP 0: Create app
            var createRequest = new UpsertAppDto
            {
                Name = appName
            };

            var app_0 = await _.Client.Apps.PostAppAsync(createRequest);


            // STEP 1: Create integration
            var emailIntegrationRequest = new CreateIntegrationDto
            {
                Type       = "SMTP",
                Properties = new Dictionary <string, string>
                {
                    ["host"]      = mailcatcher.SmtpHost,
                    ["fromEmail"] = "*****@*****.**",
                    ["fromName"]  = "Hello Notifo",
                    ["port"]      = mailcatcher.SmtpPort.ToString(CultureInfo.InvariantCulture)
                },
                Enabled = true
            };

            await _.Client.Apps.PostIntegrationAsync(app_0.Id, emailIntegrationRequest);


            // STEP 2: Create user
            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto
                    {
                        EmailAddress = "*****@*****.**"
                    }
                }
            };

            var users_0 = await _.Client.Users.PostUsersAsync(app_0.Id, userRequest);

            var user_0 = users_0.First();


            // STEP 3: Send email
            var subjectId = Guid.NewGuid().ToString();

            var publishRequest = new PublishManyDto
            {
                Requests = new List <PublishDto>
                {
                    new PublishDto
                    {
                        Topic        = $"users/{user_0.Id}",
                        Preformatted = new NotificationFormattingDto
                        {
                            Subject = new LocalizedText
                            {
                                ["en"] = subjectId
                            }
                        },
                        Settings = new Dictionary <string, ChannelSettingDto>
                        {
                            [Providers.Email] = new ChannelSettingDto
                            {
                                Send = ChannelSend.Send
                            }
                        }
                    }
                }
            };

            await _.Client.Events.PostEventsAsync(app_0.Id, publishRequest);


            var logs = await _.Client.Logs.WaitForLogEntriesAsync(app_0.Id, null, TimeSpan.FromSeconds(30));

            Assert.Contains(logs, x => x.Message.Contains("Cannot find template", StringComparison.OrdinalIgnoreCase));
        }
Ejemplo n.º 5
0
        public async Task Should_send_webhook()
        {
            var appName = Guid.NewGuid().ToString();

            // STEP 0: Create app
            var createRequest = new UpsertAppDto
            {
                Name = appName
            };

            var app_0 = await _.Client.Apps.PostAppAsync(createRequest);


            // STEP 1: Start webhook session
            var(url, sessionId) = await webhookCatcher.CreateSessionAsync();


            // STEP 2: Create integration
            var emailIntegrationRequest = new CreateIntegrationDto
            {
                Type       = "Webhook",
                Properties = new Dictionary <string, string>
                {
                    ["Url"]         = url,
                    ["SendAlways"]  = "true",
                    ["SendConfirm"] = "true"
                },
                Enabled = true
            };

            await _.Client.Apps.PostIntegrationAsync(app_0.Id, emailIntegrationRequest);


            // STEP 3: Create user
            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto()
                }
            };

            var users_0 = await _.Client.Users.PostUsersAsync(app_0.Id, userRequest);

            var user_0 = users_0.First();


            // STEP 4: Send webhook
            var subjectId = Guid.NewGuid().ToString();

            var publishRequest = new PublishManyDto
            {
                Requests = new List <PublishDto>
                {
                    new PublishDto
                    {
                        Topic        = $"users/{user_0.Id}",
                        Preformatted = new NotificationFormattingDto
                        {
                            Subject = new LocalizedText
                            {
                                ["en"] = subjectId
                            }
                        },
                        Settings = new Dictionary <string, ChannelSettingDto>
                        {
                            [Providers.WebHook] = new ChannelSettingDto
                            {
                                Send = ChannelSend.Send
                            }
                        }
                    }
                }
            };

            await _.Client.Events.PostEventsAsync(app_0.Id, publishRequest);


            // Get webhook status
            var requests = await webhookCatcher.WaitForRequestsAsync(sessionId, TimeSpan.FromSeconds(30));

            Assert.Contains(requests, x => x.Method == "POST" && x.Content.Contains(subjectId, StringComparison.OrdinalIgnoreCase));
        }
Ejemplo n.º 6
0
        public async Task Should_send_sms_with_template()
        {
            var appName = Guid.NewGuid().ToString();

            // STEP 0: Create app
            var createRequest = new UpsertAppDto
            {
                Name = appName
            };

            var app_0 = await _.Client.Apps.PostAppAsync(createRequest);


            // STEP 1: Create sms template.
            var smsTemplateRequest = new CreateChannelTemplateDto
            {
            };

            var template_0 = await _.Client.SmsTemplates.PostTemplateAsync(app_0.Id, smsTemplateRequest);

            var smsTemplate = new SmsTemplateDto
            {
                Text = "<start>{{ notification.subject }}</end>"
            };

            await _.Client.SmsTemplates.PutTemplateLanguageAsync(app_0.Id, template_0.Id, "en", smsTemplate);


            // STEP 2: Create integration
            var emailIntegrationRequest = new CreateIntegrationDto
            {
                Type       = "MessageBird",
                Properties = new Dictionary <string, string>
                {
                    ["accessKey"]    = AccessKey,
                    ["phoneNumber"]  = PhoneNumber,
                    ["phoneNumbers"] = string.Empty
                },
                Enabled = true
            };

            await _.Client.Apps.PostIntegrationAsync(app_0.Id, emailIntegrationRequest);


            // STEP 3: Create user
            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto
                    {
                        PhoneNumber = PhoneNumber
                    }
                }
            };

            var users_0 = await _.Client.Users.PostUsersAsync(app_0.Id, userRequest);

            var user_0 = users_0.First();


            // STEP 4: Send SMS
            var subjectId = Guid.NewGuid().ToString();

            var publishRequest = new PublishManyDto
            {
                Requests = new List <PublishDto>
                {
                    new PublishDto
                    {
                        Topic        = $"users/{user_0.Id}",
                        Preformatted = new NotificationFormattingDto
                        {
                            Subject = new LocalizedText
                            {
                                ["en"] = subjectId
                            }
                        },
                        Settings = new Dictionary <string, ChannelSettingDto>
                        {
                            [Providers.Sms] = new ChannelSettingDto
                            {
                                Send = ChannelSend.Send
                            }
                        }
                    }
                }
            };

            await _.Client.Events.PostEventsAsync(app_0.Id, publishRequest);


            // Get SMS status
            var messageBird = new MessageBirdClient(AccessKey);

            var text = $"<start>{subjectId}</end>";

            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)))
            {
                while (!cts.IsCancellationRequested)
                {
                    var messages = await messageBird.GetMessagesAsync(200);

                    if (messages.Items.Any(x => x.Body == text && x.Recipients.Items[0].Status == "delivered"))
                    {
                        return;
                    }

                    await Task.Delay(1000);
                }
            }

            Assert.False(true, "SMS not sent.");
        }