Exemplo n.º 1
0
        public void OverallSpamDetectorStrategy_ShouldNotDetectSpam(string messageContent1, string messageContent2, string messageContent3, string messageContent4, bool isUserSafe)
        {
            // Arrange
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);

            var contexts       = this._antiSpamTestsService.GetDefaultContexts();
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(new List <SmallMessage>
            {
                new SmallMessage(messageContent1, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-20), GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent2, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-16), GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent3, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-15), GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent4, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.UtcNow.AddSeconds(-10), GetMessagesQuery.GET_ALL_SERVERS)
            });
            var overallSpamDetector = OverallSpamDetectorStrategy.GetStrategyWithDefaultDetectors(serverMessages, userSafetyChecker.Object, this._configurationService);

            // Act
            var overallSpamProbability = overallSpamDetector.GetOverallSpamProbability(contexts);

            // Assert
            Assert.That(overallSpamProbability, Is.EqualTo(SpamProbability.None));
        }
Exemplo n.º 2
0
        public SpamProbability GetSpamProbability(bool isUserSafe, params string[] messagesContent)
        {
            // Arrange
            var spamTestsService  = new AntiSpamTestsService();
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);
            var configurationService = new Mock <IConfigurationService>();

            configurationService
            .Setup(x => x.GetConfigurationItem <PercentOfSimilarityBetweenMessagesToSuspectSpam>(It.IsAny <ulong>()))
            .Returns(new PercentOfSimilarityBetweenMessagesToSuspectSpam(GetMessagesQuery.GET_ALL_SERVERS));

            var(request, contexts) = spamTestsService.CreateRequestAndContexts(messagesContent.Last());
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(messagesContent.SkipLast(1).Select(x => new SmallMessage(x, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS)));

            var needsConfiguration = typeof(T).GetConstructors().Any(x => x.GetParameters().Any(x => x.ParameterType == typeof(IConfigurationService)));
            var spamDetector       = needsConfiguration
                ? (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object, configurationService.Object)
                : (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object);

            // Act
            return(spamDetector !.GetSpamProbability(serverMessages, request, contexts));
        }
        public void OverallSpamDetectorStrategy_ShouldNotDetectSpam(string messageContent1, string messageContent2, string messageContent3, string messageContent4, bool isUserSafe)
        {
            // Arrange
            var spamTestsService  = new AntiSpamTestsService();
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);

            var(request, contexts) = spamTestsService.CreateRequestAndContexts(messageContent4);
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(new List <SmallMessage>
            {
                new SmallMessage(messageContent1, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent2, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS),
                new SmallMessage(messageContent3, AntiSpamTestsService.DEFAULT_TEST_USER_ID, DateTime.Now, GetMessagesQuery.GET_ALL_SERVERS)
            });
            var configurationService = new Mock <IConfigurationService>();

            configurationService
            .Setup(x => x.GetConfigurationItem <PercentOfSimilarityBetweenMessagesToSuspectSpam>(It.IsAny <ulong>()))
            .Returns(new PercentOfSimilarityBetweenMessagesToSuspectSpam(GetMessagesQuery.GET_ALL_SERVERS));
            var overallSpamDetector = OverallSpamDetectorStrategy.GetStrategyWithDefaultDetectors(serverMessages, userSafetyChecker.Object, configurationService.Object);

            // Act
            var overallSpamProbability = overallSpamDetector.GetOverallSpamProbability(request, contexts);

            // Assert
            Assert.That(overallSpamProbability, Is.EqualTo(SpamProbability.None));
        }
        public SpamProbability GetSpamProbability(bool isUserSafe, params SmallMessage[] smallMessages)
        {
            // Arrange
            var userSafetyChecker = new Mock <IUserSafetyChecker>();

            userSafetyChecker
            .Setup(x => x.IsUserSafe(AntiSpamTestsService.DEFAULT_TEST_USER_ID, GetMessagesQuery.GET_ALL_SERVERS))
            .Returns(isUserSafe);

            var lastMessage    = smallMessages.Last();
            var contexts       = this._antiSpamTestsService.GetDefaultContexts(lastMessage.UserId, lastMessage.ServerId);
            var serverMessages = new ServerMessagesCacheService();

            serverMessages.OverwriteMessages(smallMessages);

            var needsConfiguration = typeof(T).GetConstructors().Any(x => x.GetParameters().Any(x => x.ParameterType == typeof(IConfigurationService)));
            var spamDetector       = needsConfiguration
                ? (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object, this._configurationService)
                : (T)Activator.CreateInstance(typeof(T), userSafetyChecker.Object);

            // Act
            return(spamDetector !.GetSpamProbability(serverMessages, contexts));
        }