Esempio n. 1
0
        public async Task UpdateOrganisation_BusinessType_Should_Not_Remove_Organisation()
        {
            //Create new user
            var newUser = UserFactory.Create(userId, "testFirst", "testLast", "9999", "*****@*****.**");

            //Create new org
            var country = context.Countries.Single(c => c.IsoAlpha2Code.Equals("gb"));
            var address = TestAddress(country);
            var org     = new Organisation("SFW Ltd", BusinessType.LimitedCompany);

            try
            {
                context.Users.Add(newUser);
                await context.SaveChangesAsync();

                context.Organisations.Add(org);
                await context.SaveChangesAsync();

                //Assign org to user
                newUser.LinkToOrganisation(org);
                await context.SaveChangesAsync();

                //Hold OrgID of newly created entity
                var oldOrgId = org.Id;

                //Update org with change in Business Type
                org = new Organisation("Name Changed", BusinessType.SoleTrader);
                context.Organisations.Add(org);
                await context.SaveChangesAsync();

                //Update user with newly created org2
                var user = await context.Users.SingleAsync(u => u.Id == newUser.Id);

                user.UpdateOrganisationOfUser(org);
                await context.SaveChangesAsync();

                Assert.True(user.Organisation.Id == org.Id);

                //Both Orgs should have different OrgIds
                Assert.False(oldOrgId == org.Id);

                //Check if old org exists
                var oldExists = context.Organisations.Any(x => x.Id == oldOrgId);
                Assert.True(oldExists);

                //Check if new org exists
                var newExists = context.Organisations.Any(x => x.Id == org.Id);
                Assert.True(newExists);
            }
            finally
            {
                context.DeleteOnCommit(org);

                context.Entry(newUser).State = EntityState.Deleted;
                context.SaveChanges();
            }
        }
Esempio n. 2
0
        public async Task HandleAsync(AllTransitStatesInEUEvent @event)
        {
            if (@event.TransportRoute.EntryCustomsOffice != null)
            {
                context.DeleteOnCommit(@event.TransportRoute.EntryCustomsOffice);
            }

            if (@event.TransportRoute.ExitCustomsOffice != null)
            {
                context.DeleteOnCommit(@event.TransportRoute.ExitCustomsOffice);
            }

            await context.SaveChangesAsync();
        }
Esempio n. 3
0
        public async Task RemoveSharedUser(Guid notificationId, Guid sharedId)
        {
            await authorization.EnsureAccessIsOwnerAsync(notificationId);

            var sharedUser = await context.SharedUser.Where(x => x.NotificationId == notificationId && x.Id == sharedId).SingleAsync();

            context.DeleteOnCommit(sharedUser);
        }
Esempio n. 4
0
        public async Task CanAddSpecialHandlingDetails()
        {
            var notification = NotificationApplicationFactory.Create(Guid.NewGuid(), NotificationType.Recovery,
                                                                     UKCompetentAuthority.England, 0);

            notification.SetSpecialHandlingRequirements("keep upright");

            context.NotificationApplications.Add(notification);
            await context.SaveChangesAsync();

            Assert.True(notification.HasSpecialHandlingRequirements);
            Assert.Equal("keep upright", notification.SpecialHandlingDetails);

            context.DeleteOnCommit(notification);
            await context.SaveChangesAsync();
        }
Esempio n. 5
0
 public void Delete(WasteDisposal wasteDisposal)
 {
     context.DeleteOnCommit(wasteDisposal);
 }
Esempio n. 6
0
        public async Task GetNotificationByIdChecksAuthorization()
        {
            var notification = NotificationApplicationFactory.Create(userId, NotificationType.Recovery,
                                                                     UKCompetentAuthority.England, 20181);

            context.NotificationApplications.Add(notification);
            context.SaveChanges();

            var notificationId = notification.Id;

            var repository = new NotificationApplicationRepository(context,
                                                                   notificationApplicationAuthorization);

            await repository.GetById(notificationId);

            A.CallTo(() => notificationApplicationAuthorization.EnsureAccessAsync(notificationId)).MustHaveHappened();

            context.DeleteOnCommit(notification);
            await context.SaveChangesAsync();
        }
Esempio n. 7
0
 public void Delete(WasteRecovery wasteRecovery)
 {
     context.DeleteOnCommit(wasteRecovery);
 }
Esempio n. 8
0
        public async Task RandomExternalUserAccessThrowsException()
        {
            var notification = NotificationApplicationFactory.Create(Guid.NewGuid(), NotificationType.Recovery,
                                                                     UKCompetentAuthority.England, 20181);
            var aspnetInternalUser = UserFactory.Create(Guid.NewGuid(), "Internal", "Internal Last", "12345",
                                                        "*****@*****.**");
            var aspnetSharedUser = UserFactory.Create(Guid.NewGuid(), "External", "Shared", "12345",
                                                      "*****@*****.**");
            var localArea = new LocalArea(Guid.NewGuid(), "Test Area", (int)UKCompetentAuthority.England);

            context.NotificationApplications.Add(notification);
            context.Users.Add(aspnetInternalUser);
            context.Users.Add(aspnetSharedUser);
            context.LocalAreas.Add(localArea);
            await context.SaveChangesAsync();

            var internalUser = new InternalUser(aspnetInternalUser.Id, "test", UKCompetentAuthority.England,
                                                localArea.Id);
            //Shared user is different to the user context.
            var sharedUser = new SharedUser(notification.Id, aspnetSharedUser.Id, DateTimeOffset.Now);

            context.SharedUser.Add(sharedUser);
            await context.SaveChangesAsync();

            context.InternalUsers.Add(internalUser);
            await context.SaveChangesAsync();

            await Assert.ThrowsAsync <SecurityException>(() => context.GetNotificationApplication(notification.Id));

            context.DeleteOnCommit(internalUser);
            context.DeleteOnCommit(sharedUser);
            await context.SaveChangesAsync();

            context.Entry(aspnetInternalUser).State = EntityState.Deleted;
            context.Entry(aspnetSharedUser).State   = EntityState.Deleted;
            context.Entry(localArea).State          = EntityState.Deleted;
            await context.SaveChangesAsync();

            context.DeleteOnCommit(notification);
            await context.SaveChangesAsync();
        }
