Пример #1
0
    public async Task Roles_Authorized(Action <IRequestExecutorBuilder> configure)
    {
        // arrange
        TestServer server = CreateTestServer(
            builder =>
        {
            configure(builder);
        },
            context =>
        {
            var identity = new ClaimsIdentity("testauth");
            identity.AddClaim(new Claim(
                                  ClaimTypes.Role,
                                  "a"));
            context.User = new ClaimsPrincipal(identity);
        });

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest { Query = "{ roles }" });

        // assert
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);
        result.MatchSnapshot();
    }
    public async Task Get_Middleware_Is_Disabled()
    {
        // arrange
        TestServer server = CreateStarWarsServer(
            configureConventions: e => e.WithOptions(
                new GraphQLServerOptions
        {
            EnableGetRequests = false
        }));

        // act
        ClientQueryResult result =
            await server.GetAsync(new ClientQueryRequest
        {
            Query = @"
                    {
                        hero {
                            name
                        }
                    }"
        });

        // assert
        result.MatchSnapshot();
    }
Пример #3
0
        public async Task NoAuthServices_Authenticated_True(
            ISchema schema)
        {
            // arrange
            TestServer server = CreateTestServer(
                services =>
            {
                services.AddGraphQL(schema);
            },
                context =>
            {
                context.User = new ClaimsPrincipal(
                    new ClaimsIdentity("testauth"));
            });

            var request     = "{ default }";
            var contentType = "application/graphql";

            // act
            HttpResponseMessage message =
                await server.SendPostRequestAsync(request, contentType, null);

            // assert
            Assert.Equal(HttpStatusCode.OK, message.StatusCode);

            var json = await message.Content.ReadAsStringAsync();

            ClientQueryResult result = JsonConvert
                                       .DeserializeObject <ClientQueryResult>(json);

            Assert.Null(result.Errors);
            result.MatchSnapshot();
        }
    public async Task SingleRequest_CreateReviewForEpisode_Variables_Unused()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest
        {
            Query     = @"
                    mutation CreateReviewForEpisode(
                        $ep: Episode!
                        $stars: Int!
                        $commentary: String!
                        $foo: Float) {
                        createReview(episode: $ep, review: {
                            stars: $stars
                            commentary: $commentary
                        } ) {
                            stars
                            commentary
                        }
                    }",
            Variables = new Dictionary <string, object>
            {
                { "ep", "EMPIRE" },
                { "stars", 5 },
                { "commentary", "This is a great movie!" }
            }
        });

        // assert
        result.MatchSnapshot();
    }
    public async Task SingleRequest_Execute_Specific_Operation(
        string operationName)
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest
        {
            Query         = @"
                    query a {
                        a: hero {
                            name
                        }
                    }

                    query b {
                        b: hero {
                            name
                        }
                    }",
            OperationName = operationName
        });

        // assert
        result.MatchSnapshot(new SnapshotNameExtension(operationName));
    }
Пример #6
0
        public async Task Roles_Authorized(IQueryExecutor executor)
        {
            // arrange
            TestServer server = CreateTestServer(
                services =>
            {
                services.AddGraphQL(executor);
            },
                context =>
            {
                var identity = new ClaimsIdentity("testauth");
                identity.AddClaim(new Claim(
                                      ClaimTypes.Role,
                                      "a"));
                context.User = new ClaimsPrincipal(identity);
            });

            var request     = "{ roles }";
            var contentType = "application/graphql";

            // act
            HttpResponseMessage message =
                await server.SendPostRequestAsync(request, contentType, null);

            // assert
            Assert.Equal(HttpStatusCode.OK, message.StatusCode);

            var json = await message.Content.ReadAsStringAsync();

            ClientQueryResult result = JsonConvert
                                       .DeserializeObject <ClientQueryResult>(json);

            Assert.Null(result.Errors);
            result.MatchSnapshot();
        }
