Esempio n. 1
0
        public async Task Should_find_user()
        {
            // STEP 0: Create user.
            var userId1 = Guid.NewGuid().ToString();
            var userId2 = Guid.NewGuid().ToString();

            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto
                    {
                        Id       = userId1,
                        FullName = userId1
                    },
                    new UpsertUserDto
                    {
                        Id       = userId2,
                        FullName = userId2
                    }
                }
            };

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


            // STEP 1: Query users
            var users = await _.Client.Users.GetUsersAsync(_.AppId, userId1);

            Assert.Equal(1, users.Total);
            Assert.Equal(userId1, users.Items[0].Id);
        }
Esempio n. 2
0
        public async Task Should_create_users()
        {
            // STEP 0: Create user.
            var userId1 = Guid.NewGuid().ToString();
            var userId2 = Guid.NewGuid().ToString();

            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto
                    {
                        Id       = userId1,
                        FullName = "name1_0"
                    },
                    new UpsertUserDto
                    {
                        Id       = userId2,
                        FullName = "name2_0"
                    }
                }
            };

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

            Assert.Equal(2, users.Count);

            var user1 = users.ElementAt(0);
            var user2 = users.ElementAt(1);

            Assert.Equal(userId1, user1.Id);
            Assert.Equal(userId2, user2.Id);
            Assert.Equal("name1_0", user1.FullName);
            Assert.Equal("name2_0", user2.FullName);
        }
Esempio n. 3
0
        public async Task <IActionResult> PostUsers(string appId, [FromBody] UpsertUsersDto request)
        {
            var response = new List <UserDto>();

            if (request?.Requests != null)
            {
                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)
                {
                    if (dto != null)
                    {
                        var update = dto.ToUpdate();

                        var user = await userStore.UpsertAsync(appId, dto.Id, update, HttpContext.RequestAborted);

                        response.Add(UserDto.FromDomainObject(user));
                    }
                }
            }

            return(Ok(response));
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public async Task Should_update_users()
        {
            // STEP 0: Create user.
            var userId1 = Guid.NewGuid().ToString();
            var userId2 = Guid.NewGuid().ToString();

            var userRequest = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto
                    {
                        Id       = userId1,
                        FullName = "name1_0"
                    },
                    new UpsertUserDto
                    {
                        Id       = userId2,
                        FullName = "name2_0"
                    }
                }
            };

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


            // STEP 1: Update user.
            var userRequest2 = new UpsertUsersDto
            {
                Requests = new List <UpsertUserDto>
                {
                    new UpsertUserDto
                    {
                        Id       = userId1,
                        FullName = "name1_1"
                    }
                }
            };

            await _.Client.Users.PostUsersAsync(_.AppId, userRequest2);


            // Get users
            var users = await _.Client.Users.GetUsersAsync(_.AppId, take : 100000);

            var user1 = users.Items.SingleOrDefault(x => x.Id == userId1);
            var user2 = users.Items.SingleOrDefault(x => x.Id == userId2);

            Assert.Equal("name1_1", user1?.FullName);
            Assert.Equal("name2_0", user2?.FullName);
        }
Esempio n. 6
0
        public async Task <IActionResult> PostUsers(string appId, [FromBody] UpsertUsersDto request)
        {
            var response = new List <UserDto>();

            if (request?.Requests != null)
            {
                foreach (var dto in request.Requests)
                {
                    if (dto != null)
                    {
                        var update = dto.ToUpdate();

                        var user = await userStore.UpsertAsync(appId, dto.Id, update, HttpContext.RequestAborted);

                        response.Add(UserDto.FromDomainObject(user));
                    }
                }
            }

            return(Ok(response));
        }
Esempio n. 7
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.");
        }
Esempio n. 8
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));
        }
Esempio n. 9
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));
        }
Esempio n. 10
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.");
        }