From 55e00ff35b4cf123d9f1cdc723524150f9f067a8 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Fri, 27 Oct 2023 12:31:38 +0800 Subject: [PATCH] Add application.test.ts, still has bugs --- test/application.test.ts | 92 ++++++++++++++++++++++++++++++++++++++++ test/util.ts | 62 +++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 test/application.test.ts create mode 100644 test/util.ts diff --git a/test/application.test.ts b/test/application.test.ts new file mode 100644 index 0000000..5f148b3 --- /dev/null +++ b/test/application.test.ts @@ -0,0 +1,92 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { SDK } from '../src' +import * as util from './util' +import { Config } from '../src/config' +import { Application } from '../src/application' + +test('TestApplication', async () => { + const testConfig: Config = { + endpoint: util.TestCasdoorEndpoint, + clientId: util.TestClientId, + clientSecret: util.TestClientSecret, + certificate: util.TestJwtPublicKey, + orgName: util.TestCasdoorOrganization, + appName: util.TestCasdoorApplication, + } + const sdk = new SDK(testConfig) + + const name = util.getRandomName('application') + + // Add a new object + const application: Application = { + owner: 'admin', + name: name, + createdTime: new Date().toISOString(), + displayName: name, + logo: 'https://cdn.casbin.org/img/casdoor-logo_1185x256.png', + homepageUrl: 'https://casdoor.org', + description: 'Casdoor Website', + organization: 'casbin', + } + + const addResponse = await sdk.addApplication(application) + if (!addResponse) { + throw new Error('Failed to add object') + } + + // Get all objects and check if our added object is in the list + const applications = await sdk.getApplications() + const found = applications.some((item) => item.Name === name) + if (!found) { + throw new Error('Added object not found in list') + } + + // Get the object + const retrievedApplication = await sdk.getApplication(name) + if (retrievedApplication.name !== name) { + throw new Error( + `Retrieved object does not match added object: ${retrievedApplication.name} != ${name}`, + ) + } + + // Update the object + const updatedDescription = 'Updated Casdoor Website' + retrievedApplication.description = updatedDescription + const updateResponse = await sdk.updateApplication(retrievedApplication) + if (!updateResponse) { + throw new Error('Failed to update object') + } + + // Validate the update + const updatedApplication = await sdk.getApplication(name) + if (updatedApplication.description !== updatedDescription) { + throw new Error( + `Failed to update object, description mismatch: ${updatedApplication.description} != ${updatedDescription}`, + ) + } + + // Delete the object + const deleteResponse = await sdk.deleteApplication(retrievedApplication) + if (!deleteResponse) { + throw new Error('Failed to delete object') + } + + // Validate the deletion + const deletedApplication = await sdk.getApplication(name) + if (deletedApplication) { + throw new Error("Failed to delete object, it's still retrievable") + } +}) diff --git a/test/util.ts b/test/util.ts new file mode 100644 index 0000000..26dd77f --- /dev/null +++ b/test/util.ts @@ -0,0 +1,62 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +export const TestCasdoorEndpoint = 'https://demo.casdoor.com' +export const TestClientId = '294b09fbc17f95daf2fe' +export const TestClientSecret = 'dd8982f7046ccba1bbd7851d5c1ece4e52bf039d' +export const TestCasdoorOrganization = 'casbin' +export const TestCasdoorApplication = 'app-vue-python-example' + +export const TestJwtPublicKey = `-----BEGIN CERTIFICATE----- +MIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh +c2Rvb3IgT3JnYW5pemF0aW9uMRUwEwYDVQQDEwxDYXNkb29yIENlcnQwHhcNMjEx +MDE1MDgxMTUyWhcNNDExMDE1MDgxMTUy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t4rxzCvl +2nR42Fybap3O/g2JXMhNNROwZmNjgpsF7XVENCSuFO1jTywLaqjuXCg54IL7XVLG +omKNNNcc8h1FCeKj/nnbGMhodnFWKDTsJcbNmcOPNHo6ixzqMy/Hqc+mWYv7maAG +Jtevs3qgMZ8F9Qzr3HpUc6R3ZYYWDY/xxPisuKftOPZgtH979XC4mdf0WPnOBLqL +2DJ1zaBmjiGJolvb7XNVKcUfDXYw85ZTZQ5b9clI4e+6bmyWqQItlwt+Ati/uFEV +XzCj70B4lALX6xau1kLEpV9O1GERizYRz5P9NJNA7KoO5AVMp9w0DQTkt+LbXnZE +HHnWKy8xHQKZF9sR7YBPGLs/Ac6tviv5Ua15OgJ/8dLRZ/veyFfGo2yZsI+hKVU5 +nCCJHBcAyFnm1hdvdwEdH33jDBjNB6ciotJZrf/3VYaIWSalADosHAgMWfXuWP+h +8XKXmzlxuHbTMQYtZPDgspS5aK+S4Q9wb8RRAYo= +-----END CERTIFICATE-----` + +export function getRandomCode(length: number) { + const stdNums = '0123456789' + let result = '' + for (let i = 0; i < length; i++) { + result += stdNums.charAt(Math.floor(Math.random() * stdNums.length)) + } + return result +} + +export function getRandomName(prefix: string) { + return `${prefix}_${getRandomCode(6)}` +}