Skip to content

Commit

Permalink
allow the user to be specified in the controller test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sskirby committed May 28, 2024
1 parent 9ba534e commit 1411f0b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ _none_
* Expose the `#check_changes` and `#write_schema_to_file` methods on the
`NulogyGraphqlApi::Tasks::SchemaGenerator#generate_schema` 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)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/nulogy_graphql_api/rspec/graphql_helpers.rb
Original file line number Diff line number Diff line change
@@ -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) } || {}

Expand All @@ -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)
Expand Down

0 comments on commit 1411f0b

Please sign in to comment.