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);
        }
Esempio n. 3
0
        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));
        }
Esempio n. 7
0
        public void UpdateFocusedSchoolYear_Should_UpdateDataContext()
        {
            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 appUserSession = new AppUserSession
            {
                Id = appUserSessionId.ToString(),
                FocusedSchoolYearId = oldFocusedSchoolYear
            };

            var userSessions = new List <AppUserSession>(new[] { appUserSession });

            EntityFrameworkMocks.SetupMockDbSet(
                EntityFrameworkMocks.GetQueryableMockDbSet(userSessions),
                ValidationPortalDbContextMock,
                x => x.AppUserSessions,
                x => x.AppUserSessions = It.IsAny <DbSet <AppUserSession> >(),
                userSessions);

            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.UpdateFocusedSchoolYear(newFocusedSchoolYear);

            ValidationPortalDbContextMock.Verify(x => x.SaveChanges(), Times.Once);
            LoggingServiceMock.Verify(x => x.LogErrorMessage(It.IsAny <string>()), Times.Never);
            ValidationPortalDbContextMock.Object.AppUserSessions.First().FocusedSchoolYearId.ShouldEqual(newFocusedSchoolYear);
        }
Esempio n. 8
0
        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();
        }