def test_connect_wrong_pw(self): def _login_cb(req): self.assertEqual(json.loads(req.body), { 'user': '******', 'password': '******' }) headers = { "Set-Cookie": "session_cookie=v_8yOI7FI-WKr-5QU6nxoJkVtAu5rKI-; Path=/api/;" } return (401, headers, json.dumps({ "success": False, "session_cookie": None, "error_message": "Invalid user/password combination.", "past_login_info": None })) responses.add_callback( responses.HEAD, "http://127.0.0.1:8080/api/v2/schema/controller/root/core/aaa/session/login", callback=lambda req: (401, {}, ""), content_type="application/json") responses.add_callback(responses.POST, "http://127.0.0.1:8080/api/v1/auth/login", callback=_login_cb, content_type="application/json") with self.assertRaises(requests.exceptions.HTTPError) as context: pybsn.connect("http://127.0.0.1:8080", "admin", "foo") self.assertEqual(context.exception.response.status_code, 401)
def test_connect_token(self): def _aaa_cb(req): self.assertEqual(req.headers["Cookie"], "session_cookie=some_token") return (200, {}, json.dumps([ { "auth-context-type" : "session-token", "user-info" : { "full-name" : "Default admin", "group" : [ "admin" ], "user-name" : "admin" } } ]) ) responses.add_callback(responses.GET, "http://127.0.0.1:8080/api/v1/data/controller/core/aaa/auth-context", callback=_aaa_cb, content_type="application/json") pybsn.connect("http://127.0.0.1:8080", token="some_token")
def test_connect_token_wrong(self): def _aaa_cb(req): self.assertEqual(req.headers["Cookie"], "session_cookie=wrong_token") return (401, {}, json.dumps({ "description":"Authorization failed: No session or token found for cookie", "error-code":401 })) responses.add_callback(responses.GET, "http://127.0.0.1:8080/api/v1/data/controller/core/aaa/auth-context", callback=_aaa_cb, content_type="application/json") with self.assertRaises(requests.exceptions.HTTPError) as context: pybsn.connect("http://127.0.0.1:8080", token="wrong_token") self.assertEqual(context.exception.response.status_code, 401)
def test_client_contextmanager(self): responses.add( responses.GET, "http://127.0.0.1:8080/api/v1/data/controller/core/healthy", status=200, json=[{ "status": "healthy" }]) with pybsn.connect("http://127.0.0.1:8080") as client: client.get("controller/core/healthy")
def test_client_contextmanager_with_session(self): responses.add(method=responses.DELETE, url=re.compile(r"http://127\.0\.0\.1:8080/api/v1/data/controller/core/aaa/session" r"(?:%5B|\[)auth-token='value'(?:%5D|\])"), status=204) responses.add(responses.GET, "http://127.0.0.1:8080/api/v1/data/controller/core/healthy", status=200, json= [ { "status" : "healthy"}] ) with pybsn.connect("http://127.0.0.1:8080") as client: client.session.cookies.set_cookie( requests.cookies.create_cookie(name="session_cookie", value="value")) client.get("controller/core/healthy")
def test_connect_legacy_login(self): responses.add_callback( responses.HEAD, "http://127.0.0.1:8080/api/v2/schema/controller/root/core/aaa/session/login", callback=lambda req: (401, {}, ""), content_type="application/json") responses.add_callback(responses.POST, "http://127.0.0.1:8080/api/v1/auth/login", callback=self._login_cb, content_type="application/json") client = pybsn.connect("http://127.0.0.1:8080", "admin", "somepassword")
def test_connect_login(self): def _login_cb(req): self.assertEqual(json.loads(req.body), {'user': '******', 'password': '******'}) headers = { "Set-Cookie": "session_cookie=v_8yOI7FI-WKr-5QU6nxoJkVtAu5rKI-; Path=/api/;" } return (200, headers, json.dumps( {"success": True, "session_cookie": "UPhNWlmDN0re8cg9xsqe9QT1QvQTznji", "error_message": "", "past_login_info": {"failed_login_count": 0, "last_success_login_info": {"host": "127.0.0.1", "timestamp": "2019-05-19T19:16:22.328Z"}}})) responses.add_callback(responses.POST, "http://127.0.0.1:8080/api/v1/auth/login", callback=_login_cb, content_type="application/json") client = pybsn.connect("http://127.0.0.1:8080", "admin", "somepassword")
import pybsn import argparse import time import npyscreen from collections import namedtuple parser = argparse.ArgumentParser(description='View debug counters for all switches') parser.add_argument('--host', '-H', type=str, default="127.0.0.1", help="Controller IP/Hostname to connect to") parser.add_argument('--user', '-u', type=str, default="admin", help="Username") parser.add_argument('--password', '-p', type=str, default="adminadmin", help="Password") parser.add_argument('filter', nargs='*', help="Limit counters to those containing this string") args = parser.parse_args() bcf = pybsn.connect(args.host, args.user, args.password) class DebugCounter(namedtuple("DebugCounter", ["name", "description", "value", "initial_value"])): pass class DebugCounterSource(object): def __init__(self, bcf): self.bcf = bcf self.initial_values = {} def get_debug_counters(self): switch_counters = self.bcf.root.applications.bcf.info.statistic.switch_counter() for switch_counter in switch_counters: for counter in switch_counter['counter']: name = switch_counter['switch-name'] + ':' + counter['name']
def setUp(self): self.client = pybsn.connect("http://127.0.0.1:8080")