How does one E2E test OTP login?

I have developed login with OTP to verify the register mail id. I want to write cypress test case for this feature but the autogenerated OTP is receiving in the mail but when running the cypress test case based upon the predefined JSON file all the controls values get fetch from the JSON but the OTP is autogenerated so could not stored in the test spec file or JSON file. How do I write a test for this, given that the OTP changes every time when the new user is login with new Email ID?

To enter the email OTP in Cypress, I use the .type() command to simulate typing the OTP into the input field. However, since the OTP is generated dynamically, I need a way to retrieve it during the test execution.

One approach is to stub the API call that generates the OTP so that you can control its response and return a known OTP value
In the test code I use, the cy.intercept() command intercepts the API call to generate the OTP (POST /api/generate-otp) and returns a known OTP value (123456). This allows to control the OTP value during the test execution. After entering the OTP, you can proceed with the registration process as needed