test: interim unit tests

!1
This commit is contained in:
2024-05-10 14:13:33 +09:30
parent 789777a270
commit 5ca58f1883
11 changed files with 286 additions and 2 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@ venv/**
itsm/static/**
__pycache__
itsm/db.sqlite3
**.coverage
artifacts/

View File

@ -11,7 +11,7 @@ variables:
include:
# - local: .gitlab/integration_test.gitlab-ci.yml
- local: .gitlab/pytest.gitlab-ci.yml
# - local: .gitlab/unit-test.gitlab-ci.yml
- project: nofusscomputing/projects/gitlab-ci
ref: development

View File

@ -0,0 +1,36 @@
Unit:
stage: test
image: python:3.11-alpine3.19
needs: []
script:
- pip install -r requirements.txt
- pip install -r requirements_test.txt
- cd itsm
- pytest --cov --cov-report term --cov-report xml:../artifacts/coverage.xml --cov-report html:../artifacts/coverage/ --junit-xml=../artifacts/test.junit.xml
artifacts:
expire_in: "30 days"
when: always
reports:
coverage_report:
coverage_format: cobertura
path: artifacts/coverage.xml
junit:
- artifacts/unit.JUnit.xml
paths:
- artifacts/
rules:
- if: # Occur on merge
$CI_COMMIT_BRANCH
&&
(
$CI_PIPELINE_SOURCE == "push"
||
$CI_PIPELINE_SOURCE == "web"
)
when: always
- when: never

View File

@ -30,3 +30,13 @@ python3 manage.py createsuperuser
```
Updates to python modules will need to be captured with SCM. This can be done by running `pip freeze > requirements.txt` from the running virtual environment.
## Running Tests
test can be run by running the following:
1. `pip install -r requirements_test.txt -r requirements.txt`
1. `pytest --cov --cov-report html --cov=./`

View File

@ -0,0 +1,13 @@
from django.conf import settings
import pytest
import unittest
class Test_aa_settings_default(unittest.TestCase):
@pytest.mark.django_db
def test_setting_login_required_default(self):
""" By default login should be required
"""
assert settings.LOGIN_REQUIRED

View File

@ -0,0 +1,38 @@
from django.conf import settings
from django.shortcuts import reverse
from django.test import TestCase, Client
import pytest
import unittest
from structure.models import Organization
@pytest.mark.django_db
def test_setting_login_required():
"""Some docstring defining what the test is checking."""
client = Client()
url = reverse('home')
# client.force_login(user)
# default_settings = settings
settings.LOGIN_REQUIRED = True
response = client.get(url)
assert response.status_code == 302
# settings = default_settings
@pytest.mark.django_db
def test_setting_login_required_not():
"""Some docstring defining what the test is checking."""
client = Client()
url = reverse('home')
settings.LOGIN_REQUIRED = False
response = client.get(url)
assert response.status_code == 200

8
itsm/pytest.ini Normal file
View File

@ -0,0 +1,8 @@
[pytest]
DJANGO_SETTINGS_MODULE = itsm.settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %Y-%m-%d %H:%M:%S

View File

@ -0,0 +1,122 @@
from django.conf import settings
from django.shortcuts import reverse
from django.test import TestCase, Client
import pytest
import unittest
import requests
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from structure.models import Organization
# class Test_app_structure_auth(unittest.TestCase):
User = get_user_model()
@pytest.fixture
def user() -> User:
return User.objects.create_user(username="testuser", password="testpassword")
# @pytest.fixture
# def organization() -> Organization:
# return Organization.objects.create(
# name='Test org',
# )
@pytest.mark.django_db
def test_require_login_organizations():
"""Some docstring defining what the test is checking."""
client = Client()
url = reverse('Structure:Organizations')
response = client.get(url)
assert response.status_code == 302
@pytest.mark.django_db
def test_require_login_organization_pk():
"""Some docstring defining what the test is checking."""
client = Client()
url = reverse('Structure:_singleorg', kwargs={'pk': 1})
response = client.get(url)
assert response.status_code == 302
@pytest.mark.django_db
def test_login_view_organizations_no_permission(user):
"""Some docstring defining what the test is checking."""
client = Client()
url = reverse('Structure:Organizations')
client.force_login(user)
response = client.get(url)
assert response.status_code == 403
@pytest.mark.skip(reason="to be written")
def test_organizations_permission_change(user):
"""ensure user with permission can change organization
Args:
user (_type_): _description_
"""
pass
@pytest.mark.skip(reason="to be written")
def test_organizations_permission_delete_denied(user):
"""ensure non-admin user cant delete organization
Args:
user (_type_): _description_
"""
pass
@pytest.mark.skip(reason="to be written")
def test_team_permission_add_in_org(user):
"""ensure user with add permission to an organization can add team
Args:
user (_type_): _description_
"""
pass
@pytest.mark.skip(reason="to be written")
def test_team_permission_add_not_in_org(user):
"""ensure user with add permission to an organization can add team
Args:
user (_type_): _description_
"""
pass
@pytest.mark.skip(reason="to be written")
def test_team_permission_change(user):
"""ensure user can change a team
Args:
user (_type_): _description_
"""
pass
@pytest.mark.skip(reason="to be written")
def test_team_permission_delete(user):
"""ensure user can delete a team
Args:
user (_type_): _description_
"""
pass

View File

@ -0,0 +1,47 @@
from django.test import TestCase
import pytest
import requests
import unittest
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from structure.models import Organization, Team
@pytest.fixture
def organization() -> Organization:
return Organization.objects.create(
name='Test org',
)
@pytest.fixture
def team() -> Team:
return Team.objects.create(
name='Team one',
organization = Organization.objects.create(
name='Test org',
),
)
@pytest.mark.django_db
def test_org_name_character_count(organization) -> None:
organization.name = 'A' * 256
with pytest.raises(ValidationError) as e:
organization.full_clean()
assert 'Ensure this value has at most 200 characters' in str(e.value)
@pytest.mark.django_db
def test_team_name_character_count(team) -> None:
team.name = 'A' * 256
with pytest.raises(ValidationError) as e:
team.full_clean()
assert 'Ensure this value has at most 200 characters' in str(e.value)

View File

@ -5,5 +5,5 @@ from . import views
app_name = "Structure"
urlpatterns = [
path("", views.IndexView.as_view(), name="Organizations"),
path("<int:pk>/", views.OrganizationView.as_view()),
path("<int:pk>/", views.OrganizationView.as_view(), name='_singleorg'),
]

8
requirements_test.txt Normal file
View File

@ -0,0 +1,8 @@
iniconfig==2.0.0
packaging==24.0
pluggy==1.5.0
pytest==8.2.0
pytest-django==4.8.0
coverage==7.5.1
pytest-cov==5.0.0