public void UpdateFocusedSchoolYear_Should_FailSilentlyBecauseItJustDoes() { var announcements = new List <Announcement>(); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(announcements), ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); var oldFocusedSchoolYear = 12345; var newFocusedSchoolYear = 23456; var appUserSessionId = 123; var schoolYears = new List <SchoolYear>(new[] { new SchoolYear { Id = newFocusedSchoolYear, Enabled = true } }); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(schoolYears), ValidationPortalDbContextMock, x => x.SchoolYears, x => x.SchoolYears = It.IsAny <DbSet <SchoolYear> >(), schoolYears); var userIdentity = new ValidationPortalIdentity(); var appUserSession = new AppUserSession { Id = appUserSessionId.ToString(), FocusedSchoolYearId = oldFocusedSchoolYear, UserIdentity = userIdentity }; DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var httpContext = new HttpContext( new HttpRequest(string.Empty, "http://wearedoubleline.com", string.Empty), new HttpResponse(new StringWriter())); httpContext.Items[AppUserService.SessionItemName] = null; HttpContextProviderMock.SetupGet(x => x.CurrentHttpContext).Returns(httpContext); LoggingServiceMock.Setup(x => x.LogDebugMessage(It.IsAny <string>())); var appUserService = new AppUserService( DbContextFactoryMock.Object, HttpContextProviderMock.Object, LoggingServiceMock.Object); appUserService.UpdateFocusedSchoolYear(newFocusedSchoolYear); ValidationPortalDbContextMock.Verify(x => x.SaveChanges(), Times.Never); }
public void DeleteAnnouncement_Should_ThrowOnNonexistentAnnouncement() { var announcement = new Announcement { Id = 12345, Message = "test contact info" }; var announcements = new List <Announcement>(new[] { announcement }); var announcementDbSetMock = EntityFrameworkMocks.GetQueryableMockDbSet(announcements); EntityFrameworkMocks.SetupMockDbSet <ValidationPortalDbContext, Announcement>( announcementDbSetMock, ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var announcementService = new AnnouncementService(DbContextFactoryMock.Object, AppUserServiceMock.Object, LoggingServiceMock.Object); var badAnnouncementId = announcement.Id + 1; Assert.Throws( Is.TypeOf <Exception>().And.Message.EqualTo($"Could not delete an announcement because announcement with ID {badAnnouncementId} was not found"), () => announcementService.DeleteAnnouncement(badAnnouncementId)); }
public void SetUp() { EntityFrameworkMocks.SetupValidationPortalDbContext(ValidationPortalDbContextMock); TestSchoolYears = new List <SchoolYear>(new[] { new SchoolYear("2017", "2018", false) { Id = 0 }, new SchoolYear("2018", "2019", true) { Id = 1 }, new SchoolYear("2019", "2020", true) { Id = 2 }, }); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(TestSchoolYears), ValidationPortalDbContextMock, x => x.SchoolYears, x => x.SchoolYears = It.IsAny <DbSet <SchoolYear> >(), TestSchoolYears); ValidationPortalDbContextMock.As <IDisposable>().Setup(x => x.Dispose()); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); }
public void DeleteAnnouncement_Should_DeleteTheAnnouncement() { var announcement = new Announcement { Id = 12345, Message = "test contact info" }; var announcements = new List <Announcement>(new[] { announcement }); var announcementDbSetMock = EntityFrameworkMocks.GetQueryableMockDbSet(announcements); EntityFrameworkMocks.SetupMockDbSet( announcementDbSetMock, ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var announcementService = new AnnouncementService(DbContextFactoryMock.Object, AppUserServiceMock.Object, LoggingServiceMock.Object); announcementService.DeleteAnnouncement(announcement.Id); announcementDbSetMock.Verify(x => x.Remove(announcement), Times.Once); ValidationPortalDbContextMock.Verify(x => x.SaveChanges(), Times.Once); }
public void GetAllEdOrgs_Should_ReturnAllEdOrgs() { var allEdOrgs = new List <EdOrg> { new EdOrg { Id = 12345 }, new EdOrg { Id = 23456 }, new EdOrg { Id = 34567 } }; EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(allEdOrgs), ValidationPortalDbContextMock, x => x.EdOrgs, x => x.EdOrgs = It.IsAny <DbSet <EdOrg> >(), allEdOrgs); var edOrgService = new EdOrgService( DbContextFactoryMock.Object, AppUserServiceMock.Object, SchoolYearServiceMock.Object, SchoolYearDbContextFactoryMock.Object, LoggingServiceMock.Object); var result = edOrgService.GetAllEdOrgs(); result.ShouldHaveSameItems(allEdOrgs); }
public void UpdateFocusedEdOrg_Should_FailSilentlyBecauseItJustDoes() { var authorizedEdOrgId = 12345; DefaultTestAppUserSession.UserIdentity.AuthorizedEdOrgs.Add(new EdOrg { Id = authorizedEdOrgId }); var announcements = new List <Announcement>(); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(announcements), ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); var oldFocusedEdOrg = 12345; var newFocusedEdOrg = 23456; var randomAuthorizedEdOrg = newFocusedEdOrg + 1; var appUserSessionId = 123; var userIdentity = new ValidationPortalIdentity { AuthorizedEdOrgs = new List <EdOrg>(new[] { new EdOrg { Id = randomAuthorizedEdOrg } }) }; var appUserSession = new AppUserSession { Id = appUserSessionId.ToString(), FocusedEdOrgId = oldFocusedEdOrg, UserIdentity = userIdentity }; DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var httpContext = new HttpContext( new HttpRequest(string.Empty, "http://wearedoubleline.com", string.Empty), new HttpResponse(new StringWriter())); httpContext.Items[AppUserService.SessionItemName] = appUserSession; HttpContextProviderMock.SetupGet(x => x.CurrentHttpContext).Returns(httpContext); LoggingServiceMock.Setup(x => x.LogDebugMessage(It.IsAny <string>())); var appUserService = new AppUserService( DbContextFactoryMock.Object, HttpContextProviderMock.Object, LoggingServiceMock.Object); appUserService.UpdateFocusedEdOrg(newFocusedEdOrg.ToString()); ValidationPortalDbContextMock.Verify(x => x.SaveChanges(), Times.Never); }
public void GetAnnouncements_Should_ReturnAll() { AppUserServiceMock.Setup(x => x.GetSession()).Returns(DefaultTestAppUserSession); // authorized ed orgs should match limited-to ed orgs var authorizedEdOrgId = 12345; DefaultTestAppUserSession.UserIdentity.AuthorizedEdOrgs.Add(new EdOrg { Id = authorizedEdOrgId }); var announcementIdToDismiss = 2345; var announcementIdToKeep = 3456; var announcements = new List <Announcement>( new[] { new Announcement { Id = announcementIdToKeep, Message = "test contact info" }, new Announcement { Id = announcementIdToDismiss, Message = "dismiss me" } }); EntityFrameworkMocks.SetupMockDbSet <ValidationPortalDbContext, Announcement>( EntityFrameworkMocks.GetQueryableMockDbSet(announcements), ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var announcementService = new AnnouncementService(DbContextFactoryMock.Object, AppUserServiceMock.Object, LoggingServiceMock.Object); var result = announcementService.GetAnnouncements(); result.ShouldNotBeNull(); result.ShouldNotBeEmpty(); result.ShouldContain(announcements.First(x => x.Id == announcementIdToKeep)); result.ShouldContain(announcements.First(x => x.Id == announcementIdToDismiss)); }
public void GetAnnouncement_Should_LogAndThrowOnNonexistentId() { AppUserServiceMock.Setup(x => x.GetSession()).Returns(DefaultTestAppUserSession); LoggingServiceMock.Setup(x => x.LogErrorMessage(It.IsAny <string>())); // authorized ed orgs should match limited-to ed orgs var authorizedEdOrgId = 12345; DefaultTestAppUserSession.UserIdentity.AuthorizedEdOrgs.Add(new EdOrg { Id = authorizedEdOrgId }); var announcementIdToReturn = 2345; var announcementIdToNotReturn = 3456; var announcementIdThatsBogus = 4567; var announcements = new List <Announcement>( new[] { new Announcement { Id = announcementIdToReturn, Message = "return me" }, new Announcement { Id = announcementIdToNotReturn, Message = "leave me out" } }); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(announcements), ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var announcementService = new AnnouncementService(DbContextFactoryMock.Object, AppUserServiceMock.Object, LoggingServiceMock.Object); Assert.Throws <Exception>(() => announcementService.GetAnnouncement(announcementIdThatsBogus)); LoggingServiceMock.Verify(x => x.LogErrorMessage(It.IsAny <string>())); }
public void GetAnnouncement_Should_ReturnOneAnnouncement() { AppUserServiceMock.Setup(x => x.GetSession()).Returns(DefaultTestAppUserSession); // authorized ed orgs should match limited-to ed orgs var authorizedEdOrgId = 12345; DefaultTestAppUserSession.UserIdentity.AuthorizedEdOrgs.Add(new EdOrg { Id = authorizedEdOrgId }); var announcementIdToReturn = 2345; var announcementIdToNotReturn = 3456; var announcements = new List <Announcement>( new[] { new Announcement { Id = announcementIdToReturn, Message = "return me", }, new Announcement { Id = announcementIdToNotReturn, Message = "leave me out", } }); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(announcements), ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var announcementService = new AnnouncementService(DbContextFactoryMock.Object, AppUserServiceMock.Object, LoggingServiceMock.Object); var result = announcementService.GetAnnouncement(announcementIdToReturn); result.ShouldNotBeNull(); result.Id.ShouldEqual(announcementIdToReturn); }
public void UpdateFocusedEdOrg_Should_LogException() { var announcements = new List <Announcement>(); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(announcements), ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); var oldFocusedEdOrg = 12345; var newFocusedEdOrg = 23456; var appUserSessionId = 123; var appUserSession = new AppUserSession { Id = appUserSessionId.ToString(), FocusedEdOrgId = oldFocusedEdOrg, UserIdentity = null }; DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var httpContext = new HttpContext( new HttpRequest(string.Empty, "http://wearedoubleline.com", string.Empty), new HttpResponse(new StringWriter())); httpContext.Items[AppUserService.SessionItemName] = appUserSession; HttpContextProviderMock.SetupGet(x => x.CurrentHttpContext).Returns(httpContext); LoggingServiceMock.Setup(x => x.LogDebugMessage(It.IsAny <string>())); LoggingServiceMock.Setup(x => x.LogErrorMessage(It.IsAny <string>())); var appUserService = new AppUserService( DbContextFactoryMock.Object, HttpContextProviderMock.Object, LoggingServiceMock.Object); appUserService.UpdateFocusedEdOrg(newFocusedEdOrg.ToString()); ValidationPortalDbContextMock.Verify(x => x.SaveChanges(), Times.Never); LoggingServiceMock.Verify(x => x.LogErrorMessage(It.IsAny <string>()), Times.Once()); }
public void SetUp() { var logs = new List <Log>(); IdentityLogs = new List <Log>(); for (var i = 0; i < 10; i++) { IdentityLogs.Add( new Log { Id = i, Url = new Uri($"https://test.wearedoubleline.com/{EdFiApiLogService.ApiName}/identity/").ToString(), Method = $"method {i}" }); } logs.AddRange(IdentityLogs); ApiLogs = new List <Log>(); for (var i = 10; i < 20; i++) { ApiLogs.Add( new Log { Id = i, Url = new Uri($"https://test.wearedoubleline.com/{EdFiApiLogService.ApiName}/data/v3/").ToString(), Method = $"method {i}" }); } logs.AddRange(ApiLogs); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(logs), DbContextMock, x => x.Logs, x => x.Logs = It.IsAny <DbSet <Log> >(), logs); DbContextMock.As <IDisposable>().Setup(x => x.Dispose()); DbContextFactoryMock.Setup(x => x.Create()).Returns(DbContextMock.Object); }
public void SaveNewAnnouncement_Should_SaveAnnouncement() { var announcement = new Announcement { Id = 0, Priority = 1, Message = "test message", ContactInfo = "3-2-1, contact", LinkUrl = "http://wearedoubleline.com", Expiration = DateTime.Now.AddMonths(1) }; var announcements = new List <Announcement>(new[] { announcement }); var dbSetMock = EntityFrameworkMocks.GetQueryableMockDbSet(announcements); EntityFrameworkMocks.SetupMockDbSet <ValidationPortalDbContext, Announcement>( dbSetMock, ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); var announcementService = new AnnouncementService( DbContextFactoryMock.Object, AppUserServiceMock.Object, LoggingServiceMock.Object); announcementService.SaveAnnouncement( announcement.Id, announcement.Priority, announcement.Message, announcement.ContactInfo, announcement.LinkUrl, announcement.Expiration); dbSetMock.Verify(x => x.Add(It.Is <Announcement>(y => y.Id == announcement.Id)), Times.Once); ValidationPortalDbContextMock.Verify(x => x.SaveChanges(), Times.Once); }
public void SaveExistingAnnouncement_Should_ThrowIfNotExistsAndLogError() { var announcement = new Announcement { Id = 1234, Priority = 1, Message = "test message", ContactInfo = "3-2-1, contact", LinkUrl = "http://wearedoubleline.com", Expiration = DateTime.Now.AddMonths(1) }; var announcements = new List <Announcement>(new[] { announcement }); DbContextFactoryMock.Setup(x => x.Create()).Returns(ValidationPortalDbContextMock.Object); EntityFrameworkMocks.SetupMockDbSet <ValidationPortalDbContext, Announcement>( EntityFrameworkMocks.GetQueryableMockDbSet(announcements), ValidationPortalDbContextMock, x => x.Announcements, x => x.Announcements = It.IsAny <DbSet <Announcement> >(), announcements); LoggingServiceMock.Setup(x => x.LogErrorMessage(It.IsAny <string>())); var announcementService = new AnnouncementService(DbContextFactoryMock.Object, AppUserServiceMock.Object, LoggingServiceMock.Object); Assert.Throws <Exception>(() => announcementService.SaveAnnouncement( announcement.Id + 1, announcement.Priority, announcement.Message, announcement.ContactInfo, announcement.LinkUrl, announcement.Expiration)); }
public void SetUp() { LoggingServiceMock.Setup(x => x.LogDebugMessage(It.IsAny <string>())); LoggingServiceMock.Setup(x => x.LogErrorMessage(It.IsAny <string>())); LoggingServiceMock.Setup(x => x.LogWarningMessage(It.IsAny <string>())); LoggingServiceMock.Setup(x => x.LogInfoMessage(It.IsAny <string>())); EntityFrameworkMocks.SetupValidationPortalDbContext(ValidationPortalDbContextMock); DefaultTestAppUserSession = new AppUserSession { FocusedEdOrgId = 1234, FocusedSchoolYearId = 1, Id = "234", ExpiresUtc = DateTime.Now.AddMonths(1), UserIdentity = new ValidationPortalIdentity { AuthorizedEdOrgs = new List <EdOrg>() }, }; var appUserSessions = new List <AppUserSession>(new[] { DefaultTestAppUserSession }); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(appUserSessions), ValidationPortalDbContextMock, x => x.AppUserSessions, x => x.AppUserSessions = It.IsAny <DbSet <AppUserSession> >(), appUserSessions); DbContextFactoryMock .Setup(x => x.Create()) .Returns(ValidationPortalDbContextMock.Object); OdsConfigurationValuesMock .Setup(x => x.GetRawOdsConnectionString(It.IsAny <string>())) .Returns <string>(x => $"Test Connection string: EdFi_Ods_{x};"); var ruleValidations = new List <RuleValidation>(); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(ruleValidations), SchoolYearDbContextMock, x => x.RuleValidations, x => x.RuleValidations = It.IsAny <DbSet <RuleValidation> >(), ruleValidations); var ruleValidationDetails = new List <RuleValidationDetail>(); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(ruleValidationDetails), SchoolYearDbContextMock, x => x.RuleValidationDetails, x => x.RuleValidationDetails = It.IsAny <DbSet <RuleValidationDetail> >(), ruleValidationDetails); var ruleValidationRuleComponents = new List <RuleValidationRuleComponent>(); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(ruleValidationRuleComponents), SchoolYearDbContextMock, x => x.RuleValidationRuleComponents, x => x.RuleValidationRuleComponents = It.IsAny <DbSet <RuleValidationRuleComponent> >(), ruleValidationRuleComponents); SchoolYearDbContextMock.As <IDisposable>().Setup(x => x.Dispose()); SchoolYearDbContextFactoryMock .Setup(x => x.CreateWithParameter(It.IsAny <string>())) .Returns(SchoolYearDbContextMock.Object); }
public void GetEdOrgById_Should_ReturnSpecifiedEdOrgIfPresent() { const int testEdOrgId = 12345; const int testSchoolYearId = 111; const string testSchoolName = "12345 Elementary"; var allEdOrgs = new List <EdOrg> { new EdOrg { Id = testEdOrgId, SchoolYearId = testSchoolYearId, OrganizationName = testSchoolName, OrganizationShortName = testSchoolName }, new EdOrg { Id = 23456 }, new EdOrg { Id = 34567 } }; EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(allEdOrgs), ValidationPortalDbContextMock, x => x.EdOrgs, x => x.EdOrgs = It.IsAny <DbSet <EdOrg> >(), allEdOrgs); var testSchoolYear = new SchoolYear { Id = testSchoolYearId, StartYear = "1776", EndYear = "1777", Enabled = true }; var schoolYears = new List <SchoolYear>(new[] { testSchoolYear }); EntityFrameworkMocks.SetupMockDbSet( EntityFrameworkMocks.GetQueryableMockDbSet(schoolYears), ValidationPortalDbContextMock, x => x.SchoolYears, x => x.SchoolYears = It.IsAny <DbSet <SchoolYear> >(), schoolYears); var edOrgService = new EdOrgService( DbContextFactoryMock.Object, AppUserServiceMock.Object, SchoolYearServiceMock.Object, SchoolYearDbContextFactoryMock.Object, LoggingServiceMock.Object); SchoolYearServiceMock.Setup(x => x.GetSchoolYearById(testSchoolYearId)).Returns(testSchoolYear); LoggingServiceMock.Setup(x => x.LogDebugMessage($"EdOrg cache: {allEdOrgs.Count} currently in ValidationPortal database")); var result = edOrgService.GetEdOrgById(testEdOrgId, testSchoolYearId); result.ShouldNotBeNull(); }