Skip to content

Commit

Permalink
Merge pull request #4645 from logto-io/gao-refactor-integration-test-…
Browse files Browse the repository at this point in the history
…apis

refactor(test): add ApiFactory for integration tests
  • Loading branch information
gao-sun authored Oct 13, 2023
2 parents f0e6cf3 + e75a0cf commit d5a8762
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 120 deletions.
29 changes: 29 additions & 0 deletions packages/integration-tests/src/api/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { authedAdminApi } from './api.js';

export class ApiFactory<
Schema extends Record<string, unknown>,
PostData extends Record<string, unknown>,
PatchData extends Record<string, unknown> = Partial<PostData>,
> {
constructor(public readonly path: string) {}

async create(data: PostData): Promise<Schema> {
return authedAdminApi.post(this.path, { json: data }).json<Schema>();
}

async getList(params?: URLSearchParams): Promise<Schema[]> {
return authedAdminApi.get(this.path + '?' + (params?.toString() ?? '')).json<Schema[]>();
}

async get(id: string): Promise<Schema> {
return authedAdminApi.get(this.path + '/' + id).json<Schema>();
}

async update(id: string, data: PatchData): Promise<Schema> {
return authedAdminApi.patch(this.path + '/' + id, { json: data }).json<Schema>();
}

async delete(id: string): Promise<void> {
await authedAdminApi.delete(this.path + '/' + id);
}
}
50 changes: 13 additions & 37 deletions packages/integration-tests/src/api/organization-scope.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
import { type OrganizationScope } from '@logto/schemas';

import { authedAdminApi } from './api.js';

export const createOrganizationScope = async (name: string, description?: string) => {
return authedAdminApi
.post('organization-scopes', {
json: {
name,
description,
},
})
.json<OrganizationScope>();
};

export const getOrganizationScopes = async (params?: URLSearchParams) => {
return authedAdminApi
.get('organization-scopes?' + (params?.toString() ?? ''))
.json<OrganizationScope[]>();
};

export const getOrganizationScope = async (id: string) => {
return authedAdminApi.get('organization-scopes/' + id).json<OrganizationScope>();
};

export const updateOrganizationScope = async (id: string, name: string, description?: string) => {
return authedAdminApi
.patch('organization-scopes/' + id, {
json: {
name,
description,
},
})
.json<OrganizationScope>();
};

export const deleteOrganizationScope = async (id: string) => {
return authedAdminApi.delete('organization-scopes/' + id);
};
import { ApiFactory } from './factory.js';

class OrganizationScopeApi extends ApiFactory<
OrganizationScope,
{ name: string; description?: string }
> {
constructor() {
super('organization-scopes');
}
}

/** API methods for operating organization template scopes. */
export const scopeApi = new OrganizationScopeApi();
41 changes: 8 additions & 33 deletions packages/integration-tests/src/api/organization.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import { type Organization } from '@logto/schemas';

import { authedAdminApi } from './api.js';
import { ApiFactory } from './factory.js';

export const createOrganization = async (name: string, description?: string) => {
return authedAdminApi
.post('organizations', {
json: {
name,
description,
},
})
.json<Organization>();
};
class OrganizationApi extends ApiFactory<Organization, { name: string; description?: string }> {
constructor() {
super('organizations');
}
}

export const getOrganizations = async (params?: URLSearchParams) => {
return authedAdminApi.get('organizations?' + (params?.toString() ?? '')).json<Organization[]>();
};

export const getOrganization = async (id: string) => {
return authedAdminApi.get('organizations/' + id).json<Organization>();
};

export const updateOrganization = async (id: string, name: string, description?: string) => {
return authedAdminApi
.patch('organizations/' + id, {
json: {
name,
description,
},
})
.json<Organization>();
};

