/// <summary>
        /// Instantiates the extended Test Case class and passes it the SpiraTest Test Case ID
        /// </summary>
        /// <param name="test">The NUnit Test</param>
        /// <param name="member">The method whose attribute we need to read</param>
        /// <returns></returns>
        public Test Decorate(Test test, System.Reflection.MemberInfo member)
        {
            const string METHOD_NAME = "Decorate: ";

            try
            {
                if (test is TestMethod)
                {
                    Attribute attribute = Reflect.GetAttribute(member, "Inflectra.SpiraTest.AddOns.SpiraTestNUnitAddIn.SpiraTestFramework.SpiraTestCaseAttribute", false);
                    if (attribute != null)
                    {
                        //Get the test case id from the test case attribute
                        int testCaseId = (int)Reflect.GetPropertyValue(attribute, "TestCaseId", BindingFlags.Public | BindingFlags.Instance);
                        test = new SpiraTestCase((TestMethod)test, testCaseId);
                    }
                }

                return(test);
            }
            catch (Exception exception)
            {
                //Log error then rethrow
                System.Diagnostics.EventLog.WriteEntry(SpiraTestAddin.SOURCE_NAME, CLASS_NAME + METHOD_NAME + exception.Message, System.Diagnostics.EventLogEntryType.Error);
                throw exception;
            }
        }
Example #2
0
 public DebuggerTestSuite(Type type)
     : base(type)
 {
     this.Type      = type;
     this.Attribute = (DebuggerTestFixtureAttribute)Reflect.GetAttribute(
         type, DebuggerTestAddIn.DebuggerTestFixtureAttribute, false);
 }
        /// <summary>
        /// Examine the a Test and either return it as is, modify it
        /// or return a different TestCase.
        /// </summary>
        /// <param name="test">The Test to be decorated</param>
        /// <param name="member">The MethodInfo used to construct the test</param>
        /// <returns>The resulting Test</returns>
        public Test Decorate(Test test, System.Reflection.MemberInfo member)
        {
            var attr = Reflect.GetAttribute(member, "SymformNUnitExtension.InconclusiveTestAttribute", false);

            if (attr == null)
            {
                return(test);
            }

            var nunitTest = test as NUnitTestMethod;

            if (nunitTest != null)
            {
                return(new InconclusiveTestCase(nunitTest));
            }

            var paramTests = test as ParameterizedMethodSuite;

            if (paramTests != null)
            {
                Decorate(paramTests);
            }

            return(test);
        }
        public Test Decorate(Test test, MemberInfo member)
        {
            if (member == null)
            {
                return(test);
            }

            TestCase testCase = test as TestCase;

            if (testCase == null)
            {
                return(test);
            }

            Attribute repeatAttr = Reflect.GetAttribute(member, RepeatAttributeType, true);

            if (repeatAttr == null)
            {
                return(test);
            }

            object propVal = Reflect.GetPropertyValue(repeatAttr, "Count",
                                                      BindingFlags.Public | BindingFlags.Instance);

            if (propVal == null)
            {
                return(test);
            }

            int count = (int)propVal;

            return(new RepeatedTestCase(testCase, count));
        }
Example #5
0
 public void GetAttribute()
 {
     Assert.IsNull(Reflect.GetAttribute(myType, "Colors.RedAttribute", false), "Red");
     Assert.AreEqual("GreenAttribute",
                     Reflect.GetAttribute(myType, "Colors.GreenAttribute", false).GetType().Name);
     Assert.IsNull(Reflect.GetAttribute(myType, "Colors.BlueAttribute", false), "Blue");
 }
        private string[] GetScripts(Type fixtureType)
        {
            var scripts = new List <string>();

            Attribute fixtureAttribute = Reflect.GetAttribute(fixtureType,
                                                              "IronPythonTest.Framework.PythonFixtureAttribute",
                                                              true);

            Attribute[] scriptAttributes = Reflect.GetAttributes(fixtureType,
                                                                 "IronPythonTest.Framework.ScriptAttribute",
                                                                 true);

            if (scriptAttributes != null)
            {
                scripts.AddRange(scriptAttributes.Select(attr => (string)Reflect.GetPropertyValue(attr, "FileName")));
            }

            var discoverEmbeddedResources =
                (bool)Reflect.GetPropertyValue(fixtureAttribute, "DiscoverEmbeddedResources");

            if (discoverEmbeddedResources)
            {
                var discoveryKey = (string)Reflect.GetPropertyValue(fixtureAttribute, "DiscoveryKey");

                scripts.AddRange(FindSuitablePythonScripts(fixtureType.Assembly, discoveryKey));
            }

            return(scripts.ToArray());
        }
        /// <summary>
        /// Initializes an instance of this class.
        /// </summary>
        /// <remarks>
        /// This constructor creates the root test suite.
        /// </remarks>
        /// <param name="fixtureType">The type of the test fixture.</param>
        public BrowserStackFixture(Type fixtureType) : base(fixtureType)
        {
            BrowserStackFixtureAttribute attr = (BrowserStackFixtureAttribute)Reflect.GetAttribute(fixtureType, typeof(BrowserStackFixtureAttribute).FullName, true);

            this.Fixture = Reflect.Construct(fixtureType);

            MethodInfo method = Reflect.GetNamedMethod(fixtureType, attr.Argument);

            Params = (List <string>)method.Invoke(null, null);

            foreach (string param in Params)
            {
                this.Add(new BrowserStackFixture(fixtureType, param));
            }
        }
