From b6c766888d8dd5c1af5f0f1b9f4283d983aa7c49 Mon Sep 17 00:00:00 2001 From: KobeNguyenT <7845001+kobenguyent@users.noreply.github.com> Date: Sat, 19 Aug 2023 19:07:31 +0200 Subject: [PATCH] fix: add more methods to graphql helper (#3787) --- docs/helpers/GraphQL.md | 21 ++++++++++++++ lib/helper/GraphQL.js | 25 +++++++++++++++++ test/graphql/GraphQL_test.js | 54 ++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/docs/helpers/GraphQL.md b/docs/helpers/GraphQL.md index df69292d5..82d7bbfa5 100644 --- a/docs/helpers/GraphQL.md +++ b/docs/helpers/GraphQL.md @@ -68,6 +68,27 @@ Prepares request for axios call Returns **[object][2]** graphQLRequest +### amBearerAuthenticated + +Adds a header for Bearer authentication + +```js +// we use secret function to hide token from logs +I.amBearerAuthenticated(secret('heregoestoken')) +``` + +#### Parameters + +- `accessToken` **([string][3] | CodeceptJS.Secret)** Bearer access token + +### haveRequestHeaders + +Sets request headers for all requests of this test + +#### Parameters + +- `headers` **[object][2]** headers list + ### sendMutation Send query to GraphQL endpoint over http diff --git a/lib/helper/GraphQL.js b/lib/helper/GraphQL.js index 5f9196845..fb87f16a2 100644 --- a/lib/helper/GraphQL.js +++ b/lib/helper/GraphQL.js @@ -81,6 +81,8 @@ class GraphQL extends Helper { 'Content-Type': 'application/json', }); + request.headers = { ...this.headers, ...request.headers }; + if (this.config.onRequest) { await this.config.onRequest(request); } @@ -200,5 +202,28 @@ class GraphQL extends Helper { _setRequestTimeout(newTimeout) { this.options.timeout = newTimeout; } + + /** + * Sets request headers for all requests of this test + * + * @param {object} headers headers list + */ + haveRequestHeaders(headers) { + this.headers = { ...this.headers, ...headers }; + } + + /** + * Adds a header for Bearer authentication + * + * ```js + * // we use secret function to hide token from logs + * I.amBearerAuthenticated(secret('heregoestoken')) + * ``` + * + * @param {string | CodeceptJS.Secret} accessToken Bearer access token + */ + amBearerAuthenticated(accessToken) { + this.haveRequestHeaders({ Authorization: `Bearer ${accessToken}` }); + } } module.exports = GraphQL; diff --git a/test/graphql/GraphQL_test.js b/test/graphql/GraphQL_test.js index e27f0898f..371b951c9 100644 --- a/test/graphql/GraphQL_test.js +++ b/test/graphql/GraphQL_test.js @@ -136,4 +136,58 @@ describe('GraphQL', () => { }); }); }); + + describe('headers', () => { + it('should set headers for all requests multiple times', async () => { + I.haveRequestHeaders({ 'XY1-Test': 'xy1-first' }); + I.haveRequestHeaders({ 'XY1-Test': 'xy1-second' }); + I.haveRequestHeaders({ 'XY2-Test': 'xy2' }); + + const response = await I.sendQuery('{ user(id: 0) { id name email }}'); + + response.config.headers.should.have.property('XY1-Test'); + response.config.headers['XY1-Test'].should.eql('xy1-second'); + + response.config.headers.should.have.property('XY2-Test'); + response.config.headers['XY2-Test'].should.eql('xy2'); + + response.config.headers.should.have.property('X-Test'); + response.config.headers['X-Test'].should.eql('test'); + }); + + it('should override the header set for all requests', async () => { + I.haveRequestHeaders({ 'XY-Test': 'first' }); + + const response = await I.sendQuery('{ user(id: 0) { id name email }}'); + + response.config.headers.should.have.property('XY-Test'); + response.config.headers['XY-Test'].should.eql('first'); + + response.config.headers.should.have.property('X-Test'); + response.config.headers['X-Test'].should.eql('test'); + }); + + it('should set Bearer authorization', async () => { + I.amBearerAuthenticated('token'); + const response = await I.sendQuery('{ user(id: 0) { id name email }}'); + + response.config.headers.should.have.property('Authorization'); + response.config.headers.Authorization.should.eql('Bearer token'); + + response.config.headers.should.have.property('X-Test'); + response.config.headers['X-Test'].should.eql('test'); + }); + + it('should set Bearer authorization multiple times', async () => { + I.amBearerAuthenticated('token1'); + I.amBearerAuthenticated('token2'); + const response = await I.sendQuery('{ user(id: 0) { id name email }}'); + + response.config.headers.should.have.property('Authorization'); + response.config.headers.Authorization.should.eql('Bearer token2'); + + response.config.headers.should.have.property('X-Test'); + response.config.headers['X-Test'].should.eql('test'); + }); + }); });