/// <summary>
    /// Returns the data to be used to test the theory
    /// </summary>
    /// <param name="methodInfo"></param>
    /// <returns></returns>
    public override IEnumerable <object[]> GetData(MethodInfo methodInfo)
    {
        methodInfo.CreateClientInstance();

        string url = _url;

        if (_obkInlineDataEnum == UrlTypeEnum.InController)
        {
            var controllerName = methodInfo.ReflectedType.Name.Split("Controller")[0];
            url = $"{controllerName}/{_url}";
        }

        var allRoles = MilvaTestClient <MilvaTestStartup> .AcceptedRoles;

        allRoles.IsNull("Please enter application Roles.");

        _acceptedRoles.Trim();
        allRoles.Trim();

        TestExpectected testExpectected;

        var testLanguage = StaticMethods.GetRandomLanguageForTest();

        if (_authorizeTypeEnum == AuthorizeTypeEnum.Or)
        {
            foreach (var role in allRoles)
            {
                var isAccepted = _acceptedRoles.Any(p => p == role);

                var token = RequestHelper.GetTokenByRoles(role).GetAwaiter().GetResult();

                if (isAccepted)
                {
                    testExpectected = TestExpectected.GetTestExpectectedInstance(null, null, null);

                    yield return(new object[] { SafetyTestInject.GetSafetyTestInject(url, _httpMethod, new CultureInfo(testLanguage).GetSpecificStringLocalizer(), testLanguage, testExpectected, isAccepted, token) });
                }
                else
                {
                    testExpectected = TestExpectected.GetTestExpectectedInstance(statusCode: MilvaStatusCodes.Status403Forbidden, isSuccesful: false, messageKey: "Forbidden");

                    yield return(new object[] { SafetyTestInject.GetSafetyTestInject(url, _httpMethod, new CultureInfo(testLanguage).GetSpecificStringLocalizer(), testLanguage, testExpectected, isAccepted, token) });
                }
            }

            testExpectected = TestExpectected.GetTestExpectectedInstance(statusCode: MilvaStatusCodes.Status401Unauthorized, isSuccesful: false, messageKey: "Unauthorized");

            yield return(new object[] { SafetyTestInject.GetSafetyTestInject(url, _httpMethod, new CultureInfo(testLanguage).GetSpecificStringLocalizer(), testLanguage, testExpectected, false, null) });
        }
        else if (_authorizeTypeEnum == AuthorizeTypeEnum.And)
        {
            throw new MilvaDeveloperException("This option is not ready yet.");
        }
        else if (_authorizeTypeEnum == AuthorizeTypeEnum.None)
        {
            testExpectected = TestExpectected.GetTestExpectectedInstance(null, null, null);

            yield return(new object[] { SafetyTestInject.GetSafetyTestInject(url, _httpMethod, new CultureInfo(testLanguage).GetSpecificStringLocalizer(), testLanguage, testExpectected, true, null) });
        }
    }
Esempio n. 2
0
 /// <summary>
 /// Checks the result of safety tests.
 /// </summary>
 /// <param name="testExpectected"></param>
 /// <param name="objectResponse"></param>
 /// <param name="isAccepted"></param>
 /// <param name="stringLocalizer"></param>
 public static void CheckResponseForSecurity(TestExpectected testExpectected, ObjectResponse <object> objectResponse, bool isAccepted, IStringLocalizer stringLocalizer)
 {
     if (isAccepted)
     {
         NotEqual(objectResponse.StatusCode, StatusCodes.Status403Forbidden);
         NotEqual(objectResponse.StatusCode, StatusCodes.Status401Unauthorized);
     }
     else
     {
         CheckMessage(testExpectected.MessageKey, objectResponse.Message, stringLocalizer);
         Equal(testExpectected.StatusCode, objectResponse.StatusCode);
         Equal(testExpectected.Successful, objectResponse.Success);
     }
 }