Esempio n. 9
0
 private async Task DeleteEntity(Entity entity)
 {
     context.DeleteOnCommit(entity);
     await context.SaveChangesAsync();
 }
Esempio n. 10
0
        /// <summary>
        /// Create a clone of a notification excluding shipment info.
        /// </summary>
        /// <param name="sourceId">The notification id to copy from</param>
        /// <param name="destinationId">The notification id to copy to</param>
        /// <returns>the clone of the notification</returns>
        private async Task <NotificationApplication> CloneNotification(Guid sourceId, Guid destinationId)
        {
            var destination = await context.GetNotificationApplication(destinationId);

            var destinationAssessment = await context.NotificationAssessments.SingleAsync(p => p.NotificationApplicationId == destinationId);

            var destinationFinancialGuarantee = await context.FinancialGuarantees.SingleAsync(fg => fg.NotificationId == destinationId);

            var destinationAnnexCollection = await context.AnnexCollections.SingleAsync(p => p.NotificationId == destinationId);

            var destinationFacilities = await context.Facilities.SingleAsync(f => f.NotificationId == destinationId);

            var destinationCarriers = await context.Carriers.SingleAsync(c => c.NotificationId == destinationId);

            var destinationProducers = await context.Producers.SingleAsync(p => p.NotificationId == destinationId);

            var clone = await GetCopyOfSourceNotification(sourceId);

            var clonedAssessment = await GetCopyOfNotificationAssessment(destinationId);

            copier.CopyNotificationProperties(clone, destination);

            context.NotificationApplications.Add(clone);

            // Remove the destination.
            context.DeleteOnCommit(destinationAssessment);
            context.DeleteOnCommit(destinationFinancialGuarantee);
            context.DeleteOnCommit(destinationAnnexCollection);
            context.DeleteOnCommit(destinationFacilities);
            context.DeleteOnCommit(destinationCarriers);
            context.DeleteOnCommit(destinationProducers);
            await context.SaveChangesAsync();

            context.DeleteOnCommit(destination);
            await context.SaveChangesAsync();

            // Set child objects which have lookup properties. These can't be copied by the
            // main copy process due to detaching the change tracker.
            var source = await context.GetNotificationApplication(sourceId);

            copier.CopyLookupEntities(source, clone);

            // Update foreign key on assessment object
            SetNotificationApplicationIdOnAssessment(clonedAssessment, clone.Id);
            context.NotificationAssessments.Add(clonedAssessment);

            // Add financial guarantee
            context.FinancialGuarantees.Add(new FinancialGuaranteeCollection(clone.Id));

            // Transport route
            await CloneTransportRoute(sourceId, clone.Id, destination.CompetentAuthority);

            await wasteRecoveryCopier.CopyAsync(context, sourceId, clone.Id);

            await exporterCopier.CopyAsync(context, sourceId, clone.Id);

            await importerCopier.CopyAsync(context, sourceId, clone.Id);

            await facilityCopier.CopyAsync(context, sourceId, clone.Id);

            await carrierCopier.CopyAsync(context, sourceId, clone.Id);

            await producerCopier.CopyAsync(context, sourceId, clone.Id);

            await meansOfTransportCopier.CopyAsync(context, sourceId, clone.Id);

            await CloneTechnologyEmployed(sourceId, clone.Id);

            context.AnnexCollections.Add(new AnnexCollection(clone.Id));

            return(clone);
        }
        public async Task InternalUserSameCompetentAuthorityAccess()
        {
            // Setup.
            var notification = NotificationApplicationFactory.Create(Guid.NewGuid(), NotificationType.Recovery,
                                                                     UKCompetentAuthority.England, 20181);
            var aspnetInternalUser = UserFactory.Create(Guid.NewGuid(), "Internal", "Internal Last", "12345",
                                                        "*****@*****.**");
            var localArea = new LocalArea(Guid.NewGuid(), "Test Area", (int)UKCompetentAuthority.England);

            context.NotificationApplications.Add(notification);
            context.Users.Add(aspnetInternalUser);
            context.LocalAreas.Add(localArea);
            await context.SaveChangesAsync();

            var internalUser = new InternalUser(aspnetInternalUser.Id, "test", UKCompetentAuthority.England,
                                                localArea.Id);

            context.InternalUsers.Add(internalUser);
            await context.SaveChangesAsync();

            A.CallTo(() => userContext.UserId).Returns(Guid.Parse(internalUser.UserId));

            var authorization = new NotificationApplicationAuthorization(context, userContext);

            // Assert.
            // There's no assertion for 'does not throw exception' so just executing it as normal.
            await authorization.EnsureAccessAsync(notification.Id);

            // Clear data.
            context.DeleteOnCommit(internalUser);
            await context.SaveChangesAsync();

            context.Entry(aspnetInternalUser).State = EntityState.Deleted;
            context.Entry(localArea).State          = EntityState.Deleted;
            await context.SaveChangesAsync();

            context.DeleteOnCommit(notification);
            await context.SaveChangesAsync();
        }
        public async Task DeleteById(Guid transactionId)
        {
            var transaction = await context.NotificationTransactions.Where(t => t.Id == transactionId).SingleAsync();

            context.DeleteOnCommit(transaction);
        }