コード例 #1
0
ファイル: RegisterHandlerTests.cs プロジェクト: montr/montr
        public async Task Handle_NormalValues_RegisterUser()
        {
            // arrange
            var cancellationToken = new CancellationToken();
            var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory();
            var dbContextFactory  = new DefaultDbContextFactory();

            var appOptionsAccessorMock = new Mock <IOptionsMonitor <AppOptions> >();

            appOptionsAccessorMock.Setup(x => x.CurrentValue)
            .Returns(() => new AppOptions {
                AppUrl = "https://app.montr.net"
            });
            var appOptionsAccessor = appOptionsAccessorMock.Object;

            var identityServiceFactory = new IdentityServiceFactory();

            var classifierRepositoryFactoryBuilder = new ClassifierRepositoryFactoryBuilder(dbContextFactory)
            {
                UserTypeCode = ClassifierTypeCode.User
            };
            var classifierRepositoryFactory = classifierRepositoryFactoryBuilder.Build();

            // todo: test EmailConfirmationService

            /*
             * var dbMessageTemplateRepository = new DbMessageTemplateRepository(dbContextFactory);
             * var templateRenderer = new MustacheTemplateRenderer(dbMessageTemplateRepository);
             * var emailConfirmationService = new EmailConfirmationService(userManager, appUrlBuilder, new Mock<IEmailSender>().Object, templateRenderer);
             */

            var appUrlBuilder = new DefaultAppUrlBuilder(appOptionsAccessor);
            var emailConfirmationServiceMock = new Mock <IEmailConfirmationService>();

            var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory);

            var handler = new RegisterHandler(new NullLogger <RegisterHandler>(), classifierRepositoryFactory,
                                              identityServiceFactory.UserManager, identityServiceFactory.SignInManager, appUrlBuilder, emailConfirmationServiceMock.Object);

            using (var _ = unitOfWorkFactory.Create())
            {
                await generator.EnsureClassifierTypeRegistered(Numerator.GetDefaultMetadata(), cancellationToken);

                await generator.EnsureClassifierTypeRegistered(User.GetDefaultMetadata(), cancellationToken);

                // act
                var command = new Register
                {
                    Email    = "*****@*****.**",
                    Password = Guid.NewGuid().ToString()
                };

                var result = await handler.Handle(command, cancellationToken);

                // assert
                Assert.IsTrue(result.Success);
            }
        }
コード例 #2
0
        // todo: use numerator repository (?)
        public async Task <ApiResult> InsertNumerator(Numerator numerator, GenerateNumberRequest request, CancellationToken cancellationToken)
        {
            var type = await EnsureClassifierTypeRegistered(Numerator.GetDefaultMetadata(), cancellationToken);

            var numeratorUid = Guid.NewGuid();

            using (var db = _dbContextFactory.Create())
            {
                await db.GetTable <DbClassifier>()
                .Value(x => x.Uid, numeratorUid)
                .Value(x => x.TypeUid, type.Uid)
                .Value(x => x.StatusCode, ClassifierStatusCode.Active)
                .Value(x => x.Code, numeratorUid.ToString().Substring(0, 32))
                .Value(x => x.Name, numerator.Name ?? "Test numerator")
                .Value(x => x.IsActive, true)
                .Value(x => x.IsSystem, false)
                .InsertAsync(cancellationToken);

                await db.GetTable <DbNumerator>()
                .Value(x => x.Uid, numeratorUid)
                .Value(x => x.EntityTypeCode, request.EntityTypeCode)
                .Value(x => x.Pattern, numerator.Pattern ?? Numerator.DefaultPattern)
                .Value(x => x.Periodicity, numerator.Periodicity.ToString())
                .Value(x => x.KeyTags, numerator.KeyTags != null ? string.Join(DbNumerator.KeyTagsSeparator, numerator.KeyTags) : null)
                .InsertAsync(cancellationToken);

                await db.GetTable <DbNumeratorEntity>()
                .Value(x => x.IsAutoNumbering, true)
                .Value(x => x.NumeratorUid, numeratorUid)
                .Value(x => x.EntityUid, request.EntityTypeUid)
                .InsertAsync(cancellationToken);

                return(new ApiResult {
                    Uid = numeratorUid
                });
            }
        }
