public ApiControllerExtensionsTests()
        {
            HttpConfiguration config = new HttpConfiguration();
            _user = new WebHookUser();

            _managerMock = new Mock<IWebHookManager>();
            _resolverMock = new Mock<IDependencyResolver>();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookManager)))
                .Returns(_managerMock.Object)
                .Verifiable();

            config.DependencyResolver = _resolverMock.Object;

            ClaimsIdentity identity = new ClaimsIdentity();
            Claim claim = new Claim(ClaimTypes.Name, "TestUser");
            identity.AddClaim(claim);
            _principal = new ClaimsPrincipal(identity);

            _context = new HttpRequestContext()
            {
                Configuration = config,
                Principal = _principal
            };
            _controller = new TestController()
            {
                RequestContext = _context
            };
        }
        public ApiControllerExtensionsTests()
        {
            HttpConfiguration config = new HttpConfiguration();

            _user = new WebHookUser();

            _managerMock  = new Mock <IWebHookManager>();
            _resolverMock = new Mock <IDependencyResolver>();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookManager)))
            .Returns(_managerMock.Object)
            .Verifiable();

            config.DependencyResolver = _resolverMock.Object;

            ClaimsIdentity identity = new ClaimsIdentity();
            Claim          claim    = new Claim(ClaimTypes.Name, "TestUser");

            identity.AddClaim(claim);
            _principal = new ClaimsPrincipal(identity);

            _context = new HttpRequestContext()
            {
                Configuration = config,
                Principal     = _principal
            };
            _controller = new TestController()
            {
                RequestContext = _context
            };
        }
Exemple #3
0
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="ControllerBase.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The <see cref="ControllerBase"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</param>
        /// <param name="predicate">A function to test each <see cref="WebHook"/> to see whether it fulfills the condition. The
        /// predicate is passed the <see cref="WebHook"/> and the user who registered it. If the predicate returns <c>true</c> then
        /// the <see cref="WebHook"/> is included; otherwise it is not.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this ControllerBase controller, IEnumerable <NotificationDictionary> notifications, Func <WebHook, string, bool> predicate)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }
            if (notifications == null)
            {
                throw new ArgumentNullException(nameof(notifications));
            }
            if (!notifications.Any())
            {
                return(0);
            }


            // Get the User ID from the User principal
            IWebHookUser user   = controller.HttpContext.RequestServices.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = controller.HttpContext.RequestServices.GetManager();

            return(await manager.NotifyAsync(userId, notifications, predicate));
        }
        public void GetUser_ReturnsDefaultInstance_IfNoneRegistered()
        {
            // Act
            IWebHookUser actual = _resolverMock.Object.GetUser();

            // Assert
            Assert.IsType <WebHookUser>(actual);
        }
        /// <inheritdoc />
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);

            _manager = Configuration.DependencyResolver.GetManager();
            _store   = Configuration.DependencyResolver.GetStore();
            _user    = Configuration.DependencyResolver.GetUser();
        }
 /// <summary>
 /// For testing purposes
 /// </summary>
 internal static void Reset()
 {
     _filterManager   = null;
     _filterProviders = null;
     _store           = null;
     _sender          = null;
     _manager         = null;
     _user            = null;
 }
        public void GetUser_ReturnsSingleInstance()
        {
            // Act
            IWebHookUser actual1 = CustomServices.GetUser();
            IWebHookUser actual2 = CustomServices.GetUser();

            // Assert
            Assert.Same(actual1, actual2);
        }
 /// <summary>
 /// For testing purposes
 /// </summary>
 internal static void Reset()
 {
     _filterManager        = null;
     _store                = null;
     _sender               = null;
     _manager              = null;
     _registrationsManager = null;
     _user = null;
 }