Пример #7
0
    public async Task Policy_Resources_Is_IResolverContext(Action <IRequestExecutorBuilder> configure)
    {
        // arrange
        TestServer server = CreateTestServer(
            builder =>
        {
            configure(builder);
            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy("HasAgeDefined", policy =>
                                  policy.RequireAssertion(context =>
                                                          context.Resource is IResolverContext));
            });
        },
            context =>
        {
            context.User = new ClaimsPrincipal(
                new ClaimsIdentity("testauth"));
        });

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest { Query = "{ age }" });

        // assert
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);
        result.MatchSnapshot();
    }
    public async Task Upload_File_Inline_InputObject()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        var query = @"
                query ($upload: Upload!) {
                    objectUpload(input: { file: $upload })
                }";

        var request = JsonConvert.SerializeObject(
            new ClientQueryRequest
        {
            Query     = query,
            Variables = new Dictionary <string, object>
            {
                { "upload", null }
            }
        });

        // act
        var form = new MultipartFormDataContent
        {
            { new StringContent(request), "operations" },
            { new StringContent("{ \"1\": [\"variables.upload\"] }"), "map" },
            { new StringContent("abc"), "1", "foo.bar" },
        };

        ClientQueryResult result = await server.PostMultipartAsync(form, path : "/upload");

        // assert
        result.MatchSnapshot();
    }
Пример #9
0
    public async Task Policy_Authorized(Action <IRequestExecutorBuilder> configure)
    {
        // arrange
        TestServer server = CreateTestServer(
            builder =>
        {
            configure(builder);
            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy("HasAgeDefined", policy =>
                                  policy.RequireAssertion(context =>
                                                          context.User.HasClaim(c =>
                                                                                c.Type == ClaimTypes.DateOfBirth)));
            });
        },
            context =>
        {
            var identity = new ClaimsIdentity("testauth");
            identity.AddClaim(new Claim(
                                  ClaimTypes.DateOfBirth,
                                  "2013-05-30"));
            context.User = new ClaimsPrincipal(identity);
        });

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest { Query = "{ age }" });

        // assert
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);
        result.MatchSnapshot();
    }
Пример #10
0
    public async Task DefaultPolicy_NotFound(Action <IRequestExecutorBuilder> configure)
    {
        // arrange
        TestServer server = CreateTestServer(
            builder =>
        {
            configure(builder);
            builder.Services.AddAuthorization(options =>
            {
                options.DefaultPolicy = null;
            });
        },
            context =>
        {
            context.User = new ClaimsPrincipal(
                new ClaimsIdentity("testauth"));
        });

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest { Query = "{ default }" });

        // assert
        Assert.Equal(HttpStatusCode.OK, result.StatusCode);
        result.MatchSnapshot();
    }
    public async Task SingleRequest_CreateReviewForEpisode_Omit_NonNull_Variable()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest
        {
            Query     = @"
                    mutation CreateReviewForEpisode(
                        $ep: Episode!
                        $review: ReviewInput!) {
                        createReview(episode: $ep, review: $review) {
                            stars
                            commentary
                        }
                    }",
            Variables = new Dictionary <string, object>
            {
                {
                    "review",
                    new Dictionary <string, object>
                    {
                        { "stars", 5 }, { "commentary", "This is a great movie!" },
                    }
                }
            }
        });

        // assert
        result.MatchSnapshot();
    }
    public async Task SingleRequest_Incomplete()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result = await server.PostAsync("{ \"query\":    ");

        // assert
        result.MatchSnapshot();
    }
    public async Task BatchRequest_Empty(string request, int id)
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result = await server.PostAsync(request);

        // assert
        result.MatchSnapshot(new SnapshotNameExtension(id.ToString()));
    }
    public async Task EmptyRequest()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result = await server.PostAsync(string.Empty);

        // assert
        result.MatchSnapshot();
    }
    public async Task Ensure_Middleware_Mapping()
    {
        // arrange
        TestServer server = CreateStarWarsServer("/foo");

        // act
        ClientQueryResult result = await server.PostAsync(string.Empty);

        // assert
        result.MatchSnapshot();
    }
Пример #16
0
    public async Task Get_ActivePersistedQuery()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result =
            await server.GetActivePersistedQueryAsync("md5Hash", "60ddx/GGk4FDObSa6eK0sg==");

        // assert
        result.MatchSnapshot();
    }
    public async Task MapGraphQLHttp_Simple_IsAlive_Test()
    {
        // arrange
        TestServer server = CreateServer(endpoint => endpoint.MapGraphQLHttp());

        // act
        ClientQueryResult result = await server.PostAsync(
            new ClientQueryRequest { Query = "{ __typename }" });

        // assert
        result.MatchSnapshot();
    }
    public async Task Simple_IsAlive_Test()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result = await server.PostAsync(
            new ClientQueryRequest { Query = "{ __typename }" });

        // assert
        result.MatchSnapshot();
    }
Пример #19
0
    public async Task Get_ActivePersistedQuery_NotFound()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result =
            await server.GetActivePersistedQueryAsync("md5Hash", "abc");

        // assert
        result.MatchSnapshot();
    }
Пример #20
0
        public async Task PipedAuthorizeDirectives_Authorized(
            ISchema schema)
        {
            // arrange
            TestServer server = CreateTestServer(
                services =>
            {
                services.AddAuthorization(options =>
                {
                    options.AddPolicy("a", policy =>
                                      policy.RequireAssertion(context =>
                                                              context.User.HasClaim(c =>
                                                                                    c.Type == ClaimTypes.DateOfBirth)));

                    options.AddPolicy("b", policy =>
                                      policy.RequireAssertion(context =>
                                                              context.User.HasClaim(c =>
                                                                                    c.Type == ClaimTypes.Country)));
                });

                services.AddGraphQL(schema);
            },
                context =>
            {
                var identity = new ClaimsIdentity("testAuth");
                identity.AddClaim(new Claim(
                                      ClaimTypes.DateOfBirth,
                                      "2013-05-30"));
                identity.AddClaim(new Claim(
                                      ClaimTypes.Country,
                                      "US"));
                context.User = new ClaimsPrincipal(identity);
            });

            var request     = "{ piped }";
            var contentType = "application/graphql";

            // act
            HttpResponseMessage message =
                await server.SendPostRequestAsync(request, contentType, null);

            // assert
            Assert.Equal(HttpStatusCode.OK, message.StatusCode);

            var json = await message.Content.ReadAsStringAsync();

            ClientQueryResult result = JsonConvert
                                       .DeserializeObject <ClientQueryResult>(json);

            Assert.Null(result.Errors);
            result.MatchSnapshot();
        }
    public async Task EmptyForm_Test()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        var form = new MultipartFormDataContent();

        ClientQueryResult result = await server.PostMultipartAsync(form);

        // assert
        result.MatchSnapshot();
    }
    public async Task Include_Query_Plan()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result = await server.PostAsync(
            new ClientQueryRequest { Query = "{ __typename }" },
            includeQueryPlan : true);

        // assert
        result.MatchSnapshot();
    }
Пример #23
0
    public async Task Simple_IsAlive_Test_On_Non_GraphQL_Path()
    {
        // arrange
        TestServer server = CreateStarWarsServer("/foo");

        // act
        ClientQueryResult result = await server.GetAsync(
            new ClientQueryRequest { Query = "{ __typename }" },
            "/foo");

        // assert
        result.MatchSnapshot();
    }
Пример #24
0
        public async Task Policy_Is_Executed_After_Resolver_User_Is_Denied(ISchema schema)
        {
            // arrange
            TestServer server = CreateTestServer(
                services =>
            {
                services.AddAuthorization(options =>
                {
                    options.AddPolicy("a", policy =>
                                      policy.RequireAssertion(context =>
                    {
                        if (context.Resource is IMiddlewareContext m &&
                            m.Result is string s &&
                            s == "bar")
                        {
                            return(true);
                        }
                        return(false);
                    }));
                });

                services.AddGraphQL(schema);
            },
                context =>
            {
                var identity = new ClaimsIdentity("testauth");
                identity.AddClaim(new Claim(
                                      ClaimTypes.DateOfBirth,
                                      "2013-05-30"));
                context.User.AddIdentity(identity);
            });

            var request     = "{ afterResolver }";
            var contentType = "application/graphql";

            // act
            HttpResponseMessage message =
                await server.SendPostRequestAsync(request, contentType, null);

            // assert
            Assert.Equal(HttpStatusCode.OK, message.StatusCode);

            var json = await message.Content.ReadAsStringAsync();

            ClientQueryResult result = JsonConvert
                                       .DeserializeObject <ClientQueryResult>(json);

            Assert.NotNull(result.Errors);
            result.MatchSnapshot();
        }
    public async Task MapWithNoOperations_Test()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        var form = new MultipartFormDataContent
        {
            { new StringContent("{}"), "map" },
        };

        ClientQueryResult result = await server.PostMultipartAsync(form);

        // assert
        result.MatchSnapshot();
    }
    public async Task MissingObjectPathsForKey_Test()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        var form = new MultipartFormDataContent
        {
            { new StringContent("{}"), "operations" },
            { new StringContent("{ \"1\": [] }"), "map" },
        };

        ClientQueryResult result = await server.PostMultipartAsync(form);

        // assert
        result.MatchSnapshot();
    }
    public async Task Upload_File_In_List()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        var query = @"
                query ($input: [[InputWithFileInput!]]) {
                    listUpload(input: $input)
                }";

        var request = JsonConvert.SerializeObject(
            new ClientQueryRequest
        {
            Query     = query,
            Variables = new Dictionary <string, object>
            {
                {
                    "input",
                    new List <object>
                    {
                        new List <object>
                        {
                            new Dictionary <string, object> {
                                { "file", null }
                            }
                        }
                    }
                }
            }
        });

        // act
        var form = new MultipartFormDataContent
        {
            { new StringContent(request), "operations" },
            { new StringContent("{ \"1\": [\"variables.input.0.0.file\"] }"), "map" },
            { new StringContent("abc"), "1", "foo.bar" },
        };

        ClientQueryResult result = await server.PostMultipartAsync(form, path : "/upload");

        // assert
        result.MatchSnapshot();
    }
    public async Task SingleRequest_SyntaxError()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest
        {
            Query = @"
                    {
                        ähero {
                            name
                        }
                    }"
        });

        // assert
        result.MatchSnapshot();
    }
    public async Task SingleRequest_GetHeroName_Casing_Is_Preserved()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        // act
        ClientQueryResult result =
            await server.PostAsync(new ClientQueryRequest
        {
            Query = @"
                    {
                        HERO: hero {
                            name
                        }
                    }"
        });

        // assert
        result.MatchSnapshot();
    }
    public async Task Upload_Too_Large_File_Test()
    {
        // arrange
        TestServer server = CreateStarWarsServer();

        var query = @"
                query ($upload: Upload!) {
                    singleUpload(file: $upload)
                }";

        var request = JsonConvert.SerializeObject(
            new ClientQueryRequest
        {
            Query     = query,
            Variables = new Dictionary <string, object>
            {
                { "upload", null }
            }
        });

        var count  = 1024 * 1024 * 129;
        var buffer = new byte[count];

        for (int i = 0; i < buffer.Length; i++)
        {
            buffer[i] = 0xFF;
        }

        // act
        var form = new MultipartFormDataContent
        {
            { new StringContent(request), "operations" },
            { new StringContent("{ \"1\": [\"variables.upload\"] }"), "map" },
            { new ByteArrayContent(buffer), "1", "foo.bar" },
        };

        ClientQueryResult result = await server.PostMultipartAsync(form, path : "/upload");

        // assert
        result.MatchSnapshot();
    }