Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhwaniartefact committed Jun 6, 2024
1 parent f2bfec1 commit b8d8592
Showing 1 changed file with 72 additions and 152 deletions.
224 changes: 72 additions & 152 deletions tests/test_fixity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import calendar
import json
import sys
import uuid
from datetime import datetime
from unittest import mock
Expand All @@ -17,34 +18,37 @@
STORAGE_SERVICE_USER = "test"
STORAGE_SERVICE_KEY = "dfe83300db5f05f63157f772820bb028bd4d0e27"

FOOBAR = mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {"files": {"missing": [], "changed": [], "untracked": []}},
"timestamp": None,
},
},
spec=requests.Response,
)

@mock.patch(
"requests.get",
side_effect=[

@pytest.fixture
def foobar():
return [
mock.Mock(
**{
"status_code": 200,
"json.return_value": {},
},
spec=requests.Response,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {
"files": {"missing": [], "changed": [], "untracked": []}
},
"timestamp": None,
},
},
spec=requests.Response,
),
],
)
def test_scan(_get):
FOOBAR,
]


@mock.patch("requests.get")
def test_scan(_get, foobar):
_get.side_effect = foobar
aip_id = uuid.uuid4()

response = fixity.scan(
Expand All @@ -71,29 +75,6 @@ def test_scan(_get):

@mock.patch(
"requests.get",
side_effect=[
mock.Mock(
**{
"status_code": 200,
"json.return_value": {},
},
spec=requests.Response,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {
"files": {"missing": [], "changed": [], "untracked": []}
},
"timestamp": None,
},
},
spec=requests.Response,
),
],
)
@mock.patch(
"requests.post",
Expand All @@ -102,7 +83,8 @@ def test_scan(_get):
mock.Mock(status_code=201, spec=requests.Response),
],
)
def test_scan_if_report_url_exist(_post: mock.Mock, _get: mock.Mock):
def test_scan_if_report_url_exists(_post, _get, foobar):
_get.side_effect = foobar
aip_id = uuid.uuid4()

response = fixity.scan(
Expand Down Expand Up @@ -155,29 +137,6 @@ def test_scan_if_report_url_exist(_post: mock.Mock, _get: mock.Mock):

@mock.patch(
"requests.get",
side_effect=[
mock.Mock(
**{
"status_code": 200,
"json.return_value": {},
},
spec=requests.Response,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {
"files": {"missing": [], "changed": [], "untracked": []}
},
"timestamp": None,
},
},
spec=requests.Response,
),
],
)
@mock.patch(
"requests.post",
Expand All @@ -194,9 +153,8 @@ def test_scan_if_report_url_exist(_post: mock.Mock, _get: mock.Mock):
),
],
)
def test_scan_handle_exceptions_if_report_url_exist(
_post: mock.Mock, _get: mock.Mock, capsys
):
def test_scan_handles_exceptions_if_report_url_exist(_post, _get, capsys, foobar):
_get.side_effect = foobar
aip_id = uuid.uuid4()

response = fixity.scan(
Expand Down Expand Up @@ -234,7 +192,7 @@ def test_scan_handle_exceptions_if_report_url_exist(
),
],
)
def test_scan_handle_exceptions(_get, capsys):
def test_scan_handles_exceptions(_get, capsys):
aip_id = uuid.uuid4()

response = fixity.scan(
Expand Down Expand Up @@ -273,7 +231,7 @@ def test_scan_handle_exceptions(_get, capsys):
),
],
)
def test_scan_handle_exceptions_if_no_scan_attempted(_get):
def test_scan_handles_exceptions_if_no_scan_attempted(_get):
aip_id = uuid.uuid4()

