示例#1
0
def test_filter_servers_good():
    servers_stub = get_servers_stub()

    # test default features argument is None
    try:
        actual_servers = features.filter_servers(servers_stub)
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113", "us2853"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test default features argument is empyt list
    try:
        actual_servers = features.filter_servers(servers_stub)
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113", "us2853"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test multiple features are good
    try:
        actual_servers = features.filter_servers(
            servers_stub, ["openvpn_udp", "openvpn_tcp"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113", "us2853"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False
示例#2
0
def test_to_string():
    # test with server stub
    servers_ = get_servers_stub()
    expected_string = get_stub("servers_stub_to_string.txt").rstrip()

    actual_string = servers.to_string(servers_)
    assert actual_string == expected_string
示例#3
0
def test_load_filter_when_load_is_valid_should_not_throw_error():
    servers_ = get_servers_stub()

    try:
        actual_servers = LoadFilter(servers_).apply(0)
        assert isinstance(actual_servers, list)
    except LoadError:
        assert False

    try:
        actual_servers = LoadFilter(servers_).apply(1)
        assert isinstance(actual_servers, list)
    except LoadError:
        assert False

    try:
        actual_servers = LoadFilter(servers_).apply(50)
        assert isinstance(actual_servers, list)
    except LoadError:
        assert False

    try:
        actual_servers = LoadFilter(servers_).apply(99)
        assert isinstance(actual_servers, list)
    except LoadError:
        assert False

    try:
        actual_servers = LoadFilter(servers_).apply(100)
        assert isinstance(actual_servers, list)
    except LoadError:
        assert False
示例#4
0
def test_filter_servers_when_match_is_invalid():
    servers_ = get_servers_stub()

    try:
        filter_servers(servers_, 10, "wrong")
        assert False
    except ValueError as error:
        assert str(error) == 'Match must be one of "exact","max" or "min".'
示例#5
0
def test_load_filter_when_load_is_not_an_integer():
    servers_ = get_servers_stub()

    try:
        LoadFilter(servers_).apply("1001")
        assert False
    except LoadError as error:
        assert str(error) == "Wrong type \"<class 'str'>\": 1001"
示例#6
0
def test_load_filter_when_load_is_under_minimum():
    servers_ = get_servers_stub()

    try:
        LoadFilter(servers_).apply(-1)
        assert False
    except LoadError as error:
        assert str(error) == "Load must be >= 0 and <= 100."
示例#7
0
def test_filter_servers_when_match_is_default():
    servers_ = get_servers_stub()

    actual_servers = filter_servers(servers_, 10)
    domains = list()
    domains.append("nl80")
    expected_servers = get_expected_servers_by_domain(domains)

    assert actual_servers == expected_servers
示例#8
0
def test_max_load_filter_when_load_is_valid():
    servers_ = get_servers_stub()

    actual_servers = MaxLoadFilter(servers_).apply(10)
    domains = list()
    domains.append("nl80")
    expected_servers = get_expected_servers_by_domain(domains)

    assert actual_servers == expected_servers
示例#9
0
def test_filter_servers_when_match_is_exact():
    servers_ = get_servers_stub()

    actual_servers = filter_servers(servers_, 20, "exact")
    domains = list()
    domains.append("de112")
    domains.append("de113")
    expected_servers = get_expected_servers_by_domain(domains)

    assert actual_servers == expected_servers
示例#10
0
def test_load_filter_when_load_is_valid():
    servers_ = get_servers_stub()

    domains = list()
    domains.append("de112")
    domains.append("de113")
    expected_servers = get_expected_servers_by_domain(domains)
    actual_servers = LoadFilter(servers_).apply(20)

    assert actual_servers == expected_servers
示例#11
0
def test_get_servers(requests_mock):
    servers_ = get_servers_stub()
    url_ = "https://api.nordvpn.com/server"
    expected_useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) \
Gecko/20100101 Firefox/60.0"
    header = {"User-Agent": expected_useragent}

    requests_mock.get(url_, headers=header, json=servers_)

    actual_servers = servers.get_servers()
    actual_useragent = requests_mock.last_request.headers["User-Agent"]
    actual_url = requests_mock.last_request.url
    assert actual_servers == servers_
    assert actual_url == url_
    assert actual_useragent == expected_useragent
示例#12
0
def test_min_load_filter_when_load_is_valid():
    servers_ = get_servers_stub()

    actual_servers = MinLoadFilter(servers_).apply(20)
    domains = list()
    domains.append("de111")
    domains.append("de112")
    domains.append("de113")
    domains.append("nl81")
    domains.append("nl82")
    domains.append("us-ca5")
    domains.append("us2853")
    expected_servers = get_expected_servers_by_domain(domains)

    assert actual_servers == expected_servers
示例#13
0
def test_filter_servers_when_match_is_min():
    servers_ = get_servers_stub()

    actual_servers = filter_servers(servers_, 20, "min")
    domains = list()
    domains.append("de111")
    domains.append("de112")
    domains.append("de113")
    domains.append("nl81")
    domains.append("nl82")
    domains.append("us-ca5")
    domains.append("us2853")
    expected_servers = get_expected_servers_by_domain(domains)

    assert actual_servers == expected_servers
示例#14
0
def test_filter_servers_with_every_type():
    servers_stub = get_servers_stub()

    # test with double
    try:
        actual_servers = categories.filter_servers(servers_stub, ["double"])
        expected_servers = get_expected_servers_by_domain(["us-ca5"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False

    # test with dedicated
    try:
        actual_servers = categories.filter_servers(servers_stub, ["dedicated"])
        expected_servers = get_expected_servers_by_domain(["us2853"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False

    # test with standard
    try:
        actual_servers = categories.filter_servers(servers_stub, ["standard"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False

    # test with p2p
    try:
        actual_servers = categories.filter_servers(servers_stub, ["p2p"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False

    # test with obfuscated
    try:
        actual_servers = categories.filter_servers(servers_stub,
                                                   ["obfuscated"])
        expected_servers = get_expected_servers_by_domain(
            ["nl80", "nl81", "nl82"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False
示例#15
0
def test_filter_servers_good():
    servers_stub = get_servers_stub()
    # test default categories with no argument
    try:
        actual_servers = categories.filter_servers(servers_stub)
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False

    # test default type argument with empty list
    try:
        actual_servers = categories.filter_servers(servers_stub, list())
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False

    # test multiple categories
    try:
        actual_servers = categories.filter_servers(servers_stub,
                                                   ["standard", "p2p"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except categories.CategoriesError:
        assert False

    # test all categories at once should return empty list
    try:
        actual_servers = categories.filter_servers(
            servers_stub,
            ["double", "dedicated", "standard", "p2p", "obfuscated"])
        assert actual_servers == list()
    except categories.CategoriesError:
        assert False
示例#16
0
def test_filter_servers_good():
    servers_stub = get_servers_stub()

    # test param when param is not given
    try:
        actual_servers = countries.filter_servers(servers_stub)
        expected_servers = servers_stub
        assert actual_servers == expected_servers
    except countries.CountryError:
        assert False

    # test param when param is an empty list
    try:
        actual_servers = countries.filter_servers(servers_stub)
        expected_servers = servers_stub
        assert actual_servers == expected_servers
    except countries.CountryError:
        assert False

    # test param with multiple valid countries
    try:
        actual_servers = countries.filter_servers(servers_stub, ["de", "nl"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113", "nl80", "nl81", "nl82"]
        )
        assert actual_servers == expected_servers
    except countries.CountryError:
        assert False

    # test with uppercase country code
    try:
        actual_servers = countries.filter_servers(servers_stub, ["DE"])
        expected_servers = get_expected_servers_by_domain(["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except countries.CountryError:
        assert False

    # test with mixedcase country code
    try:
        actual_servers = countries.filter_servers(servers_stub, ["De"])
        expected_servers = get_expected_servers_by_domain(["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except countries.CountryError:
        assert False

    # test with multiple mixedcase country codes
    try:
        actual_servers = countries.filter_servers(servers_stub, ["De", "nL"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113", "nl80", "nl81", "nl82"]
        )
        assert actual_servers == expected_servers
    except countries.CountryError:
        assert False

    # test param: server is empty
    try:
        actual_servers = countries.filter_servers(list(), ["de"])
        assert actual_servers == list()
    except BaseException:
        assert False
示例#17
0
def test_filter_servers_with_every_feature():
    servers_stub = get_servers_stub()

    # test with ikev2
    try:
        actual_servers = features.filter_servers(servers_stub, ["ikev2"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with openvpn_udp
    try:
        actual_servers = features.filter_servers(servers_stub, ["openvpn_udp"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113", "us2853"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with openvpn_tcp
    try:
        actual_servers = features.filter_servers(servers_stub, ["openvpn_tcp"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113", "us-ca5", "us2853"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with socks
    try:
        actual_servers = features.filter_servers(servers_stub, ["socks"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with proxy
    try:
        actual_servers = features.filter_servers(servers_stub, ["proxy"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with pptp
    try:
        actual_servers = features.filter_servers(servers_stub, ["pptp"])
        assert actual_servers == list()
    except features.FeatureError:
        assert False

    # test with l2tp
    try:
        actual_servers = features.filter_servers(servers_stub, ["l2tp"])
        assert actual_servers == list()
    except features.FeatureError:
        assert False

    # test with openvpn_xor_udp
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["openvpn_xor_udp"])
        expected_servers = get_expected_servers_by_domain(
            ["nl80", "nl81", "nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with openvpn_xor_tcp
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["openvpn_xor_tcp"])
        expected_servers = get_expected_servers_by_domain(
            ["nl80", "nl81", "nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with proxy_cybersec
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["proxy_cybersec"])
        expected_servers = get_expected_servers_by_domain(
            ["de111", "de112", "de113"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with proxy_ssl
    try:
        actual_servers = features.filter_servers(servers_stub, ["proxy_ssl"])
        expected_servers = get_expected_servers_by_domain(["de112", "de113"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with proxy_ssl_cybersec
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["proxy_ssl_cybersec"])
        expected_servers = get_expected_servers_by_domain(["de111", "de112"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with ikev2_v6
    try:
        actual_servers = features.filter_servers(servers_stub, ["ikev2_v6"])
        expected_servers = get_expected_servers_by_domain(["nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with openvpn_udp_v6
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["openvpn_udp_v6"])
        expected_servers = get_expected_servers_by_domain(["nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with openvpn_tcp_v6
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["openvpn_tcp_v6"])
        expected_servers = get_expected_servers_by_domain(["nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with wireguard_udp
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["wireguard_udp"])
        expected_servers = get_expected_servers_by_domain(["nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with openvpn_udp_tls_crypt
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["openvpn_udp_tls_crypt"])
        expected_servers = get_expected_servers_by_domain(["nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False

    # test with openvpn_tcp_tls_crypt
    try:
        actual_servers = features.filter_servers(servers_stub,
                                                 ["openvpn_tcp_tls_crypt"])
        expected_servers = get_expected_servers_by_domain(["nl82"])
        assert actual_servers == expected_servers
    except features.FeatureError:
        assert False