Skip to content

Commit

Permalink
refactor(test): refactor authentication tests (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Oct 12, 2023
1 parent 71984ed commit e0d459f
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 57 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The full API of this library can be found in [api.md file](https://github.com/op
import OpenAI from 'openai';

const openai = new OpenAI({
apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"]
apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"]
});

async function main() {
Expand Down Expand Up @@ -73,7 +73,7 @@ This library includes TypeScript definitions for all request params and response
import OpenAI from 'openai';

const openai = new OpenAI({
apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"]
apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"]
});

async function main() {
Expand Down
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import * as API from 'openai/resources/index';

export interface ClientOptions {
/**
* Defaults to process.env["OPENAI_API_KEY"].
* Defaults to process.env['OPENAI_API_KEY'].
*/
apiKey?: string;

/**
* Defaults to process.env['OPENAI_ORG_ID'].
*/
organization?: string | null;

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*/
Expand Down Expand Up @@ -72,21 +77,20 @@ export interface ClientOptions {
* Only set this option to `true` if you understand the risks and have appropriate mitigations in place.
*/
dangerouslyAllowBrowser?: boolean;

organization?: string | null;
}

/** API Client for interfacing with the OpenAI API. */
export class OpenAI extends Core.APIClient {
apiKey: string;
organization?: string | null;
organization: string | null;

private _options: ClientOptions;

/**
* API Client for interfacing with the OpenAI API.
*
* @param {string} [opts.apiKey=process.env['OPENAI_API_KEY']] - The API Key to send to the API.
* @param {string} [opts.apiKey==process.env['OPENAI_API_KEY'] ?? undefined]
* @param {string | null} [opts.organization==process.env['OPENAI_ORG_ID'] ?? null]
* @param {string} [opts.baseURL] - Override the default base URL for the API.
* @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
Expand All @@ -95,7 +99,6 @@ export class OpenAI extends Core.APIClient {
* @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.
* @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
* @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
* @param {string | null} [opts.organization]
*/
constructor({
apiKey = Core.readEnv('OPENAI_API_KEY'),
Expand All @@ -104,7 +107,7 @@ export class OpenAI extends Core.APIClient {
}: ClientOptions = {}) {
if (apiKey === undefined) {
throw new Errors.OpenAIError(
"The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'my apiKey' }).",
"The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'My API Key' }).",
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/audio/transcriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/audio/translations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/chat/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/edits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/embeddings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/fine-tunes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/fine-tuning/jobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
2 changes: 1 addition & 1 deletion tests/api-resources/moderations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OpenAI from 'openai';
import { Response } from 'node-fetch';

const openai = new OpenAI({
apiKey: 'something1234',
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

Expand Down
57 changes: 21 additions & 36 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('instantiate client', () => {
const client = new OpenAI({
baseURL: 'http://localhost:5000/',
defaultHeaders: { 'X-My-Default-Header': '2' },
apiKey: 'my api key',
apiKey: 'My API Key',
});

test('they are used in the request', () => {
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('instantiate client', () => {
const client = new OpenAI({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo' },
apiKey: 'my api key',
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo');
});
Expand All @@ -64,7 +64,7 @@ describe('instantiate client', () => {
const client = new OpenAI({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo', hello: 'world' },
apiKey: 'my api key',
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world');
});
Expand All @@ -73,7 +73,7 @@ describe('instantiate client', () => {
const client = new OpenAI({
baseURL: 'http://localhost:5000/',
defaultQuery: { hello: 'world' },
apiKey: 'my api key',
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo');
});
Expand All @@ -82,7 +82,7 @@ describe('instantiate client', () => {
test('custom fetch', async () => {
const client = new OpenAI({
baseURL: 'http://localhost:5000/',
apiKey: 'my api key',
apiKey: 'My API Key',
fetch: (url) => {
return Promise.resolve(
new Response(JSON.stringify({ url, custom: true }), {
Expand All @@ -99,7 +99,7 @@ describe('instantiate client', () => {
test('custom signal', async () => {
const client = new OpenAI({
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
apiKey: 'my api key',
apiKey: 'My API Key',
fetch: (...args) => {
return new Promise((resolve, reject) =>
setTimeout(
Expand All @@ -124,57 +124,42 @@ describe('instantiate client', () => {

describe('baseUrl', () => {
test('trailing slash', () => {
const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'my api key' });
const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' });
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});

test('no trailing slash', () => {
const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'my api key' });
const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'My API Key' });
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});
});

test('maxRetries option is correctly set', () => {
const client = new OpenAI({ maxRetries: 1, apiKey: 'my api key' });
const client = new OpenAI({ maxRetries: 1, apiKey: 'My API Key' });
expect(client.maxRetries).toEqual(1);

// default
const client2 = new OpenAI({ apiKey: 'my api key' });
const client2 = new OpenAI({ apiKey: 'My API Key' });
expect(client2.maxRetries).toEqual(2);
});

test('with minimal arguments', () => {
// set API Key via env var
process.env['OPENAI_API_KEY'] = 'env var api key';
test('with environment variable arguments', () => {
// set options via env var
process.env['OPENAI_API_KEY'] = 'My API Key';
const client = new OpenAI();
expect(client.apiKey).toBe('env var api key');
expect(client.apiKey).toBe('My API Key');
});

test('with apiKey argument', () => {
process.env['OPENAI_API_KEY'] = 'env var api key';

const client = new OpenAI({ apiKey: 'another api key' });
expect(client.apiKey).toBe('another api key');
});

test('with options argument', () => {
process.env['OPENAI_API_KEY'] = 'env var api key';

// apiKey
const client = new OpenAI({ apiKey: 'my api key' });
expect(client.apiKey).toBe('my api key');
});

test('with disabled authentication', () => {
// fails if no API Key provided
expect(() => {
new OpenAI();
}).toThrow();
test('with overriden environment variable arguments', () => {
// set options via env var
process.env['OPENAI_API_KEY'] = 'another My API Key';
const client = new OpenAI({ apiKey: 'My API Key' });
expect(client.apiKey).toBe('My API Key');
});
});

describe('request building', () => {
const client = new OpenAI({ apiKey: 'my api key' });
const client = new OpenAI({ apiKey: 'My API Key' });

describe('Content-Length', () => {
test('handles multi-byte characters', () => {
Expand All @@ -200,7 +185,7 @@ describe('retries', () => {
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new OpenAI({ apiKey: 'my api key', timeout: 2000, fetch: testFetch });
const client = new OpenAI({ apiKey: 'My API Key', timeout: 2000, fetch: testFetch });

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
Expand Down

0 comments on commit e0d459f

Please sign in to comment.