зеркало из https://github.com/mozilla/MozDef.git
Merge pull request #1543 from mozilla/test-rest-api
Set up a Mongo Client to test REST API endpoints with
This commit is contained in:
Коммит
c21df20059
1
Makefile
1
Makefile
|
@ -49,6 +49,7 @@ tests: build-tests run-tests ## Run all tests (getting/building images as neede
|
|||
run-tests-resources-external: ## Just spin up external resources for tests and have them listen externally
|
||||
docker-compose -f docker/compose/docker-compose-tests.yml -p test-$(NAME) run -p 9200:9200 -d elasticsearch
|
||||
docker-compose -f docker/compose/docker-compose-tests.yml -p test-$(NAME) run -p 5672:5672 -d rabbitmq
|
||||
docker-compose -f docker/compose/docker-compose-tests.yml -p test-$(NAME) run -p 3002:3002 -d mongodb
|
||||
|
||||
.PHONY: run-tests-resources
|
||||
run-tests-resources: ## Just run the external resources required for tests
|
||||
|
|
|
@ -19,6 +19,24 @@ services:
|
|||
# - 9200:9200
|
||||
networks:
|
||||
- default
|
||||
mongodb:
|
||||
image: mozdef/mozdef_mongodb
|
||||
build:
|
||||
context: ../../
|
||||
dockerfile: docker/compose/mongodb/Dockerfile
|
||||
cache_from:
|
||||
- mozdef_mongodb:latest
|
||||
- mozdef_base:latest
|
||||
- mozdef/mongodb
|
||||
- mozdef/mozdef_base
|
||||
depends_on:
|
||||
- base
|
||||
restart: always
|
||||
command: ["bin/mongod", "--config", "/etc/mongod.conf"]
|
||||
ports:
|
||||
- 3002:3002
|
||||
networks:
|
||||
- default
|
||||
rabbitmq:
|
||||
image: mozdef/mozdef_rabbitmq
|
||||
build:
|
||||
|
|
|
@ -7,3 +7,5 @@ mqserver=rabbitmq
|
|||
mqport=5672
|
||||
alertexchange=alerts
|
||||
alertqueue=mozdef.alert
|
||||
mongohost=mongodb
|
||||
mongoport=3002
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
[options]
|
||||
esservers=http://localhost:9200
|
||||
mongohost=localhost
|
||||
mongoport=3002
|
||||
listen_host=0.0.0.0
|
||||
|
|
|
@ -7,3 +7,5 @@ mqalertserver=localhost
|
|||
mqserver=localhost
|
||||
alertexchange=alerts
|
||||
alertqueue=mozdef.alert
|
||||
mongohost=localhost
|
||||
mongoport=3002
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
[options]
|
||||
esservers=http://localhost:9200
|
||||
mongohost=localhost
|
||||
mongoport=3002
|
||||
listen_host=0.0.0.0
|
||||
|
|
|
@ -3,6 +3,7 @@ import sys
|
|||
import mock
|
||||
import importlib
|
||||
from configlib import OptionParser
|
||||
from pymongo import MongoClient
|
||||
|
||||
from mozdef_util.utilities.dot_dict import DotDict
|
||||
|
||||
|
@ -30,4 +31,8 @@ class RestTestSuite(HTTPTestSuite):
|
|||
importlib.reload(plugins)
|
||||
from rest import index as rest_index
|
||||
self.application = rest_index.application
|
||||
|
||||
super().setup()
|
||||
|
||||
self.mongoclient = MongoClient(
|
||||
self.options.mongohost, self.options.mongoport)
|
||||
|
|
|
@ -16,6 +16,35 @@ from dateutil.parser import parse
|
|||
from .rest_test_suite import RestTestSuite
|
||||
|
||||
|
||||
class TestMongoConnection(RestTestSuite):
|
||||
routes = ['/test', '/test/']
|
||||
status_code = 200
|
||||
|
||||
def setup(self):
|
||||
super().setup()
|
||||
self.mongoclient.test_database.tests.insert_one({
|
||||
'name': 'test_item_1',
|
||||
'value': 32
|
||||
})
|
||||
|
||||
def teardown(self):
|
||||
super().teardown()
|
||||
self.mongoclient.test_database.tests.delete_one({
|
||||
'name': 'test_item_1'
|
||||
})
|
||||
|
||||
def test_route_endpoints(self):
|
||||
db = self.mongoclient.test_database
|
||||
|
||||
db.tests.update_one(
|
||||
{'name': 'test_item_1'},
|
||||
{'$inc': {'value': 32}})
|
||||
|
||||
found = db.tests.find_one({'name': 'test_item_1'})
|
||||
|
||||
assert found.get('value') == 64
|
||||
|
||||
|
||||
class TestTestRoute(RestTestSuite):
|
||||
routes = ['/test', '/test/']
|
||||
|
||||
|
@ -284,6 +313,7 @@ class TestLoginCountsRoute(RestTestSuite):
|
|||
assert type(json_resp[2]['end']) == str
|
||||
assert parse(json_resp[2]['begin']).tzname() == 'UTC'
|
||||
|
||||
|
||||
# Routes left need to have unit tests written for:
|
||||
# @route('/veris')
|
||||
# @route('/veris/')
|
||||
|
|
|
@ -43,6 +43,9 @@ def parse_config_file():
|
|||
options.mqport = getConfig('mqport', 5672, options.configfile)
|
||||
options.mqack = getConfig('mqack', True, options.configfile)
|
||||
|
||||
options.mongohost = getConfig('mongohost', 'localhost', options.configfile)
|
||||
options.mongoport = getConfig('mongoport', 3002, options.configfile)
|
||||
|
||||
CONFIG_FILE_CONTENTS = options
|
||||
|
||||
return CONFIG_FILE_CONTENTS
|
||||
|
|
Загрузка…
Ссылка в новой задаче