public async Task ShouldHandleChangeSaveAndDispatchWhenTlsTestResultIsReceived()
        {
            string snsTopicArn = "snsTopicArn";

            A.CallTo(() => _tlsEntityConfig.SnsTopicArn).Returns(snsTopicArn);

            string hostName = "testhostname";

            TlsEntityState stateFromDb = new TlsEntityState(hostName)
            {
                TlsState = TlsState.Created
            };

            A.CallTo(() => _dao.Get(hostName)).Returns(Task.FromResult(stateFromDb));
            A.CallTo(() => _dao.GetDomainsFromHost(hostName)).Returns(Task.FromResult(new List <string> {
                "test.gov.uk"
            }));



            A.CallTo(() => _tlsEntityConfig.NextScheduledInSeconds).Returns(33);
            A.CallTo(() => _clock.GetDateTimeUtc()).Returns(DateTime.MinValue);

            CertificateResults certificateResults = new CertificateResults(null, null);

            TlsResultsEvaluated message = new TlsResultsEvaluated(hostName, false, new TlsRecords(
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS))
                                                                      , new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS))
                                                                      , new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS))
                                                                      , new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS)),
                                                                      new TlsRecord(new TlsEvaluatedResult(Guid.NewGuid(), EvaluatorResult.PASS))
                                                                      ), certificateResults);

            await _tlsEntity.Handle(message);

            Assert.AreEqual(stateFromDb.TlsState, TlsState.Evaluated);
            Assert.AreEqual(stateFromDb.TlsRecords, message.TlsRecords);
            Assert.AreEqual(stateFromDb.LastUpdated, message.Timestamp);
            List <string> domains = new List <string>();

            A.CallTo(() => _changeNotifierComposite.Handle(stateFromDb, message, A <List <string> > ._)).MustHaveHappenedOnceExactly();
            A.CallTo(() => _dao.Save(stateFromDb)).MustHaveHappenedOnceExactly();
            A.CallTo(() => _dispatcher.Dispatch(A <TlsRecordEvaluationsChanged> .That.Matches(a => a.Id == message.Id && a.TlsRecords == message.TlsRecords && a.CertificateResults == message.Certificates), snsTopicArn)).MustHaveHappenedOnceExactly();
            A.CallTo(() => _dispatcher.Dispatch(A <CreateScheduledReminder> .That.Matches(a => a.ResourceId == hostName && a.Service == "Tls" && a.ScheduledTime.Second == 33), snsTopicArn)).MustHaveHappenedOnceExactly();
        }
Example #2
0
        public async Task Handle(TlsResultsEvaluated message)
        {
            string messageId = message.Id.ToLower();

            TlsEntityState state = await LoadState(messageId, nameof(message));

            await _domainStatusPublisher.Publish(message);

            state.TlsState     = TlsState.Evaluated;
            state.FailureCount = message.Failed ? state.FailureCount + 1 : 0;

            CreateScheduledReminder reminder;

            if (!message.Failed ||
                message.Failed && state.FailureCount >= _tlsEntityConfig.MaxTlsRetryAttempts)
            {
                List <string> domains = await _dao.GetDomainsFromHost(messageId);

                _changeNotifiersComposite.Handle(state, message, domains);

                state.CertificateResults = message.Certificates;
                state.TlsRecords         = message.TlsRecords;
                state.LastUpdated        = message.Timestamp;

                await _dao.Save(state);

                _dispatcher.Dispatch(new TlsRecordEvaluationsChanged(messageId, state.TlsRecords, state.CertificateResults),
                                     _tlsEntityConfig.SnsTopicArn);

                reminder = new CreateScheduledReminder(Guid.NewGuid().ToString(), ServiceName, messageId,
                                                       _clock.GetDateTimeUtc().AddSeconds(_tlsEntityConfig.NextScheduledInSeconds));
            }
            else
            {
                await _dao.Save(state);

                reminder = new CreateScheduledReminder(Guid.NewGuid().ToString(), ServiceName, messageId,
                                                       _clock.GetDateTimeUtc().AddSeconds(_tlsEntityConfig.FailureNextScheduledInSeconds));
            }

            _dispatcher.Dispatch(reminder, _tlsEntityConfig.SnsTopicArn);
        }