示例#1
0
        private async void GetBlockMethodReturnsOkResponse()
        {
            //Arrange
            string         data           = Guid.NewGuid().ToString();
            EncryptService encryptService = new EncryptService();
            string         encryptedPass  = encryptService.EncryptData("1111");
            User           user           = new User("Dimitry", "Sukalo", encryptedPass);

            Block    genesisBlock = new Block();
            UserFile userFile     = new UserFile(data, data, new byte[1]);
            Block    block        = new Block(userFile, user, genesisBlock);

            Mock <IQueryable <Block> > blocks = new List <Block>()
            {
                genesisBlock,
                block
            }.AsQueryable().BuildMock();
            Mock <DbSet <Block> > blockMock = new Mock <DbSet <Block> >();

            SetSettingsInDb(blockMock, blocks);

            Mock <IQueryable <User> > users = new List <User>()
            {
                user
            }.AsQueryable().BuildMock();

            Mock <DbSet <User> > usersMock = new Mock <DbSet <User> >();

            SetSettingsInDb(usersMock, users);

            Mock <BlockChainContext> db = new Mock <BlockChainContext>();

            db.Setup(m => m.Blocks).Returns(blockMock.Object);
            db.Setup(m => m.Users).Returns(usersMock.Object);

            Mock <UserManager <User> > userManagerMock = new Mock <UserManager <User> >(new Mock <IUserStore <User> >().Object,
                                                                                        null, null, null, null, null, null, null, null);

            Mock <IHttpContextAccessor> _contextAccessor = new Mock <IHttpContextAccessor>();

            Mock <IUserClaimsPrincipalFactory <User> > _userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >();
            Mock <SignInManager <User> > signInMock = new Mock <SignInManager <User> >(userManagerMock.Object,
                                                                                       _contextAccessor.Object, _userPrincipalFactory.Object, null, null, null, null);

            Mock <UnitOfWork>     unitMock       = new Mock <UnitOfWork>(db.Object, signInMock.Object, userManagerMock.Object);
            Mock <DecryptService> decryptMock    = new Mock <DecryptService>();
            GetDataService        getDataService = new GetDataService(unitMock.Object, decryptMock.Object);

            //Act
            ResultOfApiRequest <UserFile> result = await getDataService.GetFile(user.Token, "1111", data);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(HttpStatusCode.OK, result?.StatusCode);
        }
示例#2
0
        private async void RegisterMethodReturnsOkStatusCode()
        {
            //Arrange
            EncryptService encryptService = new EncryptService();

            string password = encryptService.EncryptData("1111");

            User user = new User("1111", "1111", "1111")
            {
                UserName = "******"
            };

            Mock <IQueryable <User> > users = new List <User>().AsQueryable().BuildMock();

            Mock <DbSet <User> > usersMock = new Mock <DbSet <User> >();

            SetSettingsInDb(usersMock, users);

            Mock <BlockChainContext> db = new Mock <BlockChainContext>();

            db.Setup(m => m.Users).Returns(usersMock.Object);

            Mock <UserManager <User> > userManagerMock = new Mock <UserManager <User> >(new Mock <IUserStore <User> >().Object,
                                                                                        null, null, null, null, null, null, null, null);

            Mock <IHttpContextAccessor> _contextAccessor = new Mock <IHttpContextAccessor>();

            Mock <IUserClaimsPrincipalFactory <User> > _userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >();
            Mock <SignInManager <User> > signInMock = new Mock <SignInManager <User> >(userManagerMock.Object,
                                                                                       _contextAccessor.Object, _userPrincipalFactory.Object, null, null, null, null);


            userManagerMock.Setup(m => m.CreateAsync(user, password)).ReturnsAsync(IdentityResult.Success);

            Mock <UnitOfWork>     unitMock        = new Mock <UnitOfWork>(db.Object, signInMock.Object, userManagerMock.Object);
            Mock <EncryptService> encryptMock     = new Mock <EncryptService>();
            RegisterService       registerService = new RegisterService(unitMock.Object, encryptMock.Object);

            //Act
            ResultOfApiRequest <User> result = await registerService.RegisterUser(user);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(user.Id, result?.Data?.Id);
            Assert.Equal(user.Name, result?.Data?.Name);
            Assert.Equal(user.Surname, result?.Data?.Surname);
            Assert.Equal(user.Password, result?.Data?.Password);
            Assert.Equal(user.Token, result?.Data?.Token);
            Assert.Equal(HttpStatusCode.OK, result?.StatusCode);
        }
