Exemple #1
0
def seed_shelters():
    i = 0
    while i < len(shelterIds) -1:

        response = requests.get(f'https://api.petfinder.com/v2/organizations/{shelterIds[i]}', auth=BearerAuth('eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJIYWdiU0ZpcWJ6S3lLeHh5dm1OVTlJV1RSVTZTZDBORFo4WmxRU0h1M2lFQlFRbm83WSIsImp0aSI6IjZlMTg1ZTY5MWUyMTg2OGRkNjA2NzBhMjRmYjkwMTAxNjAxNjhhYWExYTBhN2M4YWZkZmRjYTNkZmVmNWUyMWVkZWY3NjE1N2MxOGQxMmU4IiwiaWF0IjoxNjIxOTAwNjk0LCJuYmYiOjE2MjE5MDA2OTQsImV4cCI6MTYyMTkwNDI5NCwic3ViIjoiIiwic2NvcGVzIjpbXX0.Hw5OVsn94TaHn0jQCS50AeSdXFv5U5s-XYmS8SolYjz0EjyGbJPxZvVb6HLRNWzTgbvQUBVYdY0ABC3ZkQcziMrZiQI60gxo4kFGFceWsLTCYMQHwkhQY0ierphZ9ldZeGmVyjX3rtDtOWrgSIyze0x76dIzLOZvg_LLAJWAue1Wr0konec9d4RlVq7ZuEyokmGdqBBud1M5e5_uXd_ie8h5ivJBHQqWy1M-EqsuC1sQwGlOm7AyU44ABlfEgyBNY4cMYY8ZuGxSAw1XOF8oabb87POLoQF4imZ6D_Oy-8x3KZYuuvgG2OURivCR086gzhsHeEDH6wYVjDvmR71ixw')).json()
        item = response['organization']
        if item['address']['address1']:
            demo = Shelter(
                name = item['name'],
                address = item['address']['address1'] + ' ' + item['address']['city'] + ' ' + item['address']['state'] + ' ' + item['address']['postcode'] + ' ' + item['address']['country'],
                phone_number = item['phone'],
                office_hours = '9-5 ' + '9-5 ' + '9-5 ' + '9-5 ' + '9-5 ' + '12-4 ' + '12-4 ',
                email = item['email'],
                description = item['mission_statement'],
                image_url = item['photos'][0]['full'],
            )
        else:
             demo = Shelter(
                name = item['name'],
                address = item['address']['city'] + ' ' + item['address']['state'] + ' ' + item['address']['postcode'] + ' ' + item['address']['country'],
                phone_number = item['phone'],
                office_hours = '9-5 ' + '9-5 ' + '9-5 ' + '9-5 ' + '9-5 ' + '12-4 ' + '12-4 ',
                email = item['email'],
                description = item['mission_statement'],
                image_url = item['photos'][0]['full'],
            )
        db.session.add(demo)
        i += 1
    db.session.commit()
def test_start_call_multiple(mockObj, app_with_envion_DB, inactive_shelter, test_shelters):
    '''It should call the url to initiate the Twilio flow with the correct data for each active shelter'''
    for s in test_shelters:
        db.session.add(Shelter(**s))

    s2 = Shelter(**inactive_shelter)
    db.session.add(s2)
    db.session.commit()
    client = app_with_envion_DB.test_client()
    client.get('/twilio/start_call/')

    assert mockObj.call_count == 2
    mockObj.assert_any_call(twil_url, urllib.parse.urlencode(getDataRoute(test_shelters[0])).encode())
    mockObj.assert_any_call(twil_url, urllib.parse.urlencode(getDataRoute(test_shelters[1])).encode())
