Beispiel #1
0
        public async Task ValidateRequestInvalidController()
        {
            var sut = new ExecutionFilterAttribute(Mock.Of <IAuthManager>(), Mock.Of <IContextManager>(), AuthenticationLevel.User);

            var result = await sut.ValidateRequest(null, "token");

            Assert.IsFalse(result.isValid);
            Assert.AreEqual("InvalidController", result.errPhrase);
        }
Beispiel #2
0
        public void RetrieveParametersNoToken()
        {
            var headers = new HeaderDictionary();

            string actual;

            ExecutionFilterAttribute.RetrieveParameters(headers, out actual);

            Assert.IsNull(actual);
        }
Beispiel #3
0
        public void HasAuthenticateAttributeReturnsFalse()
        {
            var descriptor = new ControllerActionDescriptor
            {
                MethodInfo = GetMethodInfo <ExecutionFilterTest>(x => x.TestControllerWithoutAuthenticateAttribute())
            };

            var result = ExecutionFilterAttribute.HasAuthenticateAttribute(descriptor);

            Assert.IsFalse(result);
        }
Beispiel #4
0
        public void OnActionExecutingTokenInvalid()
        {
            const string token = "MyToken";

            var authManager = new Mock <IAuthManager>();

            authManager.Setup(c => c.VerifyAccessToken(It.IsAny <string>())).Returns <string>(value =>
            {
                if (value == token)
                {
                    return(Task.FromResult(new AuthToken
                    {
                        IsVerified = true,
                        Token = value,
                        UserId = 1,
                        ValidUntil = DateTime.Now.AddDays(1)
                    }));
                }

                return(Task.FromResult(default(AuthToken)));
            });

            var sut = new ExecutionFilterAttribute(authManager.Object, Mock.Of <IContextManager>(), AuthenticationLevel.User);

            var dictionary =
                new HeaderDictionary(new Dictionary <string, StringValues> {
                { "Authorization", "Bearer " + token + "somesuffix" }
            });

            var feature = new Mock <IHttpRequestFeature>();

            feature.Setup(c => c.Headers).Returns(dictionary);

            var controller = new BaseController();

            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set(feature.Object);
            var actionContext = new ActionContext(httpContext,
                                                  new RouteData(new RouteValueDictionary()), new ControllerActionDescriptor());
            var context = new ActionExecutingContext(new ControllerContext(actionContext), new List <IFilterMetadata>(),
                                                     new ConcurrentDictionary <string, object>(), controller);

            var thread = new Thread(() =>
            {
                sut.OnActionExecuting(context);
            });

            thread.Start();
            thread.Join();

            Assert.IsNull(controller.Token);
        }
Beispiel #5
0
        public void RetrieveParametersFormatMismatch3()
        {
            var headers = new HeaderDictionary();

            headers.Add("Authorization", "");

            string actual;

            ExecutionFilterAttribute.RetrieveParameters(headers, out actual);

            Assert.IsNull(actual);
        }
Beispiel #6
0
        public async Task ValidateRequestTokenNotValid()
        {
            const string tokenString = "sampletoken";

            var authManager = GetAuthManager(null);

            var sut = new ExecutionFilterAttribute(authManager, Mock.Of <IContextManager>(), AuthenticationLevel.User);

            var result = await sut.ValidateRequest(Mock.Of <IBaseController>(), tokenString);

            Assert.IsFalse(result.isValid);
            Assert.AreEqual("InvalidToken", result.errPhrase);
        }
Beispiel #7
0
        public void RetrieveParametersFormatMismatch2()
        {
            const string token = "sampletoken";

            var headers = new HeaderDictionary();

            headers.Add("Authorization", token);

            string actual;

            ExecutionFilterAttribute.RetrieveParameters(headers, out actual);

            Assert.IsNull(actual);
        }
Beispiel #8
0
        public void RetrieveParameters()
        {
            const string token = "sampletoken";

            var headers = new HeaderDictionary();

            headers.Add("Authorization", "Bearer " + token);

            string actual;

            ExecutionFilterAttribute.RetrieveParameters(headers, out actual);

            Assert.AreEqual(actual, token);
        }
Beispiel #9
0
        public void SetCulture()
        {
            var actualCultureName   = "";
            var actualUiCultureName = "";

            var thread = new Thread(() =>
            {
                ExecutionFilterAttribute.SetCulture(Thread.CurrentThread);

                actualCultureName   = Thread.CurrentThread.CurrentCulture.Name;
                actualUiCultureName = Thread.CurrentThread.CurrentUICulture.Name;
            });

            thread.Start();
            thread.Join();

            Assert.AreEqual("en-US", actualCultureName);
            Assert.AreEqual("en-US", actualUiCultureName);
        }
Beispiel #10
0
        public async Task ValidateRequest()
        {
            const string tokenString = "sampletoken";

            var controller = new Mock <IBaseController>();

            var token       = GetAuthToken(tokenString, true);
            var authManager = GetAuthManager(token);

            AuthToken actual = null;

            controller
            .SetupSet(p => p.Token = It.IsAny <AuthToken>())
            .Callback <AuthToken>(value => actual = value);

            var sut = new ExecutionFilterAttribute(authManager, Mock.Of <IContextManager>(), AuthenticationLevel.User);

            var result = await sut.ValidateRequest(controller.Object, tokenString);

            Assert.AreSame(token, actual);
            Assert.IsTrue(result.isValid);
            Assert.AreEqual("", result.errPhrase);
        }
Beispiel #11
0
        public void ProceedWithExecution(bool expected, bool isValid, AuthenticationLevel authenticationLevel, bool hasAuthenticateAttribute)
        {
            var result = ExecutionFilterAttribute.ProceedWithExecution(isValid, authenticationLevel, hasAuthenticateAttribute);

            Assert.AreEqual(expected, result);
        }
Beispiel #12
0
        public void HasAuthenticateAttributeDescriptorIsNull()
        {
            var result = ExecutionFilterAttribute.HasAuthenticateAttribute(null);

            Assert.IsFalse(result);
        }
Beispiel #13
0
        public void HasAuthenticateAttributeMethodIsNull()
        {
            var result = ExecutionFilterAttribute.HasAuthenticateAttribute(new ControllerActionDescriptor());

            Assert.IsFalse(result);
        }