def test_delete_user_who_is_not_in_the_db(self):
        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 0)

        process_account_event(message_body(event="delete", uid=UID, iss=ISS))

        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 0)
    def test_reset_user_by_legacy_uid_format(self):
        self.database.allocate_user(EMAIL, generation=12)

        process_account_event(
            message_body(
                event="reset",
                uid=EMAIL,
                generation=43,
            ))

        user = self.database.get_user(EMAIL)
        self.assertEquals(user["generation"], 42)
    def test_password_change(self):
        self.database.allocate_user(EMAIL, generation=12)

        process_account_event(
            message_body(
                event="passwordChange",
                uid=UID,
                iss=ISS,
                generation=43,
            ))

        user = self.database.get_user(EMAIL)
        self.assertEquals(user["generation"], 42)
    def test_password_change_user_not_in_db(self):
        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 0)

        process_account_event(
            message_body(
                event="passwordChange",
                uid=UID,
                iss=ISS,
                generation=43,
            ))

        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 0)
    def test_reset_user_who_is_not_in_the_db(self):
        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 0)

        process_account_event(
            message_body(
                event="reset",
                uid=UID,
                iss=ISS,
                generation=43,
            ))

        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 0)
    def test_delete_user_by_legacy_uid_format(self):
        self.database.allocate_user(EMAIL)
        user = self.database.get_user(EMAIL)
        self.database.update_user(user, client_state="abcdef")
        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 2)
        self.assertTrue(records[0]["replaced_at"] is not None)

        process_account_event(message_body(
            event="delete",
            uid=EMAIL,
        ))

        records = list(self.database.get_user_records(EMAIL))
        self.assertEquals(len(records), 2)
        for row in records:
            self.assertTrue(row["replaced_at"] is not None)
    def test_malformed_events(self):

        # Unknown event type.
        process_account_event(
            message_body(
                event="party",
                uid=UID,
                iss=ISS,
                generation=43,
            ))
        self.assertMessageWasLogged("Dropping unknown event type")
        self.clearLogs()

        # Missing event type.
        process_account_event(message_body(
            uid=UID,
            iss=ISS,
            generation=43,
        ))
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()

        # Missing uid.
        process_account_event(message_body(
            event="delete",
            iss=ISS,
        ))
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()

        # Missing generation for reset events.
        process_account_event(message_body(
            event="reset",
            uid=UID,
            iss=ISS,
        ))
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()

        # Missing generation for passwordChange events.
        process_account_event(
            message_body(
                event="passwordChange",
                uid=UID,
                iss=ISS,
            ))
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()

        # Missing issuer with nonemail uid
        process_account_event(message_body(
            event="delete",
            uid=UID,
        ))
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()

        # Non-JSON garbage.
        process_account_event("wat")
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()

        # Non-JSON garbage in Message field.
        process_account_event('{ "Message": "wat" }')
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()

        # Badly-typed JSON value in Message field.
        process_account_event('{ "Message": "[1, 2, 3"] }')
        self.assertMessageWasLogged("Invalid account message")
        self.clearLogs()