Some features end outside your system. A one-time password goes by SMS, an order confirmation by email, a delivery update by WhatsApp or push notification. Your code can do its part perfectly and the customer still gets nothing, or gets it twice, or gets it eleven minutes late. Testing this properly means choosing between two approaches, and the choice is more interesting than it first looks.
Approach one: go through the real channel
Use a real phone number and a real inbox. The tester triggers a login, waits for the SMS, reads the code and types it in. Its great strength is realism. It exercises everything: your application, the messaging provider, the telecom network, the handset. It can reveal that messages to one operator arrive slowly, or that the sender name shows incorrectly, or that a link in the email is broken by a mail client.
In India there is an extra layer. Commercial and transactional SMS must go through the telecom regulator’s framework in which senders and message templates are registered with the operators (the DLT system under TRAI rules). A message that does not match a registered template can be blocked. That is a real failure mode that no mock can show you, and it often surfaces only on a production-like route. When wording changes, the registered template may need to change too.
The weaknesses are clear. Real channels are slow, and delivery time is outside your control. They cost money per message. They cannot be run in parallel by hundreds of tests on one shared number. They are unreliable in CI. And they cannot be automated cleanly: reading an SMS from a handset in a script is a project in itself. Teams that rely on this approach alone end up with OTP checks that are manual, run rarely and skipped when the release is late.
Approach two: intercept it before it leaves
Here the test environment swaps the real sender for something observable. The options differ in how much they replace.
- A fixed code for specific test numbers, so the tester knows the OTP in advance. Simple, but it must be impossible to enable in production, which deserves a test of its own.
- A test-only endpoint or database table that exposes the last message sent to an address. The test triggers the action, then fetches the message and reads the code.
- A fake mail server that catches all outgoing email in a browsable inbox, such as MailHog or Mailpit, so nothing reaches real people and tests can read the content.
- The provider’s own sandbox or test mode, where it offers one, which accepts requests and reports status without delivering.
This approach is fast, free, repeatable and parallel-safe. It lets you test things that are painful in the real world: a code that expires after the set time, a third wrong attempt locking the account, resending twice in a row, using a code on a different number, using a code twice. The weakness is that you are testing up to the boundary and trusting the other side of it. A broken provider credential or an unregistered template will pass every hooked test.
Taking a side
Use hooks for the bulk, and the real channel for a thin smoke check. The behaviour that matters in an OTP flow is logic, and logic is cheap to test with hooks. The delivery path matters too, but it fails rarely and for reasons you can monitor, so a small number of real sends per release plus production monitoring is a better use of effort than ten manual sends before every build.
The OTP logic worth covering with hooks:
- Correct code accepted; wrong code rejected with a message that does not reveal whether the account exists.
- Expiry: a code is rejected just after its validity window, and accepted just before. Control the clock rather than waiting.
- A new code invalidates the previous one, or the rules say otherwise and are tested as stated.
- Limits: attempts per code, resends per hour, requests per number, and what the user sees on reaching them.
- Single use: the same code cannot complete two logins.
- Codes are not leaked: not in the response body of the request that asks for the code, not in logs, not in the page source.
Notice the last item. A fixed or exposed test code is exactly the thing that must never exist in production. Test that the production configuration rejects the test mode, and keep that configuration under review.
Notifications beyond OTPs
The same split applies to emails and push messages. Check content against the data that triggered it: right name, right amount, rupee formatting, date in the right time zone, working links with correct tracking and no broken placeholders such as an empty first name. Check that the message is sent once per event, so a retry in the background does not send three order confirmations. Check what happens when the provider is down: is the message queued and retried, or lost? Check unsubscribe handling and that transactional messages are not blocked by marketing opt-outs where they should still go.
For the real-channel smoke, keep one or two owned test numbers and mailboxes, with a short written list of what to verify by eye: sender name, wording, link, rendering on a phone, delay. Run it after changes to the provider, the templates or the sending configuration, and not on every build.
Finally, watch production. Delivery rate by operator and median delivery time are available from most providers, and a sudden drop is usually the first sign of a problem no test environment could show. Testing narrows the risk; monitoring catches the part testing cannot reach.
Leave a Reply