@ufdevsllc/authme2.0
Version:
SDK for license management and remote monitoring with automatic system tracking, license validation, and remote control capabilities
224 lines (190 loc) • 8.49 kB
JavaScript
import mongoose from 'mongoose';
import SystemTracking from '../../models/system-tracking.js';
describe('SystemTracking Model', () => {
beforeAll(async () => {
await mongoose.connect('mongodb://localhost:27017/test-license-sdk-system');
});
afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
});
beforeEach(async () => {
await SystemTracking.deleteMany({});
});
describe('Schema Validation', () => {
const validSystemData = {
licenseKey: 'TEST-LICENSE-123',
systemInfo: {
os: 'Linux',
platform: 'linux',
arch: 'x64',
memory: 8589934592,
cpu: {
model: 'Intel Core i7',
cores: 8,
speed: 2400
}
},
deploymentInfo: {
ipAddress: '192.168.1.100',
serverLocation: 'US-East',
environment: 'production',
timestamp: new Date()
},
corsSettings: {
allowedOrigins: ['https://example.com'],
methods: ['GET', 'POST'],
headers: ['Content-Type', 'Authorization']
}
};
test('should create a valid system tracking record', async () => {
const systemTracking = new SystemTracking(validSystemData);
const saved = await systemTracking.save();
expect(saved._id).toBeDefined();
expect(saved.licenseKey).toBe(validSystemData.licenseKey);
expect(saved.systemInfo.os).toBe(validSystemData.systemInfo.os);
expect(saved.deploymentInfo.ipAddress).toBe(validSystemData.deploymentInfo.ipAddress);
expect(saved.isActive).toBe(true);
});
test('should require licenseKey', async () => {
const systemTracking = new SystemTracking({ ...validSystemData, licenseKey: undefined });
await expect(systemTracking.save()).rejects.toThrow('License key is required');
});
test('should require system info fields', async () => {
const noOS = new SystemTracking({
...validSystemData,
systemInfo: { ...validSystemData.systemInfo, os: undefined }
});
await expect(noOS.save()).rejects.toThrow('Operating system is required');
const noPlatform = new SystemTracking({
...validSystemData,
systemInfo: { ...validSystemData.systemInfo, platform: undefined }
});
await expect(noPlatform.save()).rejects.toThrow('Platform is required');
const noArch = new SystemTracking({
...validSystemData,
systemInfo: { ...validSystemData.systemInfo, arch: undefined }
});
await expect(noArch.save()).rejects.toThrow('Architecture is required');
const noMemory = new SystemTracking({
...validSystemData,
systemInfo: { ...validSystemData.systemInfo, memory: undefined }
});
await expect(noMemory.save()).rejects.toThrow('Memory information is required');
});
test('should validate IP address format', async () => {
const invalidIP = new SystemTracking({
...validSystemData,
deploymentInfo: { ...validSystemData.deploymentInfo, ipAddress: 'invalid-ip' }
});
await expect(invalidIP.save()).rejects.toThrow('Invalid IP address format');
});
test('should accept valid IPv4 and IPv6 addresses', async () => {
const ipv4System = new SystemTracking({
...validSystemData,
deploymentInfo: { ...validSystemData.deploymentInfo, ipAddress: '192.168.1.1' }
});
await expect(ipv4System.save()).resolves.toBeDefined();
const ipv6System = new SystemTracking({
...validSystemData,
licenseKey: 'TEST-LICENSE-IPV6',
deploymentInfo: { ...validSystemData.deploymentInfo, ipAddress: '2001:0db8:85a3:0000:0000:8a2e:0370:7334' }
});
await expect(ipv6System.save()).resolves.toBeDefined();
});
test('should validate environment enum', async () => {
const invalidEnv = new SystemTracking({
...validSystemData,
deploymentInfo: { ...validSystemData.deploymentInfo, environment: 'invalid' }
});
await expect(invalidEnv.save()).rejects.toThrow('Environment must be one of: development, staging, production, testing');
});
test('should validate CORS methods', async () => {
const invalidMethod = new SystemTracking({
...validSystemData,
corsSettings: {
...validSystemData.corsSettings,
methods: ['INVALID_METHOD']
}
});
await expect(invalidMethod.save()).rejects.toThrow('Invalid HTTP method');
});
test('should validate field lengths', async () => {
const longOS = new SystemTracking({
...validSystemData,
systemInfo: { ...validSystemData.systemInfo, os: 'a'.repeat(51) }
});
await expect(longOS.save()).rejects.toThrow('OS name cannot exceed 50 characters');
const longLocation = new SystemTracking({
...validSystemData,
deploymentInfo: { ...validSystemData.deploymentInfo, serverLocation: 'a'.repeat(101) }
});
await expect(longLocation.save()).rejects.toThrow('Server location cannot exceed 100 characters');
});
});
describe('Virtual Properties', () => {
test('should calculate isRecentlyActive correctly', async () => {
const recentSystem = new SystemTracking({
licenseKey: 'RECENT-LICENSE-123',
systemInfo: {
os: 'Linux',
platform: 'linux',
arch: 'x64',
memory: 8589934592
},
deploymentInfo: {
ipAddress: '192.168.1.100',
environment: 'production'
},
lastSeen: new Date() // Current time
});
const oldSystem = new SystemTracking({
licenseKey: 'OLD-LICENSE-123',
systemInfo: {
os: 'Linux',
platform: 'linux',
arch: 'x64',
memory: 8589934592
},
deploymentInfo: {
ipAddress: '192.168.1.101',
environment: 'production'
},
lastSeen: new Date(Date.now() - 25 * 60 * 60 * 1000) // 25 hours ago
});
expect(recentSystem.isRecentlyActive).toBe(true);
expect(oldSystem.isRecentlyActive).toBe(false);
});
});
describe('Instance Methods', () => {
let systemTracking;
beforeEach(async () => {
systemTracking = new SystemTracking({
licenseKey: 'TEST-LICENSE-METHODS-123',
systemInfo: {
os: 'Linux',
platform: 'linux',
arch: 'x64',
memory: 8589934592
},
deploymentInfo: {
ipAddress: '192.168.1.100',
environment: 'production'
}
});
await systemTracking.save();
});
test('updateLastSeen should update timestamp', async () => {
const originalLastSeen = systemTracking.lastSeen;
// Wait a bit to ensure timestamp difference
await new Promise(resolve => setTimeout(resolve, 10));
await systemTracking.updateLastSeen();
expect(systemTracking.lastSeen.getTime()).toBeGreaterThan(originalLastSeen.getTime());
});
test('deactivate should set isActive to false', async () => {
expect(systemTracking.isActive).toBe(true);
await systemTracking.deactivate();
expect(systemTracking.isActive).toBe(false);
});
});
});