7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
186 lines
6.9 KiB
JavaScript
186 lines
6.9 KiB
JavaScript
const puppeteer = require('puppeteer');
|
|
const AuthHelper = require('./auth_helper');
|
|
const { getPuppeteerLaunchOptions } = require('./puppeteer_config');
|
|
|
|
// Test changing API key via direct API calls
|
|
(async () => {
|
|
let browser;
|
|
let authHelper;
|
|
|
|
try {
|
|
console.log('=== Starting API Key Test via API ===');
|
|
|
|
// Test configuration
|
|
const TEST_API_KEY_1 = 'test-api-key-12345';
|
|
const TEST_API_KEY_2 = 'changed-api-key-67890';
|
|
const BASE_URL = 'http://localhost:5000';
|
|
|
|
// Launch browser
|
|
browser = await puppeteer.launch(getPuppeteerLaunchOptions());
|
|
|
|
const page = await browser.newPage();
|
|
|
|
// Initialize auth helper
|
|
authHelper = new AuthHelper(page);
|
|
|
|
// Create test user
|
|
const timestamp = Date.now();
|
|
const testUsername = `api_test_${timestamp}`;
|
|
const testPassword = 'testpass123';
|
|
|
|
console.log(`Creating user: ${testUsername}`);
|
|
await authHelper.register(testUsername, testPassword);
|
|
|
|
const isAuthenticated = await authHelper.isLoggedIn();
|
|
if (!isAuthenticated) {
|
|
throw new Error('Failed to authenticate after registration');
|
|
}
|
|
console.log('✓ User authenticated successfully');
|
|
|
|
// Test 1: Get current settings via API
|
|
console.log('\nTest 1: Getting current settings...');
|
|
const currentSettings = await page.evaluate(async () => {
|
|
const response = await fetch('/api/settings', {
|
|
credentials: 'same-origin'
|
|
});
|
|
return await response.json();
|
|
});
|
|
|
|
console.log(`Total settings: ${currentSettings.settings?.length || 0}`);
|
|
const apiKeySetting = currentSettings.settings?.find(s =>
|
|
s.key === 'llm.openai_endpoint.api_key' ||
|
|
s.setting_key === 'llm.openai_endpoint.api_key'
|
|
);
|
|
console.log(`Current API key setting found: ${!!apiKeySetting}`);
|
|
if (apiKeySetting) {
|
|
console.log(`Current value: ${apiKeySetting.value ? '***' + apiKeySetting.value.slice(-5) : 'empty'}`);
|
|
}
|
|
|
|
// Test 2: Update API key via API
|
|
console.log(`\nTest 2: Setting API key to: ${TEST_API_KEY_1}`);
|
|
const updateResult1 = await page.evaluate(async (key) => {
|
|
const response = await fetch('/api/settings', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({
|
|
'llm.openai_endpoint.api_key': key
|
|
})
|
|
});
|
|
return {
|
|
ok: response.ok,
|
|
status: response.status,
|
|
data: await response.json()
|
|
};
|
|
}, TEST_API_KEY_1);
|
|
|
|
console.log(`Update result: ${updateResult1.ok ? 'SUCCESS' : 'FAILED'} (status: ${updateResult1.status})`);
|
|
if (updateResult1.data.message) {
|
|
console.log(`Message: ${updateResult1.data.message}`);
|
|
}
|
|
|
|
// Test 3: Verify the update
|
|
console.log('\nTest 3: Verifying update...');
|
|
const verifySettings1 = await page.evaluate(async () => {
|
|
const response = await fetch('/api/settings', {
|
|
credentials: 'same-origin'
|
|
});
|
|
return await response.json();
|
|
});
|
|
|
|
const updatedSetting1 = verifySettings1.settings?.find(s =>
|
|
s.key === 'llm.openai_endpoint.api_key' ||
|
|
s.setting_key === 'llm.openai_endpoint.api_key'
|
|
);
|
|
|
|
if (updatedSetting1) {
|
|
console.log(`Verified value: ${updatedSetting1.value ? '***' + updatedSetting1.value.slice(-5) : 'empty'}`);
|
|
if (updatedSetting1.value && updatedSetting1.value.includes(TEST_API_KEY_1.slice(-5))) {
|
|
console.log('✓ API key 1 set successfully!');
|
|
} else {
|
|
console.log('✗ API key 1 was not set correctly');
|
|
}
|
|
}
|
|
|
|
// Test 4: Change to second API key
|
|
console.log(`\nTest 4: Changing API key to: ${TEST_API_KEY_2}`);
|
|
const updateResult2 = await page.evaluate(async (key) => {
|
|
const response = await fetch('/api/settings', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({
|
|
'llm.openai_endpoint.api_key': key
|
|
})
|
|
});
|
|
return {
|
|
ok: response.ok,
|
|
status: response.status,
|
|
data: await response.json()
|
|
};
|
|
}, TEST_API_KEY_2);
|
|
|
|
console.log(`Update result: ${updateResult2.ok ? 'SUCCESS' : 'FAILED'} (status: ${updateResult2.status})`);
|
|
|
|
// Test 5: Final verification
|
|
console.log('\nTest 5: Final verification...');
|
|
const verifySettings2 = await page.evaluate(async () => {
|
|
const response = await fetch('/api/settings', {
|
|
credentials: 'same-origin'
|
|
});
|
|
return await response.json();
|
|
});
|
|
|
|
const updatedSetting2 = verifySettings2.settings?.find(s =>
|
|
s.key === 'llm.openai_endpoint.api_key' ||
|
|
s.setting_key === 'llm.openai_endpoint.api_key'
|
|
);
|
|
|
|
if (updatedSetting2) {
|
|
console.log(`Final value: ${updatedSetting2.value ? '***' + updatedSetting2.value.slice(-5) : 'empty'}`);
|
|
if (updatedSetting2.value && updatedSetting2.value.includes(TEST_API_KEY_2.slice(-5))) {
|
|
console.log('✓ API key 2 set successfully!');
|
|
} else {
|
|
console.log('✗ API key 2 was not set correctly');
|
|
}
|
|
}
|
|
|
|
// Test 6: Check if settings persist after navigation
|
|
console.log('\nTest 6: Checking persistence after navigation...');
|
|
await page.goto(`${BASE_URL}/`, { waitUntil: 'domcontentloaded' });
|
|
await page.goto(`${BASE_URL}/settings`, { waitUntil: 'domcontentloaded' });
|
|
|
|
const finalCheck = await page.evaluate(async () => {
|
|
const response = await fetch('/api/settings', {
|
|
credentials: 'same-origin'
|
|
});
|
|
return await response.json();
|
|
});
|
|
|
|
const finalSetting = finalCheck.settings?.find(s =>
|
|
s.key === 'llm.openai_endpoint.api_key' ||
|
|
s.setting_key === 'llm.openai_endpoint.api_key'
|
|
);
|
|
|
|
if (finalSetting && finalSetting.value) {
|
|
console.log(`Settings persisted after navigation: ${finalSetting.value ? '***' + finalSetting.value.slice(-5) : 'empty'}`);
|
|
}
|
|
|
|
console.log('\n=== API Key Test Completed ===');
|
|
|
|
} catch (error) {
|
|
console.error(`Test failed: ${error.message}`);
|
|
console.error(error);
|
|
process.exit(1);
|
|
} finally {
|
|
if (browser) {
|
|
await browser.close();
|
|
console.log('Browser closed');
|
|
}
|
|
}
|
|
})();
|