def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):

    from tests.app.conftest import sample_job as create_job
    from tests.app.conftest import sample_service as create_service
    from tests.app.conftest import sample_template as create_template
    from tests.app.conftest import sample_user as create_user

    one_job = create_job(notify_db, notify_db_session, sample_template.service,
                         sample_template)

    other_user = create_user(notify_db, notify_db_session,
                             email="*****@*****.**")
    other_service = create_service(notify_db, notify_db_session,
                                   user=other_user, service_name="other service")
    other_template = create_template(notify_db, notify_db_session,
                                     service=other_service)
    other_job = create_job(notify_db, notify_db_session, service=other_service,
                           template=other_template)

    one_job_from_db = get_jobs_by_service(one_job.service_id)
    other_job_from_db = get_jobs_by_service(other_job.service_id)

    assert len(one_job_from_db) == 1
    assert one_job == one_job_from_db[0]

    assert len(other_job_from_db) == 1
    assert other_job == other_job_from_db[0]

    assert one_job_from_db != other_job_from_db
def test_get_api_keys_should_return_all_keys_for_service(notify_api, notify_db,
                                                         notify_db_session,
                                                         sample_api_key):
    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            another_user = create_user(notify_db, notify_db_session, email='*****@*****.**')

            another_service = create_sample_service(
                notify_db,
                notify_db_session,
                service_name='another',
                user=another_user,
                email_from='another'
            )
            # key for another service
            create_sample_api_key(notify_db, notify_db_session, service=another_service)

            # this service already has one key, add two more, one expired
            create_sample_api_key(notify_db, notify_db_session, service=sample_api_key.service)
            one_to_expire = create_sample_api_key(notify_db, notify_db_session, service=sample_api_key.service)
            expire_api_key(service_id=one_to_expire.service_id, api_key_id=one_to_expire.id)

            assert ApiKey.query.count() == 4

            auth_header = create_authorization_header()
            response = client.get(url_for('service.get_api_keys',
                                          service_id=sample_api_key.service_id),
                                  headers=[('Content-Type', 'application/json'), auth_header])
            assert response.status_code == 200
            json_resp = json.loads(response.get_data(as_text=True))
            assert len(json_resp['apiKeys']) == 3
def test_get_api_keys_should_return_all_keys_for_service(notify_api, notify_db,
                                                         notify_db_session,
                                                         sample_api_key):
    with notify_api.test_request_context():
        with notify_api.test_client() as client:
            another_user = create_user(notify_db, notify_db_session, email='*****@*****.**')
            another_service = create_sample_service(notify_db, notify_db_session, service_name='another',
                                                    user=another_user)
            create_sample_api_key(notify_db, notify_db_session, service=another_service)
            api_key2 = ApiKey(**{'service_id': sample_api_key.service_id, 'name': 'second_api_key'})
            api_key3 = ApiKey(**{'service_id': sample_api_key.service_id, 'name': 'third_api_key',
                                 'expiry_date': datetime.utcnow() + timedelta(hours=-1)})
            save_model_api_key(api_key2)
            save_model_api_key(api_key3)
            assert ApiKey.query.count() == 4

            auth_header = create_authorization_header(path=url_for('service.get_api_keys',
                                                                   service_id=sample_api_key.service_id),
                                                      method='GET')
            response = client.get(url_for('service.get_api_keys',
                                          service_id=sample_api_key.service_id),
                                  headers=[('Content-Type', 'application/json'), auth_header])
            assert response.status_code == 200
            json_resp = json.loads(response.get_data(as_text=True))
            assert len(json_resp['apiKeys']) == 3
def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
    one_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template)

    other_user = create_user(notify_db, notify_db_session, email="*****@*****.**")
    other_service = create_service(notify_db, notify_db_session, user=other_user, service_name="other service",
                                   email_from='other.service')
    other_template = create_template(notify_db, notify_db_session, service=other_service)
    other_job = create_job(notify_db, notify_db_session, service=other_service, template=other_template)

    one_job_from_db = dao_get_jobs_by_service_id(one_job.service_id).items
    other_job_from_db = dao_get_jobs_by_service_id(other_job.service_id).items

    assert len(one_job_from_db) == 1
    assert one_job == one_job_from_db[0]

    assert len(other_job_from_db) == 1
    assert other_job == other_job_from_db[0]

    assert one_job_from_db != other_job_from_db