def test_usermanagement_login_unsuccsessful_invalid_credentials(): response = app.test_client().post('/usermanagement/login/', json={ "username": "******", "password": "******" }) assert response.status_code == 401 and json.loads(response.get_data(as_text=True)) == {'msg': 'Authorization error. Credentials do not match!'}
def test_usermanagement_login_unsuccsessful_user_doesnt_exist(): response = app.test_client().post('/usermanagement/login/', json={ "username": "******", "password": "******" }) assert response.status_code == 401 and json.loads(response.get_data(as_text=True)) == {'msg': 'User with that username does not exist.'}
def test_usermanagement_login_succsessful(): response = app.test_client().post('/usermanagement/login/', json={ "username": "******", "password": "******" }) assert response.status_code == 200
def test_usermanagement_adminlogin_unsuccsessful_user_not_admin(): response = app.test_client().post('/usermanagement/adminlogin/', json={ "username": "******", "password": "******" }) assert response.status_code == 401 and json.loads(response.get_data(as_text=True)) == {'msg': 'Authorization error. User is not administrator!'}
def test_method_not_allowed_users2flowers_with_id(): response = app.test_client().post('/users2flowers/666', json={ "date_of_inception": "2019-09-22", "email": True, "flower_id": "20" }) assert response.status_code == 405
def test_usermanagement_register_unsuccsessful(): response = app.test_client().post('/usermanagement/register/', json={ "username": "******", "password": "******", "email": "*****@*****.**" }) assert response.status_code == 403
def test_delete_flower_by_id_not_exist_in_db(): response = app.test_client().delete('/flowers/6969420666') data = json.loads(response.get_data(as_text=True)) assert data == { "msg": "Flower with flower_id 6969420666 does not exist in DB." } and response.status_code == 200
def test_usermanagement_login_redirect_succsessful(): response = app.test_client().post('/api/usermanagement/login/', headers=jwt_header_auth, json={ "username": "******", "password": "******" }) assert response.status_code == 307
def test_method_not_allowed(): response = app.test_client().put('/flowers/', json={ "description": "description2", "name_lat": "name_lat2", "name_ser": "name_ser2", "watering_period": "5" }) assert response.status_code == 405
def test_post_users2flowers_unsuccsessful(): response = app.test_client().post('/users2flowers/', json={ "date_of_inception": "2019-09-22", "email": True, "flower_id": "20" }) data = json.loads(response.get_data(as_text=True)) assert data == {'msg': 'Data is not valid.'} and response.status_code == 403
def test_method_not_allowed(): response = app.test_client().put('/users/', json={ "first_name": "TestPostUser", "last_name": "TestPostUser", "username": "******", "email": "*****@*****.**", "admin": "True" }) assert response.status_code == 405
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() #使用db.create_all()方法创建数据库和表。 self.client = app.test_client() #创建测试客户端对象, 将其保存为类属性self.client, self.runner = app.test_cli_runner( ) #Flask提供了app.test_cli_runner()方法用于在测试中调用命令函数、捕捉输出 user = User(username='******', email='hzj.com') user.set_password('123') db.session.add(user) db.session.commit()
def test_post_flower_unsuccsessful(): response = app.test_client().post('/flowers/', json={ "description": "description2", "name_lat": "name_lat2", "name_ser": "name_ser2" }) data = json.loads(response.get_data(as_text=True)) assert data == { 'msg': 'Data is not valid.' } and response.status_code == 403
def test_post_user_unsuccsessful(): response = app.test_client().post('/users/', json={ "first_name": "TestPostUser", "last_name": "TestPostUser", "username": "******", "email": "*****@*****.**", "admin": "True" }) data = json.loads(response.get_data(as_text=True)) assert data == { 'msg': 'Data is not valid.' } and response.status_code == 403
def test_get_all_flowers_succsessful(): response = app.test_client().get('/flowers/') data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 or response.status_code == 204
def test_method_not_allowed_by_using_username(): response = app.test_client().delete('/users/foobar') assert response.status_code == 405
def test_jwt_token_not_present(): response = app.test_client().get('/api/users/') assert response.status_code == 403
def test_get_user_by_username_succsessful(): response = app.test_client().get('/users/mcrnic') assert response.status_code == 200
def test_route_to_one_user2flower_redirect_succsessful(): response = app.test_client().get('/api/users2flowers/14', headers=jwt_header_auth) assert response.status_code == 307
def test_path_not_found(): response = app.test_client().get('/api/nonexistingroute/', headers=jwt_header_auth) data = json.loads(response.get_data(as_text=True)) assert response.status_code == 404 and data == {'msg': 'Path not found'}
def test_jwt_token_expired(): response = app.test_client().get('/api/users/', headers=jwt_header_auth_expired) data = json.loads(response.get_data(as_text=True)) assert response.status_code == 401 and data == {'msg': 'JWT has expired.'}
def test_jwt_token_invalid(): response = app.test_client().get('/api/users/', headers=jwt_header_auth_invalid) assert response.status_code == 401
def test_get_flower_by_id_succsessful(): response = app.test_client().get('/flowers/20') data = json.loads(response.get_data(as_text=True)) assert response.status_code == 200 or data == "No data to return."
def test_route_to_all_flowers_redirect_succsessful(): response = app.test_client().get('/api/flowers/', headers=jwt_header_auth) assert response.status_code == 307
def test_get_user_by_username_not_existing(): response = app.test_client().get('/users/foobar') assert response.status_code == 204