public async void PaysBillWithRawId()
        {
            var options = TestUtil.GetMemDbOptions("PaysBillWithRawId");

            const double rate = 1;

            Tenant        tenant;
            BillingPeriod period;

            using (var db = new AptMgmtDbContext(options))
            {
                var repo = (IBill) new BillRepository(db);
                tenant = TestUtil.NewTenant(db);

                TestUtil.UseResource(db, tenant.TenantId, 5, ResourceType.Power);
                TestUtil.UseResource(db, tenant.TenantId, 5, ResourceType.Power);
                period = TestUtil.NewBillingPeriod(db);

                TestUtil.NewResourceRate(db, rate, ResourceType.Power, period);

                var payment = repo.PayBill(tenant.TenantId, 3, ResourceType.Power, period.BillingPeriodId);

                db.SaveChanges();
            }

            using (var db = new AptMgmtDbContext(options))
            {
                var repo = (IBill) new BillRepository(db);
                var bill = await repo.GetBill(tenant.TenantId, ResourceType.Power, period);

                Assert.Equal(3, bill.Paid);
                Assert.Equal(rate * 7, bill.Owed());
                Assert.Equal(rate, bill.Rate);
            }
        }
        public async void BuildsDTO()
        {
            var options = TestUtil.GetMemDbOptions("OpensMaintenanceRequest");

            AptMgmtPortalAPI.DTO.MaintenanceRequestDTO dto;
            AptMgmtPortalAPI.Entity.MaintenanceRequest req;
            using (var db = new AptMgmtDbContext(options))
            {
                var mock = new Mock <ILogger <UserRepository> >();
                ILogger <UserRepository> logger = mock.Object;
                User user = TestUtil.NewUser(db);

                var repo = (IUser) new UserRepository(logger, db);

                req = new AptMgmtPortalAPI.Entity.MaintenanceRequest();
                req.MaintenanceRequestId = 999;
                req.OpeningUserId        = user.UserId;

                db.Add(req);
                db.SaveChanges();

                dto = await AptMgmtPortalAPI.DTO.MaintenanceRequestDTO.Build(req, repo);
            }

            Assert.Equal(req.MaintenanceRequestId, dto.MaintenanceRequestId);
        }
Example #3
0
        public static User NewUser(AptMgmtDbContext context)
        {
            var user = new User();

            context.Add(user);
            context.SaveChanges();
            return(user);
        }
Example #4
0
        public static Tenant NewTenant(AptMgmtDbContext context)
        {
            var tenant = new Tenant();

            context.Add(tenant);
            context.SaveChanges();
            return(tenant);
        }
Example #5
0
        public static Unit NewUnit(AptMgmtDbContext context)
        {
            var unit = new Unit();

            unit.UnitNumber = "3111";
            unit.TenantId   = TestUtil.NewTenant(context).TenantId;
            context.Add(unit);
            context.SaveChanges();
            return(unit);
        }