response = fixity.scan(
Expand Down Expand Up @@ -311,7 +269,11 @@ def test_scan_message(status, error_message):

@mock.patch(
"requests.get",
side_effect=[
)
def test_scanall(_get, capsys, foobar):
aip1_uuid = "41e12f76-354e-402d-85ee-f812cb72f6e6"
aip2_uuid = "807ecfb7-08b1-4435-87ec-5c6bfbe62225"
_get.side_effect = [
mock.Mock(
**{
"status_code": 200,
Expand All @@ -321,63 +283,22 @@ def test_scan_message(status, error_message):
{
"package_type": "AIP",
"status": "UPLOADED",
"uuid": str(uuid.uuid4()),
"uuid": aip1_uuid,
},
{
"package_type": "AIP",
"status": "UPLOADED",
"uuid": str(uuid.uuid4()),
"uuid": aip2_uuid,
},
],
},
},
spec=requests.Response,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {},
},
spec=requests.Response,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {
"files": {"missing": [], "changed": [], "untracked": []}
},
"timestamp": None,
},
},
spec=requests.Response,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {},
},
spec=requests.Response,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {
"files": {"missing": [], "changed": [], "untracked": []}
},
"timestamp": None,
},
},
spec=requests.Response,
),
],
)
def test_scanall(_get, capsys):
*foobar,
*foobar,
]

response = fixity.scanall(
ss_url=STORAGE_SERVICE_URL,
ss_user=STORAGE_SERVICE_USER,
Expand All @@ -388,7 +309,14 @@ def test_scanall(_get, capsys):
assert response is True

captured = capsys.readouterr()
assert "Successfully scanned 2 AIPs" in captured.err
expected_output = "\n".join(
[
f"Fixity scan succeeded for AIP: {aip1_uuid}",
f"Fixity scan succeeded for AIP: {aip2_uuid}",
"Successfully scanned 2 AIPs",
]
)
assert expected_output == captured.err.strip()


@mock.patch(
Expand All @@ -403,12 +331,12 @@ def test_scanall(_get, capsys):
{
"package_type": "AIP",
"status": "UPLOADED",
"uuid": str(uuid.uuid4()),
"uuid": "77adb748-8d9c-47ec-b593-53465749ce0e",
},
{
"package_type": "AIP",
"status": "UPLOADED",
"uuid": str(uuid.uuid4()),
"uuid": "32f62f8b-ecfd-419e-a3e9-911ec23d0573",
},
],
},
Expand All @@ -423,20 +351,7 @@ def test_scanall(_get, capsys):
spec=requests.Response,
side_effect=Exception,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {
"files": {"missing": [], "changed": [], "untracked": []}
},
"timestamp": None,
},
},
spec=requests.Response,
),
FOOBAR,
mock.Mock(
**{
"status_code": 401,
Expand All @@ -445,23 +360,10 @@ def test_scanall(_get, capsys):
spec=requests.Response,
side_effect=Exception,
),
mock.Mock(
**{
"status_code": 200,
"json.return_value": {
"success": True,
"message": "",
"failures": {
"files": {"missing": [], "changed": [], "untracked": []}
},
"timestamp": None,
},
},
spec=requests.Response,
),
FOOBAR,
],
)
def test_scanall_handle_exceptions(_get):
def test_scanall_handles_exceptions(_get, capsys):
response = fixity.scanall(
ss_url=STORAGE_SERVICE_URL,
ss_user=STORAGE_SERVICE_USER,
Expand All @@ -470,3 +372,21 @@ def test_scanall_handle_exceptions(_get):
)

assert response is False

captured = capsys.readouterr()
assert (
"Internal error encountered while scanning AIP 77adb748-8d9c-47ec-b593-53465749ce0e (StorageServiceError)\n"
in captured.out
)

@mock.patch("requests.get")
@mock.patch("fixity.fixity.main.session", return_value = SESSION)
def test_main(_session,_get,monkeypatch,foobar):
_get.side_effect = foobar
monkeypatch.setenv("STORAGE_SERVICE_URL",STORAGE_SERVICE_URL)
monkeypatch.setenv("STORAGE_SERVICE_USER",STORAGE_SERVICE_USER)
monkeypatch.setenv("STORAGE_SERVICE_KEY",STORAGE_SERVICE_KEY)
aip = str(uuid.uuid4())
sys.argv = ['fixity.py','scan',aip]
result = fixity.main()
assert result == 0

0 comments on commit b8d8592

Please sign in to comment.