Path: blob/master/web-gui/tests/unit/test_session_api.py
1292 views
import pytest1from hashlib import md52from datetime import datetime3from random import getrandbits4from buildyourownbotnet import c25from buildyourownbotnet.core.dao import session_dao6from buildyourownbotnet.server import SessionThread7from ..conftest import app_client, new_user, login, cleanup8910def test_api_session_new(app_client, new_user):11"""12Given a user,13when a POST request is sent to /api/session/new endpoint with valid session parameters,14check that the session metadata is correctly stored in the database and the metadata is returned as JSON.15"""16uid = md5(bytes(getrandbits(10))).hexdigest()17session_dict = {18"id": 1,19"online": True,20"public_ip": '1.2.3.4',21"local_ip": '192.1.1.168',22"mac_address": '00:0A:95:9D:68:16',23"username": 'test_user',24"administrator": True,25"platform": 'linux2',26"device": 'test_device',27"architecture": 'x32',28"latitude": 0.00,29"longitude": 0.00,30"owner": new_user.username31}32res = app_client.post('/api/session/new', json=session_dict)33assert res.status_code == 2003435session_metadata = res.json36assert isinstance(session_metadata, dict)37for key, val in session_dict.items():38assert session_metadata.get(key) == val39cleanup()4041def test_api_session_remove(app_client, new_user, new_session):42"""43Given a user and a session,44when a POST request is sent to /api/session/remove with a valid session UID,45check the session metadata is correctly removed from the database.46"""47login(app_client, new_user.username, 'test_password')4849# create dummy session50dummy_session = SessionThread(id=1, c2=c2, connection=None)51dummy_session.info = dict(new_session.serialize())52c2.sessions[new_user.username] = {new_session.uid: dummy_session}5354# save session uid because new_session will be deleted55uid = new_session.uid5657res = app_client.post('/api/session/remove',58data={'session_uid': uid},59follow_redirects=True,60headers = {"Content-Type":"application/x-www-form-urlencoded"}61)62assert res.status_code == 20063assert session_dao.get_session(uid) is None64assert uid not in c2.sessions[new_user.username]6566def test_api_session_remove_invalid(app_client, new_user, new_session):67"""68Given a user and a session,69when a POST request is sent to /api/session/remove with invalid/missing session UID,70check the session metadata is correctly removed from the database.71"""72login(app_client, new_user.username, 'test_password')7374# invalid uid75res = app_client.post('/api/session/remove',76data={'session_uid': '123'},77follow_redirects=True,78headers = {"Content-Type":"application/x-www-form-urlencoded"}79)80assert res.status_code == 2008182def test_api_session_remove_unauthenticated(app_client, new_user, new_session):83"""84Given an unauthenticated user and a session,85when a POST request is sent to /api/session/remove,86check that a HTTP 403 forbidden status is returned and the session is not removed.87"""88res = app_client.post('/api/session/remove',89data={'session_uid': new_session.uid},90follow_redirects=True,91headers = {"Content-Type":"application/x-www-form-urlencoded"}92)93assert res.status_code == 40394assert session_dao.get_session(new_session.uid) is not None959697def test_api_session_poll(app_client, new_user, new_session):98"""99Given an authenticated user with at least 1 session,100when a GET request is sent to /api/session/poll,101check that any new sessions' metadata is returned in JSON format,102and that the sessions are marked as no longer being new in the database.103"""104login(app_client, new_user.username, 'test_password')105106# check valid response107res = app_client.get("/api/session/poll")108assert res.status_code == 200109110# check correct data type returned with correct number of new sessions111sessions_list = res.json112assert isinstance(sessions_list, list)113assert len(sessions_list) == 1114115# check session metadata is accurate116session_metadata = sessions_list[0]117for key, val in session_metadata.items():118assert session_metadata.get(key) == val119120# check subsequent polls don't return the same old session121res = app_client.get("/api/session/poll")122assert res.status_code == 200123124# check correct data type returned with correct number of new sessions125sessions_list = res.json126assert isinstance(sessions_list, list)127assert len(sessions_list) == 0128129