# Buggy code
def main(staging=False):
    app = journalist_app.create_app(config)
    with app.app_context():
        # Add two test users
        test_password = "correct horse battery staple profanity oil chewy"
        test_otp_secret = "JHCOGO7VCER3EJ4L"

        add_test_user("journalist", test_password, test_otp_secret, is_admin=True)

        if staging:
            return

        add_test_user("dellsberg", test_password, test_otp_secret, is_admin=False)

        # Add test sources and submissions
        num_sources = int(os.getenv("NUM_SOURCES", 2))
        for _ in range(num_sources):
            create_source_and_submissions()


# Traceback (AttributeError)
172.17.0.1 - - [31/Mar/2020 20:57:10] "GET /api/v1/replies HTTP/1.1" 500 -
Traceback (most recent call last):
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/journalist_app/api.py", line 48, in decorated_function
return f(*args, **kwargs)
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/journalist_app/api.py", line 310, in get_all_replies
{'replies': [reply.to_json() for reply in replies]}), 200
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/journalist_app/api.py", line 310, in <listcomp>
{'replies': [reply.to_json() for reply in replies]}), 200
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/models.py", line 289, in to_json
'journalist_username': self.journalist.username,
AttributeError: 'NoneType' object has no attribute 'username'

# Fixed code
def main(staging=False):
    app = journalist_app.create_app(config)
    with app.app_context():
        # Add two test users
        test_password = "correct horse battery staple profanity oil chewy"
        test_otp_secret = "JHCOGO7VCER3EJ4L"

        add_test_user("journalist", test_password, test_otp_secret, is_admin=True)

        if staging:
            return

        add_test_user("dellsberg", test_password, test_otp_secret, is_admin=False)

        journalist_tobe_deleted = add_test_user(
            "clarkkent",
            test_password,
            test_otp_secret,
            is_admin=False,
            first_name="Clark",
            last_name="Kent",
        )

        # Add test sources and submissions
        num_sources = int(os.getenv("NUM_SOURCES", 2))
        for i in range(num_sources):
            if i == 0:
                # For the first source, the journalist who replied will be deleted
                create_source_and_submissions(
                    journalist_who_replied=journalist_tobe_deleted
                )
                continue
            create_source_and_submissions()
        # Now let us delete one journalist
        db.session.delete(journalist_tobe_deleted)
        db.session.commit()