export const deleteOrganization = async (id: string) => {
return authedAdminApi.delete('organizations/' + id);
};
/** API methods for operating organizations. */
export const organizationApi = new OrganizationApi();
45 changes: 21 additions & 24 deletions packages/integration-tests/src/tests/api/organization-scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@ import { generateStandardId } from '@logto/shared';
import { isKeyInObject } from '@silverhand/essentials';
import { HTTPError } from 'got';

import {
createOrganizationScope,
getOrganizationScopes,
getOrganizationScope,
updateOrganizationScope,
deleteOrganizationScope,
} from '#src/api/organization-scope.js';
import { scopeApi } from '#src/api/organization-scope.js';

const randomId = () => generateStandardId(4);

describe('organization scopes', () => {
it('should fail if the name of the new organization scope already exists', async () => {
const name = 'test' + randomId();
await createOrganizationScope(name);
const response = await createOrganizationScope(name).catch((error: unknown) => error);
await scopeApi.create({ name });
const response = await scopeApi.create({ name }).catch((error: unknown) => error);

assert(response instanceof HTTPError);

Expand All @@ -30,9 +24,9 @@ describe('organization scopes', () => {

it('should get organization scopes successfully', async () => {
const [name1, name2] = ['test' + randomId(), 'test' + randomId()];
await createOrganizationScope(name1, 'A test organization scope.');
await createOrganizationScope(name2);
const scopes = await getOrganizationScopes();
await scopeApi.create({ name: name1, description: 'A test organization scope.' });
await scopeApi.create({ name: name2 });
const scopes = await scopeApi.getList();

expect(scopes).toContainEqual(
expect.objectContaining({ name: name1, description: 'A test organization scope.' })
Expand All @@ -43,13 +37,13 @@ describe('organization scopes', () => {
it('should get organization scopes with pagination', async () => {
// Add 20 scopes to exceed the default page size
await Promise.all(
Array.from({ length: 30 }).map(async () => createOrganizationScope('test' + randomId()))
Array.from({ length: 30 }).map(async () => scopeApi.create({ name: 'test' + randomId() }))
);

const scopes = await getOrganizationScopes();
const scopes = await scopeApi.getList();
expect(scopes).toHaveLength(20);

const scopes2 = await getOrganizationScopes(
const scopes2 = await scopeApi.getList(
new URLSearchParams({
page: '2',
page_size: '10',
Expand All @@ -61,22 +55,25 @@ describe('organization scopes', () => {
});

it('should be able to create and get organization scopes by id', async () => {
const createdScope = await createOrganizationScope('test' + randomId());
const scope = await getOrganizationScope(createdScope.id);
const createdScope = await scopeApi.create({ name: 'test' + randomId() });
const scope = await scopeApi.get(createdScope.id);

expect(scope).toStrictEqual(createdScope);
});

it('should fail when try to get an organization scope that does not exist', async () => {
const response = await getOrganizationScope('0').catch((error: unknown) => error);
const response = await scopeApi.get('0').catch((error: unknown) => error);

expect(response instanceof HTTPError && response.response.statusCode).toBe(404);
});

it('should be able to update organization scope', async () => {
const createdScope = await createOrganizationScope('test' + randomId());
const createdScope = await scopeApi.create({ name: 'test' + randomId() });
const newName = 'test' + randomId();
const scope = await updateOrganizationScope(createdScope.id, newName, 'test description.');
const scope = await scopeApi.update(createdScope.id, {
name: newName,
description: 'test description.',
});
expect(scope).toStrictEqual({
...createdScope,
name: newName,
Expand All @@ -85,14 +82,14 @@ describe('organization scopes', () => {
});

it('should be able to delete organization scope', async () => {
const createdScope = await createOrganizationScope('test' + randomId());
await deleteOrganizationScope(createdScope.id);
const response = await getOrganizationScope(createdScope.id).catch((error: unknown) => error);
const createdScope = await scopeApi.create({ name: 'test' + randomId() });
await scopeApi.delete(createdScope.id);
const response = await scopeApi.get(createdScope.id).catch((error: unknown) => error);
expect(response instanceof HTTPError && response.response.statusCode).toBe(404);
});

it('should fail when try to delete an organization scope that does not exist', async () => {
const response = await deleteOrganizationScope('0').catch((error: unknown) => error);
const response = await scopeApi.delete('0').catch((error: unknown) => error);
expect(response instanceof HTTPError && response.response.statusCode).toBe(404);
});
});
49 changes: 23 additions & 26 deletions packages/integration-tests/src/tests/api/organization.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { HTTPError } from 'got';

import {
createOrganization,
deleteOrganization,
getOrganization,
getOrganizations,
updateOrganization,
} from '#src/api/organization.js';
import { organizationApi } from '#src/api/organization.js';

describe('organizations', () => {
it('should get organizations successfully', async () => {
await createOrganization('test', 'A test organization.');
await createOrganization('test2');
const organizations = await getOrganizations();
await organizationApi.create({ name: 'test', description: 'A test organization.' });
await organizationApi.create({ name: 'test2' });
const organizations = await organizationApi.getList();

expect(organizations).toContainEqual(
expect.objectContaining({ name: 'test', description: 'A test organization.' })
Expand All @@ -24,12 +18,14 @@ describe('organizations', () => {

it('should get organizations with pagination', async () => {
// Add 20 organizations to exceed the default page size
await Promise.all(Array.from({ length: 30 }).map(async () => createOrganization('test')));
await Promise.all(
Array.from({ length: 30 }).map(async () => organizationApi.create({ name: 'test' }))
);

const organizations = await getOrganizations();
const organizations = await organizationApi.getList();
expect(organizations).toHaveLength(20);

const organizations2 = await getOrganizations(
const organizations2 = await organizationApi.getList(
new URLSearchParams({
page: '2',
page_size: '10',
Expand All @@ -41,25 +37,24 @@ describe('organizations', () => {
});

it('should be able to create and get organizations by id', async () => {
const createdOrganization = await createOrganization('test');
const organization = await getOrganization(createdOrganization.id);
const createdOrganization = await organizationApi.create({ name: 'test' });
const organization = await organizationApi.get(createdOrganization.id);

expect(organization).toStrictEqual(createdOrganization);
});

it('should fail when try to get an organization that does not exist', async () => {
const response = await getOrganization('0').catch((error: unknown) => error);
const response = await organizationApi.get('0').catch((error: unknown) => error);

expect(response instanceof HTTPError && response.response.statusCode).toBe(404);
});

it('should be able to update organization', async () => {
const createdOrganization = await createOrganization('test');
const organization = await updateOrganization(
createdOrganization.id,
'test2',
'test description.'
);
const createdOrganization = await organizationApi.create({ name: 'test' });
const organization = await organizationApi.update(createdOrganization.id, {
name: 'test2',
description: 'test description.',
});
expect(organization).toStrictEqual({
...createdOrganization,
name: 'test2',
Expand All @@ -68,14 +63,16 @@ describe('organizations', () => {
});

it('should be able to delete organization', async () => {
const createdOrganization = await createOrganization('test');
await deleteOrganization(createdOrganization.id);
const response = await getOrganization(createdOrganization.id).catch((error: unknown) => error);
const createdOrganization = await organizationApi.create({ name: 'test' });
await organizationApi.delete(createdOrganization.id);
const response = await organizationApi
.get(createdOrganization.id)
.catch((error: unknown) => error);
expect(response instanceof HTTPError && response.response.statusCode).toBe(404);
});

it('should fail when try to delete an organization that does not exist', async () => {
const response = await deleteOrganization('0').catch((error: unknown) => error);
const response = await organizationApi.delete('0').catch((error: unknown) => error);
expect(response instanceof HTTPError && response.response.statusCode).toBe(404);
});
});

0 comments on commit d5a8762

Please sign in to comment.