-
Notifications
You must be signed in to change notification settings - Fork 4
Testing and Mocking
We may need to mock some resources in cases that we do not want to continuously make network requests (such as in the case of stripe).
You can run the script pnpm --prefix=server timestamp yyyy-mm-dd yyyy-mm-dd (Format startDate then endDate) to get a timestamp that can be sent as a request body in swagger
https://stackoverflow.com/a/72482906
jest.mock('stripe', () => {
const stripe = jest.requireActual('stripe');
jest.spyOn(stripe.resources.Customers.prototype, 'create')
.mockImplementation(() => (
Promise.resolve({id: 'stripe-test-id'})
));
return stripe;
})
Warning
Common mistake! This applies to all endpoint testing but make sure you run pnpm dev --filter server every time you add new routes or change their decorators otherwise you might get stuck debugging why you keep getting 404 while its spelt correctly!
We can use the emulator for firebase when doing jest testing, but prefer to play with staging instance if testing manually
use pnpm firebase-test "jest <file name>" to run a test that requires firestore or firebase auth. Note that file name should be the name not a path
For testing the server files (with firebase emulator)
pnpm test:route <Controller Name e.g AdminController>pnpm test:service` <Service Name e.g UserController>pnpm test:routes
pnpm test:server
- Start by installing ngrock and add the auth token provided in the dashboard.
ngrok config add-authtoken <TOKEN>
- Load into the dashboard and find the Cloud Edge -> Edges tab and create a new Edge, this is to bypass the ngrock browser warning.
- In the routes section, Routes -> Request Headers and add the key:
ngrok-skip-browser-warningand the value:true
Note: Due to free ngrock account limitations, you cannot save the configuration but the request headers still work.
- You can then start the tunel by clicking
Start a Tunnel - After starting the tunnel, copy the
Copy and paste the following into your terminal.field and change the port to the server port(you can start the UASC server by runningpnpm dev --filter server). - Add the ngrock URL to the stripe webhook dashboard.
- Easier to follow steps as below !
Make sure you have Stripe CLI installed.
For local testing, ensure that your server .env file has a STRIPE_API_KEY, which stores the stripe API key for local testing purposes. This is the Standard key secret from the Stripe developer dashboard. Ask for credentials from the relevant person to get access to this.
For receiving test webhook events, you will also need to generate a STRIPE_WEBHOOK_SECRET. Do this by:
- Logging in to Stripe
stripe login
and, in three separate terminals,
- Forward Stripe webhook events to the dev server:
stripe listen --forward-to localhost:8000/webhook
which generates a webhook secret. You will need to place this secret under the STRIPE_WEBHOOK_EVENT key in the server .env file.
- Start the server in another terminal
pnpm dev --filter server
- Trigger Stripe webhook events in another terminal
stripe trigger checkout.session.completed --add checkout_session:metadata.type=membership --add checkout_session:client_reference_id=REFERENCE_ID_HERE
You can test with invalid metadata and client_reference_id, which should result in different HTTP status codes from the server.
To create a UID to test with, head to https://console.firebase.google.com/ and login with the credentials.
Go into the UASC project. Build->Authentication->Add user. Enter an email and password. Copy the User ID.
Head over to Firebase Database->Data->Click the users collection->Add document->Set the Document ID as the UID and add a field called "membership" set it to "guest".