Claims no Testmethod

Asked

Viewed 75 times

3

Good morning, you guys, I’m starting TDD in Visual Studio, and I’m having problems/difficulties passing Claims permissions on the request that is made to the server. The following error is occurring:

Message: Test method Maintestes.Areas.Auxiliary.Controllers.AX001_CFOP.SvcControllerUnitTest.Autocompletebycfoptest threw Exception: System.Invalidoperationexception: ID7024: There was an attempt to use the Claimsprincipalpermission attribute and possibly there is no configuration section defined. See the internal exception for details. Also, make sure that the Claimsauthorizationmanager element is set in the section

Below the code of my test method

    /// <summary>
    ///     Realiza teste unitario no metodo AutocompleteByCFOP
    /// </summary>
    /// <remarks>
    ///     Foi utilizado microsoft fakes para simular comunicação com request e como banco de dados para isolamento do codigo.
    /// </remarks>
    [TestMethod]
    [Owner("Julio")]
    [TestCategory("UnitTest")]
    [TestCategory("MainTests")]
    public void AutocompleteByCfopTest()
    {
        // Cria objeto de retorno do metodo fake
        var listCfop = new List<TechShop.Model.AX001_CFOP>
        {
            _ax001Cfop
        };

        // Classe fake para o manager
        var manager = new StubAX001_CFOPManager()
        {
            FindExpressionOfFuncOfAX001_CFOPBoolean = s => listCfop
        };

        // Cria um metodo Fake do HttpRequestMessage para substituir o request
        using (var request = new StubHttpRequestMessage())
        {
            request.SetConfiguration(new HttpConfiguration());
            request.Method = HttpMethod.Get;
            request.Headers.Add("Authorization",
                                "Bearer 0cr1oRh2byktwIXIDQspQtCkh-kmwZ716NwcfVoeUJ4HJ8mJ2X8FIHcBBRMF3K6I8AZcUYXj7RuvWoQrJm3V6AxGF3OIpxWZMOSwxNdxUVCmwZWZF2hju-tgAM5");

            using (var controller = new CFOPSvcController { Request = request, Manager = manager })
            {
                using (var response = controller.AutocompleteByCFOP(_ax001Cfop.AX001_ID.ToString(), _ax001Cfop.AX001_Type, _ax001Cfop.AX001_Origin))
                {
                    foreach (var item in (List<CFOPFormVM>)((ObjectContent)response.Content).Value)
                    {
                        Assert.AreEqual(item.ID, _ax001Cfop.AX001_ID);
                        Assert.AreEqual(item.Description, _ax001Cfop.AX001_Description);
                    }
                }
            }
        }
    }

Has anyone been through this or knows the solution to apply Claims in Testing?

  • 1

    And did you do what exception message suggested? " [...] possibly there is no configuration section set. See the internal exception for details. Also, make sure that the Claimsauthorizationmanager element is set in the section"

  • Yes, even enter the "Claims" manually. var Claims = new List<Claim>() { new Claim(Claimtypes.Role, "auxiliary-cfop:query;view;add;! Edit;! delete") }; var Identity = new Claimsidentity(Claims, Claimtypes.Role); var principal = new Claimsprincipal(Identity); controller.User = new Claimsmain(main);

1 answer

0


Ola Julio Mendonça

You need to define the elements of claimsAuthorizationManager and identityConfiguration in your project configuration file (App.config), you must inform the system.identityModel and system.identityModel.services tag in configSections.

<configuration>
    <configSections>
        <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> 
    </configSections>
    <system.identityModel>
        <identityConfiguration>
            <claimsAuthorizationManager type="MainTests.Security.TestAuthorizationManager, MainTests" />
        </identityConfiguration>
    </system.identityModel>
</configuration>

You must define the class path that validates the authorization and the class project name in claimsAuthorizationManager.

References:

https://forums.asp.net/p/2106526/6091223.aspx?Re+Performing+TestMethod+with+Claims

  • I think your observation is valid, but my intention is to give the merits to the person who answered the question, without belittling the forum that really solved the problem. Answer will be improved.

  • I had to post 4 to 5 times the answer why part of my answer was not being shown.

  • Got it, now my comment doesn’t make sense anymore(fortunately :D) since you edited the answer, I will delete to not pollute the content.

  • I could remove the negative vote too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.