Exemple #9
0
        public WebHookRegistrationsControllerTests()
        {
            _resolverMock = new Mock <IDependencyResolver>();
            _managerMock  = new Mock <IWebHookManager>();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookManager)))
            .Returns(_managerMock.Object)
            .Verifiable();

            _store = new MemoryWebHookStore();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookStore)))
            .Returns(_store)
            .Verifiable();

            _user = new WebHookUser();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookUser)))
            .Returns(_user)
            .Verifiable();

            _filterProviderMock = new Mock <IWebHookFilterProvider>();
            _filterManager      = new WebHookFilterManager(new[]
            {
                new WildcardWebHookFilterProvider(),
                _filterProviderMock.Object
            });
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookFilterManager)))
            .Returns(_filterManager)
            .Verifiable();

            _config = new HttpConfiguration();
            _config.DependencyResolver = _resolverMock.Object;

            ClaimsIdentity identity = new ClaimsIdentity();
            Claim          claim    = new Claim(ClaimTypes.Name, TestUser);

            identity.AddClaim(claim);
            IPrincipal principal = new ClaimsPrincipal(identity);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Address);

            request.SetConfiguration(_config);
            HttpRequestContext requestContext = new HttpRequestContext()
            {
                Configuration = _config,
                Principal     = principal,
                Url           = new UrlHelper(request),
            };

            _controllerContext = new HttpControllerContext()
            {
                Configuration  = _config,
                Request        = new HttpRequestMessage(),
                RequestContext = requestContext,
            };
            _controller = new WebHookRegistrationsControllerMock();
            _controller.Initialize(_controllerContext);
        }
        /// <inheritdoc />
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);

            // The Microsoft.AspNet.WebHooks library registeres an extension method for the DependencyResolver.
            // Sadly we cannot access these properties by using out Autofac dependency injection.
            // In order to access them we have to resolve them through the Configuration.
            _manager = Configuration.DependencyResolver.GetManager();
            _store   = Configuration.DependencyResolver.GetStore();
            _user    = Configuration.DependencyResolver.GetUser();
        }
        public WebHookRegistrationsControllerTests()
        {
            _resolverMock = new Mock<IDependencyResolver>();
            _managerMock = new Mock<IWebHookManager>();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookManager)))
                .Returns(_managerMock.Object)
                .Verifiable();

            _store = new MemoryWebHookStore();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookStore)))
                .Returns(_store)
                .Verifiable();

            _user = new WebHookUser();
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookUser)))
                .Returns(_user)
                .Verifiable();

            _filterProviderMock = new Mock<IWebHookFilterProvider>();
            _filterManager = new WebHookFilterManager(new[]
            {
                new WildcardWebHookFilterProvider(),
                _filterProviderMock.Object
            });
            _resolverMock.Setup(r => r.GetService(typeof(IWebHookFilterManager)))
                .Returns(_filterManager)
                .Verifiable();

            _config = new HttpConfiguration();
            _config.DependencyResolver = _resolverMock.Object;

            ClaimsIdentity identity = new ClaimsIdentity();
            Claim claim = new Claim(ClaimTypes.Name, TestUser);
            identity.AddClaim(claim);
            IPrincipal principal = new ClaimsPrincipal(identity);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Address);
            request.SetConfiguration(_config);
            HttpRequestContext requestContext = new HttpRequestContext()
            {
                Configuration = _config,
                Principal = principal,
                Url = new UrlHelper(request),
            };

            _controllerContext = new HttpControllerContext()
            {
                Configuration = _config,
                Request = new HttpRequestMessage(),
                RequestContext = requestContext,
            };
            _controller = new WebHookRegistrationsControllerMock();
            _controller.Initialize(_controllerContext);
        }
        public void SetUser_GetUser_Roundtrips()
        {
            // Arrange
            Mock <IWebHookUser> userMock = new Mock <IWebHookUser>();

            // Act
            CustomServices.SetUser(userMock.Object);
            IWebHookUser actual = CustomServices.GetUser();

            // Assert
            Assert.Same(userMock.Object, actual);
        }
