Пример #1
0
        public void SpfRecordsWithSameMessageIdAndDifferentMessageTypeDoesNotRaiseANotificationTest()
        {
            Guid id = Guid.NewGuid();

            SpfRecords spfRecords =
                NotifierTestUtil.CreateSpfRecords(messages: new List <Message>
            {
                new Message(id, "SPF", MessageType.warning, "hello", "markdown")
            });

            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords =
                    NotifierTestUtil.CreateSpfRecords(messages: new List <Message>
                {
                    new Message(id, "SPF", MessageType.error, "world", "markdown")
                })
            };
            SpfRecordsEvaluated message = new SpfRecordsEvaluated(Id, spfRecords, 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);

            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher, removed: false, added: false);

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfAdvisoryAdded> ._, A <string> ._))
            .MustNotHaveHappened();
            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfAdvisoryRemoved> ._, A <string> ._))
            .MustNotHaveHappened();
        }
        public void SpfRecordsAreDifferentTest()
        {
            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords = NotifierTestUtil.CreateSpfRecords("spf1")
            };
            SpfRecordsEvaluated message =
                new SpfRecordsEvaluated(Id, NotifierTestUtil.CreateSpfRecords("spf2"), 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);


            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher, record: true, added: true, removed: true);

            A.CallTo(() => _dispatcher.Dispatch(A <SpfRecordAdded> ._, A <string> ._)).MustHaveHappenedOnceExactly();
            A.CallTo(() => _dispatcher.Dispatch(A <SpfRecordRemoved> ._, A <string> ._)).MustHaveHappenedOnceExactly();

            A.CallTo(() => _dispatcher.Dispatch(A <SpfRecordAdded> .That.Matches(
                                                    _ => _.Records.Count == 1 && _.Records[0].Equals("spf2")),
                                                A <string> ._))
            .MustHaveHappenedOnceExactly();

            A.CallTo(() => _dispatcher.Dispatch(A <SpfRecordRemoved> .That.Matches(
                                                    _ => _.Records.Count == 1 && _.Records[0].Equals("spf1")),
                                                A <string> ._))
            .MustHaveHappenedOnceExactly();
        }
Пример #3
0
 public void Handle(SpfEntityState state, Common.Messaging.Abstractions.Message message)
 {
     foreach (IChangeNotifier changeNotifier in _notifiers)
     {
         changeNotifier.Handle(state, message);
     }
 }
Пример #4
0
        public async void Handle(SpfEntityState state, Message message)
        {
            if (message is SpfRecordsEvaluated evaluated)
            {
                List <SpfRecord> currentRecords = await Process(state.SpfRecords);

                List <SpfRecord> newRecords = await Process(evaluated.Records);

                if (!currentRecords.CollectionEqual(newRecords, new SpfRecordsReferencedEqualityComparer()))
                {
                    List <string> currentReferencedRecords = currentRecords.SelectMany(x => x.RecordsStrings).ToList();
                    List <string> newReferencedRecords     = newRecords.SelectMany(x => x.RecordsStrings).ToList();

                    List <string> removedRecords = currentReferencedRecords.Except(newReferencedRecords).ToList();
                    List <string> addedRecords   = newReferencedRecords.Except(currentReferencedRecords).ToList();

                    if (addedRecords.Any())
                    {
                        _dispatcher.Dispatch(new SpfReferencedRecordAdded(state.Id, addedRecords), _spfEntityConfig.SnsTopicArn);
                    }

                    if (removedRecords.Any())
                    {
                        _dispatcher.Dispatch(new SpfReferencedRecordRemoved(state.Id, removedRecords), _spfEntityConfig.SnsTopicArn);
                    }
                }
            }
        }
Пример #5
0
        public async void Handle(SpfEntityState state, Common.Messaging.Abstractions.Message message)
        {
            if (message is SpfRecordsEvaluated evaluated)
            {
                List <SpfRecord> currentRecords = await Process(state.SpfRecords);

                List <SpfRecord> newRecords = await Process(evaluated.Records);

                List <Message> currentRecordsMessages = currentRecords.SelectMany(x => x.Messages).ToList();
                currentRecordsMessages.AddRange(state.Messages);

                List <Message> newRecordsMessages = newRecords.SelectMany(x => x.Messages).ToList();
                newRecordsMessages.AddRange(evaluated.Messages);

                List <Message> removedMessages = currentRecordsMessages.Except(newRecordsMessages, _messageEqualityComparer).ToList();

                List <Message> addedMessages = newRecordsMessages.Except(currentRecordsMessages, _messageEqualityComparer).ToList();

                if (addedMessages.Any())
                {
                    _dispatcher.Dispatch(new SpfReferencedAdvisoryAdded(state.Id, addedMessages.Select(x => new AdvisoryMessage(x.MessageType, x.Text)).ToList()), _spfEntityConfig.SnsTopicArn);
                }

                if (removedMessages.Any())
                {
                    _dispatcher.Dispatch(new SpfReferencedAdvisoryRemoved(state.Id, addedMessages.Select(x => new AdvisoryMessage(x.MessageType, x.Text)).ToList()), _spfEntityConfig.SnsTopicArn);
                }
            }
        }
