def test_match_fails(s3, sqs, mocker, kms): with pytest.raises(yara.Error): set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, "test0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesMatchError() matcher.matcher_lambda_handler(get_records(), None)
def test_key_not_found(s3, sqs, mocker, kms): with pytest.raises(ClientError) as err: set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=clean_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, "test0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesNoMatch() matcher.matcher_lambda_handler(get_records(), None) assert err.typename == 'ClientError'
def test_load_is_called(s3, sqs, mocker, kms): set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=quarantine_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, f"{tdr_standard_dirty_key}0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesMatchFound() matcher.matcher_lambda_handler(get_records(), None) yara.load.assert_called_once_with("output")
def test_copy_to_clean_bucket(s3, sqs, s3_client, mocker, kms): set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=clean_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, f"{tdr_standard_dirty_key}0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesNoMatch() matcher.matcher_lambda_handler(get_records(), None) res = s3_client.get_object(Bucket=clean_s3_bucket, Key=f"{tdr_standard_copy_key}0") assert res["Body"].read() == b"test"
def test_output_sent_to_queue(s3, sqs, mocker, kms): set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=quarantine_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, f"{tdr_standard_dirty_key}0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesMatchFound() matcher.matcher_lambda_handler(get_records(), None) res = sqs.receive_message(QueueUrl=output_queue_url) print(res["Messages"]) assert len(res["Messages"]) == 1
def test_no_copy_to_quarantine_clean(s3, sqs, s3_client, mocker, kms): with pytest.raises(ClientError) as err: set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=quarantine_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=clean_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, f"{tdr_standard_dirty_key}0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesNoMatch() matcher.matcher_lambda_handler(get_records(), None) s3_client.get_object(Bucket=quarantine_s3_bucket, Key="consignmentId") assert err.typename == 'NoSuchKey'
def test_multiple_match_found(s3, sqs, mocker, kms): set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=quarantine_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, f"{tdr_standard_dirty_key}0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesMultipleMatchFound() res = matcher.matcher_lambda_handler(get_records(), None) assert res[0]["result"] == "testmatch\ntestmatch"
def test_correct_output(s3, sqs, mocker, kms): set_environment(kms) sqs.create_queue(QueueName=output_sqs_queue) s3.create_bucket(Bucket=dirty_s3_bucket, CreateBucketConfiguration=location) s3.create_bucket(Bucket=quarantine_s3_bucket, CreateBucketConfiguration=location) s3.Object(dirty_s3_bucket, f"{tdr_standard_dirty_key}0").put(Body="test") mocker.patch('yara.load') yara.load.return_value = MockRulesMatchFound() res = matcher.matcher_lambda_handler(get_records(), None) assert res[0]["software"] == "yara" assert res[0]["softwareVersion"] == yara.__version__ assert res[0]["databaseVersion"] == "1"
def test_no_records(): res = matcher.matcher_lambda_handler([], None) assert res == []