Exemple #13
0
        public static IWebHookRegistrationsManager GetRegistrationsManager(this IDependencyScope services)
        {
            IWebHookRegistrationsManager registrationsManager = services.GetService <IWebHookRegistrationsManager>();

            if (registrationsManager == null)
            {
                IWebHookManager       manager       = services.GetManager();
                IWebHookStore         store         = services.GetStore();
                IWebHookFilterManager filterManager = services.GetFilterManager();
                IWebHookUser          userManager   = services.GetUser();
                registrationsManager = CustomServices.GetRegistrationsManager(manager, store, filterManager, userManager);
            }
            return(registrationsManager);
        }
        public void GetUser_ReturnsDependencyInstance_IfRegistered()
        {
            // Arrange
            Mock <IWebHookUser> instanceMock = new Mock <IWebHookUser>();

            _resolverMock.Setup(r => r.GetService(typeof(IWebHookUser)))
            .Returns(instanceMock.Object)
            .Verifiable();

            // Act
            IWebHookUser actual = _resolverMock.Object.GetUser();

            // Assert
            Assert.Same(instanceMock.Object, actual);
            instanceMock.Verify();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="WebHookRegistrationsManager"/> class with default
 /// settings.
 /// </summary>
 /// <param name="manager">The current <see cref="IWebHookManager"/>.</param>
 /// <param name="store">The current <see cref="IWebHookStore"/>.</param>
 /// <param name="filterManager">The current <see cref="IWebHookFilterManager"/>.</param>
 /// <param name="userManager">The current <see cref="IWebHookUser"/>.</param>
 public WebHookRegistrationsManager(IWebHookManager manager, IWebHookStore store, IWebHookFilterManager filterManager, IWebHookUser userManager)
 {
     if (manager == null)
     {
         throw new ArgumentNullException(nameof(manager));
     }
     if (store == null)
     {
         throw new ArgumentNullException(nameof(store));
     }
     if (filterManager == null)
     {
         throw new ArgumentNullException(nameof(filterManager));
     }
     if (userManager == null)
     {
         throw new ArgumentNullException(nameof(userManager));
     }
     _manager       = manager;
     _store         = store;
     _filterManager = filterManager;
     _userManager   = userManager;
 }
Exemple #16
0
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="Controller.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The MVC <see cref="Controller"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</param>
        /// <param name="predicate">A function to test each <see cref="WebHook"/> to see whether it fulfills the condition. The
        /// predicate is passed the <see cref="WebHook"/> and the user who registered it. If the predicate returns <c>true</c> then
        /// the <see cref="WebHook"/> is included; otherwise it is not.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this Controller controller, IEnumerable <NotificationDictionary> notifications, Func <WebHook, string, bool> predicate)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (notifications == null)
            {
                throw new ArgumentNullException("notifications");
            }
            if (!notifications.Any())
            {
                return(0);
            }

            // Get the User ID from the User principal
            IWebHookUser user   = DependencyResolver.Current.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = DependencyResolver.Current.GetManager();

            return(await manager.NotifyAsync(userId, notifications, predicate));
        }
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="Controller.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The MVC <see cref="Controller"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this Controller controller, params NotificationDictionary[] notifications)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (notifications == null)
            {
                throw new ArgumentNullException("notifications");
            }
            if (notifications.Length == 0)
            {
                return(0);
            }

            // Get the User ID from the User principal
            IWebHookUser user   = DependencyResolver.Current.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = DependencyResolver.Current.GetManager();

            return(await manager.NotifyAsync(userId, notifications));
        }
        /// <summary>
        /// Gets an <see cref="IWebHookUser"/> implementation registered with the Dependency Injection engine
        /// or a default implementation if none are registered.
        /// </summary>
        /// <param name="services">The <see cref="IDependencyScope"/> implementation.</param>
        /// <returns>The registered <see cref="IWebHookUser"/> instance or a default implementation if none are registered.</returns>
        public static IWebHookUser GetUser(this IDependencyScope services)
        {
            IWebHookUser userId = services.GetService <IWebHookUser>();

            return(userId ?? CustomServices.GetUser());
        }
        public static IWebHookRegistrationsManager GetRegistrationsManager(IWebHookManager manager, IWebHookStore store, IWebHookFilterManager filterManager, IWebHookUser userManager)
        {
            if (_registrationsManager != null)
            {
                return(_registrationsManager);
            }
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }
            if (filterManager == null)
            {
                throw new ArgumentNullException(nameof(filterManager));
            }
            if (userManager == null)
            {
                throw new ArgumentNullException(nameof(userManager));
            }

            IWebHookRegistrationsManager instance = new WebHookRegistrationsManager(manager, store, filterManager, userManager);

            Interlocked.CompareExchange(ref _registrationsManager, instance, null);
            return(_registrationsManager);
        }
Exemple #20
0
 /// <summary>
 /// Sets a default <see cref="IWebHookUser"/> implementation which is used if none are registered with the
 /// Dependency Injection engine.
 /// </summary>
 /// <param name="instance">The <see cref="IWebHookUser"/> to use. If <c>null</c> then a default implementation is used.</param>
 public static void SetUser(IWebHookUser instance)
 {
     _user = instance;
 }
Exemple #21
0
 /// <summary>
 /// For testing purposes
 /// </summary>
 internal static void Reset()
 {
     _filterManager = null;
     _filterProviders = null;
     _store = null;
     _sender = null;
     _manager = null;
     _user = null;
 }
Exemple #22
0
 /// <summary>
 /// Sets a default <see cref="IWebHookUser"/> implementation which is used if none are registered with the 
 /// Dependency Injection engine.
 /// </summary>
 /// <param name="instance">The <see cref="IWebHookUser"/> to use. If <c>null</c> then a default implementation is used.</param>
 public static void SetUser(IWebHookUser instance)
 {
     _user = instance;
 }
        /// <inheritdoc />
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);

            _manager = Configuration.DependencyResolver.GetManager();
            _store = Configuration.DependencyResolver.GetStore();
            _user = Configuration.DependencyResolver.GetUser();
        }
Exemple #24
0
        /// <summary>
        /// Gets an <see cref="IWebHookUser"/> implementation registered with the Dependency Injection engine
        /// or a default implementation if none are registered.
        /// </summary>
        /// <param name="services">The <see cref="IDependencyResolver"/> implementation.</param>
        /// <returns>The registered <see cref="IWebHookUser"/> instance or a default implementation if none are registered.</returns>
        public static IWebHookUser GetUser(this IDependencyResolver services)
        {
            IWebHookUser user = services.GetService <IWebHookUser>();

            return(user ?? CustomServices.GetUser());
        }