acmucsd/membership-portal

Mock the lowercased emails in user registration tests

Closed this issue · 0 comments

Currently in our user registration tests such as this one, our mocking pattern is as such:

  • We create a fake user called user via. UserRegistrationFactory that isn't persisted to the database. This user's email can have uppercases, since faker.internet.email() can produce emails with uppercased names (e.g. "Dominic65@yahoo.com" was one generated by faker).
  • We then mock the sendEmailVerification with user.email, meaning a potentially uppercased email is mocked
  • We call the controller's /registration API, which lowercases the email in its first line of code (here)
  • It will then insert users / send emails with that lowercased email later in the controller
  • Later in the test, we verify that sendEmailVerification with user.email is called once.

The issue is in the last step above - user.email at this line of code is lowercased because it was passed into the controller method and that method modifies the user object. Whereas user.email at the mock at the start of the test is uppercased. This means that the sendEmailVerification method with the proper user's email is NOT mocked, meaning that SendGrid might actually be sending emails to this user's lowercased email.

However, I checked SendGrid and didn't see any emails being sent to testing-like email addresses (they were all to valid UCSD students' email addresses). So I'm unsure what the behavior actually is, but we should still make sure that we are mocking the lowercased email addresses vs. the generated ones, since the generated ones from UserRegistrationFactory could be uppercased.

One solution is to simply lowercase the fake generated email directly in UserRegistrationFactory, but this then misses the edge case of actual users submitting uppercased emails - we still want to make sure we are handling this. The real solution should be to mock the lowercased email in the when() calls in the auth tests.