def test_next(client_fixture: fixture) -> None: username = "******" password = "" page = client_fixture.post( "/login?next=https://nothing?asdf=asdf", data={ "user": username, "password": password }, ) assert page.status_code == 400 # check a valid next page = client_fixture.post("/login?next=https://localhost/", data={ "user": username, "password": password }) assert page.status_code == 302 # check an invalid next page = client_fixture.post("/login?next=https://google.com/", data={ "user": username, "password": password }) assert page.status_code == 400
def test_for_wrong_func_api(TASK_LINE: pytest.fixture, API_test: pytest.fixture) -> None: wrong_name = 'blablabla' response = API_test.post('/'+wrong_name, json={}) answer = json.loads(response.data) assert len(tasks.TASK_LINE) == 6 assert answer == {"msg": "blablabla not registered in TASK_LINE", "status": "KeyError"}
def login(client_fixture: fixture, username: str, password: str) -> Response: # 302 becuase tests autologin. assert client_fixture.get("/login").status_code == 302 return client_fixture.post( url_for("auth_bp.login"), data=dict(user=username, password=password), follow_redirects=True, )
def test_run_api(t: Case, TASK_LINE: pytest.fixture, API_test: pytest.fixture) -> None: """ Testing of function from cli_test.py ['charcount', 'manyarg', 'mult', 'multiprint'] by API POST http-request """ params_copy = copy.deepcopy(t.params) response = API_test.post('/'+t.name, json=t.params) answer = json.loads(response.data) assert len(tasks.TASK_LINE) == 6 assert t.params == params_copy, "You shouldn't change inputs" assert answer == t.result
def test_edit_project(client_fixture: fixture) -> None: mimetype = "application/x-www-form-urlencoded" headers = {"Content-Type": mimetype, "Accept": mimetype} p_id, t_id = create_demo_task() # enable task to test reschedule Task.query.filter_by(id=t_id).update({"enabled": 1}) db.session.commit() date_now = datetime.now() data = { "project_name": "test cron project", "project_desc": "my cron project description", "project_cron": "1", "project_cron_year": "1", "project_cron_mnth": "1", "project_cron_week": "1", "project_cron_day": "1", "project_cron_wday": "1", "project_cron_hour": "1", "project_cron_min": "1", "project_cron_sec": "1", "project_cron_sdate": datetime.strftime(date_now, "%Y-%m-%d %H:%M"), "project_cron_edate": datetime.strftime(date_now, "%Y-%m-%d %H:%M"), "project_globalParams": "@this-is-cool", } page = client_fixture.post( url_for("project_bp.edit_project", project_id=p_id), data=data, follow_redirects=True, headers=headers, ) # check project project = Project.query.get(p_id) assert project.name == data["project_name"] assert project.description == data["project_desc"] assert project.cron == int(data["project_cron"]) # verify intv and oneoff are disabled. assert project.intv == 0 assert project.ooff == 0 assert request.path == url_for("project_bp.one_project", project_id=p_id) # check that task was rescheduled assert (TaskLog.query.filter_by(task_id=t_id, error=1, status_id=7).filter( TaskLog.message.like( "%Failed to schedule task%")) # type: ignore[union-attr] .first() is not None)
def test_edit_task(client_fixture: fixture) -> None: # get project id _, t_id = create_demo_task() mimetype = "application/x-www-form-urlencoded" headers = {"Content-Type": mimetype, "Accept": mimetype} data = { "name": "test task", } page = client_fixture.post( url_for("task_edit_bp.task_edit_post", task_id=t_id), data=data, follow_redirects=True, headers=headers, ) # get id t_id = int(request.path.split("/")[-1]) # check redirect assert request.path == url_for("task_bp.one_task", task_id=t_id) # try an enabled task data = {"name": "test task", "task-ooff": "1"} page = client_fixture.post( url_for("task_edit_bp.task_edit_post", task_id=t_id), data=data, follow_redirects=True, headers=headers, ) # get id t_id = int(request.path.split("/")[-1]) # check redirect assert request.path == url_for("task_bp.one_task", task_id=t_id)
def login_client(app: fixture, client: fixture, insert_test_user: fixture): """test login client""" with app.app_context(): data = { "email": TEST_EMAIL, "password": TEST_PASSWORD, } res = client.post(LOGIN_URL, data=data) access_token = json.loads(res.get_data()).get("data", {}).get("token") assert len(access_token) > 0 headers = {"Authorization": "Bearer " + access_token} client.token = access_token client.headers = headers return client
def test_create_cron_project(client_fixture: fixture) -> None: mimetype = "application/x-www-form-urlencoded" headers = {"Content-Type": mimetype, "Accept": mimetype} date_now = datetime.now() data = { "project_name": "test cron project", "project_desc": "my cron project description", "project_cron": "1", "project_cron_year": "1", "project_cron_mnth": "1", "project_cron_week": "1", "project_cron_day": "1", "project_cron_wday": "1", "project_cron_hour": "1", "project_cron_min": "1", "project_cron_sec": "1", "project_cron_sdate": datetime.strftime(date_now, "%Y-%m-%d %H:%M"), "project_cron_edate": datetime.strftime(date_now, "%Y-%m-%d %H:%M"), "project_globalParams": "@this-is-cool", } page = client_fixture.post( url_for("project_bp.new_project"), data=data, follow_redirects=True, headers=headers, ) # get id p_id = int(request.path.split("/")[-1]) # check redirect assert request.path == url_for("project_bp.one_project", project_id=p_id) # check project project = Project.query.get(p_id) assert project.name == data["project_name"] assert project.description == data["project_desc"] assert project.cron == int(data["project_cron"]) # verify intv and oneoff are disabled. assert project.intv == 0 assert project.ooff == 0
def test_register( client: fixture, mock_verification_code_email: fixture, clear_db: fixture, init_db: fixture, ) -> None: # 获取验证码 r = client.get(SEND_VERIFICATION_CODE_URL, query_string={"email": TEST_EMAIL}) assert r.status_code == 200 code = redis_get_str(TEST_EMAIL) # 尝试注册 post_data = { "verification_code": code, "email": TEST_EMAIL, "password": TEST_PASSWORD, "username": TEST_USERNAME, } r = client.post(REGISTER_URL, data=post_data) assert r.status_code == 200 response_data = json.loads(r.get_data()) assert len(response_data.get("data", {}).get("token")) > 0
def test_new_database(client_fixture: fixture) -> None: # add a connection mimetype = "application/x-www-form-urlencoded" headers = {"Content-Type": mimetype, "Accept": mimetype} data = { "name": "Test Connection", "description": "description", "address": "outer space", "contact": "joe", "email": "[email protected]", "phone": "411", } response = client_fixture.post( "/connection/new", data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["description"] in response.get_data(as_text=True) assert data["address"] in response.get_data(as_text=True) assert data["contact"] in response.get_data(as_text=True) assert data["email"] in response.get_data(as_text=True) assert data["phone"] in response.get_data(as_text=True) # new editor soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "New Database"})["href"] response = client_fixture.get( url, follow_redirects=True, ) soup = BeautifulSoup(response.data, features="lxml") url = soup.find("form", attrs={"method": "post"})["action"] data = { "name": "Test Database", "database_type": "1", "connection_string": "outer space", } response = client_fixture.post( url, data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["connection_string"] in response.get_data(as_text=True) # edit soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "Edit Database Connection"})["href"] response = client_fixture.get( url, follow_redirects=True, ) data = { "name": "Test Database edited", "database_type": "2", "connection_string": "outer space edited", } response = client_fixture.post( url, data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["connection_string"] in response.get_data(as_text=True) # delete soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "Delete Database Connection"})["href"] response = client_fixture.get( url, follow_redirects=True, )
def test_new_smb(client_fixture: fixture) -> None: # add a connection mimetype = "application/x-www-form-urlencoded" headers = {"Content-Type": mimetype, "Accept": mimetype} data = { "name": "Test Connection", "description": "description", "address": "outer space", "contact": "joe", "email": "[email protected]", "phone": "411", } response = client_fixture.post( "/connection/new", data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["description"] in response.get_data(as_text=True) assert data["address"] in response.get_data(as_text=True) assert data["contact"] in response.get_data(as_text=True) assert data["email"] in response.get_data(as_text=True) assert data["phone"] in response.get_data(as_text=True) # new editor soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "New SMB"})["href"] response = client_fixture.get( url, follow_redirects=True, ) soup = BeautifulSoup(response.data, features="lxml") url = soup.find("form", attrs={"method": "post"})["action"] data = { "name": "Test SMB", "server_name": "smbserver", "server_ip": "1.2.3.4", "share_name": "myshare", "path": "nowhere/around/here", "username": "******", "password": "******", } response = client_fixture.post( url, data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["server_name"] in response.get_data(as_text=True) assert data["server_ip"] in response.get_data(as_text=True) assert data["share_name"] in response.get_data(as_text=True) assert data["path"] in response.get_data(as_text=True) assert data["username"] in response.get_data(as_text=True) assert data["password"] in response.get_data(as_text=True) # edit soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "Edit SMB Connection"})["href"] response = client_fixture.get( url, follow_redirects=True, ) data = { "name": "Test SMB edited", "server_name": "smbserver edited", "server_ip": "1.2.3.5", "share_name": "myshareedited", "path": "nowhere/around/here/edited", "username": "******", "password": "******", } response = client_fixture.post( url, data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["server_name"] in response.get_data(as_text=True) assert data["server_ip"] in response.get_data(as_text=True) assert data["share_name"] in response.get_data(as_text=True) assert data["path"] in response.get_data(as_text=True) assert data["username"] in response.get_data(as_text=True) assert data["password"] in response.get_data(as_text=True) # delete soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "Delete SMB Connection"})["href"] response = client_fixture.get( url, follow_redirects=True, )
def test_new_connection(client_fixture: fixture) -> None: response = client_fixture.get("/connection/new") assert response.status_code == 200 mimetype = "application/x-www-form-urlencoded" headers = {"Content-Type": mimetype, "Accept": mimetype} data = { "name": "Test Connection", "description": "description", "address": "outer space", "contact": "joe", "email": "[email protected]", "phone": "411", } response = client_fixture.post( "/connection/new", data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["description"] in response.get_data(as_text=True) assert data["address"] in response.get_data(as_text=True) assert data["contact"] in response.get_data(as_text=True) assert data["email"] in response.get_data(as_text=True) assert data["phone"] in response.get_data(as_text=True) # edit soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "Edit Connection"})["href"] response = client_fixture.get( url, follow_redirects=True, ) data = { "name": "Test Connection edited", "description": "description edited", "address": "outer space edited", "contact": "joe edited", "email": "[email protected] edited", "phone": "411 edited", } response = client_fixture.post( url, data=data, headers=headers, follow_redirects=True, ) assert data["name"] in response.get_data(as_text=True) assert data["description"] in response.get_data(as_text=True) assert data["address"] in response.get_data(as_text=True) assert data["contact"] in response.get_data(as_text=True) assert data["email"] in response.get_data(as_text=True) assert data["phone"] in response.get_data(as_text=True) # delete soup = BeautifulSoup(response.data, features="lxml") url = soup.find("a", attrs={"title": "Delete Connection"})["href"] response = client_fixture.get( url, follow_redirects=True, ) assert b"Connection deleted." in response.data