Skip to content

Commit

Permalink
Client creds tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharpandey13 committed Aug 23, 2024
1 parent 01c6af7 commit d986c5e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 15 deletions.
26 changes: 18 additions & 8 deletions test/management/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PostCredentialsOperationRequest,
PostCredentialsRequest,
ManagementClient,
GetClientsRequest,
} from '../../src/index.js';

import { RequiredError } from '../../src/lib/errors.js';
Expand Down Expand Up @@ -138,16 +139,25 @@ describe('ClientsManager', () => {
it('should pass the parameters in the query-string', (done) => {
nock.cleanAll();

const queryParameters: GetClientsRequest | any = {
fields: 'name,email',
include_fields: true,
page: 0,
per_page: 50,
include_totals: false,
from: '12345',
take: 50,
is_global: true,
is_first_party: false,
app_type: 'web,mobile',
client_ids: 'client1,client2,client3',
q: 'name:John AND email:[email protected]',
};

const data = [{ client_id: '1' }];
const request = nock(API_URL)
.get('/clients')
.query({
include_fields: true,
fields: 'test',
})
.reply(200, data);
const request = nock(API_URL).get('/clients').query(queryParameters).reply(200, data);

clients.getAll({ include_fields: true, fields: 'test' }).then(() => {
clients.getAll({ ...queryParameters }).then(() => {
expect(request.isDone()).toBe(true);
done();
});
Expand Down
90 changes: 89 additions & 1 deletion test/management/organizations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import nock from 'nock';

const API_URL = 'https://tenant.auth0.com/api/v2';

import { OrganizationsManager, ManagementClient, RequiredError } from '../../src/index.js';
import {
OrganizationsManager,
ManagementClient,
RequiredError,
GetOrganizationClientGrantsRequest,
GetOrganizationClientGrants200Response,
ApiResponse,
} from '../../src/index.js';

describe('OrganizationsManager', () => {
let organizations: OrganizationsManager;
Expand Down Expand Up @@ -1386,4 +1393,85 @@ describe('OrganizationsManager', () => {
});
});
});

describe('#getOrganizationClientGrants', () => {
const token = 'test_token';

const data: GetOrganizationClientGrantsRequest | any = {
id: 'org_123',
audience: 'audience',
client_id: 'client_id',
grant_ids: ['grant_id1', 'grant_id2'],
page: 1,
per_page: 10,
include_totals: true,
};

beforeEach(() => {
request = nock(API_URL)
.get(`/organizations/${data.id}/client-grants`)
.query({
audience: data.audience,
client_id: data.client_id,
grant_ids: data.grant_ids,
page: data.page,
per_page: data.per_page,
include_totals: data.include_totals,
})
.reply(200, { grants: [] });
});

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

it('should return a promise if no callback is given', async () => {
const promise = organizations.getOrganizationClientGrants(data);
expect(promise).toBeInstanceOf(Promise);
await promise;
});

it('should perform a GET request to /api/v2/organizations/:id/client-grants', async () => {
await organizations.getOrganizationClientGrants(data);
expect(request.isDone()).toBe(true);
});

it('should include the token in the Authorization header', async () => {
const scope = nock(API_URL, {

Check failure on line 1440 in test/management/organizations.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (18.17)

'scope' is assigned a value but never used

Check failure on line 1440 in test/management/organizations.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (20.3)

'scope' is assigned a value but never used
reqheaders: {
authorization: `Bearer ${token}`,
},
})
.get(`/organizations/${data.id}/client-grants`)
.query({
audience: data.audience,
client_id: data.client_id,
grant_ids: data.grant_ids,
page: data.page,
per_page: data.per_page,
include_totals: data.include_totals,
})
.reply(200, { grants: [] });

const result: ApiResponse<GetOrganizationClientGrants200Response> =
await organizations.getOrganizationClientGrants(data);
expect(result.status).toBe(200);
});

it('should pass the query parameters correctly', async () => {
await organizations.getOrganizationClientGrants(data);
expect(request.isDone()).toBe(true);
});

it('should pass any errors to the promise catch handler', async () => {
request = nock(API_URL)
.get(`/organizations/${data.id}/client-grants`)
.query(true)
.reply(500, {});

organizations.getOrganizationClientGrants(data).catch((err) => {
expect(err).toBeDefined();
});
});
});
});
20 changes: 14 additions & 6 deletions test/management/resource-servers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nock from 'nock';
import queryString from 'querystring';

const API_URL = 'https://tenant.auth0.com/api/v2';

Expand Down Expand Up @@ -81,18 +82,25 @@ describe('ResourceServersManager', () => {
});
});

it('should include the token in the Authorization header', (done) => {
it('should include the token in the Authorization header', async () => {
nock.cleanAll();

const queryParams = {
identifiers: ['123'],
page: 0,
per_page: 10,
include_totals: true,
include_fields: true,
};

const request = nock(API_URL)
.get('/resource-servers')
.get(`/resource-servers?${queryString.stringify(queryParams)}`)
.matchHeader('Authorization', `Bearer ${token}`)
// .query(queryParams)
.reply(200, []);

resourceServers.getAll().then(() => {
expect(request.isDone()).toBe(true);
done();
});
await resourceServers.getAll(queryParams);
expect(request.isDone()).toBe(true);
});
});

Expand Down

0 comments on commit d986c5e

Please sign in to comment.