def test_checker(client, def_status): """use `/checker` endpoint with winner user target_url: /checker/{classroom_id}/{secret_id} """ classroom_id = 1 index = 1 target_user = test_user staff = checker secret_id = target_user['secret_id'] with client.application.app_context(): lottery_id = Lottery.query.filter_by(classroom_id=classroom_id, index=index).first().id user = User.query.filter_by(secret_id=secret_id).first() application = Application(user_id=user.id, lottery_id=lottery_id, status=def_status) db.session.add(application) db.session.commit() with mock.patch('api.routes.api.get_prev_time_index', return_value=index): resp = as_user_get(client, staff['secret_id'], staff['g-recaptcha-response'], f'/checker/{classroom_id}/{secret_id}') assert resp.status_code == 200 assert resp.get_json()['status'] == def_status
def test_get_allapplications_admin(client): """test 403 is returned from the API to admin target_url: /applications """ lottery_id = 1 make_application(client, admin['secret_id'], lottery_id) resp = as_user_get(client, admin['secret_id'], admin['g-recaptcha-response'], '/applications') assert resp.status_code == 403
def test_get_specific_application_admin(client): """test 403 is returned from the API to admin target_url: /applications/<id> """ lottery_id = 1 application_id = make_application(client, admin['secret_id'], lottery_id) resp = as_user_get(client, admin['secret_id'], admin['g-recaptcha-response'], f'/applications/{application_id}') assert resp.status_code == 403
def test_checker_invalid_user(client): """attempt to use `/checker` endpoint with wrong user """ classroom_id = 1 index = 1 staff = checker secret_id = "NOTEXIST_SECRET_KEY" with mock.patch('api.routes.api.get_prev_time_index', return_value=index): resp = as_user_get(client, staff['secret_id'], staff['g-recaptcha-response'], f'/checker/{classroom_id}/{secret_id}') assert resp.status_code == 404 assert resp.get_json()['message'] == 'No such user found'
def test_get_allapplications(client): """test proper infomation is returned from the API to a normal user target_url: /applications """ lottery_id = 1 make_application(client, test_user['secret_id'], lottery_id) resp = as_user_get(client, test_user['secret_id'], test_user['g-recaptcha-response'], '/applications') with client.application.app_context(): db_status = Application.query.all() application_list = applications_schema.dump(db_status)[0] assert resp.get_json() == application_list
def test_checker_no_application(client): """attempt to use `/checker` endpoint without application for that target_url: /checker/{classroom_id}/{secret_id} """ classroom_id = 1 index = 1 target_user = test_user secret_id = target_user['secret_id'] staff = checker with mock.patch('api.routes.api.get_prev_time_index', return_value=index): resp = as_user_get(client, staff['secret_id'], staff['g-recaptcha-response'], f'/checker/{classroom_id}/{secret_id}') assert resp.status_code == 404 assert resp.get_json()['message'] == 'No application found'
def test_get_specific_application_invaild_id(client): """test proper errpr is returned from the API target_url: /applications/<id> """ lottery_id = 1 application_id = make_application(client, test_user['secret_id'], lottery_id) # application id to test idx = application_id + 1 resp = as_user_get(client, test_user['secret_id'], test_user['g-recaptcha-response'], f'/applications/{idx}') assert resp.status_code == 404 assert 'Not found' in resp.get_json()['message']
def test_get_specific_application_normal(client): """test proper infromation is returned from the API to a normal user target_url: /applications/<id> """ lottery_id = 1 application_id = make_application(client, test_user['secret_id'], lottery_id) resp = as_user_get(client, test_user['secret_id'], test_user['g-recaptcha-response'], f'/applications/{application_id}') with client.application.app_context(): db_status = Application.query.filter_by(id=application_id).first() application = application_schema.dump(db_status)[0] assert resp.get_json() == application
def test_status(client): """test it return a vaild response 1. test: response contains 'id' 2. test: response matches the data in DB auth: required target_url: /status """ user = test_user resp = as_user_get(client, user['secret_id'], user['g-recaptcha-response'], '/status') assert 'id' in resp.get_json() with client.application.app_context(): db_status = User.query.filter_by(secret_id=user['secret_id']).first() assert resp.get_json() == user_schema.dump(db_status)[0]
def test_get_allapplications(client): """test proper infomation is returned from the API to a normal user target_url: /applications """ with client.application.app_context(): lottery = Lottery.query.get(1) user = User.query.filter_by(secret_id=test_user['secret_id']).one() apps = [user2application(user, lottery) for _ in range(2)] apps[1].created_on += datetime.timedelta(days=-1) add_db(apps) correct_resp = applications_schema.dump([apps[0]])[0] resp = as_user_get(client, test_user['secret_id'], test_user['g-recaptcha-response'], '/applications') assert resp.get_json() == correct_resp