Example #6
0
        public static Payment NewPayment(AptMgmtDbContext context, int tenantId)
        {
            var payment = new Payment();

            payment.TenantId = tenantId;

            context.Add(payment);
            context.SaveChanges();

            return(payment);
        }
        public async void CancelsMaintenanceRequest()
        {
            var options = TestUtil.GetMemDbOptions("CancelsMaintenanceRequest");

            var                unitNumber = "test unit";
            User               user;
            Tenant             tenant;
            MaintenanceRequest maintRequest;
            Unit               unit;

            using (var db = new AptMgmtDbContext(options))
            {
                user = TestUtil.NewUser(db);
                var maintenanceRepo = (IMaintenance) new MaintenanceRepository(db);

                var requestData = new AptMgmtPortalAPI.DataModel.MaintenanceRequestModel();
                requestData.OpenNotes              = "notes";
                requestData.UnitNumber             = unitNumber;
                requestData.MaintenanceRequestType = MaintenanceRequestType.Plumbing;

                maintRequest = await maintenanceRepo.OpenMaintenanceRequest(user.UserId, requestData);

                tenant        = TestUtil.NewTenant(db);
                tenant.UserId = user.UserId;
                db.SaveChanges();

                // Maintenance requests are assigned to units, but may be opened by any user of
                // the application. This means the occupying tenant may open a maintenance request,
                // but only if they have a UserId, and a Manager may also open a maintenance request
                // on behalf of a Tenant of User.
                //
                // In order to cancel a maintenance request, we must have a way to determine if
                // the User is indeed the Tenant of the request. This is accomplished by checking
                // if the user is occupying the unit, and if so, they will be permitted to cancel
                // the request.
                var tenantRepo = (ITenant) new TenantRepository(db);
                unit = TestUtil.NewUnit(db, unitNumber);
                await tenantRepo.AssignToUnit(tenant.TenantId, unitNumber);
            }

            using (var db = new AptMgmtDbContext(options))
            {
                var repo     = (IMaintenance) new MaintenanceRepository(db);
                var canceled = await repo.CancelMaintenanceRequest(user.UserId,
                                                                   maintRequest.MaintenanceRequestId,
                                                                   "test cancel");

                Assert.NotNull(canceled.TimeClosed);

                var requests = await repo.GetOpenMaintenanceRequests(unit.UnitNumber);

                Assert.Empty(requests);
            }
        }
Example #8
0
        public static Tenant NewTenant(AptMgmtDbContext context)
        {
            var tenant = new Tenant();

            tenant.FirstName = "testFirstName";
            tenant.Email     = "*****@*****.**";
            tenant.UserId    = TestUtil.NewUser(context).UserId;
            context.Add(tenant);
            context.SaveChanges();
            return(tenant);
        }
Example #9
0
        public static User NewUserWithAnAPIKey(AptMgmtDbContext context)
        {
            var user = new User
            {
                ApiKey = "test-key100"
            };

            context.Add(user);
            context.SaveChanges();
            return(user);
        }
Example #10
0
        public static Unit NewUnit(AptMgmtDbContext context, string unitNumber)
        {
            var unit = new Unit();

            unit.UnitNumber = unitNumber;

            context.Add(unit);
            context.SaveChanges();

            return(unit);
        }
Example #11
0
        public static AgreementTemplate NewAgreement(AptMgmtDbContext context, string title)
        {
            var agreement = new AgreementTemplate();

            agreement.Title = title;

            context.Add(agreement);
            context.SaveChanges();

            return(agreement);
        }
        public async void GetsPaymentsOfResourceType()
        {
            var options = TestUtil.GetMemDbOptions("GetsPaymentsOfResourceType");

            Tenant        tenant;
            Payment       payment;
            BillingPeriod period;

            using (var db = new AptMgmtDbContext(options))
            {
                period = TestUtil.NewBillingPeriod(db);
                tenant = TestUtil.NewTenant(db);

                var repo = (ITenant) new TenantRepository(db);
                payment = TestUtil.NewPayment(db, tenant.TenantId);
                payment.ResourceType    = ResourceType.Power;
                payment.BillingPeriodId = period.BillingPeriodId;
                payment.Amount          = 12;

                // We will query for power payment (above). This should not appear.
                var waterPayment = TestUtil.NewPayment(db, tenant.TenantId);
                waterPayment.ResourceType    = ResourceType.Water;
                waterPayment.BillingPeriodId = period.BillingPeriodId;
                waterPayment.Amount          = 6;

                // This is a power payment, but is for a different period.
                var outOfPeriodPayment = TestUtil.NewPayment(db, tenant.TenantId);
                outOfPeriodPayment.ResourceType = ResourceType.Power;
                outOfPeriodPayment.Amount       = 10;

                var oldPeriod = TestUtil.NewBillingPeriod(db);
                oldPeriod.PeriodStart = DateTime.Now - new TimeSpan(22, 0, 0, 0);
                oldPeriod.PeriodEnd   = DateTime.Now - new TimeSpan(20, 0, 0, 0);

                outOfPeriodPayment.BillingPeriodId = oldPeriod.BillingPeriodId;

                db.SaveChanges();
            }

            using (var db = new AptMgmtDbContext(options))
            {
                var repo = (IBill) new BillRepository(db);

                var paymentsFromDb = await repo.GetPayments(tenant.TenantId, ResourceType.Power, period);

                Assert.Single(paymentsFromDb);

                var powerPayment = paymentsFromDb.Single();
                Assert.Equal(12, powerPayment.Amount);
            }
        }