示例#3
0
        private async void AddDataToTheChainReturnsOkRequest()
        {
            //Arrange
            Mock <IQueryable <Block> > blocks    = new List <Block>().AsQueryable().BuildMock();
            Mock <DbSet <Block> >      blockMock = new Mock <DbSet <Block> >();

            SetSettingsInDb(blockMock, blocks);
            EncryptService encryptService = new EncryptService();
            string         encryptedPass  = encryptService.EncryptData("1111");
            User           user           = new User("Dimitry", "Sukalo", encryptedPass);

            Mock <IQueryable <User> > users = new List <User>()
            {
                user
            }.AsQueryable().BuildMock();

            Mock <DbSet <User> > usersMock = new Mock <DbSet <User> >();

            SetSettingsInDb(usersMock, users);

            Mock <BlockChainContext> db = new Mock <BlockChainContext>();

            db.Setup(m => m.Blocks).Returns(blockMock.Object);
            db.Setup(m => m.Users).Returns(usersMock.Object);

            Mock <UserManager <User> > userManagerMock = new Mock <UserManager <User> >(new Mock <IUserStore <User> >().Object,
                                                                                        null, null, null, null, null, null, null, null);

            Mock <IHttpContextAccessor> _contextAccessor = new Mock <IHttpContextAccessor>();

            Mock <IUserClaimsPrincipalFactory <User> > _userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >();
            Mock <SignInManager <User> > signInMock = new Mock <SignInManager <User> >(userManagerMock.Object,
                                                                                       _contextAccessor.Object, _userPrincipalFactory.Object, null, null, null, null);

            Mock <UnitOfWork>      unitMock        = new Mock <UnitOfWork>(db.Object, signInMock.Object, userManagerMock.Object);
            Mock <DecryptService>  decryptMock     = new Mock <DecryptService>();
            Mock <PostDataService> postDataService = new Mock <PostDataService>(unitMock.Object, decryptMock.Object);
            FormFile file = new FormFile(Stream.Null, 1, 1, "testfile", "");

            //Act
            ResultOfApiRequest <string> resultOfApiRequest = await postDataService.Object.AddDataToTheChain(file, user.Token, "1111", true);

            //Assert
            Assert.NotNull(resultOfApiRequest);
            Assert.Equal(HttpStatusCode.OK, resultOfApiRequest?.StatusCode);
        }
示例#4
0
        private void EcnryptDataTest()
        {
            //Arrange
            EncryptService encryptService = new EncryptService();
            string         text           = Guid.NewGuid().ToString();

            byte[] plainTextBytes = Encoding.UTF8.GetBytes(text);
            string encryptedData  = Convert.ToBase64String(plainTextBytes);

            //Act
            string result = encryptService.EncryptData(text);

            //Assert
            Assert.NotNull(result);
            Assert.NotEqual(text, result);
            Assert.Equal(encryptedData, result);
        }
