# Path: multiscanner/modules/Antivirus/Metadefender.py
# TYPE = "Antivirus"
# NAME = "Metadefender"
# DEFAULTCONF = {
#     "ENABLED": False,
#     "API URL": 'http://metadefender:8008/',
#     "timeout": 60,
#     "running timeout": 30,
#     "fetch delay seconds": 5,
#     "poll interval seconds": 5,
#     "user agent": "user_agent",
#     "API key": ""
# }
# PERCENT_SCAN_COMPLETE = 100
# MD_SCAN_RES_CODES = {0: 'No threats Found', 1: 'Infected/Known',
#                      2: 'Suspicious', 3: 'Failed to Scan',
#                      4: 'Cleaned/Deleted', 5: 'Unknown',
#                      6: 'Quarantined', 7: 'Skipped - Clean',
#                      8: 'Skipped - Infected', 9: 'Exceeded Archive Depth',
#                      10: 'Not Scanned/No Scan Results', 11: 'Aborted',
#                      12: 'Encrypted', 13: 'Exceeded Archive Size',
#                      14: 'Exceeded Archive File Number',
#                      15: 'Password Protected', 16: 'Exceeded Archive Timeout'}
# MD_UNKNOWN_SCAN_RES = 5
# MD_HTTP_ERR_CODES = {400: 'Unsupported HTTP Method or invalid HTTP Request',
#                      401: 'API key is missing or invalid',
#                      404: 'Scan result not found',
#                      500: 'Server temporarily unavailable'
#                      }
# UNKNOWN_ERROR = 'Unknown Error'
# STATUS_SUCCESS = 'Success'
# STATUS_FAIL = 'Failure'
# STATUS_PENDING = 'Pending'
# STATUS_TIMEOUT = 'Timeout'
# def check(conf=DEFAULTCONF):
# def _parse_scan_result(response):
# def _submit_sample(fname, scan_url, user_agent, api_key=None):
# def _retrieve_scan_results(results_url, scan_id, api_key=None):
# def scan(filelist, conf=DEFAULTCONF):


