コード例 #1
0
        private void RecordAction(Func <AuditRecorder, Func <IAuditEventArguments, Task> > recordMethodFactory, bool isSuccess)
        {
            DateTime expectedWhen        = new DateTime(2018, 3, 5, 10, 23, 11, DateTimeKind.Utc);
            var      expectedSubject     = new UserResourceActor(fixture.Create <string>());
            string   expectedAction      = fixture.Create <string>();
            string   expectedDescription = fixture.Create <string>();
            var      expectedResource    = new AuditableResource(fixture.Create <string>());


            IAuditEventArguments auditEventContext = AuditContextMockingHelper.CreateAuditEventContext(expectedSubject,
                                                                                                       expectedAction, expectedResource, expectedDescription);

            now = () => expectedWhen;

            var sut = CreateSut();

            recordMethodFactory(sut)(auditEventContext).Wait();

            auditEntities.Verify(ae => ae.Add(new AuditEntity()
            {
                Source             = expectedSource,
                SubjectIdentifier  = expectedSubject.Identifier,
                SubjectType        = expectedSubject.Type,
                Subject            = "",
                Description        = expectedDescription,
                Resource           = expectedResource.Name,
                ResourceType       = expectedResource.Type,
                ResourceIdentifier = expectedResource.Identifier,
                Succeeded          = isSuccess,
                Action             = expectedAction,
                When = expectedWhen
            }), Times.Once);

            unitOfWork.Verify(uow => uow.Commit(), Times.Once);
        }
コード例 #2
0
        public IAuditQuery AndResource(AuditableResource resource)
        {
            criteriaBuilder.AndStringMatch(Matches.Exactly, nameof(AuditEntity.Resource), resource.Identifier);
            criteriaBuilder.AndStringMatch(Matches.Exactly, nameof(AuditEntity.ResourceType), resource.Type);

            return(this);
        }
コード例 #3
0
 /// <summary>
 /// Creates an instance of an Audit Event to be recorded
 /// </summary>
 /// <param name="actor">The subject responsible for the event</param>
 /// <param name="action">Action being performed</param>
 /// <param name="resource">Resource the action is being applied to</param>
 /// <param name="description">Description of the event</param>
 public AuditEventArguments(ResourceActor actor, string action, AuditableResource resource, FormattedString description)
 {
     Actor       = actor;
     Action      = action;
     Resource    = resource;
     Description = description;
 }
コード例 #4
0
        public void AndResource_WhenCalled_ShouldAddToCriteria()
        {
            var expectedMatch = new AuditableResource("Clients", "https://ids.acme.com");

            var sut = CreateSut();

            sut.AndResource(expectedMatch);

            criteriaBuilder.Verify(
                cb => cb.AndStringMatch(Matches.Exactly, nameof(AuditEntity.Resource), expectedMatch.Identifier),
                Times.Once);
            criteriaBuilder.Verify(
                cb => cb.AndStringMatch(Matches.Exactly, nameof(AuditEntity.ResourceType), expectedMatch.Type),
                Times.Once);
        }
コード例 #5
0
        public void RecordFailure_WhenCalled_ShouldLocalizeActionContext()
        {
            var expectedSubject  = new UserResourceActor("fred");
            var expectedResource = new AuditableResource("identityServer");

            string action = "login";

            localizer.Setup(sl => sl[action]).Returns(new LocalizedString("login", "la login", true));

            var sut = CreateSut();

            IAuditEventArguments auditEventContext =
                AuditContextMockingHelper.CreateAuditEventContext(expectedSubject, action, expectedResource, "");

            sut.RecordSuccess(auditEventContext).Wait();

            recordAuditableActions.Verify(raa => raa.RecordSuccess(It.IsAny <LocalizedAuditEventArguments>()), Times.Once);
        }
コード例 #6
0
        public void RecordFailure_WhenCalled_ShouldLocalizeActionAndForward()
        {
            var expectedSubject  = new UserResourceActor("fred");
            var expectedResource = new AuditableResource("identityServer");

            string          actionKey = "login";
            string          expectedTranslationText = "la Login";
            LocalizedString expectedTranslation     = new LocalizedString(actionKey, expectedTranslationText, false);

            localizer.Setup(sl => sl[actionKey]).Returns(expectedTranslation);

            var sut = CreateSut();

            IAuditEventArguments auditEventContext =
                AuditContextMockingHelper.CreateAuditEventContext(expectedSubject, actionKey, expectedResource, "");

            sut.RecordFailure(auditEventContext).Wait();

            recordAuditableActions.Verify(
                raa => raa.RecordFailure(It.Is <IAuditEventArguments>(aec => aec.Action == expectedTranslationText)), Times.Once);
        }
コード例 #7
0
        public void GivenIHaveAnAuditSource_WhenAnAttemptIsMadeToRecordFailure_ThenShouldWriteNewAuditEntryInDatabase()
        {
            var          expectedSubject     = new UserResourceActor("andy");
            const string expectedtedAction   = "Login";
            var          expectedResource    = new AuditableResource("Client", "3232-4343-342-34123", "AdminUI");
            const string expectedDescription = "Logging in";

            var sut = CreateSut();

            sut.RecordFailure(AuditContextMockingHelper.CreateAuditEventContext(expectedSubject, expectedtedAction, expectedResource, expectedDescription)).Wait();

            var auditEntries = databaseContext.AuditEntries.Where(ae => !ae.Succeeded &&
                                                                  ae.SubjectIdentifier == expectedSubject.Identifier &&
                                                                  ae.SubjectType == expectedSubject.Type &&
                                                                  ae.Action == expectedtedAction &&
                                                                  ae.Resource == expectedResource.Name &&
                                                                  ae.ResourceType == expectedResource.Type &&
                                                                  ae.ResourceIdentifier == expectedResource.Identifier &&
                                                                  ae.Description == expectedDescription).ToList();

            Assert.Single(auditEntries);
        }
コード例 #8
0
        public static IAuditEventArguments CreateAuditEventContext(ResourceActor subject, string action, AuditableResource expectedResource,
                                                                   string expectedDescription)
        {
            Mock <IAuditEventArguments> context = new Mock <IAuditEventArguments>();

            context.Setup(c => c.Description).Returns(expectedDescription);
            context.Setup(c => c.Actor).Returns(subject);
            context.Setup(c => c.Resource).Returns(expectedResource);
            context.Setup(c => c.Action).Returns(action);

            return(context.Object);
        }
コード例 #9
0
 protected bool Equals(AuditableResource other)
 {
     return(string.Equals(Type, other.Type) && string.Equals(Name, other.Name) && string.Equals(Identifier, other.Identifier));
 }