const Parse = require('parse/node');
const APP_ID = process.env.PARSE_SERVER_APPLICATION_ID;
const MASTER_KEY = process.env.PARSE_SERVER_MASTER_KEY;
const REST_API_KEY = process.env.PARSE_SERVER_REST_API_KEY;
const JAVASCRIPT_KEY = process.env.PARSE_SERVER_JAVASCRIPT_KEY;
const PARSE_PORT = process.env.PARSE_SERVER_PORT;
if (!PARSE_PORT) {
console.error('Error: PARSE_SERVER_PORT is not set');
process.exit(1);
}
const SERVER_URL = `http://0.0.0.0:${PARSE_PORT}/parse`; const TEST_USERNAME = process.env.ADMIN_USERNAME;
const TEST_PASSWORD = process.env.ADMIN_PASSWORD;
if (APP_ID && JAVASCRIPT_KEY && SERVER_URL && MASTER_KEY) {
Parse.initialize(APP_ID, JAVASCRIPT_KEY, MASTER_KEY);
Parse.serverURL = SERVER_URL;
console.log('Parse SDK initialized with App ID, JavaScript Key, and Master Key.');
} else {
console.error('Error: Missing APP_ID, JAVASCRIPT_KEY, MASTER_KEY, or SERVER_URL for Parse SDK initialization.');
process.exit(1);
}
async function testParseConnection() {
console.log('Testing Parse Server connection...');
if (!APP_ID || !MASTER_KEY || !SERVER_URL || !TEST_USERNAME || !TEST_PASSWORD || !REST_API_KEY || !JAVASCRIPT_KEY) {
console.error('Error: Missing required environment variables for connection test script.');
process.exit(1);
}
try {
console.log(`Initialized Parse with:
- App ID: ${APP_ID}
- Server URL: ${SERVER_URL}`);
try {
console.log('\nTest 1: Testing basic connection...');
const health = await fetch(`${SERVER_URL}/health`);
const healthData = await health.json();
console.log(`Health check response: ${JSON.stringify(healthData)}`);
if (healthData.status === "initialized" || healthData.status === "ok") {
console.log('✅ Server is initialized and ready');
} else {
throw new Error(`Unexpected health status: ${healthData.status}`);
}
console.log('✅ Basic connection successful');
} catch (error) {
console.error('❌ Basic connection failed:', error.message);
}
try {
console.log('\nTest 2: Testing user login...');
console.log(`Attempting to log in as: ${TEST_USERNAME}`);
const user = await Parse.User.logIn(TEST_USERNAME, TEST_PASSWORD);
console.log(`✅ Login successful! User ID: ${user.id}`);
console.log(`Session token: ${user.getSessionToken()}`);
} catch (error) {
console.error('❌ Login failed:', error.message);
try {
console.log('\nChecking if user exists...');
const query = new Parse.Query(Parse.User);
query.equalTo('username', TEST_USERNAME);
const existingUser = await query.first({ useMasterKey: true });
if (existingUser) {
console.log(`User ${TEST_USERNAME} exists in the database.`);
} else {
console.log(`User ${TEST_USERNAME} does not exist in the database.`);
}
} catch (userCheckError) {
console.error('Failed to check if user exists:', userCheckError.message);
}
}
try {
console.log('\nTest 3: Creating a test object...');
const TestObject = Parse.Object.extend('TestObject');
const testObject = new TestObject();
testObject.set('foo', 'bar');
testObject.set('timestamp', new Date());
const savedObject = await testObject.save(null, { useMasterKey: true });
console.log(`✅ Test object created with ID: ${savedObject.id}`);
} catch (error) {
console.error('❌ Failed to create test object:', error.message);
}
} catch (error) {
console.error('Fatal error during tests:', error);
}
}
testParseConnection().then(() => {
console.log('\nTests completed.');
});