Example #8
0
        public Test Decorate(Test test, MemberInfo member)
        {
            NUnitTestMethod testMethod = test as NUnitTestMethod;

            if (testMethod != null && testMethod.RunState == RunState.Runnable)
            {
                List <Attribute> ignoreAttributes = new List <Attribute>();
                Attribute[]      ignoreAttr       = Reflect.GetAttributes(member, IgnoreBrowserAttributeTypeFullName, true);
                if (ignoreAttr != null)
                {
                    ignoreAttributes.AddRange(ignoreAttr);
                }

                Attribute[] ignoreClassAttributes = Reflect.GetAttributes(member.DeclaringType, IgnoreBrowserAttributeTypeFullName, true);
                if (ignoreClassAttributes != null)
                {
                    ignoreAttributes.AddRange(ignoreClassAttributes);
                }

                foreach (Attribute attr in ignoreAttributes)
                {
                    IgnoreBrowserAttribute browserToIgnoreAttr = attr as IgnoreBrowserAttribute;
                    if (browserToIgnoreAttr != null && IgnoreTestForBrowser(browserToIgnoreAttr.Value))
                    {
                        string ignoreReason = "Ignoring browser " + EnvironmentManager.Instance.Browser.ToString() + ".";
                        if (!string.IsNullOrEmpty(browserToIgnoreAttr.Reason))
                        {
                            ignoreReason = ignoreReason + " " + browserToIgnoreAttr.Reason;
                        }

                        test.RunState     = RunState.Ignored;
                        test.IgnoreReason = ignoreReason;
                    }
                }


                if (test.RunState == RunState.Runnable)
                {
                    NeedsFreshDriverAttribute needsDriverAttr = Reflect.GetAttribute(member, NeedsFreshDriverAttributeTypeFullName, false) as NeedsFreshDriverAttribute;
                    if (needsDriverAttr != null)
                    {
                        test = new WebDriverTestMethod(testMethod, needsDriverAttr.BeforeTest, needsDriverAttr.AfterTest);
                    }
                }
            }

            return(test);
        }
        public Test Decorate(Test test, System.Reflection.MemberInfo member)
        {
            if (test is NUnitTestMethod)
            {
                Attribute attr = Reflect.GetAttribute(
                    member, "NUnit.Framework.Extensions.MaxTimeAttribute", false);
                if (attr != null)
                {
                    int  maxTime       = (int)Reflect.GetPropertyValue(attr, "MaxTime", BindingFlags.Public | BindingFlags.Instance);
                    bool expectFailure = (bool)Reflect.GetPropertyValue(attr, "ExpectFailure", BindingFlags.Public | BindingFlags.Instance);
                    test = new MaxTimeTestCase((NUnitTestMethod)test, maxTime, expectFailure);
                }
            }

            return(test);
        }
Example #10
0
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var typeController   = filterContext.Controller.GetType();
                var actionMethodInfo = filterContext.ActionDescriptor;
                var passController   = Reflect.GetAttribute <AuthorizationPassAttribute>(typeController);
                var passAction       = actionMethodInfo.Attribute <AuthorizationPassAttribute>();

                var passControllerIsValid = true;
                var passActionIsValid     = true;

                if (passController != null)
                {
                    passControllerIsValid = AuthorizationHelper.Validate(passController.Roles);
                }

                if (passAction != null)
                {
                    passActionIsValid = AuthorizationHelper.Validate(passAction.Roles);
                }

                if (!(passActionIsValid && passControllerIsValid))
                {
                    HandleUnauthorizedInfo model = null;
                    if (!passControllerIsValid)
                    {
                        model = new HandleUnauthorizedInfo();
                    }
                    else
                    {
                        model = new HandleUnauthorizedInfo();
                    }

                    filterContext.Result = new ViewResult
                    {
                        ViewName = "Unauthorized",
                        TempData = filterContext.Controller.TempData,
                        ViewData = new ViewDataDictionary <HandleUnauthorizedInfo>(model),
                    };

                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.StatusCode = 500;
                }
            }
        }
