alphagov/email-alert-service — pull request #24 by rboulton (2014-11-05 07:40:14+00:00)
Title: Fix missing key errors in LockHandler

No messages are currently being sent because the LockHandler expects to
be able to get `title` and `public_updated_at` keys from the hash passed
to the sidekiq worker, but the worker is only being passed the "formatted
email" (which has `subject` and `body` keys).

This commit fixes this by changing the format of the messages passed to
the sidekiq worker to include a `public_updated_at` key, and to move the
existing data under a `formatted` key:

{
"formatted" => {
"subject" => "foo",
"body" => "bar"
},
"public_updated_at" => timestamp
}

The LockHandler is changed to expect to be given a title and ISO-format
timestamp, instead of just the whole sidekiq message. I think it's best
to explicitly give it the values it needs to form its lock key, rather
than leave it to extract that from the sidekiq message, since this keeps
the interpretation of the values in the sidekiq message in one part of
the code.

The LockHandler tests were assuming that `public_updated_at` would be a
human readable timestamp - it should actually be an ISO format timestamp
(coming from the content-store), so while updating the tests I changed
the test values to be ISO format, too.

I also fixed the "does not send an email if there is an existing lock
key" LockHandler test to require that the email is sent exactly once,
rather than at most once, and also to use a time relative to now rather
than a fixed timestamp. In about 3 months, this test would have stopped
calling the method in question at all, since we don't attempt to send
messages if they're older than 3 months, but the test would have
continued to pass.

We'll want to purge the sidekiq queues after deploying this, since the
existing messages on them don't have enough information to be processed
correctly. This should be fine since there is no public link to the
email signup pages yet, so only test users should be subscribed.