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

Allow to use Authorization header for authentication #135

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ Typesafe access to [Testing Farm's REST API](https://api.dev.testing-farm.io/red
```typescript
import TestingFarmAPI from "testing-farm";

const api = new TestingFarmAPI("https://api.dev.testing-farm.io/v0.1", "api-key");

// Passing api key in data - not recommended
const api = new TestingFarmAPI("https://api.dev.testing-farm.io/v0.1");

await api.about();
```

> [!WARNING]
>
> Passing the API key in request body is deprecated and not recommended. It is better to pass it in the constructor.
> This way the API key will be passed in the request header as part of `Authorization` header.

### List a Test Requests

documentation of - [`GET /requests`](https://api.dev.testing-farm.io/redoc#operation/get_test_requests_v0_1_requests_get)
Expand Down
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PublicLink } from './link';
import { ApiKeyLink, PublicLink, TestingFarmLink } from './link';
import { isError } from './util';
import {
About,
Expand All @@ -17,7 +17,6 @@
import {
composesSchema,
aboutSchema,
urlSchema,
ranchSchema,
newRequestResponseSchema,
newRequestSchema,
Expand All @@ -41,17 +40,21 @@
aboutSchema,
newRequestResponseSchema,
requestSchema,
urlSchema,
ranchSchema,
newRequestSchema,
requestIdSchema,
};

export default class TestingFarmAPI {
private readonly link: PublicLink;

constructor(instance: string) {
this.link = new PublicLink(urlSchema.parse(instance));
private readonly link: TestingFarmLink;

constructor(instance: string, apiKey?: string) {
// Use PublicLink only for endpoints that don't require authentication
if (!apiKey) {
this.link = new PublicLink(new URL(instance));
} else {
this.link = new ApiKeyLink(new URL(instance), apiKey);
}

Check warning on line 57 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L56-L57

Added lines #L56 - L57 were not covered by tests
}

async requests(filter: RequestsFilter): Promise<Request[]>;
Expand Down
33 changes: 28 additions & 5 deletions src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
}

export abstract class TestingFarmLink {
protected readonly instance: URL;

constructor(instance: string) {
this.instance = new URL(instance);
}
constructor(protected readonly instance: URL) {}

protected abstract request(config: AxiosRequestConfig): Promise<unknown>;

Expand Down Expand Up @@ -101,3 +97,30 @@
return performRequest(config);
}
}

/**
* Handles authentication using an API key.
*/
export class ApiKeyLink extends TestingFarmLink {
public constructor(
instance: URL,
private readonly apiKey: string
) {
super(instance);
}

Check warning on line 110 in src/link.ts

View check run for this annotation

Codecov / codecov/patch

src/link.ts#L106-L110

Added lines #L106 - L110 were not covered by tests

protected async request(config: AxiosRequestConfig): Promise<unknown> {
return performRequest({
...config,
headers: {
...(config.headers ?? {}),

Check warning on line 116 in src/link.ts

View check run for this annotation

Codecov / codecov/patch

src/link.ts#L113-L116

Added lines #L113 - L116 were not covered by tests
// https://api.dev.testing-farm.io/redoc#operation/request_a_new_test_v0_1_requests_post
// OAuth2: OAuth2PasswordBearer
// The API key for authentication.
// Flow type: password
// Token URL: token
Authorization: `Bearer ${this.apiKey}`,
},
});
}

Check warning on line 125 in src/link.ts

View check run for this annotation

Codecov / codecov/patch

src/link.ts#L122-L125

Added lines #L122 - L125 were not covered by tests
}
4 changes: 2 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const requestsFilterSchema = z.object({
export type RequestsFilter = z.infer<typeof requestsFilterSchema>;

export const newRequestSchema = z.object({
api_key: z.any(),
api_key: z.any().optional(),
test: testObjectSchema,
environments: z.array(environmentSchema).optional(),
notification: notificationSchema.optional().nullable(),
Expand Down Expand Up @@ -208,7 +208,7 @@ export const requestSchema = z.object({
export type Request = z.infer<typeof requestSchema>;

export const cancelRequestSchema = z.object({
api_key: z.any(),
api_key: z.any().optional(),
});

export type CancelRequest = z.infer<typeof cancelRequestSchema>;
Expand Down
7 changes: 5 additions & 2 deletions test/integration/cancel-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ describe('Test Testing Farm DELETE /requests/{request_id}', () => {
});

test.todo('unsafe response', async () => {
const api = new TestingFarmAPI('https://api.dev.testing-farm.io/v0.1');
const api = new TestingFarmAPI(
'https://api.dev.testing-farm.io/v0.1',
'api_key'
);

const response = await api.cancelRequest(
'f053796b-452e-4da2-b4e1-26eb2f3e721f',
{ api_key: 'api_key' },
{},
false
);
expect(response).toBeTypeOf('object');
Expand Down