Example #11
0
        public Test Decorate(Test test, MemberInfo member)
        {
            Attribute attr = Reflect.GetAttribute(member, "NUnit.Framework.MultiCultureAttribute", true);

            if (attr == null)
            {
                return(test);
            }

            string cultures = Reflect.GetPropertyValue(attr, "Cultures") as string;

            if (cultures == null)
            {
                return(test);
            }

            return(new MultiCultureTestSuite(test, cultures));
        }
Example #12
0
        public IEnumerable GetDataFor(ParameterInfo parameter)
        {
            Attribute attr = Reflect.GetAttribute(parameter, ParameterDataAttribute, false);

            if (attr == null)
            {
                return(null);
            }

            MethodInfo getData = attr.GetType().GetMethod(
                GetDataMethod,
                new Type[] { typeof(ParameterInfo) });

            if (getData == null)
            {
                return(null);
            }

            return(getData.Invoke(attr, new object[] { parameter }) as IEnumerable);
        }
Example #13
0
        static public void Apply(object inObject, UnityEngine.Object inHost = null)
        {
            if (inObject == null)
            {
                return;
            }

            Type objType = inObject.GetType();

            bool bChanged = false;

            foreach (var field in objType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                if (!field.IsPublic && !field.IsDefined(typeof(SerializeField)))
                {
                    continue;
                }

                DefaultAssetAttribute defaultAssetAttr = Reflect.GetAttribute <DefaultAssetAttribute>(field);
                if (defaultAssetAttr != null)
                {
                    Type               type  = field.FieldType;
                    string             name  = defaultAssetAttr.AssetName;
                    UnityEngine.Object asset = (UnityEngine.Object)field.GetValue(inObject);
                    if (asset == null)
                    {
                        asset = AssetDBUtils.FindAsset(type, name);
                        if (asset != null)
                        {
                            field.SetValue(inObject, asset);
                            bChanged = true;
                        }
                    }
                }
            }

            if (bChanged && inHost)
            {
                EditorUtility.SetDirty(inHost);
            }
        }
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            var typeController           = actionContext.ControllerContext.Controller.GetType();
            var actionMethodInfo         = actionContext.ActionDescriptor;
            var allowAnonymousController = Reflect.GetAttribute <AllowAnonymousAttribute>(typeController);
            var allowAnonymousAction     = actionMethodInfo.GetCustomAttributes <AllowAnonymousAttribute>().FirstOrDefault();

            ExecutionContext.SetObject("isLogged", 0);
            if (allowAnonymousAction == null)
            {
                var token  = actionContext.Request.Headers.GetValues("token").FirstOrDefault();
                int userId = 0;
                int.TryParse(actionContext.Request.Headers.GetValues("user").FirstOrDefault(), out userId);

                if (!AuthorizationHelper.ValidateToken(userId, token))
                {
                    actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                }
                ExecutionContext.SetObject("isLogged", userId);
            }
        }
