0

Trying to write some integration tests for the first time in .NET - specifically for HotChocolate.

I’ve got had tests working with the WebApplicationFactory, the final part left is trying to now mock the authentication. I tried to set it up based on Mock Authentication but I’m querying the data with IRequestExecutor and finding the de-facto way of setting it up doesn't actually fire:

        [Fact]
        public async Task GetJokes()
        {
            var query =
                @"query Jokes {
                  jokes(jokeLength: SMALL) {
                    nodes {
                      id
                      body
                    }
                  }
                }";
            
            var request = QueryRequestBuilder.New().SetQuery(query).Create();
            var executor = await _factory.Services.GetRequestExecutorAsync();
            var result = await executor.ExecuteAsync(request);
            (await result.ToJsonAsync()).MatchSnapshot();
        }

When I debug this I never hit HandleAuthenticateAsync in my AuthHandler’s (setup from the Mock Authentication linked above), so the claims are never added - so looks like those only run for the httpClient requests - is there a way to configure the IRequestExecutor to be authenticated too?

Kieran Osgood
  • 918
  • 6
  • 17

1 Answers1

1

You can add the ClaimsPrincipal directly as property to the QueryRequestBuilder.

var request = QueryRequestBuilder
.New()
.SetQuery(query)
.AddProperty(nameof(ClaimsPrincipal), new ClaimsPrincipal(new ClaimsIdentity("test")))
.Create();
Matthias
  • 1,267
  • 1
  • 15
  • 27
  • You, are a saint. Thank you so much! Out of curiosity, is there anywhere in documentation this is mentioned? Struggled hard to get this working for a while - but I couldn't locate anything concrete pointing me towards what extension methods to use etc. – Kieran Osgood Sep 09 '21 at 04:06
  • I found it somewhere in the [slack channel](https://app.slack.com/client/TD98NH6TS/). Saw your questions there too. ^^ It's a big issue that most answers can only be found in slack and are not public. – Matthias Sep 09 '21 at 08:59
  • Ah! I did wonder if you were in the channel (name was familiar) - I did try searching the channels but didn't quite know what I was looking for I suppose - But yeah it'd be great if regularly the most common things got converted to documentation - theres a lot of fantastic work going on improving the code but the documentation can be hard to locate - especially fresh example – Kieran Osgood Sep 09 '21 at 09:28