oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.08k stars 2.47k forks source link

Got this crash when testing | sending mails with aws SES is not working! #10864

Open BennyAlex opened 1 week ago

BennyAlex commented 1 week ago

How can we reproduce the crash?

No response

JavaScript/TypeScript code that reproduces the crash?

import { routes, startServer } from '@/startServer';
import mailUtils from '@/utils/mailUtils';
import * as Sentry from '@sentry/node';
import { afterAll, beforeAll, describe, test } from 'bun:test';
import fetchWrapper from './fetchWrapper';

// start the server before running the tests
console.log('\n\nStarting server...\n');

beforeAll(async () => {
    startServer();
});

const errors: any[] = [];

const prodReceiver = [
    'bla@eye-able.com',
    'bla2@eye-able.com',
    'bla3@eye-able.com'
];

const additionalParams: Record<string, any> = {
    /* '/search/auditlicenses': {
        searchterm: 'eye-able.com'
    },
    '/search/assistlicensesbydomain': {
        domain: 'eye-able.com'
    },
    '/search/reportentries': {
        searchterm: 'eye-able.com'
    },
    '/search/users': {
        searchterm: 'api tester'
    }, */
    '/search/assistlicenses': {
        searchterm: 'eye-able.com'
    },
    '/search/organisations': {
        searchterm: 'web inclusion'
    },
    '/assistconfig': {
        domain: 'eye-able.com'
    },
    '/translatelicense': {
        domain: 'eye-able.com',
        key: 'bla'
    },
    '/licenseconstraints': {
        userid: 3
    },
    '/users': {
        id: 3
    },
    '/assistlicenses': {
        domain: 'eye-able.com'
    },
    '/reportentries/config': {
        entryid: 1
    }
};

if (Object.keys(routes).length < 20) {
    await onError('No or not enough routes found. Routes: ' + JSON.stringify(routes, null, 4));
    throw new Error('No or not enough routes found. Routes: ' + JSON.stringify(routes, null, 4));
}

let cookie = '';

async function onError(error: any) {
    if (Bun.env.PROD) {
        Sentry.captureException('CRITICAL API ERROR!: ' + error);
    }

    console.log('\nSending error mail...\n');

    return await mailUtils.sendCriticalErrorMail({
        receiver: Bun.env.PROD
            ? prodReceiver
            : ['bla@eye-able.com', 'blabla@gmail.com'],
        data: {
            errorInfo: error
        }
    });
}

describe('API tests', () => {
    /*  afterAll(async () => {

        // After all tests are done, check if any test failed
        if (errors.length) {
            console.error('afterAll | At least one test failed!');
            console.log('Errors:', JSON.stringify(errors, null, 4));
            await onError(errors);
        }

        const { numFailedTests } = jest.getState().testResults;
        if (numFailedTests > 0) {
            console.error('Some tests failed!');
        } else {
            console.log('All tests passed!');
        }
    }); */
    // Check if any test failed after running all tests
    afterAll(async () => {
        if (errors.length) {
            console.error('At least one test failed!');
            console.log('Errors:', JSON.stringify(errors, null, 4));
            await onError(errors);
        } else {
            console.log('All tests passed!');
        }
    });

    // Login and retrieve token
    beforeAll(async () => {
        try {
            if (!Bun.env.TEST_PASSWORD) {
                throw new Error('TEST_PASSWORD not set in .env file');
            }

            const res = await fetchWrapper('/authentication/login', {
                method: 'POST',
                body: JSON.stringify({
                    email: blabla@eye-able.com',
                    password: Bun.env.TEST_PASSWORD
                }),
                headers: {
                    'Content-Type': 'application/json'
                }
            });

            cookie = res.headers.get('SET-COOKIE') || '';
            if (!cookie) {
                throw new Error('Cookie not found in login response');
            }
        } catch (error) {
            console.error('Login failed:', error);
            await onError('API Login failed!');
            throw new Error('Login failed, stopping all tests.'); // This will stop further tests from running in this describe block
        }
    });

    // Test all GET routes
    describe('checking GET methods of all routes', () => {
        for (let [path, handlers] of Object.entries(routes)) {
            for (const handler of handlers) {
                if (handler.method && handler.method === 'GET') {
                    test(`GET ${path}`, async () => {
                        try {
                            if (additionalParams[path]) {
                                const params = new URLSearchParams();
                                for (const [key, value] of Object.entries(additionalParams[path])) {
                                    params.set(key, String(value));
                                }
                                path += '?' + params.toString();
                            }

                            await fetchWrapper(path, {}, cookie);
                        } catch (error) {
                            console.error(`GET ${path} failed:`, error);
                            errors.push(error);
                            throw error;
                        }
                    });
                }
            }
        }
    });
});

Relevant log output

No response

Stack Trace (bun.report)

Bun v1.1.6 (e58d67b) on linux x86_64 [TestCommand]

Segmentation fault at address 0x00000000

BennyAlex commented 1 week ago

I thinks its related to mail sending which is done via amazon SES

BennyAlex commented 1 week ago
import { SESClient, SendTemplatedEmailCommand } from '@aws-sdk/client-ses';

const id = Bun.env.AWS_ACCESS_KEY_ID || '';
const secret = Bun.env.AWS_SECRET_ACCESS_KEY || '';

if (!id || !secret) {
    throw new Error('No AWS credentials found');
}

export const mailClient = new SESClient({
    region: 'eu-central-1',
    credentials: {
        accessKeyId: id,
        secretAccessKey: secret
    }
});
BennyAlex commented 1 week ago

I can't send any emails with SES! Both with the old v2 version and also the v3 version