Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PassThrough AuthClient #1771

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/auth/passthrough.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {GaxiosOptions} from 'gaxios';
import {AuthClient} from './authclient';

/**
* An AuthClient without any Authentication information. Useful for:
* - Anonymous access
* - Local Emulators
* - Testing Environments
*
*/
export class PassThroughClient extends AuthClient {
/**
* Creates a request without any authentication headers or checks.
*
* @remarks
*
* In testing environments it may be useful to change the provided
* {@link AuthClient.transporter} for any desired request overrides/handling.
*
* @param opts
* @returns The response of the request.
*/
async request<T>(opts: GaxiosOptions) {
return this.transporter.request<T>(opts);
}

/**
* A required method of the base class.
* Always will return an empty object.
*
* @returns {}
*/
async getAccessToken() {
return {};
danielbankhead marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* A required method of the base class.
* Always will return an empty object.
*
* @returns {}
*/
async getRequestHeaders() {
danielbankhead marked this conversation as resolved.
Show resolved Hide resolved
return {};
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export {
PluggableAuthClient,
PluggableAuthClientOptions,
} from './auth/pluggable-auth-client';
export {PassThroughClient} from './auth/passthrough';
export {DefaultTransporter} from './transporters';

const auth = new GoogleAuth();
Expand Down
25 changes: 3 additions & 22 deletions test/test.authclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,10 @@

import {strict as assert} from 'assert';

import {GaxiosOptions, GaxiosPromise, GaxiosResponse} from 'gaxios';
import {AuthClient} from '../src';
import {Headers} from '../src/auth/oauth2client';
import {PassThroughClient} from '../src';
import {snakeToCamel} from '../src/util';

describe('AuthClient', () => {
class TestAuthClient extends AuthClient {
request<T>(opts: GaxiosOptions): GaxiosPromise<T> {
throw new Error('Method not implemented.');
}

getRequestHeaders(url?: string | undefined): Promise<Headers> {
throw new Error('Method not implemented.');
}

getAccessToken(): Promise<{
token?: string | null | undefined;
res?: GaxiosResponse<any> | null | undefined;
}> {
throw new Error('Method not implemented.');
}
}

it('should accept and normalize snake case options to camel case', () => {
const expected = {
project_id: 'my-projectId',
Expand All @@ -49,11 +30,11 @@ describe('AuthClient', () => {
const camelCased = snakeToCamel(key) as keyof typeof authClient;

// assert snake cased input
let authClient = new TestAuthClient({[key]: value});
let authClient = new PassThroughClient({[key]: value});
assert.equal(authClient[camelCased], value);

// assert camel cased input
authClient = new TestAuthClient({[camelCased]: value});
authClient = new PassThroughClient({[camelCased]: value});
assert.equal(authClient[camelCased], value);
}
});
Expand Down
62 changes: 62 additions & 0 deletions test/test.passthroughclient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {strict as assert} from 'assert';

import * as nock from 'nock';

import {PassThroughClient} from '../src';

describe('AuthClient', () => {
before(async () => {
nock.disableNetConnect();
});

afterEach(async () => {
nock.cleanAll();
});

describe('#getAccessToken', () => {
it('should return an empty object', async () => {
const client = new PassThroughClient();
const token = await client.getAccessToken();

assert.deepEqual(token, {});
});
});

describe('#getRequestHeaders', () => {
it('should return an empty object', async () => {
const client = new PassThroughClient();
const token = await client.getRequestHeaders();

assert.deepEqual(token, {});
});
});

describe('#request', () => {
it('should return the expected response', async () => {
const url = 'https://google.com';
const example = {test: 'payload'};
const scope = nock(url).get('/').reply(200, example);

const client = new PassThroughClient();
const response = await client.request({url});

assert.deepEqual(response.data, example);

scope.done();
});
});
});
Loading