def test_task_status_when_success(app, client):
    expected_response = EXPECTED_TASK_STATUS_SUCCESSFUL_RESPONSE
    with mock.patch("relengapi.blueprints.archiver.create_and_upload_archive") as caua:
        caua.AsyncResult.return_value = fake_successful_task_status()
        response = client.get('/archiver/status/{task_id}'.format(task_id=123))
    eq_(cmp(json.loads(response.data)['result'], expected_response), 0,
        "A successful task status check does not equal expected status.")
def test_tracker_is_deleted_when_task_status_shows_task_complete(app, client):
    with app.app_context():
        task_id = 'foo'
        session = app.db.session(tables.DB_DECLARATIVE_BASE)
        now = datetime.datetime(2015, 7, 14, 23, 19, 42, tzinfo=pytz.UTC)  # freeze time
        pending_expiry = now + datetime.timedelta(seconds=60)
        session.add(tables.ArchiverTask(task_id=task_id, s3_key='key', created_at=now,
                                        pending_expires_at=pending_expiry,
                                        src_url='https://foo.com', state="PENDING"))
        session.commit()
        with mock.patch("relengapi.blueprints.archiver.create_and_upload_archive") as caua:
            caua.AsyncResult.return_value = fake_successful_task_status()
            client.get('/archiver/status/{task_id}'.format(task_id=task_id))
        tracker = tables.ArchiverTask.query.filter(tables.ArchiverTask.task_id == task_id).first()
        eq_(tracker, None, "tracker was not deleted even though celery task completed.")