Working on an automated integration test to check that I’ve fixed a bug that manifests itself with new user accounts.
When a new account is created, the user has to click on a link they receive via email in order to activate. Test script:
- Reset the database
- Use splinter to fill in the form to request a new account
- Wait for new email
- Use imaplib to retrieve email, parse out link to click
- Use splinter to visit link
- (Exercise the rest of the system to verify the bug has been fixed)
Each time I run the test script, step #3 fails with the error: The link you clicked is no longer valid. What’s going on?
My thought process:
Run the test script the first time… Hey, where’s that email? OK, let’s abort, change something minor, try it again… Still no email. OK, finally, this time an email arrived. But the link didn’t work? What could be the problem? I know this works when I do it manually. Why would it fail when run from a script? Am I somehow retrieving the wrong email from the server? Is the system behaving differently when I follow that URL using splinter than when I manually click on the link in my email client?
I banged my head against the wall for a while on this, although the problem looks obvious in retrospect. The first initial “failed” attempts at running the script succeeded in sending the email, it just took longer than I anticipated for the emails to reach the account’s inbox. What happened was when my script finally retrieved an email from the server (after I had run the script N times), the email had been generated by the first time I had run the test script. But that was a stale link, only valid for the first attempt.
This happened each time I ran the test, where test script run M was retrieving a link associated with test run M-N. Effectively, my scripts were running out of phase with my email account.