コード例 #3
0
        public async Task CreateCompany_Should_CreateCompany()
        {
            // arrange
            var cancellationToken = new CancellationToken();
            var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory();
            var dbContextFactory  = new DefaultDbContextFactory();
            var dateTimeProvider  = new DefaultDateTimeProvider();

            var mediatorMock = new Mock <IPublisher>();

            var fieldProviderRegistry = new DefaultFieldProviderRegistry();

            fieldProviderRegistry.AddFieldType(typeof(TextField));

            // var dbFieldMetadataRepository = new DbFieldMetadataRepository(dbContextFactory, fieldProviderRegistry, new NewtonsoftJsonSerializer());
            var dbFieldDataRepository       = new DbFieldDataRepository(dbContextFactory, fieldProviderRegistry);
            var classifierRepositoryFactory = CreateClassifierRepositoryFactory(dbContextFactory);
            var dbNumberGenerator           = new DbNumberGenerator(dbContextFactory, classifierRepositoryFactory, dateTimeProvider, Array.Empty <INumberTagResolver>());
            var dbDocumentService           = new DbDocumentService(dbContextFactory, dbNumberGenerator, mediatorMock.Object);
            var jsonSerializer        = new DefaultJsonSerializer();
            var auditLogService       = new DbAuditLogService(dbContextFactory, jsonSerializer);
            var classifierRegistrator = new DefaultClassifierRegistrator(NullLogger <DefaultClassifierRegistrator> .Instance, classifierRepositoryFactory);

            var metadataRepositoryMock = new Mock <IRepository <FieldMetadata> >();

            metadataRepositoryMock
            .Setup(x => x.Search(It.IsAny <MetadataSearchRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => new SearchResult <FieldMetadata>
            {
                Rows = new FieldMetadata[]
                {
                    new TextField {
                        Key = "test1", Active = true, System = false
                    },
                    new TextField {
                        Key = "test2", Active = true, System = false
                    },
                    new TextField {
                        Key = "test3", Active = true, System = false
                    }
                }
            });

            var backgroundJobManagerMock = new Mock <IBackgroundJobManager>();

            var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory);

            var handler = new CreateCompanyHandler(unitOfWorkFactory,
                                                   dbContextFactory, dateTimeProvider, metadataRepositoryMock.Object,
                                                   dbFieldDataRepository, classifierRepositoryFactory, dbDocumentService, auditLogService, backgroundJobManagerMock.Object);

            using (var _ = unitOfWorkFactory.Create())
            {
                await generator.EnsureClassifierTypeRegistered(Numerator.GetDefaultMetadata(), cancellationToken);

                await generator.EnsureClassifierTypeRegistered(DocumentType.GetDefaultMetadata(), cancellationToken);

                foreach (var classifier in RegisterClassifiersStartupTask.GetClassifiers())
                {
                    await classifierRegistrator.Register(classifier, cancellationToken);
                }

                // act
                var company = new Company
                {
                    ConfigCode = "company",
                    Name       = "Montr Inc.",
                    Fields     = new FieldData
                    {
                        { "test1", "value1" },
                        { "test2", "value2" },
                        { "test3", "value3" }
                    }
                };

                var command = new CreateCompany
                {
                    UserUid = Guid.NewGuid(),
                    Item    = company
                };

                var result = await handler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Success);
                Assert.IsNotNull(result.Uid);
                Assert.AreNotEqual(Guid.Empty, result.Uid);

                // assert registration request inserted
                DbDocument dbDocument;
                using (var db = dbContextFactory.Create())
                {
                    dbDocument = await db.GetTable <DbDocument>()
                                 .Where(x => x.CompanyUid == result.Uid)
                                 .SingleAsync(cancellationToken);
                }

                Assert.IsNotNull(dbDocument);

                // assert registration request field data inserted
                IList <DbFieldData> fieldData;
                using (var db = dbContextFactory.Create())
                {
                    fieldData = await db.GetTable <DbFieldData>()
                                .Where(x => x.EntityTypeCode == Document.TypeCode && x.EntityUid == dbDocument.Uid)
                                .ToListAsync(cancellationToken);
                }

                Assert.AreEqual(company.Fields.Count, fieldData.Count);
                Assert.AreEqual(company.Fields["test1"], fieldData.Single(x => x.Key == "test1").Value);
                Assert.AreEqual(company.Fields["test2"], fieldData.Single(x => x.Key == "test2").Value);
                Assert.AreEqual(company.Fields["test3"], fieldData.Single(x => x.Key == "test3").Value);
            }
        }
コード例 #4
0
 protected static IEnumerable <RegisterClassifierType> GetCommands()
 {
     yield return(Numerator.GetDefaultMetadata());
 }