def test_start_call_empty_number(mockObj, app_with_envion_DB, test_shelters, shelter_no_number, shelter_empty_number):
    '''It should not try to call shelters with undefined or empty numbers'''
    for s in test_shelters:
        db.session.add(Shelter(**s))
    s2 = Shelter(**shelter_no_number)
    db.session.add(s2)
    s4 = Shelter(**shelter_empty_number)
    db.session.add(s4)

    db.session.commit()

    client = app_with_envion_DB.test_client()
    client.get('/twilio/start_call/')

    assert mockObj.call_count == 2
    mockObj.assert_any_call(twil_url, urllib.parse.urlencode(getDataRoute(test_shelters[0])).encode())
    mockObj.assert_any_call(twil_url, urllib.parse.urlencode(getDataRoute(test_shelters[1])).encode())
def test_start_call_return_false(urlopen_mock, app_with_envion_DB, test_shelters):
    '''It should return a 449 status code when there were shelters to call'''
    test_shelter = test_shelters[0]
    s = Shelter(**test_shelter)
    db.session.add(s)
    db.session.commit()
    client = app_with_envion_DB.test_client()
    rv = client.get('/twilio/start_call/')
    assert rv.status_code == 449
def test_start_call_inactive(mockObj, app_with_envion_DB, inactive_shelter):
    '''It should not call the url to initiate the Twilio flow for inactive shelters'''
    s2 = Shelter(**inactive_shelter)
    db.session.add(s2)
    db.session.commit()

    client = app_with_envion_DB.test_client()
    client.get('/twilio/start_call/')

    mockObj.assert_not_called()
def test_start_call(mockObj, app_with_envion_DB, test_shelters):
    '''It should call the url to initiate the Twilio flow with the correct data'''
    test_shelter = test_shelters[0]
    s = Shelter(**test_shelter)
    db.session.add(s)
    db.session.commit()

    client = app_with_envion_DB.test_client()
    client.get('/twilio/start_call/')

    mockObj.assert_called_with(twil_url, urllib.parse.urlencode(getDataRoute(test_shelter)).encode())
def test_get_shelters_no_auth(app_with_envion_DB, test_shelters):
    '''/api/shelters should return 401 when auth is not present '''

    for s in test_shelters:
        db.session.add(Shelter(**s))

    db.session.commit()

    client = app_with_envion_DB.test_client()

    rv = client.get('/api/shelters/')
    assert rv.status_code == 401
def test_validate_shelter_bad(app_with_envion_DB, test_shelters):
    ''' Should not return the shelter data given a known pip '''

    s = Shelter(**test_shelters[0])
    db.session.add(s)
    db.session.commit()
    data = {'shelterID': '89898198'}
    client = app_with_envion_DB.test_client()

    rv = client.post('/twilio/validate_shelter/', data=data)
    res = json.loads(rv.data)
    assert res['success'] == False
Exemple #9
0
    def setUp(self):
        db.create_all()
        db.session.add(Adoptors(name='Testname',email='*****@*****.**'))
        db.session.add(Adoptors(name='Billtest',email='*****@*****.**'))
        db.session.add(Shelter(name="Testshelter", address="123 Fake st.", city="Fake", state="Florida", zipCode="12345", website="http://test.com", maximum_capacity=10, current_capacity=5))
        db.session.add(Puppy(name="Testpup", shelter_id=1, gender="male", dateOfBirth=datetime.datetime.now(),picture="http://testpup.com",weight=1, show=True))
        db.session.add(Profile(puppy_id=1, breed="Testbreed",description="This is a test description", specialNeeds="Testblind"))
        
        db.session.add(Puppy(name="Billpup", shelter_id=1, gender="male", dateOfBirth=datetime.datetime.now(),picture="http://billpup.com",weight=2, show=True))
        db.session.add(Profile(puppy_id=2, breed="Testbreed",description="This is a test description1", specialNeeds="Testdeaf"))

        db.session.add(AdoptorsPuppies(adoptor_id=1, puppy_id=1))
        db.session.commit()