示例#5
0
        private async void LoginServiceReturnsUserIsLoggined()
        {
            //Arrange
            EncryptService encryptService = new EncryptService();
            string         userPass       = "******";
            string         encryptedPass  = encryptService.EncryptData(userPass);
            User           user           = new User("Dimitry", "Sukalo", encryptedPass)
            {
                UserName = "******"
            };

            Mock <IQueryable <User> > users = new List <User>()
            {
                user
            }.AsQueryable().BuildMock();

            Mock <DbSet <User> > usersMock = new Mock <DbSet <User> >();

            SetSettingsInDb(usersMock, users);

            Mock <BlockChainContext> db = new Mock <BlockChainContext>();

            db.Setup(m => m.Users).Returns(usersMock.Object);

            Mock <UserManager <User> > userManagerMock = new Mock <UserManager <User> >(new Mock <IUserStore <User> >().Object,
                                                                                        null, null, null, null, null, null, null, null);

            Mock <IHttpContextAccessor> _contextAccessor = new Mock <IHttpContextAccessor>();

            Mock <IUserClaimsPrincipalFactory <User> > _userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >();
            Mock <SignInManager <User> > signInMock = new Mock <SignInManager <User> >(userManagerMock.Object,
                                                                                       _contextAccessor.Object, _userPrincipalFactory.Object, null, null, null, null);

            signInMock.Setup(s => s.PasswordSignInAsync(user.UserName, encryptedPass, true, false)).ReturnsAsync(SignInResult.Success);

            Mock <UnitOfWork>     unitMock    = new Mock <UnitOfWork>(db.Object, signInMock.Object, userManagerMock.Object);
            Mock <DecryptService> decryptMock = new Mock <DecryptService>();
            Mock <LoginService>   loginMock   = new Mock <LoginService>(unitMock.Object, decryptMock.Object);

            //Act
            bool result = await loginMock.Object.LoginUserAsync(user.Token, "1111");

            //Assert
            Assert.True(result);
        }
示例#6
0
        private void GetFilesMethodTests()
        {
            //Arrange
            string         data           = Guid.NewGuid().ToString();
            EncryptService encryptService = new EncryptService();
            string         encryptedPass  = encryptService.EncryptData("1111");
            User           user           = new User("Dimitry", "Sukalo", encryptedPass);

            Block    genesisBlock = new Block();
            UserFile userFile     = new UserFile(data, data, new byte[1]);
            Block    block        = new Block(userFile, user, genesisBlock);

            Mock <IQueryable <Block> > blocks = new List <Block>()
            {
                genesisBlock,
                block
            }.AsQueryable().BuildMock();
            Mock <DbSet <Block> > blockMock = new Mock <DbSet <Block> >();

            SetSettingsInDb(blockMock, blocks);

            Mock <IQueryable <User> > users = new List <User>()
            {
                user
            }.AsQueryable().BuildMock();

            Mock <DbSet <User> > usersMock = new Mock <DbSet <User> >();

            SetSettingsInDb(usersMock, users);

            userFile.Block = block;
            Mock <IQueryable <UserFile> > files = new List <UserFile>()
            {
                userFile
            }.AsQueryable().BuildMock();
            Mock <DbSet <UserFile> > filesMock = new Mock <DbSet <UserFile> >();

            SetSettingsInDb(filesMock, files);

            Mock <BlockChainContext> db = new Mock <BlockChainContext>();

            db.Setup(m => m.Blocks).Returns(blockMock.Object);
            db.Setup(m => m.Users).Returns(usersMock.Object);
            db.Setup(m => m.UserFiles).Returns(filesMock.Object);

            Mock <UserManager <User> > userManagerMock = new Mock <UserManager <User> >(new Mock <IUserStore <User> >().Object,
                                                                                        null, null, null, null, null, null, null, null);

            Mock <IHttpContextAccessor> _contextAccessor = new Mock <IHttpContextAccessor>();

            Mock <IUserClaimsPrincipalFactory <User> > _userPrincipalFactory = new Mock <IUserClaimsPrincipalFactory <User> >();
            Mock <SignInManager <User> > signInMock = new Mock <SignInManager <User> >(userManagerMock.Object,
                                                                                       _contextAccessor.Object, _userPrincipalFactory.Object, null, null, null, null);

            Mock <UnitOfWork>     unitMock    = new Mock <UnitOfWork>(db.Object, signInMock.Object, userManagerMock.Object);
            Mock <EncryptService> encryptMock = new Mock <EncryptService>();

            FileService fileService = new FileService(unitMock.Object, encryptMock.Object);

            //Act
            List <string> result = fileService.GetFiles("1111", user.Token);

            //Assert
            Assert.NotNull(result);
            Assert.NotNull(result?.FirstOrDefault());
        }