Commit 7a650e7e authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Merge branch 'noc-py-clean-4' into 'master'

Port legacy tests

See merge request noc/noc!1984
parents 3f6ef2c4 8bebb0ed
[run]
omit = *tests*
[report]
ignore_errors = True
......@@ -121,6 +121,7 @@ class Command(BaseCommand):
"""
from coverage.results import Numbers
from coverage.report import Reporter
from coverage.misc import NoSource
from noc.tests.conftest import _stats as stats
self.print("---[ Test session statistics ]------")
......@@ -128,7 +129,10 @@ class Command(BaseCommand):
reporter = Reporter(cov, cov.config)
totals = Numbers()
for fr in reporter.find_file_reporters(None):
analysis = cov._analyze(fr)
try:
analysis = cov._analyze(fr)
except NoSource:
continue
totals += analysis.numbers
n_passed = len(stats.get("passed", []))
n_skipped = len(stats.get("skipped", []))
......
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Unittests for local authentication method
# ---------------------------------------------------------------------
# Copyright (C) 2007-2011 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
from __future__ import with_statement
# NOC modules
from noc.main.auth.backends.localbackend import NOCLocalBackend
from noc.lib.test import NOCTestCase
class LocalBackendTestCase(NOCTestCase):
def setUp(self):
User = NOCLocalBackend.User
u = User(username="local_test", is_active=True, is_superuser=False)
u.set_password("local_test")
u.save()
def test_get_user(self):
backend = NOCLocalBackend()
user = backend.User.objects.get(username="local_test")
r = backend.get_user(user.id)
self.assertNotEqual(r, None)
self.assertEqual(r.username, "local_test")
self.assertEqual(r.is_active, True)
self.assertEqual(r.is_superuser, False)
r = backend.get_user(1278221)
self.assertEqual(r, None)
def test_authenticate(self):
backend = NOCLocalBackend()
# Valid user, valid password
r = backend.authenticate(username="local_test", password="local_test")
self.assertNotEqual(r, None)
self.assertEqual(r.username, "local_test")
self.assertEqual(r.is_active, True)
self.assertEqual(r.is_superuser, False)
# Valid user, invalid password
r = backend.authenticate(username="local_test", password="local_test1")
self.assertEqual(r, None)
# Invalid user, valid password
r = backend.authenticate(username="local_test1", password="local_test")
self.assertEqual(r, None)
# Invalid user, invalid password
r = backend.authenticate(username="local_test1", password="local_test1")
self.assertEqual(r, None)
def test_change_credentials(self):
backend = NOCLocalBackend()
user = backend.User.objects.get(username="local_test")
# Failed attempt
with self.assertRaises(ValueError):
backend.change_credentials(user, old_password="l", new_password="1",
retype_password="1")
r = backend.authenticate(username="local_test", password="local_test")
self.assertNotEqual(r, None)
r = backend.authenticate(username="local_test", password="1")
self.assertEqual(r, None)
# Failed attempt #2
with self.assertRaises(ValueError):
backend.change_credentials(user, old_password="local_test",
new_password="1", retype_password="2")
r = backend.authenticate(username="local_test", password="local_test")
self.assertNotEqual(r, None)
r = backend.authenticate(username="local_test", password="1")
self.assertEqual(r, None)
# Success
backend.change_credentials(user, old_password="local_test",
new_password="1", retype_password="1")
r = backend.authenticate(username="local_test", password="local_test")
self.assertEqual(r, None)
r = backend.authenticate(username="local_test", password="1")
self.assertNotEqual(r, None)
def test_get_or_create(self):
backend = NOCLocalBackend()
# Get existing
r = backend.get_or_create_db_user("local_test", first_name="Local",
last_name="Test")
self.assertNotEqual(r, None)
self.assertEqual(r.username, "local_test")
self.assertEqual(r.is_active, True)
self.assertEqual(r.is_superuser, False)
self.assertEqual(r.first_name, "Local")
self.assertEqual(r.last_name, "Test")
# Create
r = backend.get_or_create_db_user("local_test1", first_name="New",
last_name="User", is_active=True,
is_superuser=True,
email="test@example.com")
self.assertNotEqual(r, None)
self.assertEqual(r.username, "local_test1")
self.assertEqual(r.is_active, True)
self.assertEqual(r.is_superuser, True)
self.assertEqual(r.first_name, "New")
self.assertEqual(r.last_name, "User")
self.assertEqual(r.email, "test@example.com")
r.delete()
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# MIMEType model test
# ---------------------------------------------------------------------
# Copyright (C) 2007-2011 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
from __future__ import with_statement
# NOC modules
from noc.lib.test import ModelTestCase
from noc.main.models.mimetype import MIMEType
from noc.lib.validators import ValidationError
class MIMETypeModelTestCase(ModelTestCase):
model = MIMEType
data = [
{"extension": ".noc", "mime_type": "application/noc"},
{"extension": ".nocx", "mime_type": "application/noc-x"}
]
def test_unique(self):
m1 = MIMEType(extension=".noc", mime_type="application/test")
m1.save()
with self.assertRaises(Exception): # IntegrityError
m2 = MIMEType(extension=".noc", mime_type="application/test")
m2.save()
def test_validators(self):
m1 = MIMEType(extension="noc", mime_type="application/test")
with self.assertRaises(ValidationError):
m1.full_clean()
m2 = MIMEType(extension=".noc", mime_type="application")
with self.assertRaises(ValidationError):
m2.full_clean()
m3 = MIMEType(extension=".noc", mime_type="application/test")
m3.full_clean()
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Unittests for access system
# ---------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
from unittest import TestCase
# Third-party modules
from django.contrib.auth.models import User, Group
# NOC modules
from noc.main.models.permission import Permission
class AccessTestCase(TestCase):
permissions = set(["mod1:app1:p1", "mod1:app1:p2", "mod1:app2:p1"])
def setUp(self):
User(username="superuser", is_active=True, is_superuser=True).save()
User(username="disabled", is_active=False).save()
User(username="user1", is_active=True).save()
User(username="user2", is_active=True).save()
User(username="user3", is_active=True).save()
Group(name="group1").save()
Group(name="group2").save()
for p in self.permissions:
Permission(name=p).save()
def tearDown(self):
for p in self.permissions:
Permission.objects.get(name=p).delete()
User.objects.get(username="superuser").delete()
User.objects.get(username="disabled").delete()
User.objects.get(username="user1").delete()
User.objects.get(username="user2").delete()
User.objects.get(username="user3").delete()
Group.objects.get(name="group1").delete()
Group.objects.get(name="group2").delete()
def test_permissions(self):
superuser = User.objects.get(username="superuser")
user1 = User.objects.get(username="user1")
Permission.set_user_permissions(user1, self.permissions)
self.assertEquals(Permission.get_user_permissions(user1), self.permissions)
group1 = Group.objects.get(name="group1")
Permission.set_group_permissions(group1, self.permissions)
self.assertEquals(Permission.get_group_permissions(group1), self.permissions)
user2 = User.objects.get(username="user2")
user2.groups.add(group1)
user3 = User.objects.get(username="user3")
disabled_user = User.objects.get(username="disabled")
Permission.set_user_permissions(disabled_user, self.permissions)
disabled_user.groups.add(group1)
# Check permissions are set
for p in self.permissions:
self.assertEquals(Permission.has_perm(superuser, p), True) # Always true for superuser
self.assertEquals(Permission.has_perm(disabled_user, p), False) # Always false for disabled user
self.assertEquals(Permission.has_perm(user1, p), True) # User permissions
self.assertEquals(Permission.has_perm(user2, p), True) # Group permissions
self.assertEquals(Permission.has_perm(user3, p), False) # Not set
# Test revoking of permissions
Permission.set_user_permissions(user1, set())
self.assertEquals(Permission.get_user_permissions(user1), set([]))
Permission.set_group_permissions(group1, set())
self.assertEquals(Permission.get_group_permissions(group1), set([]))
def testUnicode(self):
for p in self.permissions:
self.assertEquals(unicode(Permission.objects.get(name=p)), p)
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Unittests for Activator
# ---------------------------------------------------------------------
# Copyright (C) 2007-2009 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
from noc.lib.test import ModelTestCase
from noc.main.models import *
from noc.sa.interfaces.base import InterfaceTypeError
#
#
#
class PyRuleTestCase(ModelTestCase):
model=PyRule
def test_pyrule(self):
# Valid version rule
v_rule=PyRule(name="v_rule",interface="IGetVersion",description="Test Rule",text="""
@pyrule
def get_version():
return {"vendor":"Cisco","platform":"CBS31X0","version":"12.2(40)EX3"}
""")
v_rule.save()
PyRule.call("v_rule")
# Test InterfaceErrors
v_rule=PyRule(name="v_rule1",interface="IGetVersion",description="Test Rule",text="""
@pyrule
def get_version():
return {"vendor":"Cisco","platform":"CBS31X0","ver":"12.2(40)EX3"}
""")
v_rule.save()
try:
PyRule.call("v_rule1")
raise AssertionError("InterfaceTypeError exception expected")
except InterfaceTypeError:
pass
[
{
"pk": 3,
"model": "main.refbook",
"fields": {
"is_enabled": true,
"last_updated": "2010-04-22 16:19:50",
"name": "ISO 3166 Country Codes",
"language": 38,
"downloader": "CSV",
"download_url": "https://cdn.nocproject.org/refbook/iso3166_1.csv",
"is_builtin": true,
"refresh_interval": 90,
"next_update": "2010-07-21 16:19:50",
"description": "ISO 3166 Country Codes"
}
},
{
"pk": 12,
"model": "main.refbookfield",
"fields": {
"description": null,
"ref_book": 3,
"search_method": "substring",
"is_required": true,
"order": 1,
"name": "Country"
}
},
{
"pk": 13,
"model": "main.refbookfield",
"fields": {
"description": null,
"ref_book": 3,
"search_method": "substring",
"is_required": true,
"order": 2,
"name": "Code"
}
}
]
\ No newline at end of file
......@@ -6,8 +6,6 @@
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from __future__ import absolute_import
# Third-party modules
import pytest
# NOC modules
......
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# vc.VC tests
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
import pytest
# NOC modules
from noc.vc.models.vctype import VCType
from noc.vc.models.vcdomain import VCDomain
from noc.vc.models.vc import VC
from noc.vc.models.error import InvalidLabelException
@pytest.mark.parametrize("data", [
{"vc_type": "802.1Q VLAN", "l1": 0, "$except": InvalidLabelException},
{"vc_type": "802.1Q VLAN", "l1": 1},
{"vc_type": "802.1Q VLAN", "l1": 100},
{"vc_type": "802.1Q VLAN", "l1": 4095},
{"vc_type": "802.1Q VLAN", "l1": 4096, "$except": InvalidLabelException}
])
def test_vc_labels(data):
# Get type
vc_type = VCType.objects.get(name=data["vc_type"])
# Create temporary domain
vc_domain = VCDomain(name="TEST DOMAIN", type=vc_type)
vc_domain.save()
vc = VC(vc_domain=vc_domain, name="TEST VC", l1=data.get("l1", 0), l2=data.get("l2", 0))
if "$except" in data:
with pytest.raises(data["$except"]):
vc.save()
else:
vc.save()
assert unicode(vc)
vc.delete()
# Cleanup
vc_domain.delete()
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# VCType model test
# ---------------------------------------------------------------------
# Copyright (C) 2007-2011 The NOC Project
# ----------------------------------------------------------------------
# vc.VCType tests
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# ----------------------------------------------------------------------
# Third-party modules
import pytest
# NOC modules
from noc.lib.test import ModelTestCase
from noc.vc.models import VCType
from noc.vc.models.vctype import VCType
class VCTypeModelTestCase(ModelTestCase):
model = VCType
data = [
@pytest.mark.parametrize(
"data", [
{
"name": "Test 1",
"min_labels": 1,
"label1_min": 0,
"label1_max": 15
},
{
}, {
"name": "Test 2",
"min_labels": 2,
"label1_min": 0,
......@@ -31,3 +28,17 @@ class VCTypeModelTestCase(ModelTestCase):
"label2_max": 15
}
]
)
def test_insert(data):
vc_type = VCType(**data)
vc_type.save()
for k in data:
assert getattr(vc_type, k) == data[k]
# Fetch record
vc_type = VCType.objects.get(name=data["name"])
assert vc_type.pk
assert unicode(vc_type)
for k in data:
assert getattr(vc_type, k) == data[k]
# Delete record
vc_type.delete()
......@@ -2,14 +2,14 @@
# ---------------------------------------------------------------------
# Template tags test
# ---------------------------------------------------------------------
# Copyright (C) 2007-2011 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Django modules
from django import template
# NOC modules
from noc.lib.test import NOCTestCase
# Third-party modules
import django.template
import pytest
PYTHON1_TPL = """
{% load python %}
......@@ -69,19 +69,13 @@ v=2
v1=7
"""
class TemplateTestCase(NOCTestCase):
def render(self, tpl, context={}):
ctx = template.Context(context)
return template.Template(tpl).render(ctx)
def test_python(self):
"""
{% python %} tag test
:return:
"""
self.assertEquals(self.render(PYTHON1_TPL, {"x": 1}),
PYTHON1_OUT)
self.assertEquals(self.render(PYTHON2_TPL, {"n": 3}),
PYTHON2_OUT)
self.assertEquals(self.render(PYTHON3_TPL, {"v": 2}),
PYTHON3_OUT)
@pytest.mark.parametrize("template,context,expected", [
(PYTHON1_TPL, {"x": 1}, PYTHON1_OUT),
(PYTHON2_TPL, {"n": 3}, PYTHON2_OUT),
(PYTHON3_TPL, {"v": 2}, PYTHON3_OUT)
])
def test_templatetags(template, context, expected):
ctx = django.template.Context(context)
result = django.template.Template(template).render(ctx)
assert result == expected
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# VC Test
# ---------------------------------------------------------------------
# Copyright (C) 2007-2009 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
from noc.lib.test import ModelTestCase
from noc.vc.models import *
class VCTestCase(ModelTestCase):
model=VC
##
## Sample data
##
def get_data(self):
return [
{
"vc_domain_id" : 1,
"name" : "VLAN1",
"l1" : 1
},
{
"vc_domain_id" : 1,
"name" : "VLAN2",
"l1" : 2
},
{
"vc_domain_id" : 1,
"name" : "VLAN3",
"l1" : 3
},
{
"vc_domain_id" : 1,
"l1" : 4
},
]
##
## Test VC Object properties
##
def object_test(self,o):
pass
##
## Test search
##
def test_search(self):
pass
##
## Test VC Type restrictions
##
def test_VCType(self):
# Test each VC Type
for vct in VCType.objects.all():
# Create domain
d=VCDomain(name=vct.name,type=vct)
d.save()
# Set L2
l2=0
if vct.min_labels>1:
l2=vct.label2_min if vct.label2_min else vct.label2_max
# Test label 1
# Lower range
vc1=VC(vc_domain=d,name="VC1",l1=vct.label1_min-1 if vct.label1_min>1 else -1,l2=l2)
try:
vc1.save()
assert False
except InvalidLabelException:
pass
# Upper range
vc2=VC(vc_domain=d,name="VC2",l1=vct.label1_max+1,l2=l2)
try:
vc2.save()
assert False
except InvalidLabelException:
pass
# Normal
vc3=VC(vc_domain=d,name="VC3",l1=vct.label1_min,l2=l2)
vc3.save()
unicode(vc3)
vc3.delete()
# Test L2
if vct.min_labels>1:
l1=vct.label1_min
vc4=VC(vc_domain=d,name="VC4",l1=l1,l2=vct.label2_min-1 if vct.label2_min>1 else -1)
try:
vc4.save()
assert False
except InvalidLabelException:
pass
vc5=VC(vc_domain=d,name="VC4",l1=l1,l2=vct.label2_max+1)
try:
vc5.save()
assert False
except InvalidLabelException:
pass
vc6=VC(vc_domain=d,name="VC4",l1=l1,l2=vct.label2_min if vct.label2_min else vct.label2_max)
vc6.save()
unicode(vc6)
vc6.prefix_set.all()
vc6.delete()
# Clean up domain
d.delete()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment