Exemplo n.º 1
0
        public async Task SpfRecordsPolledAndRecordsChangedEventsRaisedAndStateUpdated()
        {
            await PurgeAmazonSqsQueue(OutputQueueUrl);
            await TruncateDatabase(ConnectionString);

            await SetState(ConnectionString, new SpfEntityState(Id, 1, SpfState.PollPending, DateTime.Now));

            SpfRecordsPolled spfRecordsPolled = new SpfRecordsPolled(Id,
                                                                     CreateSpfRecords(), 1, TimeSpan.FromSeconds(1), new List <Contracts.SharedDomain.Message>());

            InvokeLambda(spfRecordsPolled);

            Message message1 = await ReadAmazonSqsEvent(OutputQueueUrl, ReadTimeoutSecond);

            Assert.That(message1, Is.TypeOf <SpfRecordsPolled>());

            SpfRecordsEvaluated spfRecordsEvaluated = (SpfRecordsEvaluated)message1;

            Assert.That(spfRecordsEvaluated.Id, Is.EqualTo(Id));
            Assert.That(spfRecordsEvaluated.Records, Is.EqualTo(spfRecordsPolled.Records));
            Assert.That(spfRecordsEvaluated.DnsQueryCount, Is.EqualTo(spfRecordsPolled.DnsQueryCount));
            Assert.That(spfRecordsEvaluated.ElapsedQueryTime, Is.EqualTo(spfRecordsPolled.ElapsedQueryTime));
            CollectionAssert.AreEqual(spfRecordsEvaluated.Messages, spfRecordsPolled.Messages);

            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, Is.EqualTo(spfRecordsPolled.Records));
            Assert.That(spfEntityStates[0].DnsQueryCount, Is.EqualTo(spfRecordsPolled.DnsQueryCount));
            Assert.That(spfEntityStates[0].ElapsedQueryTime, Is.EqualTo(spfRecordsPolled.ElapsedQueryTime));
            CollectionAssert.AreEqual(spfEntityStates[0].Messages, spfRecordsPolled.Messages);
        }
Exemplo n.º 2
0
        private static async Task <Message> ReadAmazonSqsEvent(string queueUrl, int waitTimeoutSPerMessageSeconds)
        {
            using (AmazonSQSClient sqsClient = new AmazonSQSClient())
            {
                ReceiveMessageResponse receiveMessageResponse =
                    await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest(queueUrl)
                {
                    WaitTimeSeconds       = waitTimeoutSPerMessageSeconds,
                    MessageAttributeNames = new List <string> {
                        "All"
                    },
                    MaxNumberOfMessages = 1
                });

                Message message = receiveMessageResponse.Messages.Count == 1
                    ? GetMessage(receiveMessageResponse.Messages.First())
                    : null;

                if (message != null)
                {
                    DeleteMessageResponse response =
                        await sqsClient.DeleteMessageAsync(queueUrl,
                                                           receiveMessageResponse.Messages.First().ReceiptHandle);
                }

                return(message);
            }
        }
Exemplo n.º 3
0
        public async Task NonFailAllTermEvaluated()
        {
            await PurgeAmazonSqsQueue(OutputQueueUrl);

            List <Term> terms = new List <Term>
            {
                new All(Qualifier.Neutral, "?All", true, false)
            };

            SpfRecords spfRecords = CreateSpfRecords(terms, isRoot: true);

            SpfRecordsPolled spfRecordsPolled = new SpfRecordsPolled(Id, spfRecords, 1, TimeSpan.FromSeconds(0),
                                                                     new List <Contracts.SharedDomain.Message>());

            InvokeLambda(spfRecordsPolled);

            Message message = await ReadAmazonSqsEvent(OutputQueueUrl, ReadTimeoutSecond);

            Assert.That(message, Is.Not.Null);
            Assert.That(message, Is.TypeOf <SpfRecordsEvaluated>());

            SpfRecordsEvaluated spfRecordsEvaluated = (SpfRecordsEvaluated)message;

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

            StringAssert.StartsWith("Only \"-all\" (do not allow other ip addresses) or \"~all\" (allow but mark other ip addresses) protect", spfRecordsEvaluated.Records.Records[0].Messages[0].Text);
        }
Exemplo n.º 4
0
        public async Task TermsExplained()
        {
            await PurgeAmazonSqsQueue(OutputQueueUrl);

            List <Term> terms = new List <Term>
            {
                new All(Qualifier.Fail, "-All", true, false)
            };

            SpfRecords spfRecords = CreateSpfRecords(terms);

            SpfRecordsPolled spfRecordsPolled = new SpfRecordsPolled(Id, spfRecords, 1, TimeSpan.FromSeconds(0),
                                                                     new List <Contracts.SharedDomain.Message>());

            InvokeLambda(spfRecordsPolled);

            Message message = await ReadAmazonSqsEvent(OutputQueueUrl, ReadTimeoutSecond);

            Assert.That(message, Is.Not.Null);
            Assert.That(message, Is.TypeOf <SpfRecordsEvaluated>());

            SpfRecordsEvaluated spfRecordsEvaluated = (SpfRecordsEvaluated)message;

            Assert.That(spfRecordsEvaluated.Records.Records.Count, Is.EqualTo(1));
            Assert.That(spfRecordsEvaluated.Records.Records[0].Terms.Count, Is.EqualTo(1));
            StringAssert.StartsWith("Do not allow any (other) ip addresses.", spfRecordsEvaluated.Records.Records[0].Terms[0].Explanation);
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 6
0
        private static void InvokeLambda(Message message)
        {
            AmazonLambdaClient client            = new AmazonLambdaClient();
            string             serializedMessage = JsonConvert.SerializeObject(message);

            SQSEvent.MessageAttribute typeAttribute = new SQSEvent.MessageAttribute
            {
                DataType    = "String",
                StringValue = message.GetType().Name
            };

            SQSEvent.SQSMessage fakeSqsMessage = new SQSEvent.SQSMessage
            {
                Body       = serializedMessage,
                Attributes = new Dictionary <string, string>
                {
                    { "SentTimestamp", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString() }
                },
                MessageAttributes = new Dictionary <string, SQSEvent.MessageAttribute>
                {
                    { "Type", typeAttribute }
                }
            };

            SQSEvent fakeSqsEvent = new SQSEvent
            {
                Records = new List <SQSEvent.SQSMessage> {
                    fakeSqsMessage
                }
            };

            string serializedFakeSqsEvent = JsonConvert.SerializeObject(fakeSqsEvent, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            InvokeRequest invokeRequest = new InvokeRequest
            {
                FunctionName = FunctionName,
                Payload      = serializedFakeSqsEvent
            };
            InvokeResponse response = client.InvokeAsync(invokeRequest).GetAwaiter().GetResult();

            string responseString = Encoding.UTF8.GetString(response.Payload.GetBuffer());

            Console.WriteLine(responseString);
        }
Exemplo n.º 7
0
        public async Task DomainAlreadyCreatedNoChangesOccur()
        {
            await PurgeAmazonSqsQueue(OutputQueueUrl);
            await TruncateDatabase(ConnectionString);

            await SetState(ConnectionString, new SpfEntityState(Id, 1, SpfState.PollPending, DateTime.Now));

            DomainCreated domainCreated = new DomainCreated(Id, "*****@*****.**", DateTime.UtcNow);

            InvokeLambda(domainCreated);

            Message message = await ReadAmazonSqsEvent(OutputQueueUrl, ReadTimeoutSecond);

            Assert.That(message, Is.Null);

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

            Assert.That(spfEntityStates.Count, Is.EqualTo(1));
            Assert.That(spfEntityStates[0].Id, Is.EqualTo(domainCreated.Id));
            Assert.That(spfEntityStates[0].Version, Is.EqualTo(1));
        }
Exemplo n.º 8
0
        public async Task EvaluationOccursForNestedRecords()
        {
            await PurgeAmazonSqsQueue(OutputQueueUrl);

            List <Term> innerTerms = new List <Term>
            {
                new All(Qualifier.Neutral, "?All", true, false)
            };

            List <Term> terms = new List <Term>
            {
                new Include(Qualifier.Pass, string.Empty, string.Empty, CreateSpfRecords(innerTerms), true)
            };

            SpfRecords spfRecords = CreateSpfRecords(terms);

            SpfRecordsPolled spfRecordsPolled = new SpfRecordsPolled(Id, spfRecords, 1, TimeSpan.FromSeconds(0),
                                                                     new List <Contracts.SharedDomain.Message>());

            InvokeLambda(spfRecordsPolled);

            Message message = await ReadAmazonSqsEvent(OutputQueueUrl, ReadTimeoutSecond);

            Assert.That(message, Is.Not.Null);
            Assert.That(message, Is.TypeOf <SpfRecordsEvaluated>());

            SpfRecordsEvaluated spfRecordsEvaluated = (SpfRecordsEvaluated)message;

            List <SpfRecord> records = spfRecordsEvaluated.Records.Records;

            Assert.That(records.Count, Is.EqualTo(1));
            Assert.That(records[0].Terms.Count, Is.EqualTo(1));
            Assert.That(records[0].Terms[0].Explanation, Is.Not.Null);

            Assert.That(records[0].Terms[0], Is.TypeOf <Include>());
            List <SpfRecord> innerRecords = ((Include)records[0].Terms[0]).Records.Records;

            Assert.That(innerRecords[0].Terms.Count, Is.EqualTo(1));
            Assert.That(innerRecords[0].Terms[0].Explanation, Is.Not.Null);
        }
Exemplo n.º 9
0
        public async Task DomainCreatedWithNoDomainCreatesEntityForDomain()
        {
            await PurgeAmazonSqsQueue(OutputQueueUrl);
            await TruncateDatabase(ConnectionString);

            DomainCreated domainCreated = new DomainCreated(Id, "*****@*****.**", DateTime.UtcNow);

            InvokeLambda(domainCreated);

            Message message = await ReadAmazonSqsEvent(OutputQueueUrl, ReadTimeoutSecond);

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

            SpfEntityCreated spfEntityCreated = (SpfEntityCreated)message;

            Assert.That(spfEntityCreated.Id, Is.EqualTo(domainCreated.Id));
            Assert.That(spfEntityCreated.Version, Is.EqualTo(1));

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

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