Example #13
0
        public static (AptMgmtPortalAPI.DTO.UserDTO, Tenant) UserInfoAndTenantForUserRepo(AptMgmtDbContext context)
        {
            var userInfo = TestUtil.NewUserDtoWithCredential(context);
            var tenant   = new Tenant
            {
                Email     = "testUser",
                UserId    = userInfo.UserId,
                FirstName = "original first name",
            };

            context.Add(tenant);
            context.SaveChanges();
            return(userInfo, tenant);
        }
Example #14
0
        public static BillingPeriod NewBillingPeriod(AptMgmtDbContext context)
        {
            var period   = new BillingPeriod();
            var timeSpan = new TimeSpan(0, 0, 5);

            // All test data is 2 years into the future.
            var testPeriod = new TimeSpan(730, 0, 0, 0);

            period.PeriodStart = DateTime.Now + testPeriod - timeSpan;
            period.PeriodEnd   = DateTime.Now + testPeriod + timeSpan;

            context.Add(period);
            context.SaveChanges();

            return(period);
        }
Example #15
0
        public static ResourceUsageRate NewResourceRate(AptMgmtDbContext context,
                                                        double rate,
                                                        ResourceType resource,
                                                        BillingPeriod period)
        {
            var newRate = new ResourceUsageRate();

            newRate.PeriodStart  = period.PeriodStart;
            newRate.PeriodEnd    = period.PeriodEnd;
            newRate.Rate         = rate;
            newRate.ResourceType = resource;

            context.Add(newRate);
            context.SaveChanges();

            return(newRate);
        }