def test_log_start_call(mockObj, app_with_envion_DB, test_shelters):
    '''Outgoing calls should make a log entry'''
    test_shelter = test_shelters[0]
    s = Shelter(**test_shelter)
    db.session.add(s)
    db.session.commit()

    client = app_with_envion_DB.test_client()
    client.get('/twilio/start_call/')
    logs = db.session.query(Log).one()

    assert logs.shelter_id == test_shelter['id']
    assert logs.action == "initialize call"
    assert logs.contact_type == "outgoing_call"
def test_validate_shelter_good(app_with_envion_DB, test_shelters):
    ''' Should return the shelter data given a known pip '''
    for s in test_shelters:
        db.session.add(Shelter(**s))

    db.session.commit()
    data = {'shelterID': test_shelters[0]['login_id']}
    client = app_with_envion_DB.test_client()

    rv = client.post('/twilio/validate_shelter/', data=data)
    res = json.loads(rv.data)
    assert res['success'] == True
    assert res['id'] == test_shelters[0]['id']
    assert res['name'] == test_shelters[0]['name']
def test_start_call_existing(pend_mock, urlopen_mock, app_with_envion_DB, test_shelters):
    '''It should only initiate calls when an existing count is not in the DB for a given date and shelter id'''
    for s in test_shelters:
        db.session.add(Shelter(**s))

    today = '2019-05-21T22:00:00'
    pend_mock.return_value = today
    count = Count(shelter_id=test_shelters[0]['id'], personcount=100, bedcount=5, day=today, time=today)
    db.session.add(count)
    db.session.commit()
    client = app_with_envion_DB.test_client()
    client.get('/twilio/start_call/')

    urlopen_mock.assert_called_once_with(twil_url, urllib.parse.urlencode(getDataRoute(test_shelters[1])).encode())
def test_start_call_return_true(pend_mock, urlopen_mock, app_with_envion_DB, test_shelters):
    '''It should return a true JSON result when there are no shelters to call'''
    test_shelter = test_shelters[0]
    s = Shelter(**test_shelter)
    db.session.add(s)

    today = '2019-05-21T22:00:00'
    pend_mock.return_value = today
    count = Count(shelter_id=test_shelter['id'], personcount=100, bedcount=5, day=today, time=today)
    db.session.add(count)
    db.session.commit()
    client = app_with_envion_DB.test_client()
    rv = client.get('/twilio/start_call/')
    data = json.loads(rv.data)
    assert data == {"success": True}
def test_validate_time_before_cutoff(pend_mock, app_with_envion_DB, test_shelters):
    ''' Should add a row to the count table with day value set today if before the cuttoff '''
    date = '2019-05-20'
    time = pendulum.parse(date + 'T' + Prefs['start_day'], tz=Prefs['timezone']).subtract(minutes=1)
    pend_mock.return_value = time
    test_shelter = test_shelters[0]
    s = Shelter(**test_shelter)
    db.session.add(s)
    db.session.commit()
    data = {'numberOfPeople': 90, 'shelterID': test_shelter['id']}
    client = app_with_envion_DB.test_client()

    rv = client.post('/twilio/save_count/', data=data)
    json.loads(rv.data)
    count = db.session.query(Count).one()
    assert count.shelter_id == test_shelter['id']
    assert str(count.day) == date
def test_validate_time_after_midnight(pend_mock, app_with_envion_DB, test_shelters):
    ''' Should set correct day (the current day) when call happens after midnight '''
    Prefs['start_day'] = "23:00"
    date = '2019-05-20'
    time = pendulum.parse(date + 'T' + '01:00', tz=Prefs['timezone'])
    pend_mock.return_value = time
    test_shelter = test_shelters[0]

    s = Shelter(**test_shelter)
    db.session.add(s)
    db.session.commit()
    data = {'numberOfPeople': 90, 'shelterID': test_shelter['id']}
    client = app_with_envion_DB.test_client()

    rv = client.post('/twilio/save_count/', data=data)
    json.loads(rv.data)
    count = db.session.query(Count).one()
    assert str(count.day) == date
