コード例 #1
0
def test_add_incomplete_dog(temp_app, temp_db):
    """Tests to see if dog with no 'name' fails."""
    res = temp_app.post('/api/dogs',
                        data=json.dumps(leelou_incomplete),
                        content_type='application/json')
    assert res.status_code == 400, 'The response should have a status code of 400 -- BAD REQUEST.'
    assert isinstance(json.loads(res.data),
                      dict), 'The data should be a json dict.'
コード例 #2
0
def test_add_dupe_dog(temp_app, temp_db):
    """Tests to see if duplicate dog entry fails."""
    res = temp_app.post('/api/dogs',
                        data=json.dumps({'name': 'normie'}),
                        content_type='application/json')
    assert res.status_code == 400, 'The response should have a status code of 400 -- BAD REQUEST.'
    assert isinstance(json.loads(res.data),
                      dict), 'The data should be a json dict.'
コード例 #3
0
def test_save_new_project_fail(temp_app, temp_db):
    '''Tests various failure cases when trying to save a new project'''

    # Tests trying to save a project with a duplicate name under the same user
    login_res = login_hello(temp_app)
    auth_token = login_res['accessToken']

    data = generate_temp_project()
    data['name'] = 'New Project 1'

    res = post_save(data, auth_token, temp_app)
    res_data = json.loads(res.data)
    assert res.status_code == 400
    assert res_data['error'] == 'A project with that name already exists'

    # Tests trying to save project with no auth header
    data = generate_temp_project()
    res = temp_app.post('/save',
                        data=json.dumps(data),
                        content_type='application/json')
    res_data = json.loads(res.data)
    assert res.status_code == 401
    assert res_data['error'] == 'No authentication provided'

    # Tests trying to save project with no auth token
    res = post_save(data, '', temp_app)
    res_data = json.loads(res.data)
    assert res.status_code == 401
    assert res_data['error'] == 'No authentication provided'

    # Tests trying to save project with an expired token
    token = generate_expired_token('access',
                                   temp_app.application.config['SECRET_KEY'])
    res = post_save(data, token, temp_app)
    res_data = json.loads(res.data)
    assert res.status_code == 401
    assert res_data['error'] == 'Invalid token'

    # Tests trying to save project with a token signed with the wrong key
    token = generate_invalid_token('access')
    res = post_save(data, token, temp_app)
    res_data = json.loads(res.data)
    assert res.status_code == 401
    assert res_data['error'] == 'Invalid token'

    # Tests trying to use a refresh token to access projects
    token = encode_auth_token('refresh', 1, datetime.timedelta(days=3),
                              temp_app.application.config['SECRET_KEY'])
    res = post_save(data, token.decode(), temp_app)
    res_data = json.loads(res.data)
    assert res.status_code == 401
    assert res_data['error'] == 'Invalid token type'
コード例 #4
0
def test_add_dog(temp_app, temp_db):
    """Tests to make sure adding a new dog works."""
    res = temp_app.post('/api/dogs',
                        data=json.dumps(leelou),
                        content_type='application/json')
    res_data = json.loads(res.data)
    assert res.status_code == 201, 'The response should have a status code of 201 - CREATED.'
    assert isinstance(res_data, dict), 'The data should be a json dict.'
    assert '_id' not in res_data, 'The response shouldn not have an _id key.'

    dogs = temp_db
    dog_in_db = dogs.find_one({'_id': 'leelou'})
    assert dog_in_db is not None
    assert dog_in_db['weight'] == 45
コード例 #5
0
def test_add_unformatted_dog(temp_app, temp_db):
    """Tests to make sure an unformatted dog gets properly formatted."""
    res = temp_app.post('/api/dogs',
                        data=json.dumps(leelou_unformatted),
                        content_type='application/json')
    res_data = json.loads(res.data)
    assert res.status_code == 201, 'The response should have a status code of 201 - CREATED.'
    assert isinstance(res_data, dict), 'The data should be a json dict.'
    assert '_id' not in res_data, 'The response shouldn not have an _id key.'

    dogs = temp_db
    dog_in_db = dogs.find_one({'_id': 'leelou'})
    assert dog_in_db is not None
    assert dog_in_db['weight'] == 45
    assert all (key in res_data for key in ('friends', 'owners', 'colors')), \
        'The response should have friends, owners, and colors keys.'
    assert isinstance(dog_in_db['friends'], list)
    assert isinstance(dog_in_db['owners'], list)
    assert isinstance(dog_in_db['colors'], list)
    assert isinstance(dog_in_db['breed'], list)