2025-08-18 03:07:12 +00:00
|
|
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
2024-10-16 14:35:38 +02:00
|
|
|
import * as helpers from './helpers/index.js';
|
2024-06-05 14:13:03 +02:00
|
|
|
|
2024-10-16 14:35:38 +02:00
|
|
|
import * as cloudly from '../ts/index.js';
|
2024-06-05 14:13:03 +02:00
|
|
|
import * as cloudlyApiClient from '../ts_apiclient/index.js';
|
2024-10-16 14:35:38 +02:00
|
|
|
import { Image } from '../ts_apiclient/classes.image.js';
|
2024-06-05 14:13:03 +02:00
|
|
|
|
2024-10-16 14:35:38 +02:00
|
|
|
let testCloudly: cloudly.Cloudly;
|
2024-06-05 14:13:03 +02:00
|
|
|
let testClient: cloudlyApiClient.CloudlyApiClient;
|
|
|
|
|
2024-10-16 14:35:38 +02:00
|
|
|
tap.preTask('should start cloudly', async () => {
|
|
|
|
testCloudly = await helpers.createCloudly();
|
|
|
|
await testCloudly.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.preTask('should create a new machine user for testing', async () => {
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log('🔵 PreTask: Creating first machine user...');
|
2024-10-16 14:35:38 +02:00
|
|
|
const machineUser = new testCloudly.authManager.CUser();
|
|
|
|
machineUser.id = await testCloudly.authManager.CUser.getNewId();
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log(` - User ID: ${machineUser.id}`);
|
2024-10-16 14:35:38 +02:00
|
|
|
machineUser.data = {
|
|
|
|
type: 'machine',
|
|
|
|
username: 'test',
|
|
|
|
password: 'test',
|
|
|
|
tokens: [{
|
|
|
|
token: 'test',
|
|
|
|
expiresAt: Date.now() + 3600 * 1000 * 24 * 365,
|
|
|
|
assignedRoles: ['admin'],
|
|
|
|
}],
|
|
|
|
role: 'admin',
|
|
|
|
};
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log(` - Username: ${machineUser.data.username}`);
|
|
|
|
console.log(` - Role: ${machineUser.data.role}`);
|
|
|
|
console.log(` - Token: 'test'`);
|
|
|
|
console.log(` - Token roles: ${machineUser.data.tokens[0].assignedRoles}`);
|
2024-10-16 14:35:38 +02:00
|
|
|
await machineUser.save();
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log('✅ PreTask: First machine user saved successfully');
|
2024-10-16 14:35:38 +02:00
|
|
|
});
|
|
|
|
|
2024-06-05 14:13:03 +02:00
|
|
|
tap.test('should create a new cloudlyApiClient', async () => {
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log('🔵 Test: Creating CloudlyApiClient...');
|
2024-06-05 14:13:03 +02:00
|
|
|
testClient = new cloudlyApiClient.CloudlyApiClient({
|
|
|
|
registerAs: 'api',
|
2024-11-06 17:19:43 +01:00
|
|
|
cloudlyUrl: `http://${helpers.testCloudlyConfig.publicUrl}:${helpers.testCloudlyConfig.publicPort}`,
|
2024-06-05 14:13:03 +02:00
|
|
|
});
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log(` - URL: http://${helpers.testCloudlyConfig.publicUrl}:${helpers.testCloudlyConfig.publicPort}`);
|
2024-10-16 14:35:38 +02:00
|
|
|
await testClient.start();
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log('✅ CloudlyApiClient started successfully');
|
2024-06-05 14:13:03 +02:00
|
|
|
expect(testClient).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2025-08-18 21:11:28 +00:00
|
|
|
tap.test('DEBUG: Check existing users', async () => {
|
|
|
|
console.log('🔍 DEBUG: Checking existing users in database...');
|
|
|
|
const allUsers = await testCloudly.authManager.CUser.getInstances({});
|
|
|
|
console.log(` - Total users found: ${allUsers.length}`);
|
|
|
|
for (const user of allUsers) {
|
|
|
|
console.log(` - User: ${user.data.username} (ID: ${user.id})`);
|
|
|
|
console.log(` - Type: ${user.data.type}`);
|
|
|
|
console.log(` - Role: ${user.data.role}`);
|
|
|
|
console.log(` - Tokens: ${user.data.tokens.length}`);
|
|
|
|
for (const token of user.data.tokens) {
|
|
|
|
console.log(` - Token: '${token.token}' | Roles: ${token.assignedRoles?.join(', ')}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-10-16 14:35:38 +02:00
|
|
|
|
2024-08-25 14:29:26 +02:00
|
|
|
tap.test('should get an identity', async () => {
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log('🔵 Test: Getting identity by token...');
|
|
|
|
console.log(` - Using token: 'test'`);
|
|
|
|
console.log(` - API URL: http://${helpers.testCloudlyConfig.publicUrl}:${helpers.testCloudlyConfig.publicPort}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const identity = await testClient.getIdentityByToken('test');
|
|
|
|
console.log('✅ Identity retrieved successfully:');
|
|
|
|
console.log(` - Identity exists: ${!!identity}`);
|
|
|
|
if (identity) {
|
|
|
|
console.log(` - Identity data:`, JSON.stringify(identity, null, 2));
|
|
|
|
}
|
|
|
|
expect(identity).toBeTruthy();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('❌ Failed to get identity:');
|
|
|
|
console.error(` - Error message: ${error.message}`);
|
|
|
|
console.error(` - Error stack:`, error.stack);
|
|
|
|
throw error;
|
|
|
|
}
|
2024-08-25 14:29:26 +02:00
|
|
|
});
|
|
|
|
|
2024-10-16 14:35:38 +02:00
|
|
|
let image: Image;
|
|
|
|
tap.test('should create and upload an image', async () => {
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log('🔵 Test: Creating and uploading image...');
|
|
|
|
console.log(` - Image name: 'test'`);
|
|
|
|
console.log(` - Image description: 'test'`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
image = await testClient.image.createImage({
|
|
|
|
name: 'test',
|
|
|
|
description: 'test'
|
|
|
|
});
|
|
|
|
console.log('✅ Image created successfully:');
|
|
|
|
console.log(` - Image ID: ${image?.id}`);
|
|
|
|
console.log(` - Image data:`, image);
|
|
|
|
expect(image).toBeInstanceOf(Image);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('❌ Failed to create image:');
|
|
|
|
console.error(` - Error message: ${error.message}`);
|
|
|
|
console.error(` - Error stack:`, error.stack);
|
|
|
|
throw error;
|
|
|
|
}
|
2024-06-05 14:13:03 +02:00
|
|
|
})
|
|
|
|
|
2024-10-16 14:35:38 +02:00
|
|
|
tap.test('should upload an image version', async () => {
|
2025-08-18 21:11:28 +00:00
|
|
|
console.log('🔵 Test: Uploading image version...');
|
|
|
|
console.log(` - Version: 'v1.0.0'`);
|
|
|
|
console.log(` - Image exists: ${!!image}`);
|
|
|
|
console.log(` - Image ID: ${image?.id}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const imageStream = await helpers.getAlpineImageReadableStream();
|
|
|
|
console.log(' - Image stream obtained successfully');
|
|
|
|
|
|
|
|
await image.pushImageVersion('v1.0.0', imageStream);
|
|
|
|
console.log('✅ Image version uploaded successfully');
|
|
|
|
} catch (error) {
|
|
|
|
console.error('❌ Failed to upload image version:');
|
|
|
|
console.error(` - Error message: ${error.message}`);
|
|
|
|
console.error(` - Error stack:`, error.stack);
|
|
|
|
throw error;
|
|
|
|
}
|
2024-10-16 14:35:38 +02:00
|
|
|
});
|
|
|
|
|
2024-08-25 14:29:26 +02:00
|
|
|
tap.test('should stop the apiclient', async (toolsArg) => {
|
2024-10-16 14:35:38 +02:00
|
|
|
await toolsArg.delayFor(10000);
|
2024-11-06 03:56:46 +01:00
|
|
|
await helpers.stopCloudly();
|
2024-06-05 14:13:03 +02:00
|
|
|
await testClient.stop();
|
2024-10-16 14:35:38 +02:00
|
|
|
await testCloudly.stop();
|
2024-06-05 14:13:03 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
export default tap.start();
|