private static async Task SeedManagers(SystemDataContext context, UserManager <UserDomain> userManager) { if (!context.Users.Any(user => user.UserName == Constants.DefaultManagerUsername)) { var newManager = new UserDomain { UserName = Constants.DefaultManagerUsername, Email = Constants.DefaultManagerUsername, CreatedOn = DateTime.Now, IsPasswordChanged = true }; var result = await userManager.CreateAsync(newManager); if (result.Succeeded) { await userManager.AddPasswordAsync(newManager, Constants.DefaultPassword); await userManager.AddToRoleAsync(newManager, Constants.RoleManager); await userManager.AddClaimsAsync(newManager, new List <Claim>() { new Claim("Role", Constants.RoleManager), new Claim("IsPasswordChanged", newManager.IsPasswordChanged.ToString()) }); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, SystemDataContext context, UserManager <UserDomain> userManager, RoleManager <IdentityRole> roleManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseStatusCodePagesWithReExecute("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Email}/{action=Index}/{id?}"); }); // Initial seeding AccountSeeder.Init(context, userManager, roleManager).Wait(); }
public async Task GetAllAttachmentsNewEmails() { TestUtils.GetContextWithEmails(nameof(GetAllAttachmentsNewEmails)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetAllAttachmentsNewEmails)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var openEmails = await sut.GetNewEmailsAsync(); int actualAttachmentsCount = 0; openEmails.ForEach(email => actualAttachmentsCount += email.Attachments.Count); int expectedAttachmentsCount = 0; TestUtils.Emails .Where(mail => mail.Status == EmailStatus.New).ToList() .ForEach(email => expectedAttachmentsCount += email.Attachments.Count); Assert.AreEqual(expectedAttachmentsCount, actualAttachmentsCount); } }
public static async Task Init(SystemDataContext context, UserManager <UserDomain> userManager, RoleManager <IdentityRole> roleManager) { context.Database.EnsureCreated(); await SeedRoles(context, roleManager); await SeedManagers(context, userManager); await SeedOperators(context, userManager); }
public static SystemDataContext GetContextWithApplications(string databaseName) { var options = GetOptions(databaseName); var context = new SystemDataContext(options); context.Applications.AddRange(Applications); context.SaveChanges(); return(context); }
public async Task CreateApplication() { TestUtils.GetContextWithApplications(nameof(CreateApplication)); var emailId = Guid.NewGuid(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(CreateApplication)))) { var sut = new ApplicationFactory(); var application = sut.Create(emailId.ToString(), "userId", "1111", "testName", "+1111"); Assert.AreEqual(emailId, application.EmailId); } }
public async Task GetAllEmails() { TestUtils.GetContextWithEmails(nameof(GetAllEmails)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetAllEmails)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var allEmails = await sut.GetAllEmailsAsync(); Assert.AreEqual(TestUtils.Emails.Count, allEmails.Count); } }
public async Task GetGmailId() { TestUtils.GetContextWithEmails(nameof(GetGmailId)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetGmailId)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var id = TestUtils.Emails[0].Id; var gmailMessageId = await sut.GetGmailIdAsync(id.ToString()); Assert.AreEqual(TestUtils.Emails[0].GmailMessageId, gmailMessageId); } }
public async Task GetOperatorUsername_Null() { TestUtils.GetContextWithApplications(nameof(GetOperatorUsername_Null)); var gmailServiceMock = new Mock <IGmailAPIService>(); var appFactoryMock = new Mock <IApplicationFactory>(); var userSericeMock = new Mock <IUserService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetOperatorUsername_Null)))) { var sut = new ApplicationService(assertContext, appFactoryMock.Object, userSericeMock.Object); var expectedUserName = await sut.GetOperatorUsernameAsync(null); Assert.AreEqual(expectedUserName, null); } }
public async Task MapsCorrectly_Name() { TestUtils.GetContextWithApplications(nameof(MapsCorrectly_Name)); var gmailServiceMock = new Mock <IGmailAPIService>(); var appFactoryMock = new Mock <IApplicationFactory>(); var userSericeMock = new Mock <IUserService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(MapsCorrectly_Name)))) { var sut = new ApplicationService(assertContext, appFactoryMock.Object, userSericeMock.Object); var openApps = await sut.GetOpenAppsAsync(); Assert.IsTrue(openApps.Any(app => app.Name == TestUtils.Applications[0].Name)); } }
private static async Task SeedRoles(SystemDataContext context, RoleManager <IdentityRole> roleManager) { if (!context.Roles.Any(role => role.Name == Constants.RoleOperator)) { await roleManager.CreateAsync(new IdentityRole { Name = Constants.RoleOperator }); } if (!context.Roles.Any(role => role.Name == Constants.RoleManager)) { await roleManager.CreateAsync(new IdentityRole { Name = Constants.RoleManager }); } }
protected void SendImage(BotActionType actionType) { var cl = new FacebookBotClient(); cl.SuppressThrowException = true; var l = WebApp.Current.DatabaseCache.BotActionImage .Where(el => el.BotCD == this.BotCD && el.ActionType == actionType).ToList(); if (l.Count == 0) { return; } var index = _Random.Next(l.Count); var rImage = l[index]; HttpResponse res = null; var rAttachment = WebApp.Current.DatabaseCache.FacebookAttachment.FirstOrDefault(el => el.Url == rImage.ImageUrl); if (rAttachment == null) { res = cl.SendImage(this.RecipientID, rImage.ImageUrl); if (res.StatusCode == HttpStatusCode.OK) { try { var o = JsonConvert.DeserializeObject <SendMediaResponse>(res.BodyText); rAttachment = new FacebookAttachmentRecord(); rAttachment.AttachmentID = o.Attachment_ID; rAttachment.Url = rImage.ImageUrl; rAttachment.CreateTime = DateTimeInfo.GetNow(); var dc = new SystemDataContext(); dc.FacebookAttachment_Insert(rAttachment); } catch (Exception ex) { HignullLog.Current.Add(ex); } } } else { res = cl.SendAttachment(this.RecipientID, rAttachment.AttachmentID); } }
public async Task GetBody() { TestUtils.GetContextWithEmails(nameof(GetBody)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetBody)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); gmailServiceMock .Setup(g => g.GetEmailBodyAsync("mockId")) .ReturnsAsync("Test Body"); var emailBody = await sut.GetBodyByGmailAsync("mockId"); Assert.AreEqual("Test Body", emailBody); } }
public async Task GetEmailId() { TestUtils.GetContextWithApplications(nameof(GetEmailId)); var gmailServiceMock = new Mock <IGmailAPIService>(); var appFactoryMock = new Mock <IApplicationFactory>(); var userSericeMock = new Mock <IUserService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetEmailId)))) { var sut = new ApplicationService(assertContext, appFactoryMock.Object, userSericeMock.Object); var appId = TestUtils.Applications[0].Id; var emailId = await sut.GetEmailId(appId.ToString()); Assert.AreEqual(TestUtils.Applications[0].EmailId.ToString(), emailId); } }
public async Task GetAttachment() { TestUtils.GetContextWithEmails(nameof(GetAttachment)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetAttachment)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var id = TestUtils.Emails[0].Id; var email = await sut.GetSingleEmailAsync(id.ToString()); var actualAttachmentCount = email.Attachments.Count(); var expectedAttachmentCount = TestUtils.Emails[0].Attachments.Count(); Assert.AreEqual(expectedAttachmentCount, actualAttachmentCount); } }
public async Task ChangeStatus() { TestUtils.GetContextWithEmails(nameof(ChangeStatus)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(ChangeStatus)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var id = TestUtils.Emails[0].Id; await sut.ChangeStatusAsync(id.ToString(), EmailStatus.Open); var email = assertContext.Emails.FirstOrDefault(mail => mail.Id == id); Assert.IsTrue(email.Status == EmailStatus.Open); } }
public async Task GetNewEmails() { TestUtils.GetContextWithEmails(nameof(GetNewEmails)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetNewEmails)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var newEmails = await sut.GetNewEmailsAsync(); var expectedOpenEmailsCount = TestUtils.Emails.Where(mail => mail.Status == EmailStatus.Open).Count(); var actualNewEmailsCount = newEmails.Count(); Assert.AreEqual(expectedOpenEmailsCount, actualNewEmailsCount); } }
public async Task ChangeStatus() { TestUtils.GetContextWithApplications(nameof(ChangeStatus)); var gmailServiceMock = new Mock <IGmailAPIService>(); var appFactoryMock = new Mock <IApplicationFactory>(); var userSericeMock = new Mock <IUserService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(ChangeStatus)))) { var sut = new ApplicationService(assertContext, appFactoryMock.Object, userSericeMock.Object); var applicationId = TestUtils.Applications[0].Id; await sut.ChangeStatusAsync(applicationId.ToString(), ApplicationStatus.Approved, "username"); var application = assertContext.Applications.FirstOrDefault(mail => mail.Id == applicationId); Assert.AreEqual(application.Status, ApplicationStatus.Approved); } }
public async Task GetOperatorUsername() { TestUtils.GetContextWithApplications(nameof(GetOperatorUsername)); var gmailServiceMock = new Mock <IGmailAPIService>(); var appFactoryMock = new Mock <IApplicationFactory>(); var userSericeMock = new Mock <IUserService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetOperatorUsername)))) { var sut = new ApplicationService(assertContext, appFactoryMock.Object, userSericeMock.Object); var actualUserName = TestUtils.Applications[0].User.UserName; var emailId = TestUtils.Applications[0].EmailId; var expectedUserName = await sut.GetOperatorUsernameAsync(emailId.ToString()); Assert.AreEqual(expectedUserName, actualUserName); } }
public async Task MapsCorrectly_EmailId() { TestUtils.GetContextWithEmails(nameof(MapsCorrectly_EmailId)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(MapsCorrectly_EmailId)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var allEmails = await sut.GetAllEmailsAsync(); var expectedEmails = TestUtils.Emails; Assert.IsTrue(allEmails.Any(email => email.Id == TestUtils.Emails[0].Id)); Assert.IsTrue(allEmails.Any(email => email.Id == TestUtils.Emails[1].Id)); Assert.IsTrue(allEmails.Any(email => email.Id == TestUtils.Emails[2].Id)); } }
public async Task ChangeStatusToClosed() { TestUtils.GetContextWithEmails(nameof(ChangeStatusToClosed)); var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(ChangeStatusToClosed)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var id = TestUtils.Emails[0].Id; await sut.ChangeStatusAsync(id.ToString(), EmailStatus.Closed); var email = assertContext.Emails.FirstOrDefault(mail => mail.Id == id); var date = new DateTime(2019, 11, 22); email.ToTerminalStatus = date; Assert.IsTrue(email.ToTerminalStatus == date); } }
public async Task GetOpenApps() { TestUtils.GetContextWithApplications(nameof(GetOpenApps)); var gmailServiceMock = new Mock <IGmailAPIService>(); var appFactoryMock = new Mock <IApplicationFactory>(); var userSericeMock = new Mock <IUserService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(GetOpenApps)))) { var sut = new ApplicationService(assertContext, appFactoryMock.Object, userSericeMock.Object); var openApps = await sut.GetOpenAppsAsync(); var expectedOpenAppsCount = TestUtils.Applications .Count(app => app.Status == ApplicationStatus.NotReviewed); var actualOpenAppsCount = openApps.Count(); Assert.AreEqual(expectedOpenAppsCount, actualOpenAppsCount); } }
public async Task MapsCorrectly_SenderName() { //Prepare database TestUtils.GetContextWithEmails(nameof(MapsCorrectly_SenderName)); //Prepare dependencies var gmailServiceMock = new Mock <IGmailAPIService>(); using (var assertContext = new SystemDataContext(TestUtils.GetOptions(nameof(MapsCorrectly_SenderName)))) { var sut = new EmailService(assertContext, gmailServiceMock.Object); var allEmails = await sut.GetAllEmailsAsync(); var expectedEmails = TestUtils.Emails; Assert.IsTrue(allEmails.Any(email => email.SenderName == TestUtils.Emails[0].SenderName)); Assert.IsTrue(allEmails.Any(email => email.SenderName == TestUtils.Emails[1].SenderName)); Assert.IsTrue(allEmails.Any(email => email.SenderName == TestUtils.Emails[2].SenderName)); } }
public GmailAPIService(SystemDataContext context) { _context = context; }
public ApplicationService(SystemDataContext context, IApplicationFactory factory, IUserService userService) { _context = context; _factory = factory; _userService = userService; }
public EmailService(SystemDataContext context, IGmailAPIService gmailService) { _context = context; _gmailService = gmailService; }
public UserService(SystemDataContext context, UserManager <UserDomain> userManager, IUserFactory factory) { _context = context; _userManager = userManager; _factory = factory; }