Exemplo n.º 1
0
        public async Task TestPostMessageFailed1()
        {
            //MemberIdTo is invalid
            //ReplyIdTo is Invalid

            HellolingoMailMessage model = new HellolingoMailMessage()
            {
                MemberIdTo = 1,
                ReplyTo    = null,
                Text       = "Hello there!"
            };

            int currentUserId = 5;

            var entitytiesMock = new Mock <IHellolingoEntities>();

            entitytiesMock.Setup(e => e.Mails_Insert(It.IsAny <byte?>(), It.IsAny <int?>(), It.IsAny <long?>(), It.IsAny <int?>(), It.IsAny <string>(), It.IsAny <string>()));

            var mailValidatorMock = new Mock <IMailBoxValidator>();

            var controller = new MailBoxController(entitytiesMock.Object, mailValidatorMock.Object);

            controller.User = new GenericPrincipal(new ClaimsIdentity(new[] {
                new Claim(CustomClaimTypes.UserId, currentUserId.ToString())
            }), null);

            mailValidatorMock.Setup(v => v.IsReplyToValid(It.IsAny <int?>(), It.IsAny <int>(), It.IsAny <int>())).Returns(Result <bool> .True);
            mailValidatorMock.Setup(v => v.IsRecipientValid(It.IsAny <int>())).Returns(Result <bool> .True);

            await controller.PostMessage(model);

            await controller.PostMessage(model);

            entitytiesMock.Verify(v => v.Mails_Insert(It.IsAny <byte?>(),
                                                      It.IsAny <int?>(),
                                                      It.IsAny <long?>(),
                                                      It.IsAny <int?>(),
                                                      It.IsAny <string>(),
                                                      It.IsAny <string>()), Times.Exactly(2));
        }
Exemplo n.º 2
0
        public async Task TestPostMessageSuccess()
        {
            //MemberIdTo is valid
            //ReplyIdTo is valid

            HellolingoMailMessage model = new HellolingoMailMessage()
            {
                MemberIdTo = 1,
                ReplyTo    = null,
                Text       = "Hello there!"
            };

            int currentUserId = 5;

            var entitytiesMock = new Mock <IHellolingoEntities>();

            entitytiesMock.Setup(e => e.Mails_Insert(It.IsAny <byte?>(), It.IsAny <int?>(), It.IsAny <long?>(), It.IsAny <int?>(), It.IsAny <string>(), It.IsAny <string>()));

            var mailValidatorMock = new Mock <IMailBoxValidator>();

            var controller = new MailBoxController(entitytiesMock.Object, mailValidatorMock.Object);

            controller.User = new GenericPrincipal(new ClaimsIdentity(new [] {
                new Claim(CustomClaimTypes.UserId, currentUserId.ToString())
            }), null);

            mailValidatorMock.Setup(v => v.IsReplyToValid(It.IsAny <int?>(), It.IsAny <int>(), It.IsAny <int>())).Returns(Result <bool> .True);
            mailValidatorMock.Setup(v => v.IsRecipientValid(It.IsAny <int>())).Returns(Result <bool> .True);

            await controller.PostMessage(model);

            entitytiesMock.Verify(v => v.Mails_Insert(It.Is((byte?reg) => reg == MailRegulationStatuses.PassAndReview),
                                                      It.Is((int?p) => p == currentUserId),
                                                      It.Is((long?p) => p == model.ReplyTo),
                                                      It.Is((int?p) => p == model.MemberIdTo),
                                                      It.Is((string p) => p == null),
                                                      It.Is((string p) => p == model.Text)), Times.Once);
        }
Exemplo n.º 3
0
        public async Task PostMessage(HellolingoMailMessage model)
        {
            if (ModelState.IsValid == false)
            {
                Log.Warn(LogTag.InvalidModelStateReceiveByPostMail, Request, model);
                return;
            }

            var recipientValidation = _mailBoxValidator.IsRecipientValid(model.MemberIdTo);

            Log.Reports(recipientValidation.Reports, Request);
            if (recipientValidation.Value == false)
            {
                return;
            }

            // Nope! You can't email yourself. Sorry!
            var userId = User.Identity.GetClaims().Id;

            if (userId == model.MemberIdTo)
            {
                Log.Error(LogTag.PostMail_SenderCannotMailHimself, Request, new { userId, model });
                return;
            }

            // Protect from reply spoofing
            var replyToValidation = _mailBoxValidator.IsReplyToValid(model.ReplyTo, userId, model.MemberIdTo);

            Log.Reports(replyToValidation.Reports, Request);
            if (replyToValidation.Value == false)
            {
                return;
            }

            // Determine regulation status
            User user = await GetLocalUser();            // _db.AspNetUsers.Find(userId).Users.First();

            var regulationStatus = MailRegulationStatuses.PassAndReview;

            if (model.ReplyTo != null)
            {
                regulationStatus = MailRegulationStatuses.AutoPass;
                var  controlledKeywords = new [] { "facebook", "skype", "whatsapp", "instagram", "snapchat", "+", "@", "wechat", "viber", "telegram", "t e l e", "hangouts", "whats app", "número", "skyoe", "twitter", "numero", "messenger", "number", "00", "kakao", " line", " qq" };
                bool isControlled       = controlledKeywords.Any(word => CultureInfo.InvariantCulture.CompareInfo.IndexOf(model.Text, word, CompareOptions.IgnoreCase) != -1);
                if (isControlled)
                {
                    regulationStatus = MailRegulationStatuses.PassAndReview;
                }
            }
            else
            {
                // Try autopass when no bad keywords are found and length is long enough, but not if the member has sent too many emails
            }

            // Store the mail
            // I'm not sure how using (_db) has benefits?
            using (_db)
                _db.Mails_Insert(regulationStatus: regulationStatus,
                                 fromId: userId,
                                 replyToMail: model.ReplyTo,
                                 toId: model.MemberIdTo,
                                 subject: null,
                                 message: model.Text);
        }