예제 #1
0
        public void Setup()
        {
            mockHttpContextAccessor = new Mock <IHttpContextAccessor>();
            mockHttpContextAccessor
            .Setup(x => x.HttpContext)
            .Returns(new DefaultHttpContext());

            mockResolverContext = new Mock <IResolverContext>();

            var options = new DbContextOptionsBuilder <MailyContext>()
                          .UseInMemoryDatabase("Maily")
                          .Options;

            context = new MailyContext(options);
            context.Database.EnsureDeleted();

            var hasher = new Hasher();

            tokenizer = new Tokenizer(context, mockHttpContextAccessor.Object, hasher);
            mockHttpContextAccessor.Object.HttpContext.Request.Headers["Authorization"] = "sampletoken";
            mutation = new UserMutation(context, tokenizer, new Hasher());

            var user = context.Users.Add(new User()
            {
                Username = "******",
                Password = hasher.CreateHash("samplepassword"),
                Token    = "sampletoken"
            }).Entity;

            context.SaveChanges();
        }
예제 #2
0
        public void Setup()
        {
            mockHttpContextAccessor = new Mock <IHttpContextAccessor>();
            mockHttpContextAccessor
            .Setup(x => x.HttpContext)
            .Returns(new DefaultHttpContext());

            var options = new DbContextOptionsBuilder <MailyContext>()
                          .UseInMemoryDatabase("Maily")
                          .Options;

            context = new MailyContext(options);
            context.Database.EnsureDeleted();

            tokenizer = new Tokenizer(context, mockHttpContextAccessor.Object, new Hasher());
            mockHttpContextAccessor.Object.HttpContext.Request.Headers["Authorization"] = "sampletoken";
            mutation = new MailMutation(context, tokenizer);

            var user = context.Users.Add(new User()
            {
                Token = "sampletoken"
            }).Entity;

            var mail = context.Mails.Add(new Mail()
            {
                User  = user,
                Value = "*****@*****.**"
            });

            context.SaveChanges();
        }
예제 #3
0
        /// <summary>
        /// Create and store an instance of <see cref="User"/>.
        /// </summary>
        /// <param name="nickname">Nickname of the user.</param>
        /// <param name="username">A unique username of the user.</param>
        /// <param name="password">An unhashed password.</param>
        /// <param name="resolverContext">Resolver context used in error reporting.</param>
        /// <returns>An instance of proxy class <see cref="UserSignUpPayload"/> containing some of user fields.</returns>
        public UserSignUpPayload SignUp(string nickname, string username, string password, IResolverContext resolverContext)
        {
            if (nickname == null || username == null || password == null)
            {
                return(null);
            }

            if (_context.Users.Any(x => x.Username == username))
            {
                resolverContext.ReportError(ErrorBuilder.New()
                                            .SetCode(UserSignUpErrorCode.UsernameNotUnique.ToString())
                                            .SetMessage("Username is not unique!")
                                            .Build());

                return(null);
            }

            var user = new User()
            {
                Nickname = nickname,
                Username = username,
                Password = _hasher.CreateHash(password)
            };

            // Add and save user to generate Id
            _context.Add(user);
            _context.SaveChanges();

            // Id and Username to ensures unique hash
            user.Token = _tokenizer.CreateToken(user.Id + ";" + user.Username);

            _context.Update(user);
            _context.SaveChanges();

            return(new UserSignUpPayload()
            {
                Id = user.Id,
                Nickname = user.Nickname,
                Token = user.Token
            });
        }
예제 #4
0
        /// <summary>
        /// Attach mail to a mail group.
        /// </summary>
        /// <param name="mailId">Id of mail to attach.</param>
        /// <param name="mailGroupId">Id of group to attach to.</param>
        /// <returns>Associate entry of mail attached to mail group.</returns>
        public MailGroupMail AddMailToMailGroup(int mailId, int mailGroupId)
        {
            var mail      = _context.Mails.SingleOrDefault(x => x.Id == mailId);
            var mailGroup = _context.MailGroups.SingleOrDefault(x => x.Id == mailGroupId);

            if (!isRequestValid(mail, mailGroup))
            {
                return(null);
            }

            var mailGroupMail = new MailGroupMail()
            {
                MailId      = mailId,
                MailGroupId = mailGroupId
            };

            _context.Add(mailGroupMail);
            _context.SaveChanges();

            return(mailGroupMail);
        }
예제 #5
0
        public void Setup()
        {
            mockHttpContextAccessor = new Mock <IHttpContextAccessor>();
            mockHttpContextAccessor.Setup(x => x.HttpContext).Returns(new DefaultHttpContext());

            var options = new DbContextOptionsBuilder <MailyContext>()
                          .UseInMemoryDatabase("Maily")
                          .Options;

            context = new MailyContext(options);
            context.Database.EnsureDeleted();

            tokenizer = new Tokenizer(context, mockHttpContextAccessor.Object, new Hasher());

            var user = context.Users.Add(new User()
            {
                Token = "sampletoken"
            }).Entity;

            context.SaveChanges();
        }