Пример #6
0
        public void SpfRecordsWithDifferentReferencesTest()
        {
            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords = NotifierTestUtil.CreateSpfRecords("spf1", terms:
                                                               new List <Term>
                {
                    new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2", terms:
                                                                                                     new List <Term>
                    {
                        new Include(Qualifier.Fail, "spf3", "abc.com", NotifierTestUtil.CreateSpfRecords("spf3"), false)
                    }), false)
                })
            };
            SpfRecordsEvaluated message =
                new SpfRecordsEvaluated(Id, NotifierTestUtil.CreateSpfRecords("spf1", terms: new List <Term>
            {
                new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2"), false)
            }), 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);


            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher, referencedChange: true, removed: true, record: true);

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfReferencedRecordAdded> ._, A <string> ._)).MustNotHaveHappened();

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfReferencedRecordRemoved> .That.Matches(
                                                               _ => _.Records.Count == 1 && _.Records[0].Equals("spf3")),
                                                           A <string> ._))
            .MustHaveHappenedOnceExactly();
        }
        public void SpfRecordsReferencedWithSameMessageTest()
        {
            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords = NotifierTestUtil.CreateSpfRecords("spf1", terms:
                                                               new List <Term>
                {
                    new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2", terms:
                                                                                                     new List <Term>
                    {
                        new Include(Qualifier.Fail, "spf3", "abc.com", NotifierTestUtil.CreateSpfRecords("spf3"), false)
                    }), false)
                })
            };

            SpfRecordsEvaluated message =
                new SpfRecordsEvaluated(Id, NotifierTestUtil.CreateSpfRecords("spf1", terms:
                                                                              new List <Term>
            {
                new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2", terms:
                                                                                                 new List <Term>
                {
                    new Include(Qualifier.Fail, "spf3", "abc.com", NotifierTestUtil.CreateSpfRecords("spf3"), false)
                }), false)
            }), 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);

            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher, referencedChange: true);
        }
 private async Task Insert(SpfEntityState state)
 {
     await MySqlHelper.ExecuteNonQueryAsync(ConnectionString,
                                            @"INSERT INTO `spf_entity`(`id`,`version`,`state`)VALUES(@domain,@version,@state)",
                                            new MySqlParameter("domain", state.Id),
                                            new MySqlParameter("version", state.Version),
                                            new MySqlParameter("state", JsonConvert.SerializeObject(state)));
 }
        public async Task SaveDuplicateEntryThrows()
        {
            SpfEntityState state1 = new SpfEntityState(Id, 1, SpfState.PollPending, DateTime.UtcNow);

            await _dao.Save(state1);

            Assert.ThrowsAsync <InvalidOperationException>(() => _dao.Save(state1));
        }
