mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-19 19:09:22 +02:00
Tests
This commit is contained in:
parent
7d82ab916e
commit
63dd354da8
3 changed files with 47 additions and 43 deletions
|
@ -93,7 +93,7 @@ def available(request, domain=""):
|
||||||
|
|
||||||
@require_http_methods(["GET"])
|
@require_http_methods(["GET"])
|
||||||
@login_not_required
|
@login_not_required
|
||||||
def get_current_full(request):
|
def get_current_full(request, file_path = "migrationdata/current-full.csv"):
|
||||||
# Open the CSV file
|
# Open the CSV file
|
||||||
file_path = "migrationdata/current-full.csv"
|
file_path = "migrationdata/current-full.csv"
|
||||||
return serve_file(file_path)
|
return serve_file(file_path)
|
||||||
|
@ -101,8 +101,7 @@ def get_current_full(request):
|
||||||
|
|
||||||
@require_http_methods(["GET"])
|
@require_http_methods(["GET"])
|
||||||
@login_not_required
|
@login_not_required
|
||||||
def get_current_federal(request):
|
def get_current_federal(request, file_path = "migrationdata/current-federal.csv"):
|
||||||
file_path = "migrationdata/current-federal.csv"
|
|
||||||
return serve_file(file_path)
|
return serve_file(file_path)
|
||||||
|
|
||||||
def serve_file(file_path):
|
def serve_file(file_path):
|
||||||
|
|
3
src/registrar/tests/data/fake_current_federal.csv
Normal file
3
src/registrar/tests/data/fake_current_federal.csv
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Domain name,Domain type,Agency,Organization name,City,State,Security Contact Email
|
||||||
|
cdomain1.gov,Federal - Executive,World War I Centennial Commission,,,,
|
||||||
|
ddomain3.gov,Federal,Armed Forces Retirement Home,,,,
|
|
|
@ -1,21 +1,25 @@
|
||||||
from unittest import skip
|
|
||||||
from django.test import Client, TestCase
|
|
||||||
from io import StringIO
|
|
||||||
import csv
|
import csv
|
||||||
|
from unittest import skip
|
||||||
|
from django.test import Client, RequestFactory, TestCase
|
||||||
|
from io import StringIO
|
||||||
from registrar.models.domain_information import DomainInformation
|
from registrar.models.domain_information import DomainInformation
|
||||||
from registrar.models.domain import Domain
|
from registrar.models.domain import Domain
|
||||||
from registrar.models.user import User
|
from registrar.models.user import User
|
||||||
|
from os import linesep
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from registrar.utility.csv_export import export_domains_to_writer
|
from registrar.utility.csv_export import export_domains_to_writer
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from unittest.mock import call, mock_open, patch
|
from unittest.mock import call, mock_open, patch
|
||||||
|
from api.views import get_current_federal, get_current_full
|
||||||
|
|
||||||
|
|
||||||
class CsvReportsTest(TestCase):
|
class CsvReportsTest(TestCase):
|
||||||
"""Tests to determine if we are uploading our reports correctly"""
|
"""Tests to determine if we are uploading our reports correctly"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Create fake domain data"""
|
"""Create fake domain data"""
|
||||||
self.client = Client(HTTP_HOST="localhost:8080")
|
self.client = Client(HTTP_HOST="localhost:8080")
|
||||||
|
self.factory = RequestFactory()
|
||||||
username = "test_user"
|
username = "test_user"
|
||||||
first_name = "First"
|
first_name = "First"
|
||||||
last_name = "Last"
|
last_name = "Last"
|
||||||
|
@ -67,10 +71,8 @@ class CsvReportsTest(TestCase):
|
||||||
fake_open = mock_open()
|
fake_open = mock_open()
|
||||||
# We don't actually want to write anything for a test case,
|
# We don't actually want to write anything for a test case,
|
||||||
# we just want to verify what is being written.
|
# we just want to verify what is being written.
|
||||||
with patch('builtins.open', fake_open), self.assertRaises(FileNotFoundError) as err:
|
with patch("builtins.open", fake_open), self.assertRaises(FileNotFoundError) as err:
|
||||||
call_command(
|
call_command("generate_current_federal_report")
|
||||||
"generate_current_federal_report"
|
|
||||||
)
|
|
||||||
error = err.exception
|
error = err.exception
|
||||||
self.assertEqual(str(error), "Could not find newly created file at 'migrationdata/current-federal.csv'")
|
self.assertEqual(str(error), "Could not find newly created file at 'migrationdata/current-federal.csv'")
|
||||||
|
|
||||||
|
@ -79,28 +81,23 @@ class CsvReportsTest(TestCase):
|
||||||
fake_open = mock_open()
|
fake_open = mock_open()
|
||||||
# We don't actually want to write anything for a test case,
|
# We don't actually want to write anything for a test case,
|
||||||
# we just want to verify what is being written.
|
# we just want to verify what is being written.
|
||||||
with patch('builtins.open', fake_open), self.assertRaises(FileNotFoundError) as err:
|
with patch("builtins.open", fake_open), self.assertRaises(FileNotFoundError) as err:
|
||||||
call_command(
|
call_command("generate_current_full_report")
|
||||||
"generate_current_full_report"
|
|
||||||
)
|
|
||||||
error = err.exception
|
error = err.exception
|
||||||
self.assertEqual(str(error), "Could not find newly created file at 'migrationdata/current-full.csv'")
|
self.assertEqual(str(error), "Could not find newly created file at 'migrationdata/current-full.csv'")
|
||||||
|
|
||||||
def test_generate_federal_report(self):
|
def test_generate_federal_report(self):
|
||||||
"""Ensures that we correctly generate current-federal.csv"""
|
"""Ensures that we correctly generate current-federal.csv"""
|
||||||
expected_file_content = [
|
expected_file_content = [
|
||||||
call('Domain name,Domain type,Agency,Organization name,City,State,Security Contact Email\r\n'),
|
call("Domain name,Domain type,Agency,Organization name,City,State,Security Contact Email\r\n"),
|
||||||
call('cdomain1.gov,Federal - Executive,World War I Centennial Commission,,,, \r\n'),
|
call("cdomain1.gov,Federal - Executive,World War I Centennial Commission,,,, \r\n"),
|
||||||
call('ddomain3.gov,Federal,Armed Forces Retirement Home,,,, \r\n')
|
call("ddomain3.gov,Federal,Armed Forces Retirement Home,,,, \r\n"),
|
||||||
]
|
]
|
||||||
fake_open = mock_open()
|
fake_open = mock_open()
|
||||||
# We don't actually want to write anything for a test case,
|
# We don't actually want to write anything for a test case,
|
||||||
# we just want to verify what is being written.
|
# we just want to verify what is being written.
|
||||||
with patch('builtins.open', fake_open):
|
with patch("builtins.open", fake_open):
|
||||||
call_command(
|
call_command("generate_current_federal_report", checkpath=False)
|
||||||
"generate_current_federal_report",
|
|
||||||
checkpath=False
|
|
||||||
)
|
|
||||||
content = fake_open()
|
content = fake_open()
|
||||||
# Now you can make assertions about how you expect 'file' to be used.
|
# Now you can make assertions about how you expect 'file' to be used.
|
||||||
content.write.assert_has_calls(expected_file_content)
|
content.write.assert_has_calls(expected_file_content)
|
||||||
|
@ -108,26 +105,23 @@ class CsvReportsTest(TestCase):
|
||||||
def test_generate_full_report(self):
|
def test_generate_full_report(self):
|
||||||
"""Ensures that we correctly generate current-full.csv"""
|
"""Ensures that we correctly generate current-full.csv"""
|
||||||
expected_file_content = [
|
expected_file_content = [
|
||||||
call('Domain name,Domain type,Agency,Organization name,City,State,Security Contact Email\r\n'),
|
call(f"Domain name,Domain type,Agency,Organization name,City,State,Security Contact Email{linesep}"),
|
||||||
call('cdomain1.gov,Federal - Executive,World War I Centennial Commission,,,, \r\n'),
|
call(f"cdomain1.gov,Federal - Executive,World War I Centennial Commission,,,, {linesep}"),
|
||||||
call('ddomain3.gov,Federal,Armed Forces Retirement Home,,,, \r\n'),
|
call(f"ddomain3.gov,Federal,Armed Forces Retirement Home,,,, {linesep}"),
|
||||||
call('adomain2.gov,Interstate,,,,, \r\n')
|
call(f"adomain2.gov,Interstate,,,,, {linesep}"),
|
||||||
]
|
]
|
||||||
fake_open = mock_open()
|
fake_open = mock_open()
|
||||||
# We don't actually want to write anything for a test case,
|
# We don't actually want to write anything for a test case,
|
||||||
# we just want to verify what is being written.
|
# we just want to verify what is being written.
|
||||||
with patch('builtins.open', fake_open):
|
with patch("builtins.open", fake_open):
|
||||||
call_command(
|
call_command("generate_current_full_report", checkpath=False)
|
||||||
"generate_current_full_report",
|
|
||||||
checkpath=False
|
|
||||||
)
|
|
||||||
content = fake_open()
|
content = fake_open()
|
||||||
# Now you can make assertions about how you expect 'file' to be used.
|
# Now you can make assertions about how you expect 'file' to be used.
|
||||||
content.write.assert_has_calls(expected_file_content)
|
content.write.assert_has_calls(expected_file_content)
|
||||||
|
|
||||||
def test_not_found_full_report(self):
|
def test_not_found_full_report(self):
|
||||||
"""Ensures that we get a not found when the report doesn't exist"""
|
"""Ensures that we get a not found when the report doesn't exist"""
|
||||||
response = self.client.get('/api/v1/get-report/current-full')
|
response = self.client.get("/api/v1/get-report/current-full")
|
||||||
|
|
||||||
# Check that the response has status code 404
|
# Check that the response has status code 404
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
|
@ -136,21 +130,29 @@ class CsvReportsTest(TestCase):
|
||||||
|
|
||||||
def test_not_found_federal_report(self):
|
def test_not_found_federal_report(self):
|
||||||
"""Ensures that we get a not found when the report doesn't exist"""
|
"""Ensures that we get a not found when the report doesn't exist"""
|
||||||
response = self.client.get('/api/v1/get-report/current-federal')
|
response = self.client.get("/api/v1/get-report/current-federal")
|
||||||
|
|
||||||
# Check that the response has status code 404
|
# Check that the response has status code 404
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
# Check that the response body contains "File not found"
|
# Check that the response body contains "File not found"
|
||||||
self.assertEqual(response.content.decode(), "File not found")
|
self.assertEqual(response.content.decode(), "File not found")
|
||||||
|
|
||||||
def test_federal_report(self):
|
def test_load_federal_report(self):
|
||||||
"""Ensures that we get a not found when the report doesn't exist"""
|
"""Tests the current-federal api link"""
|
||||||
response = self.client.get('/api/v1/get-report/current-federal')
|
self.maxDiff = None
|
||||||
|
request = self.factory.get("/fake-path")
|
||||||
|
response = get_current_federal(request, file_path="registrar/tests/data/fake_current_federal.csv")
|
||||||
|
# Check that the response has status code 200
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
# Check that the response contains what we expect
|
||||||
|
file_content = b"".join(response.streaming_content).decode("utf-8")
|
||||||
|
expected_file_content = (
|
||||||
|
f"Domain name,Domain type,Agency,Organization name,City,State,Security Contact Email{linesep}"
|
||||||
|
f"cdomain1.gov,Federal - Executive,World War I Centennial Commission,,,, {linesep}"
|
||||||
|
f"ddomain3.gov,Federal,Armed Forces Retirement Home,,,, {linesep}"
|
||||||
|
)
|
||||||
|
|
||||||
# Check that the response has status code 404
|
self.assertEqual(file_content, expected_file_content)
|
||||||
self.assertEqual(response.status_code, 404)
|
|
||||||
# Check that the response body contains "File not found"
|
|
||||||
self.assertEqual(response.content.decode(), "File not found")
|
|
||||||
|
|
||||||
|
|
||||||
class ExportDataTest(TestCase):
|
class ExportDataTest(TestCase):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue