From 86970af09a7f126d0522aa520e492b6755ebdd14 Mon Sep 17 00:00:00 2001 From: Sean Kirby Date: Tue, 28 May 2024 19:54:29 -0400 Subject: [PATCH] allow the user to be specified in the controller test helper --- CHANGELOG.md | 1 + README.md | 2 +- lib/nulogy_graphql_api/rspec/graphql_helpers.rb | 6 ++++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 545bec5..7de2b28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ _none_ * Expose the `#check_changes` and `#write_schema_to_file` methods on the `NulogyGraphqlApi::Tasks::SchemaGenerator` to give the user more control over how to build their tooling. +* Allow the user to be specified for the `request_graphql` test helper. ## 3.0.1 (2024-01-30) diff --git a/README.md b/README.md index 30c1547..c15ff39 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ The `request_graphql` helper issues a POST request against the provided URL. Thi ```ruby RSpec.describe MyApp::Graphql::Query, :graphql, type: :request do it "returns 401 Unauthorized given an unauthenticated request" do - gql_response = request_graphql(url, <<~GRAPHQL, headers: { "HTTP_AUTHORIZATION" => nil }) + gql_response = request_graphql(url, <<~GRAPHQL, headers: { "HTTP_AUTHORIZATION" => nil }, user: default_user) query { entities { id diff --git a/lib/nulogy_graphql_api/rspec/graphql_helpers.rb b/lib/nulogy_graphql_api/rspec/graphql_helpers.rb index 13debc8..ce405be 100644 --- a/lib/nulogy_graphql_api/rspec/graphql_helpers.rb +++ b/lib/nulogy_graphql_api/rspec/graphql_helpers.rb @@ -1,5 +1,7 @@ module NulogyGraphqlApi module GraphqlHelpers + # Prefer the `request_graphql` method over this one because it exercises more of the stack but doesn't run + # much slower. def execute_graphql(query, schema, variables: {}, context: {}) camelized_variables = variables.deep_transform_keys! { |key| key.to_s.camelize(:lower) } || {} @@ -13,11 +15,11 @@ def execute_graphql(query, schema, variables: {}, context: {}) response.to_h.deep_symbolize_keys end - def request_graphql(url, query, variables: {}, headers: {}) + def request_graphql(url, query, variables: {}, headers: {}, user: nil) params = { query: query, variables: variables }.to_json default_headers = { CONTENT_TYPE: "application/json", - HTTP_AUTHORIZATION: basic_auth_token(default_user.login) + HTTP_AUTHORIZATION: basic_auth_token((user || default_user).login) } post url, params: params, headers: default_headers.merge(headers)