public void AuthorizationAttribute_Ctor_And_Properties()
        {
            AuthorizationAttribute attr = new TestAuthorizationAttribute();

            Assert.IsNull(attr.ErrorMessage, "AuthorizationAttribute.ErrorMessage should default to null");
            Assert.IsNull(attr.ResourceType, "AuthorizationAttribute.ResourceType should default to null");
        }
Beispiel #2
0
        public void AuthorizationAttribute_Ctor_And_Properties()
        {
            AuthorizationAttribute attr = new TestAuthorizationAttribute();

            Assert.IsNull(attr.ErrorMessage, "AuthorizationAttribute.ErrorMessage should default to null");
            Assert.IsNull(attr.ResourceType, "AuthorizationAttribute.ResourceType should default to null");
        }
        public void AuthorizationAttribute_FormatErrorMessage()
        {
            // Default attr with no error message generates default
            TestAuthorizationAttribute attr = new TestAuthorizationAttribute();
            string expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Default_Message, "testOp");
            string message = attr.InternalFormatErrorMessage("testOp");
            Assert.AreEqual(expectedMessage, message, "Default FormatErrorMessage was not correct");

            // Literal error message (no resource type) generates formatted message
            attr = new TestAuthorizationAttribute() { ErrorMessage = "testMessage: {0}" };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, "testMessage: {0}", "testOp");
            message = attr.InternalFormatErrorMessage("testOp");
            Assert.AreEqual(expectedMessage, message, "Default FormatErrorMessage with literal error message was not correct");

            // ResourceType + legal message formats correctly
            attr = new TestAuthorizationAttribute() { ErrorMessage = "Message", ResourceType = typeof(AuthorizationResources) };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, AuthorizationResources.Message, "testOp");
            message = attr.InternalFormatErrorMessage("testOp");
            Assert.AreEqual(expectedMessage, message, "FormatErrorMessage with type + error message was not correct");

            // Negative tests
            // ResourceType with empty message throws
            attr = new TestAuthorizationAttribute() { ResourceType = typeof(AuthorizationResources) };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_ErrorMessage, attr.GetType().FullName, typeof(AuthorizationResources).FullName);
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-resourced message throws
            attr = new TestAuthorizationAttribute() { ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotAProperty" };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotAProperty");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-static message throws
            attr = new TestAuthorizationAttribute() { ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotStaticMessage" };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotStaticMessage");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-string message throws
            attr = new TestAuthorizationAttribute() { ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotStringMessage" };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotStringMessage");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-public message throws
            attr = new TestAuthorizationAttribute() { ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotPublicMessage" };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotPublicMessage");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);
        }
Beispiel #4
0
        public void AuthorizationAttribute_FormatErrorMessage()
        {
            // Default attr with no error message generates default
            TestAuthorizationAttribute attr = new TestAuthorizationAttribute();
            string expectedMessage          = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Default_Message, "testOp");
            string message = attr.InternalFormatErrorMessage("testOp");

            Assert.AreEqual(expectedMessage, message, "Default FormatErrorMessage was not correct");

            // Literal error message (no resource type) generates formatted message
            attr = new TestAuthorizationAttribute()
            {
                ErrorMessage = "testMessage: {0}"
            };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, "testMessage: {0}", "testOp");
            message         = attr.InternalFormatErrorMessage("testOp");
            Assert.AreEqual(expectedMessage, message, "Default FormatErrorMessage with literal error message was not correct");

            // ResourceType + legal message formats correctly
            attr = new TestAuthorizationAttribute()
            {
                ErrorMessage = "Message", ResourceType = typeof(AuthorizationResources)
            };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, AuthorizationResources.Message, "testOp");
            message         = attr.InternalFormatErrorMessage("testOp");
            Assert.AreEqual(expectedMessage, message, "FormatErrorMessage with type + error message was not correct");

            // Negative tests
            // ResourceType with empty message throws
            attr = new TestAuthorizationAttribute()
            {
                ResourceType = typeof(AuthorizationResources)
            };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_ErrorMessage, attr.GetType().FullName, typeof(AuthorizationResources).FullName);
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-resourced message throws
            attr = new TestAuthorizationAttribute()
            {
                ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotAProperty"
            };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotAProperty");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-static message throws
            attr = new TestAuthorizationAttribute()
            {
                ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotStaticMessage"
            };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotStaticMessage");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-string message throws
            attr = new TestAuthorizationAttribute()
            {
                ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotStringMessage"
            };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotStringMessage");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);

            // ResourceType with non-public message throws
            attr = new TestAuthorizationAttribute()
            {
                ResourceType = typeof(AuthorizationResources), ErrorMessage = "NotPublicMessage"
            };
            expectedMessage = string.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Requires_Valid_Property, typeof(AuthorizationResources).FullName, "NotPublicMessage");
            ExceptionHelper.ExpectInvalidOperationException(() => attr.InternalFormatErrorMessage("testOp"), expectedMessage);
        }