Skip to content

Commit

Permalink
Fix listAuthFactors() return (#862)
Browse files Browse the repository at this point in the history
## Description

## Documentation

Does this require changes to the WorkOS Docs? E.g. the [API
Reference](https://workos.com/docs/reference) or code snippets need
updates.

```
[ ] Yes
```

If yes, link a related docs PR and add a docs maintainer as a reviewer.
Their approval is required.
  • Loading branch information
jonatascastro12 authored Sep 14, 2023
1 parent 0e88cda commit e62a5e6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 24 deletions.
14 changes: 0 additions & 14 deletions src/users/fixtures/factor.json

This file was deleted.

23 changes: 23 additions & 0 deletions src/users/fixtures/list-factors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"object": "list",
"data": [
{
"object": "authentication_factor",
"id": "auth_factor_1234",
"created_at": "2022-03-15T20:39:19.892Z",
"updated_at": "2022-03-15T20:39:19.892Z",
"type": "totp",
"totp": {
"issuer": "WorkOS",
"qr_code": "qr-code-test",
"secret": "secret-test",
"uri": "uri-test",
"user": "some_user"
}
}
],
"list_metadata": {
"before": null,
"after": null
}
}
4 changes: 3 additions & 1 deletion src/users/interfaces/list-auth-factors-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export interface ListAuthFactorsOptions {
import { PaginationOptions } from '../../common/interfaces';

export interface ListAuthFactorsOptions extends PaginationOptions {
userId: string;
}
43 changes: 39 additions & 4 deletions src/users/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MockAdapter from 'axios-mock-adapter';
import { WorkOS } from '../workos';
import userFixture from './fixtures/user.json';
import listUsersFixture from './fixtures/list-users.json';
import factorFixture from './fixtures/factor.json';
import listFactorFixture from './fixtures/list-factors.json';

const mock = new MockAdapter(axios);
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
Expand Down Expand Up @@ -410,7 +410,20 @@ describe('UserManagement', () => {
describe('enrollAuthFactor', () => {
it('sends an enrollAuthFactor request', async () => {
mock.onPost(`/users/${userId}/auth/factors`).reply(200, {
authentication_factor: factorFixture,
authentication_factor: {
object: 'authentication_factor',
id: 'auth_factor_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
type: 'totp',
totp: {
issuer: 'WorkOS',
qr_code: 'qr-code-test',
secret: 'secret-test',
uri: 'uri-test',
user: 'some_user',
},
},
authentication_challenge: {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
Expand Down Expand Up @@ -460,13 +473,35 @@ describe('UserManagement', () => {

describe('listAuthFactors', () => {
it('sends a listAuthFactors request', async () => {
mock.onGet(`/users/${userId}/auth/factors`).reply(200, [factorFixture]);
mock.onGet(`/users/${userId}/auth/factors`).reply(200, listFactorFixture);

const resp = await workos.users.listAuthFactors({ userId });

expect(mock.history.get[0].url).toEqual(`/users/${userId}/auth/factors`);

expect(resp[0]).toMatchObject({ id: factorFixture.id });
expect(resp).toMatchObject({
object: 'list',
data: [
{
object: 'authentication_factor',
id: 'auth_factor_1234',
createdAt: '2022-03-15T20:39:19.892Z',
updatedAt: '2022-03-15T20:39:19.892Z',
type: 'totp',
totp: {
issuer: 'WorkOS',
qrCode: 'qr-code-test',
secret: 'secret-test',
uri: 'uri-test',
user: 'some_user',
},
},
],
listMetadata: {
before: null,
after: null,
},
});
});
});

Expand Down
23 changes: 18 additions & 5 deletions src/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,25 @@ export class Users {
};
}

async listAuthFactors(payload: ListAuthFactorsOptions): Promise<Factor[]> {
const { data } = await this.workos.get<FactorResponse[]>(
`/users/${payload.userId}/auth/factors`,
async listAuthFactors(
options: ListAuthFactorsOptions,
): Promise<AutoPaginatable<Factor>> {
return new AutoPaginatable(
await fetchAndDeserialize<FactorResponse, Factor>(
this.workos,
`/users/${options.userId}/auth/factors`,
deserializeFactor,
options,
),
(params) =>
fetchAndDeserialize<FactorResponse, Factor>(
this.workos,
`/users/${options.userId}/auth/factors`,
deserializeFactor,
params,
),
options,
);

return data.map(deserializeFactor);
}

async deleteUser(payload: DeleteUserOptions) {
Expand Down

0 comments on commit e62a5e6

Please sign in to comment.