Example #16
0
        public static Agreement SignAgreement(AptMgmtDbContext context, int agreementId, int tenantId)
        {
            // All test data is 2 years into the future.
            var testPeriod = new TimeSpan(730, 0, 0, 0);

            var signedAgreement = new Agreement();

            signedAgreement.TenantId            = tenantId;
            signedAgreement.AgreementTemplateId = agreementId;
            signedAgreement.SignedDate          = DateTime.Now;
            signedAgreement.StartDate           = DateTime.Now + testPeriod - new TimeSpan(0, 0, 5);
            signedAgreement.EndDate             = DateTime.Now + testPeriod + new TimeSpan(0, 0, 5);

            context.Add(signedAgreement);
            context.SaveChanges();

            return(signedAgreement);
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AptMgmtDbContext context)
        {
            try
            {
                context.Database.Migrate();
                context.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to apply migrations: {e}");
            }

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/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();

            // NOTE: These need to stay in this order.
            app.UseRouting();
            app.UseCors();
            app.UseAuthentication();
            app.UseAuthorization();

            // NOTE: Swagger must come before UseEndpoints and after UseRouting/Authentication/Authorization.
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Apartment Management Portal API v1");
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Example #18
0
        public static TenantResourceUsage UseResource(AptMgmtDbContext context,
                                                      int tenantId,
                                                      double amount,
                                                      ResourceType resourceType)
        {
            // All test data is 2 years into the future.
            var testPeriod       = new TimeSpan(730, 0, 0, 0);
            var consumedResource = new TenantResourceUsage
            {
                SampleTime   = DateTime.Now + testPeriod,
                UsageAmount  = amount,
                ResourceType = resourceType,
                TenantId     = tenantId,
            };

            context.Add(consumedResource);
            context.SaveChanges();

            return(consumedResource);
        }
        public async void GetsProjectedResourceUsages()
        {
            var options = TestUtil.GetMemDbOptions("GetsProjectedResourceUsages");

            var           testPeriod = new TimeSpan(730, 0, 0, 0);
            Tenant        tenant;
            BillingPeriod period;

            using (var db = new AptMgmtDbContext(options))
            {
                var repo = (ITenant) new TenantRepository(db);
                tenant = TestUtil.NewTenant(db);

                TestUtil.UseResource(db, tenant.TenantId, 5, ResourceType.Power);
                TestUtil.UseResource(db, tenant.TenantId, 5, ResourceType.Power);

                period = new BillingPeriod();
                var timeSpan = new TimeSpan(5, 0, 0, 0);

                // All test data is 2 years into the future.
                period.PeriodStart = DateTime.Now + testPeriod - timeSpan;
                period.PeriodEnd   = DateTime.Now + testPeriod + timeSpan;

                db.Add(period);

                db.SaveChanges();

                TestUtil.NewResourceRate(db, 1, ResourceType.Power, period);
                TestUtil.NewResourceRate(db, 2, ResourceType.Water, period);
            }

            using (var db = new AptMgmtDbContext(options))
            {
                var repo      = (IBill) new BillRepository(db);
                var projected = await repo.GetProjectedResourceUsages(tenant.TenantId, period, DateTime.Now + testPeriod);

                projected = projected.ToList();
                // Number will drift slightly due to DateTime.Now calls, round the total to get answer.
                Assert.Equal(18, Math.Round(projected.ElementAt(0).ProjectedCost()));
            }
        }
        public async void AccurateBillNumbers()
        {
            var options = TestUtil.GetMemDbOptions("AccurateBillNumbers");

            const double rate = 1;

            Tenant        tenant;
            BillingPeriod period;

            using (var db = new AptMgmtDbContext(options))
            {
                var repo = (ITenant) new TenantRepository(db);
                tenant = TestUtil.NewTenant(db);

                TestUtil.UseResource(db, tenant.TenantId, 5, ResourceType.Power);
                TestUtil.UseResource(db, tenant.TenantId, 5, ResourceType.Power);
                period = TestUtil.NewBillingPeriod(db);

                TestUtil.NewResourceRate(db, rate, ResourceType.Power, period);

                var payment = TestUtil.NewPayment(db, tenant.TenantId);
                payment.Amount          = 3; // Should still owe 7
                payment.BillingPeriodId = period.BillingPeriodId;
                payment.ResourceType    = ResourceType.Power;
                payment.TenantId        = tenant.TenantId;

                db.SaveChanges();
            }

            using (var db = new AptMgmtDbContext(options))
            {
                var repo = (IBill) new BillRepository(db);
                var bill = await repo.GetBill(tenant.TenantId, ResourceType.Power, period);

                Assert.Equal(3, bill.Paid);
                Assert.Equal(rate * 7, bill.Owed());
                Assert.Equal(rate, bill.Rate);
            }
        }
Example #21
0
        public async void EditsUserInfoWithId()
        {
            var options = TestUtil.GetMemDbOptions("EditsUserInfo");

            var mock = new Mock <ILogger <UserRepository> >();

            AptMgmtPortalAPI.Entity.User user;
            using (var db = new AptMgmtDbContext(options))
            {
                var repo     = (IUser) new UserRepository(mock.Object, db);
                var userInfo = TestUtil.NewUserDtoWithCredential(db);
                var tenant   = new Tenant
                {
                    Email     = "testUser",
                    UserId    = userInfo.UserId,
                    FirstName = "original first name",
                };
                db.Add(tenant);
                db.SaveChanges();

                user = await repo.NewUser(userInfo);
            }

            using (var db = new AptMgmtDbContext(options))
            {
                var repo     = (IUser) new UserRepository(mock.Object, db);
                var newName  = "new first name";
                var userInfo = new AptMgmtPortalAPI.DTO.UserDTO();
                userInfo.FirstName = newName;
                userInfo.LoginName = user.LoginName;
                userInfo.Password  = user.Password;
                var newInfo = await repo.UpdateUserInfo(user.UserId, userInfo);

                Assert.Equal(newName, newInfo.FirstName);
            }
        }