Example #15
0
        public Test BuildFrom(Type type)
        {
            var suite = new ParameterizedFixtureSuite(type);
            var attr  = (ParameterizedTestFixtureAttribute)Reflect.GetAttribute(type, typeof(ParameterizedTestFixtureAttribute).FullName, true);

            ITestParameterProvider provider;

            if (attr.FactoryName != null)
            {
                provider = Configuration.GetProvider(attr.FactoryName);
            }
            else
            {
                provider = Configuration.GetProvider(attr.Type);
            }

            foreach (var parameter in provider.GetParameters(type))
            {
                suite.Add(BuildFixture(type, attr, parameter));
            }

            return(suite);
        }
        /// <summary>
        /// Executes the NUnit test case
        /// </summary>
        /// <returns>The test result</returns>
        public override TestResult RunTest()
        {
            const string METHOD_NAME = "RunTest: ";

            try
            {
                //Call the base method to run the result within NUnit
                TestResult result = base.RunTest();

                //Get the URL, Login, Password and ProjectId from the parent test fixture attribute
                Type      type      = testMethod.FixtureType;
                Attribute attribute = Reflect.GetAttribute(type, "Inflectra.SpiraTest.AddOns.SpiraTestNUnitAddIn.SpiraTestFramework.SpiraTestConfigurationAttribute", false);
                if (attribute == null)
                {
                    throw new Exception("Cannot retrieve the SpiraTestConfiguration attribute from the test fixture");
                }

                //Get the various properties from the attribute
                string         url       = (string)Reflect.GetPropertyValue(attribute, "Url", BindingFlags.Public | BindingFlags.Instance);
                string         login     = (string)Reflect.GetPropertyValue(attribute, "Login", BindingFlags.Public | BindingFlags.Instance);
                string         password  = (string)Reflect.GetPropertyValue(attribute, "Password", BindingFlags.Public | BindingFlags.Instance);
                int            projectId = (int)Reflect.GetPropertyValue(attribute, "ProjectId", BindingFlags.Public | BindingFlags.Instance);
                Nullable <int> releaseId = null;
                if (Reflect.GetPropertyValue(attribute, "ReleaseId", BindingFlags.Public | BindingFlags.Instance) != null)
                {
                    releaseId = (Nullable <int>)Reflect.GetPropertyValue(attribute, "ReleaseId", BindingFlags.Public | BindingFlags.Instance);
                }
                Nullable <int> testSetId = null;
                if (Reflect.GetPropertyValue(attribute, "TestSetId", BindingFlags.Public | BindingFlags.Instance) != null)
                {
                    testSetId = (Nullable <int>)Reflect.GetPropertyValue(attribute, "TestSetId", BindingFlags.Public | BindingFlags.Instance);
                }
                string runnerName = Reflect.GetPropertyValue(attribute, "Runner", BindingFlags.Public | BindingFlags.Instance).ToString();

                //Now we need to extract the result information
                int executionStatusId = -1;
                if (!result.Executed)
                {
                    //Set status to 'Not Run'
                    executionStatusId = 3;
                }
                else
                {
                    //If no codes are found, default to blocked;
                    executionStatusId = 5;
                    if (result.IsFailure)
                    {
                        //Set status to 'Failed'
                        executionStatusId = 1;
                    }
                    if (result.IsSuccess)
                    {
                        //Set status to 'Passed'
                        executionStatusId = 2;
                    }
                    if (result.IsError)
                    {
                        //Set status to 'Failed'
                        executionStatusId = 1;
                    }
                }

                //Extract the other information
                string   testCaseName = result.Name;
                string   message      = result.Message;
                string   stackTrace   = result.StackTrace;
                int      assertCount  = result.AssertCount;
                DateTime startDate    = DateTime.Now.AddSeconds(-result.Time);
                DateTime endDate      = DateTime.Now;

                //Specify that we will be using TLS 1.2 if this is HTTPS
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                //Instantiate the web-service proxy class and set the URL from the text box
                bool success = false;
                SpiraImportExport.ImportExport spiraTestExecuteProxy = new SpiraImportExport.ImportExport();
                spiraTestExecuteProxy.Url = url + TEST_EXECUTE_WEB_SERVICES_URL;

                //Create a new cookie container to hold the session handle
                CookieContainer cookieContainer = new CookieContainer();
                spiraTestExecuteProxy.CookieContainer = cookieContainer;

                //Attempt to authenticate the user
                success = spiraTestExecuteProxy.Connection_Authenticate(login, password);
                if (!success)
                {
                    throw new Exception("Cannot authenticate with SpiraTest, check the URL, login and password");
                }

                //Now connect to the specified project
                success = spiraTestExecuteProxy.Connection_ConnectToProject(projectId);
                if (!success)
                {
                    throw new Exception("Cannot connect to the specified project, check permissions of user!");
                }

                //Now actually record the test run itself
                SpiraImportExport.RemoteTestRun remoteTestRun = new SpiraImportExport.RemoteTestRun();
                remoteTestRun.TestCaseId        = testCaseId;
                remoteTestRun.ReleaseId         = releaseId;
                remoteTestRun.TestSetId         = testSetId;
                remoteTestRun.StartDate         = startDate;
                remoteTestRun.EndDate           = endDate;
                remoteTestRun.ExecutionStatusId = executionStatusId;
                remoteTestRun.RunnerName        = runnerName;
                remoteTestRun.RunnerTestName    = testCaseName;
                remoteTestRun.RunnerAssertCount = assertCount;
                remoteTestRun.RunnerMessage     = message;
                remoteTestRun.RunnerStackTrace  = stackTrace;
                spiraTestExecuteProxy.TestRun_RecordAutomated1(remoteTestRun);

                //Close the SpiraTest connection
                spiraTestExecuteProxy.Connection_Disconnect();

                //Return the result
                return(result);
            }
            catch (Exception exception)
            {
                //Log error then rethrow (catch any errors associated with the diagnostics)
                try
                {
                    System.Diagnostics.EventLog.WriteEntry(SpiraTestAddin.SOURCE_NAME, CLASS_NAME + METHOD_NAME + exception.Message, System.Diagnostics.EventLogEntryType.Error);
                }
                catch {}
                throw exception;
            }
        }
Example #17
0
        public static bool Validate <T>() where T : class
        {
            var pass = Reflect.GetAttribute <AuthorizationPassAttribute>(typeof(T));

            return(pass != null?Validate(pass.Roles) : true);
        }