def test_validate_time_log(pend_mock, app_with_envion_DB, test_shelters):
    ''' Should enter a row in the log when saving a count '''
    date = '2019-05-20'
    phone = '123-555-5555'
    time = pendulum.parse(date + 'T' + '01:00', tz=Prefs['timezone'])
    pend_mock.return_value = time
    test_shelter = test_shelters[0]

    s = Shelter(**test_shelter)
    db.session.add(s)
    db.session.commit()
    data = {'numberOfPeople': 80, 'shelterID': test_shelter['id'], 'phone': phone}
    client = app_with_envion_DB.test_client()

    rv = client.post('/twilio/save_count/', data=data)
    json.loads(rv.data)
    log = db.session.query(Log).one()
    assert log.shelter_id == test_shelter['id']
    assert log.from_number == phone
    assert log.parsed_text == str(data['numberOfPeople'])
def test_validate_time_before_midnight(pend_mock, app_with_envion_DB, test_shelters):
    ''' Before midnight it should add a row to the count table with day value set to tomorrow and correct counts'''
    date = '2019-05-20'
    tomorrow = '2019-05-21'
    time = pendulum.parse(date + 'T' + Prefs['start_day'], tz=Prefs['timezone']).add(minutes=1)
    pend_mock.return_value = time
    test_shelter = test_shelters[0]

    s = Shelter(**test_shelter)
    db.session.add(s)
    db.session.commit()
    data = {'numberOfPeople': 90, 'shelterID': test_shelter['id']}
    client = app_with_envion_DB.test_client()

    rv = client.post('/twilio/save_count/', data=data)
    res = json.loads(rv.data)
    assert res['success'] == True
    count = db.session.query(Count).one()
    assert count.shelter_id == test_shelter['id']
    assert str(count.day) == tomorrow
    assert count.personcount == data['numberOfPeople']
    assert count.bedcount == test_shelter['capacity'] - data['numberOfPeople']
Exemple #18
0
def new_shelter():
    SHELTERS = Shelter.query.all()
    error = None
    form = CreateShelter()
    if form.validate_on_submit():
        newshelter = Shelter(name=form.name.data,
                             address=form.address.data,
                             city=form.city.data,
                             state=form.state.data,
                             zipCode=form.zipCode.data,
                             website=form.website.data,
                             maximum_capacity=form.maximum_capacity.data,
                             current_capacity=form.current_capacity.data)
        db.session.add(newshelter)
        db.session.commit()
        flash(
            "<strong>Congrats</strong> You just created a new shelter named <u>%s</u>."
            % newshelter.name, 'warning')
        return redirect(url_for('index'))
    return render_template('create_shelter.html',
                           form=form,
                           error=error,
                           SHELTERS=SHELTERS)
Exemple #19
0
def counts(client, test_shelters):
    for s in test_shelters:
        db.session.add(Shelter(**s))
    for count in fake_counts:
        db.session.add(Count(**count))
    db.session.commit()
adoptor1 = Adoptors(name="Jeff", email='*****@*****.**')
db.session.add(adoptor1)
adopter2 = Adoptors(name="Mark", email='*****@*****.**')
db.session.add(adopter2)
adopter3 = Adoptors(name="Peter", email='*****@*****.**')
db.session.add(adopter3)
adopter4 = Adoptors(name="Tom", email='*****@*****.**')
db.session.add(adopter4)
db.session.commit()

# Add Shelters
shelter1 = Shelter(name="Oakland Animal Services",
                   address="1101 29th Ave",
                   city="Oakland",
                   state="California",
                   zipCode="94601",
                   website="oaklandanimalservices.org",
                   maximum_capacity=randint(25, 28),
                   current_capacity=count_pups())
db.session.add(shelter1)

shelter2 = Shelter(name="San Francisco SPCA Mission Adoption Center",
                   address="250 Florida St",
                   city="San Francisco",
                   state="California",
                   zipCode="94103",
                   website="sfspca.org",
                   maximum_capacity=randint(25, 28),
                   current_capacity=count_pups())
db.session.add(shelter2)