Пример #1
0
        public void Authorize_DoesNotThrow_WhenConnectionIsNotListenerConnection()
        {
            var        securityContext = new SecurityContext();
            Connection connection      = Construct.Uninitialized <Connection>();

            Should.NotThrow(() => securityContext.Authorize(connection));
        }
Пример #2
0
        public void Authorize_Throws_WhenConnectionNull()
        {
            var securityContext = new SecurityContext();

            Action action = () => securityContext.Authorize(null);

            action.ShouldThrow <ArgumentNullException>()
            .ParamName.ShouldBe("connection");
        }
Пример #3
0
        public void IsAuthorized_ReturnsTrue_WhenConnectionTheSame()
        {
            ListenerConnection connection = Construct.Uninitialized <ListenerConnection>();
            var securityContext           = new SecurityContext();

            securityContext.Authorize(connection);

            var result = securityContext.IsAuthorized(connection);

            result.ShouldBeTrue();
        }
Пример #4
0
        public void IsAuthorized_ReturnsFalse_WhenConnectionNotTheSame()
        {
            ListenerConnection connection1 = Construct.Uninitialized <ListenerConnection>();
            Connection         connection2 = Construct.Uninitialized <Connection>();
            var securityContext            = new SecurityContext();

            securityContext.Authorize(connection1);

            var result = securityContext.IsAuthorized(connection2);

            result.ShouldBeFalse();
        }
Пример #5
0
        public async Task IsAuthorized_ReturnsTrue_WhenSameConnectionAuthorizedTwice()
        {
            var            authorized        = false;
            var            links             = new List <ListenerLink>();
            ILinkProcessor fakeLinkProcessor = Substitute.For <ILinkProcessor>();

            fakeLinkProcessor
            .When(instance => instance.Process(Arg.Any <AttachContext>()))
            .Do(c =>
            {
                AttachContext attachContext = c.ArgAt <AttachContext>(0);
                links.Add(attachContext.Link);
                attachContext.Complete(new Error(ErrorCode.IllegalState)
                {
                    Description = "Test"
                });
            });

            ContainerHost host = TestAmqpHost.Open();

            try
            {
                host.RegisterLinkProcessor(fakeLinkProcessor);
                Connection connection = await host.ConnectAndAttachAsync(2);

                var securityContext = new SecurityContext();
                securityContext.Authorize(links[0].Session.Connection);
                securityContext.Authorize(links[1].Session.Connection);
                authorized = securityContext.IsAuthorized(links[1].Session.Connection);

                await connection.CloseAsync();
            }
            finally
            {
                host.Close();
            }

            authorized.ShouldBeTrue();
        }
Пример #6
0
        public async Task IsAuthorized_ReturnsFalse_WhenSessionConnectionClosedBeforeAuthorized()
        {
            ListenerLink   link              = null;
            var            authorized        = false;
            ILinkProcessor fakeLinkProcessor = Substitute.For <ILinkProcessor>();

            fakeLinkProcessor
            .When(instance => instance.Process(Arg.Any <AttachContext>()))
            .Do(c =>
            {
                AttachContext attachContext = c.ArgAt <AttachContext>(0);
                link = attachContext.Link;
                attachContext.Complete(new Error(ErrorCode.IllegalState)
                {
                    Description = "Test"
                });
            });

            ContainerHost host = TestAmqpHost.Open();

            try
            {
                host.RegisterLinkProcessor(fakeLinkProcessor);
                Connection connection = await host.ConnectAndAttachAsync();

                await connection.CloseAsync();

                await Task.Delay(500);

                var securityContext = new SecurityContext();
                securityContext.Authorize(link.Session.Connection);

                authorized = securityContext.IsAuthorized(link.Session.Connection);
            }
            finally
            {
                host.Close();
            }

            authorized.ShouldBeFalse();
        }