diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml new file mode 100644 index 0000000..3af52f8 --- /dev/null +++ b/.github/workflows/e2e.yml @@ -0,0 +1,46 @@ +name: e2e tests + +on: [pull_request] + +jobs: + unittest: + runs-on: ubuntu-latest + env: + YETI_ENDPOINT: 'http://localhost:80' + strategy: + matrix: + os: [ubuntu-latest] + python-version: ["3.10"] + steps: + - uses: actions/checkout@v4 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y python3-pip git + sudo pip3 install poetry + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + cache: poetry + + - name: Install a Yeti prod deployment + run: | + git clone https://github.com/yeti-platform/yeti-docker + cd yeti-docker/prod + ./init.sh + sleep 10 + + - name: Create test Yeti user + run: | + cd yeti-docker/prod + echo "YETI_API_KEY=$(docker compose run --rm api create-user test test --admin | awk -F'test:' '{print $2}')" >> $GITHUB_ENV + + - name: Install Python dependencies + run: poetry install --no-root + + - name: e2e testing + run: | + YETI_API_KEY=$YETI_API_KEY poetry run python -m unittest tests/e2e.py diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml index cf66077..f4cae35 100644 --- a/.github/workflows/unittests.yml +++ b/.github/workflows/unittests.yml @@ -23,4 +23,4 @@ jobs: run: poetry install --no-root - name: Test with unittest run: | - poetry run python -m unittest discover -s tests/ -p '*.py' + poetry run python -m unittest tests/api.py diff --git a/tests/e2e.py b/tests/e2e.py new file mode 100644 index 0000000..c24a5c2 --- /dev/null +++ b/tests/e2e.py @@ -0,0 +1,65 @@ +import os +import time +import unittest +from unittest.mock import MagicMock, patch + +import requests + +from yeti import errors +from yeti.api import YetiApi + + +class YetiEndToEndTest(unittest.TestCase): + def setUp(self): + self.api = YetiApi(os.getenv("YETI_ENDPOINT")) + + def test_auth_api_key(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + self.api.search_indicators(name="test") + + def test_no_auth(self): + with self.assertRaises(errors.YetiAuthError) as error: + self.api.search_indicators(name="test") + self.assertIn( + "401 Client Error: Unauthorized for url: ", + str(error.exception), + ) + + def test_new_indicator(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + indicator = self.api.new_indicator( + { + "name": "test", + "type": "regex", + "description": "test", + "pattern": "test[0-9]", + "diamond": "victim", + } + ) + self.assertEqual(indicator["name"], "test") + self.assertRegex(indicator["id"], r"[0-9]+") + + def test_auth_refresh(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + self.api.search_indicators(name="test") + + time.sleep(3) + + self.api.search_indicators(name="test") + + def test_search_indicators(self): + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + self.api.auth_api_key(os.getenv("YETI_API_KEY")) + indicator = self.api.new_indicator( + { + "name": "testSearch", + "type": "regex", + "description": "test", + "pattern": "test[0-9]", + "diamond": "victim", + } + ) + time.sleep(5) + result = self.api.search_indicators(name="testSear") + self.assertEqual(len(result), 1, result) + self.assertEqual(result[0]["name"], "testSearch")