old_console/company/tests/test_company_api.py
2024-11-02 14:12:45 +03:00

98 lines
3.7 KiB
Python

import pytest
from django.urls import reverse
from rest_framework.test import APIRequestFactory, force_authenticate
from rest_framework import status
from company.models.company import Company
from company.models.location import LocationCode
from company.views.company_api import CompanyApiView
from ncircc.enums.notifications import AffectedSystemFunction
@pytest.mark.django_db
class TestCompanyApi:
url = reverse('company_api')
user = None
@pytest.fixture(autouse=True)
def setup_test(self, add_user_with_permissions):
self.user = add_user_with_permissions(username='test_api_user', password='TestApiPass123', is_superuser=True)
self.location, _ = LocationCode.objects.get_or_create(code='RU-MOS')
@pytest.mark.unit
def test_create_compy_with_valid_data(self):
"""Test creating company through api with valid data."""
count_after = Company.objects.count()
assert not count_after
data = {
'name': 'Test_NAME_company',
'is_cii': False,
'location': self.location.id,
'city': 'Moscow',
'affected_system_function': AffectedSystemFunction.NUCLEAR_POWER.value,
}
request = APIRequestFactory().post(self.url, data)
force_authenticate(request, self.user)
view = CompanyApiView.as_view()
response = view(request)
count_after = Company.objects.count()
assert response.status_code == 200
assert count_after == 1
assert response.data['name'] == data['name']
assert response.data['location']['id'] == self.location.id
assert response.data['location']['code'] == self.location.code
@pytest.mark.unit
def test_create_company_with_not_valid_date(self):
"""Test creating company through api with not valid data."""
data = {
'name': 'Test_NAME_company',
}
request = APIRequestFactory().post(self.url, data)
force_authenticate(request, self.user)
view = CompanyApiView.as_view()
response = view(request)
count_after = Company.objects.count()
assert response.status_code == 400
assert count_after == 0
assert 'city' in response.data
@pytest.mark.unit
def test_update_company_with_valid_date(self):
"""Test updating company data through api"""
Company.objects.create(name='Test_name_1', city='Moscow')
count_before = Company.objects.count()
assert count_before == 1
data = {'name': 'Test_name_2', 'is_cii': True, 'city': 'Moscow'}
request = APIRequestFactory().post(self.url, data)
force_authenticate(request, self.user)
view = CompanyApiView.as_view()
response = view(request)
count_after = Company.objects.count()
assert response.status_code == 200
assert count_after == count_before
assert response.data['name'] == data['name']
assert response.data['is_cii'] == data['is_cii']
@pytest.mark.unit
def test_get_company_data_with_created_company(self):
Company.objects.create(name='Test_name_1', city='Moscow')
count = Company.objects.count()
assert count == 1
request = APIRequestFactory().get(self.url)
force_authenticate(request, self.user)
view = CompanyApiView.as_view()
response = view(request)
assert response.status_code == 200
@pytest.mark.unit
def test_get_company_data_without_company(self, api_client):
api_client.force_authenticate(self.user)
count = Company.objects.count()
assert not count
response = api_client.get(self.url)
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"details": "company not initialized"}