Skip to content

Testing and Mocking

Benson Cho edited this page Nov 3, 2024 · 20 revisions

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).

Swagger compatiable timestamps

You can run the script yarn workspace 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

Stripe Example

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;
})

Firebase Emulators

We can use the emulator for firebase when doing jest testing, but prefer to play with staging instance if testing manually

use yarn 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, you will unfortunately have to use these commands (TODO to make this easier):

  • yarn firebase-test "jest --config=server/jest.config.endpoints.ts <file name>" For anything in the middleware folder, i.e anything suffixed with Controller.test.ts

  • yarn firebase-test "jest --config=server/jest.config.server.ts <file name>" For anything outside the middleware folder

Steps for local testing, WIP to add to Wiki.

Testing

Testing ALL endpoints:

yarn test-endpoints

Testing ALL services and other test files:

yarn test-server

ngrock testing

  1. Start by installing ngrock and add the auth token provided in the dashboard.
ngrok config add-authtoken <TOKEN>
  1. Load into the dashboard and find the Cloud Edge -> Edges tab and create a new Edge, this is to bypass the ngrock browser warning.
  2. In the routes section, Routes -> Request Headers and add the key: ngrok-skip-browser-warning and the value: true

Note: Due to free ngrock account limitations, you cannot save the configuration but the request headers still work.

  1. You can then start the tunel by clicking Start a Tunnel
  2. 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 running yarn dev-server).
  3. Add the ngrock URL to the stripe webhook dashboard.
  4. Easier to follow steps as below !

Stripe webhook testing

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:

  1. Logging in to Stripe
stripe login

and, in three separate terminals,

  1. 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.

  1. Start the server in another terminal
yarn dev-server
  1. 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.

Creating a Firebase test user

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".