Пример #10
0
        public async Task SpfRecordsEvaluatedAndRecordsChangedEventsRaisedAndStateUpdated()
        {
            await PurgeAmazonSqsQueue(OutputQueueUrl);
            await TruncateDatabase(ConnectionString);

            SpfRecords spfRecords = CreateSpfRecords();

            SpfEntityState spfEntityState = new SpfEntityState(Id, 1, SpfState.PollPending, DateTime.Now)
            {
                SpfRecords       = spfRecords,
                DnsQueryCount    = 1,
                ElapsedQueryTime = TimeSpan.FromSeconds(1),
                Messages         = new List <Contracts.SharedDomain.Message>(),
            };

            await SetState(ConnectionString, spfEntityState);

            spfRecords.Records[0].Messages
            .Add(new Contracts.SharedDomain.Message(Guid.Empty, MessageSources.SpfEvaluator, MessageType.error,
                                                    "EvaluationError", "markdown"));
            spfRecords.Records[0].Terms[0].Explanation = "Explanation";

            SpfRecordsEvaluated spfRecordsEvaluated = new SpfRecordsEvaluated(Id, spfRecords, 1,
                                                                              TimeSpan.FromSeconds(0), new List <Contracts.SharedDomain.Message>(), DateTime.UtcNow);

            InvokeLambda(spfRecordsEvaluated);

            Message message = await ReadAmazonSqsEvent(OutputQueueUrl, ReadTimeoutSecond);

            Assert.That(message, Is.TypeOf <SpfRecordEvaluationsChanged>());

            SpfRecordEvaluationsChanged spfRecordEvaluationsChanged = message as SpfRecordEvaluationsChanged;

            Assert.That(spfRecordEvaluationsChanged.Records.Records[0].Messages.Count, Is.EqualTo(1));

            Assert.That(spfRecordEvaluationsChanged.Records.Records[0].Messages[0].Text,
                        Is.EqualTo(spfRecords.Records[0].Messages[0].Text));

            Assert.That(spfRecordEvaluationsChanged.Records.Records[0].Terms[0].Explanation,
                        Is.EqualTo(spfRecords.Records[0].Terms[0].Explanation));

            List <SpfEntityState> spfEntityStates = await GetStates(ConnectionString);

            Assert.That(spfEntityStates.Count, Is.EqualTo(1));
            Assert.That(spfEntityStates[0].Id, Is.EqualTo(Id));
            Assert.That(spfEntityStates[0].Version, Is.EqualTo(2));

            Assert.That(spfEntityStates[0].SpfRecords.Records[0].Messages.Count, Is.EqualTo(1));

            Assert.That(spfEntityStates[0].SpfRecords.Records[0].Messages[0].Text,
                        Is.EqualTo(spfRecordsEvaluated.Records.Records[0].Messages[0].Text));

            Assert.That(spfEntityStates[0].SpfRecords.Records[0].Terms[0].Explanation,
                        Is.EqualTo(spfRecordsEvaluated.Records.Records[0].Terms[0].Explanation));
        }
        public async Task GetStateExistsReturnsState()
        {
            SpfEntityState state = new SpfEntityState(Id, 1, SpfState.PollPending, DateTime.UtcNow);

            await Insert(state);

            SpfEntityState stateFromDatabase = await _dao.Get(Id);

            Assert.That(stateFromDatabase.Id, Is.EqualTo(state.Id));
            Assert.That(stateFromDatabase.Version, Is.EqualTo(state.Version));
        }
        public void SpfRecordsAreSameTest()
        {
            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords = NotifierTestUtil.CreateSpfRecords()
            };
            SpfRecordsPolled message =
                new SpfRecordsPolled(Id, NotifierTestUtil.CreateSpfRecords(), 1, TimeSpan.FromSeconds(1));


            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher);
        }