import unittest
import os
import mock
import json
from multiscanner.modules.Antivirus import Metadefender
        Tests Metadefender._submit_sample()'s handling of a successful response from
        the server
        '''
        print('Running test_submit_sample_success')
        submit_resp = Metadefender._submit_sample(RANDOM_INPUT_FILES[0], 'scan_url', 'user_agent')
        self.assertEquals(submit_resp['status_code'], 200)
        self.assertEquals(submit_resp['error'], None)
        self.assertEquals(submit_resp['scan_id'], generate_scan_id(RANDOM_INPUT_FILES[0]))

    @mock.patch('Metadefender.requests.post', side_effect=mocked_requests_post_sample_failed_w_msg)
    def test_submit_sample_fail_unavailable(self, mock_get):
        '''
        Tests Metadefender._submit_sample()'s handling of a submission that fails due to
        a 500 error
        '''
        print('Running test_submit_sample_fail_unavailable')
        submit_resp = Metadefender._submit_sample(RANDOM_INPUT_FILES[1], 'scan_url', 'user_agent')
        self.assertEquals(submit_resp['status_code'], 500)
        self.assertEquals(submit_resp['error'], MSG_SERVER_UNAVAILABLE)
        self.assertEquals(submit_resp['scan_id'], None)

    @mock.patch('Metadefender.requests.post', side_effect=mocked_requests_post_sample_failed_no_msg)
    def test_submit_sample_fail_unavailable_no_msg(self, mock_get):
        '''
        Tests Metadefender._submit_sample()'s handling of a submission that fails due to
        a 500 error and where no message is returned
        '''
        print('Running test_submit_sample_fail_unavailable_no_msg')
        submit_resp = Metadefender._submit_sample(RANDOM_INPUT_FILES[1], 'scan_url', 'user_agent')
        self.assertEquals(submit_resp['status_code'], 500)

# Path: multiscanner/modules/Antivirus/Metadefender.py
# TYPE = "Antivirus"
# NAME = "Metadefender"
# DEFAULTCONF = {
#     "ENABLED": False,
#     "API URL": 'http://metadefender:8008/',
#     "timeout": 60,
#     "running timeout": 30,
#     "fetch delay seconds": 5,
#     "poll interval seconds": 5,
#     "user agent": "user_agent",
#     "API key": ""
# }
# PERCENT_SCAN_COMPLETE = 100
# MD_SCAN_RES_CODES = {0: 'No threats Found', 1: 'Infected/Known',
#                      2: 'Suspicious', 3: 'Failed to Scan',
#                      4: 'Cleaned/Deleted', 5: 'Unknown',
#                      6: 'Quarantined', 7: 'Skipped - Clean',
#                      8: 'Skipped - Infected', 9: 'Exceeded Archive Depth',
#                      10: 'Not Scanned/No Scan Results', 11: 'Aborted',
#                      12: 'Encrypted', 13: 'Exceeded Archive Size',
#                      14: 'Exceeded Archive File Number',
#                      15: 'Password Protected', 16: 'Exceeded Archive Timeout'}
# MD_UNKNOWN_SCAN_RES = 5
# MD_HTTP_ERR_CODES = {400: 'Unsupported HTTP Method or invalid HTTP Request',
#                      401: 'API key is missing or invalid',
#                      404: 'Scan result not found',
#                      500: 'Server temporarily unavailable'
#                      }
# UNKNOWN_ERROR = 'Unknown Error'
# STATUS_SUCCESS = 'Success'
# STATUS_FAIL = 'Failure'
# STATUS_PENDING = 'Pending'
# STATUS_TIMEOUT = 'Timeout'
# def check(conf=DEFAULTCONF):
# def _parse_scan_result(response):
# def _submit_sample(fname, scan_url, user_agent, api_key=None):
# def _retrieve_scan_results(results_url, scan_id, api_key=None):
# def scan(filelist, conf=DEFAULTCONF):


# Path: multiscanner/tests/module_tests/Metadefender/test_metadefender_module.py
import unittest
import os
import mock
import json
from multiscanner.modules.Antivirus import Metadefender

        Tests Metadefender._submit_sample()'s handling of a successful response from
        the server
        '''
        print('Running test_submit_sample_success')
        submit_resp = Metadefender._submit_sample(RANDOM_INPUT_FILES[0], 'scan_url', 'user_agent')
        self.assertEquals(submit_resp['status_code'], 200)
        self.assertEquals(submit_resp['error'], None)
        self.assertEquals(submit_resp['scan_id'], generate_scan_id(RANDOM_INPUT_FILES[0]))

    @mock.patch('Metadefender.requests.post', side_effect=mocked_requests_post_sample_failed_w_msg)
    def test_submit_sample_fail_unavailable(self, mock_get):
        '''
        Tests Metadefender._submit_sample()'s handling of a submission that fails due to
        a 500 error
        '''
        print('Running test_submit_sample_fail_unavailable')
        submit_resp = Metadefender._submit_sample(RANDOM_INPUT_FILES[1], 'scan_url', 'user_agent')
        self.assertEquals(submit_resp['status_code'], 500)
        self.assertEquals(submit_resp['error'], MSG_SERVER_UNAVAILABLE)
        self.assertEquals(submit_resp['scan_id'], None)

    @mock.patch('Metadefender.requests.post', side_effect=mocked_requests_post_sample_failed_no_msg)
    def test_submit_sample_fail_unavailable_no_msg(self, mock_get):
        '''
        Tests Metadefender._submit_sample()'s handling of a submission that fails due to
        a 500 error and where no message is returned
        '''
        print('Running test_submit_sample_fail_unavailable_no_msg')
        submit_resp = Metadefender._submit_sample(RANDOM_INPUT_FILES[1], 'scan_url', 'user_agent')
        self.assertEquals(submit_resp['status_code'], 500)

        self.assertEquals(submit_resp['error'], Metadefender.MD_HTTP_ERR_CODES[500])