From 69b1b85c805504784091ede2e63133e41f4ddd6b Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Thu, 21 Jul 2022 15:48:49 +0800 Subject: [PATCH] refactor(test): integration tests for applications (#1631) --- .../tests/applications.test.ts | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/packages/integration-tests/tests/applications.test.ts b/packages/integration-tests/tests/applications.test.ts index 9ff1f9935..8dec28d3b 100644 --- a/packages/integration-tests/tests/applications.test.ts +++ b/packages/integration-tests/tests/applications.test.ts @@ -1,13 +1,17 @@ -import { Application } from '@logto/schemas'; +import { Application, ApplicationType } from '@logto/schemas'; import { demoAppApplicationId } from '@logto/schemas/lib/seeds'; import { authedAdminApi } from '@/api'; -const testApplication = { - id: '', - name: 'test-app', - type: 'SPA', -}; +const createApplication = (name: string, type: ApplicationType) => + authedAdminApi + .post('applications', { + json: { + name, + type, + }, + }) + .json(); describe('admin console application', () => { it('should get demo app details successfully', async () => { @@ -19,35 +23,29 @@ describe('admin console application', () => { }); it('should create application successfully', async () => { - const application = await authedAdminApi - .post('applications', { - json: { name: testApplication.name, type: testApplication.type }, - }) - .json(); + const applicationName = 'test-create-app'; + const applicationType = ApplicationType.SPA; - expect(application.name).toBe(testApplication.name); - expect(application.type).toBe(testApplication.type); + const application = await createApplication(applicationName, applicationType); - // eslint-disable-next-line @silverhand/fp/no-mutation - testApplication.id = application.id; + expect(application.name).toBe(applicationName); + expect(application.type).toBe(applicationType); - const applications = await authedAdminApi.get('applications').json(); + const fetchedApplication = await authedAdminApi + .get(`applications/${application.id}`) + .json(); - expect(applications.some((app) => app.id === application.id)).toBeTruthy(); + expect(fetchedApplication).toBeTruthy(); }); it('should update application details successfully', async () => { - expect(testApplication.id).toBeTruthy(); + const application = await createApplication('test-update-app', ApplicationType.SPA); - const application = await authedAdminApi - .get(`applications/${testApplication.id}`) - .json(); + const newApplicationDescription = `new_${application.description ?? ''}`; - const newApplicationDescription = 'new application description'; - expect(application.description).not.toBe(newApplicationDescription); - - const newRedirectUris = ['https://logto.dev/callback']; - expect(application.oidcClientMetadata.redirectUris).not.toEqual(newRedirectUris); + const newRedirectUris = application.oidcClientMetadata.redirectUris.concat([ + 'https://logto.dev/callback', + ]); await authedAdminApi .patch(`applications/${application.id}`, { @@ -69,13 +67,14 @@ describe('admin console application', () => { }); it('should delete application successfully', async () => { - expect(testApplication.id).toBeTruthy(); + const application = await createApplication('test-delete-app', ApplicationType.SPA); - await authedAdminApi.delete(`applications/${testApplication.id}`); + await authedAdminApi.delete(`applications/${application.id}`); - const applications = await authedAdminApi.get('applications').json(); + const fetchResponseAfterDeletion = await authedAdminApi.get(`applications/${application.id}`, { + throwHttpErrors: false, + }); - const hasTestApplication = applications.some((app) => app.id === testApplication.id); - expect(hasTestApplication).toBeFalsy(); + expect(fetchResponseAfterDeletion.statusCode).toBe(404); }); });