Пример #13
0
        public void SpfRecordsWithSameMessageTest()
        {
            SpfRecords spfRecords =
                NotifierTestUtil.CreateSpfRecords();

            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords = NotifierTestUtil.CreateSpfRecords()
            };
            SpfRecordsEvaluated message = new SpfRecordsEvaluated(Id, spfRecords, 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);

            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher);
        }
        public async Task Save(SpfEntityState state)
        {
            string connectionString = await _connectionInfoAsync.GetConnectionStringAsync();

            string serializedState = JsonConvert.SerializeObject(state);

            int rowsAffected = await MySqlHelper.ExecuteNonQueryAsync(connectionString, SpfEntityDaoResouces.InsertSpfEntity,
                                                                      new MySqlParameter("domain", state.Id),
                                                                      new MySqlParameter("version", state.Version),
                                                                      new MySqlParameter("state", serializedState));

            if (rowsAffected == 0)
            {
                throw new InvalidOperationException(
                          $"Didn't update SpfEntityState because version {state.Version} has already been persisted.");
            }
        }
        public void SpfRecordsReferencedWithSameMessageIdAndDifferentMessageTypeDoesNotRaiseANotificationTest()
        {
            Guid id = Guid.NewGuid();

            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords = NotifierTestUtil.CreateSpfRecords("spf1", terms:
                                                               new List <Term>
                {
                    new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2", terms:
                                                                                                     new List <Term>
                    {
                        new Include(Qualifier.Fail, "spf3", "abc.com",
                                    NotifierTestUtil.CreateSpfRecords("spf3",
                                                                      new List <Message>
                        {
                            new Message(id, "SPF", MessageType.warning, "hello world", "markdown")
                        }), false)
                    }), false)
                })
            };
            SpfRecordsEvaluated message =
                new SpfRecordsEvaluated(Id, NotifierTestUtil.CreateSpfRecords("spf1", terms:
                                                                              new List <Term>
            {
                new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2", terms:
                                                                                                 new List <Term>
                {
                    new Include(Qualifier.Fail, "spf3", "abc.com",
                                NotifierTestUtil.CreateSpfRecords("spf3",
                                                                  new List <Message>
                    {
                        new Message(id, "SPF", MessageType.error, "hello world", "markdown")
                    }), false)
                }), false)
            }), 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);


            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher);

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfReferencedAdvisoryAdded> ._, A <string> ._)).MustNotHaveHappened();
            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfReferencedAdvisoryRemoved> ._, A <string> ._)).MustNotHaveHappened();
        }
        public void SpfRecordsReferencedWithDifferentMessageTest()
        {
            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords = NotifierTestUtil.CreateSpfRecords("spf1", terms:
                                                               new List <Term>
                {
                    new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2", terms:
                                                                                                     new List <Term>
                    {
                        new Include(Qualifier.Fail, "spf3", "abc.com", NotifierTestUtil.CreateSpfRecords("spf3"), false)
                    }), false)
                })
            };
            SpfRecordsEvaluated message =
                new SpfRecordsEvaluated(Id, NotifierTestUtil.CreateSpfRecords("spf1", terms:
                                                                              new List <Term>
            {
                new Include(Qualifier.Fail, "spf2", "abc.com", NotifierTestUtil.CreateSpfRecords("spf2", terms:
                                                                                                 new List <Term>
                {
                    new Include(Qualifier.Fail, "spf3", "abc.com",
                                NotifierTestUtil.CreateSpfRecords("spf3",
                                                                  new List <Message>
                    {
                        new Message(Guid.NewGuid(), "SPF", MessageType.info, "hello world", "markdown")
                    }), false)
                }), false)
            }), 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);


            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher, referencedChange: true, added: true);

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfReferencedAdvisoryAdded> ._, A <string> ._)).MustHaveHappenedOnceExactly();
            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfReferencedAdvisoryRemoved> ._, A <string> ._)).MustNotHaveHappened();

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfReferencedAdvisoryAdded> .That.Matches(
                                                               _ => _.Messages.Count == 1 && _.Messages[0].Text.Equals("hello world") && _.Messages[0].MessageType.ToString().Equals("info")),
                                                           A <string> ._))
            .MustHaveHappenedOnceExactly();
        }
        public void Handle(SpfEntityState state, Message message)
        {
            if (message is SpfRecordsEvaluated evaluated)
            {
                List <string> currentRecords = state.SpfRecords?.Records.SelectMany(x => x.RecordsStrings).ToList() ?? new List <string>();
                List <string> newRecords     = evaluated.Records?.Records.SelectMany(x => x.RecordsStrings).ToList() ?? new List <string>();

                List <string> removedRecords = currentRecords.Except(newRecords).ToList();
                List <string> addedRecords   = newRecords.Except(currentRecords).ToList();

                if (addedRecords.Any())
                {
                    _dispatcher.Dispatch(new SpfRecordAdded(state.Id, addedRecords), _spfEntityConfig.SnsTopicArn);
                }

                if (removedRecords.Any())
                {
                    _dispatcher.Dispatch(new SpfRecordRemoved(state.Id, removedRecords), _spfEntityConfig.SnsTopicArn);
                }
            }
        }
Пример #18
0
        public void SpfRecordsWithDifferentMessageTest()
        {
            SpfRecords spfRecords =
                NotifierTestUtil.CreateSpfRecords(messages: new List <Message>
            {
                new Message(Guid.NewGuid(), "SPF", MessageType.info, "hello", "markdown")
            });

            SpfEntityState state =
                new SpfEntityState(Id, 1, SpfState.Evaluated, DateTime.Now)
            {
                SpfRecords =
                    NotifierTestUtil.CreateSpfRecords(messages: new List <Message>
                {
                    new Message(Guid.NewGuid(), "SPF", MessageType.error, "world", "markdown")
                })
            };
            SpfRecordsEvaluated message = new SpfRecordsEvaluated(Id, spfRecords, 1, TimeSpan.MinValue, new List <Message>(), DateTime.UtcNow);

            _changeNotifier.Handle(state, message);

            NotifierTestUtil.VerifyResults(_dispatcher, removed: true, added: true);

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfAdvisoryAdded> ._, A <string> ._))
            .MustHaveHappenedOnceExactly();
            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfAdvisoryRemoved> ._, A <string> ._))
            .MustHaveHappenedOnceExactly();

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfAdvisoryRemoved> .That.Matches(
                                                               _ => _.Messages.Count == 1 && _.Messages[0].Text.Equals("world") && _.Messages.Count == 1 &&
                                                               _.Messages[0].MessageType.ToString().Equals("error")),
                                                           A <string> ._))
            .MustHaveHappenedOnceExactly();

            FakeItEasy.A.CallTo(() => _dispatcher.Dispatch(A <SpfAdvisoryAdded> .That.Matches(
                                                               _ => _.Messages.Count == 1 && _.Messages[0].Text.Equals("hello") && _.Messages.Count == 1 &&
                                                               _.Messages[0].MessageType.ToString().Equals("info")),
                                                           A <string> ._))
            .MustHaveHappenedOnceExactly();
        }
        public async Task GetNoStateExistsReturnsNull()
        {
            SpfEntityState state = await _dao.Get(Id);

            Assert.That(state, Is.Null);
        }