Skip to content

Commit

Permalink
✨ Add getVerificationCodeFromScalingo
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlaa committed Mar 4, 2025
1 parent 2432e8a commit 0abc656
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 114 deletions.
88 changes: 88 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { addMatchImageSnapshotPlugin } from '@simonsmith/cypress-image-snapshot/plugin'
import { spawn } from 'child_process'
import { defineConfig } from 'cypress'
import dotenv from 'dotenv'
import { Client } from 'pg'

dotenv.config()

Expand All @@ -21,6 +23,92 @@ export default defineConfig({
baseUrl: process.env.CYPRESS_baseUrl ?? 'http://localhost:3000',
setupNodeEvents(on) {
addMatchImageSnapshotPlugin(on)
on('task', {
getVerificationCodeFromScalingo: async () => {
let tunnelProcess = null
try {
// Set up SSH tunnel to Scalingo
const scalingoApp = process.env.SCALINGO_APP_NAME
const scalingoPostgresqlUrl = process.env.SCALINGO_POSTGRESQL_URL

if (!scalingoApp || !scalingoPostgresqlUrl) {
throw new Error(
'Missing required environment variables: SCALINGO_APP_NAME or SCALINGO_POSTGRESQL_URL'
)
}

// Use spawn instead of execSync to run the tunnel in background
tunnelProcess = spawn(
'scalingo',
['--app', scalingoApp, 'db-tunnel', scalingoPostgresqlUrl],
{
detached: true,
stdio: ['ignore', 'pipe', 'pipe'],
}
)

// Add event listeners to capture output and errors
tunnelProcess.stdout.on('data', (data) => {
console.log(`Tunnel stdout: ${data}`)
})

tunnelProcess.stderr.on('data', (data) => {
console.error(`Tunnel stderr: ${data}`)
})

// Wait longer for the tunnel to establish
console.log('Waiting for SSH tunnel to establish...')
await new Promise((resolve) => setTimeout(resolve, 8000))
console.log('SSH tunnel setup attempted')

// Extract connection parameters from tunnelOutput if needed
// For now using hardcoded values
const client = new Client({
host: 'localhost',
port: 10000,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
connectionTimeoutMillis: 10000, // Add timeout
})

await client.connect()
console.log('Database client connected')

// Query to get the verification code
const result = await client.query(
`
SELECT code FROM ngc."VerificationCode"
WHERE email = '[email protected]'
LIMIT 1
`
)

await client.end()
console.log('Database connection closed')

// Return the result
if (result.rows.length > 0) {
return result.rows[0].code
} else {
return null
}
} catch (error) {
console.error('Error getting verification code:', error)
return `Error: ${error.message}`
} finally {
// Always clean up the tunnel process
if (tunnelProcess && tunnelProcess.pid) {
try {
process.kill(-tunnelProcess.pid, 'SIGTERM')
console.log('Tunnel process terminated')
} catch (killError) {
console.error('Error terminating tunnel process:', killError)
}
}
}
},
})
},
experimentalRunAllSpecs: true,
specPattern: 'cypress/e2e/**/*.cy.js',
Expand Down
6 changes: 6 additions & 0 deletions cypress/constants/elements-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ export const SAVE_MODAL_EMAIL_INPUT = 'save-modal-email-input'
export const SAVE_MODAL_SUBMIT_BUTTON = 'save-modal-submit-button'
export const FIN_EMAIL_INPUT = 'fin-email-input'
export const FIN_EMAIL_SUBMIT_BUTTON = 'fin-email-submit-button'
export const ORGANISATION_CONNEXION_EMAIL_INPUT =
'organisation-connexion-email-input'
export const ORGANISATION_CONNEXION_SUBMIT_BUTTON =
'organisation-connexion-submit-button'
export const ORGANISATION_CONNEXION_VERIFICATION_CODE_INPUT =
'organisation-connexion-verification-code-input'
Loading

0 comments on commit 0abc656

Please sign in to comment.