diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ad7db8d0..f82cddb5 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -43,10 +43,8 @@ jobs: path: ".tfvars" - uses: actions/setup-python@v2 with: - python-version: '3.9' + python-version: '3.10' - uses: aws-actions/setup-sam@v1 - - name: sam fix https://github.com/aws/aws-sam-cli/issues/4527 - run: $(dirname $(readlink $(which sam)))/pip install --force-reinstall "cryptography==38.0.4" - uses: aws-actions/configure-aws-credentials@master with: role-to-assume: arn:aws:iam::${{ secrets.AwsAccount }}:role/github-actions-role diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 64ec6eaa..ff995d1c 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -3,11 +3,11 @@ on: push: paths: - ".github/workflows/test-python.yml" - - "python/**" + - "chat/**" workflow_dispatch: defaults: run: - working-directory: ./python + working-directory: ./chat jobs: test: runs-on: ubuntu-latest @@ -19,8 +19,9 @@ jobs: - uses: actions/setup-python@v4 with: python-version: '3.9' - cache-dependency-path: python/requirements.txt + cache-dependency-path: chat/src/requirements.txt - run: pip install -r requirements.txt + working-directory: ./chat/src - name: Check code style run: ruff check . - name: Run tests diff --git a/Makefile b/Makefile index adf43162..41f41972 100644 --- a/Makefile +++ b/Makefile @@ -37,9 +37,9 @@ deps-python: cover-python: cd chat/src && coverage run --include='src/**/*' -m unittest -v && coverage report style-python: - cd chat/src && ruff check . + cd chat && ruff check . test-python: - cd chat/src && python -m unittest -v + cd chat && python -m unittest -v build: .aws-sam/build.toml link: build cd chat/src && for src in *.py **/*.py; do for target in $$(find ../../.aws-sam/build -maxdepth 1 -type d); do if [[ -f $$target/$$src ]]; then ln -f $$src $$target/$$src; fi; done; done diff --git a/chat/src/requirements.txt b/chat/src/requirements.txt index 68ec56c3..aa6d612d 100644 --- a/chat/src/requirements.txt +++ b/chat/src/requirements.txt @@ -1,3 +1,4 @@ +# Runtime Dependencies langchain~=0.0.208 nbformat~=5.9.0 openai~=0.27.8 @@ -6,4 +7,8 @@ pyjwt~=2.6.0 python-dotenv~=1.0.0 tiktoken~=0.4.0 weaviate-client~=3.19.2 -wheel~=0.40.0 \ No newline at end of file +wheel~=0.40.0 + +# Dev/Test Dependencies +ruff~=0.1.0 +coverage~=7.3.2 diff --git a/chat/test/__init__.py b/chat/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/chat/test/fixtures/apitoken.py b/chat/test/fixtures/apitoken.py new file mode 100644 index 00000000..0f61693c --- /dev/null +++ b/chat/test/fixtures/apitoken.py @@ -0,0 +1,5 @@ +TEST_SECRET = "TEST_SECRET" +TEST_TOKEN_NAME = "dcTestToken" +TEST_TOKEN = ('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NDM1ODY2MDYxNjUs' + 'ImlhdCI6MTY4Nzg5MTM2OSwiZW50aXRsZW1lbnRzIjpbXSwiaXNMb2dnZWRJbiI6d' + 'HJ1ZSwic3ViIjoidGVzdFVzZXIifQ.vIZag1pHE1YyrxsKKlakXX_44ckAvkg7xWOoA_w4x58') diff --git a/chat/test/fixtures/events.py b/chat/test/fixtures/events.py new file mode 100644 index 00000000..1b275be7 --- /dev/null +++ b/chat/test/fixtures/events.py @@ -0,0 +1,56 @@ +from copy import deepcopy +from test.fixtures.apitoken import TEST_TOKEN_NAME, TEST_TOKEN + +POST_EVENT = { + "version": "2.0", + "routeKey": "$default", + "rawPath": "/chat", + "cookies": [ + "cookie_1=cookie_value_1", + "cookie_2=cookie_value_2", + ], + "headers": { + "Authorization": f"Bearer {TEST_TOKEN}", + "origin": "https://example.edu" + }, + "queryStringParameters": { + "param1": "value1", + "param2": "value2", + }, + "requestContext": { + "accountId": "123456789012", + "apiId": "api-id", + "domainName": "id.execute-api.us-east-1.amazonaws.com", + "domainPrefix": "id", + "http": { + "method": "POST", + "path": "/chat", + "protocol": "HTTP/1.1", + "sourceIp": "192.168.0.1/32", + "userAgent": "agent" + }, + "requestId": "id", + "routeKey": "$default", + "stage": "$default", + "time": "12/Mar/2020:19:03:58 +0000", + "timeEpoch": 1583348638390 + }, + "body": "UE9TVGVkIENvbnRlbnQ=", + "pathParameters": {}, + "isBase64Encoded": True, + "stageVariables": {} +} + +PLAIN_BODY_EVENT = deepcopy(POST_EVENT) +PLAIN_BODY_EVENT["isBase64Encoded"] = False +PLAIN_BODY_EVENT["body"] = "POSTed Content" + +NO_BODY_EVENT = deepcopy(POST_EVENT) +NO_BODY_EVENT["isBase64Encoded"] = False +NO_BODY_EVENT["body"] = "" + +NO_TOKEN_EVENT = deepcopy(POST_EVENT) +del NO_TOKEN_EVENT["headers"]["Authorization"] + +COOKIE_TOKEN_EVENT = deepcopy(NO_TOKEN_EVENT) +COOKIE_TOKEN_EVENT["cookies"].append(f"{TEST_TOKEN_NAME}={TEST_TOKEN}") diff --git a/chat/test/handlers/__init__.py b/chat/test/handlers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/chat/test/helpers/__init__.py b/chat/test/helpers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/chat/test/helpers/test_apitoken.py b/chat/test/helpers/test_apitoken.py new file mode 100644 index 00000000..ce6b6ae0 --- /dev/null +++ b/chat/test/helpers/test_apitoken.py @@ -0,0 +1,24 @@ +import os +from src.helpers.apitoken import ApiToken +from test.fixtures.apitoken import TEST_SECRET, TEST_TOKEN +from unittest import mock, TestCase + +@mock.patch.dict( + os.environ, + { + "API_TOKEN_SECRET": TEST_SECRET + } +) +class TestFunction(TestCase): + def test_empty_token(self): + subject = ApiToken() + self.assertFalse(subject.is_logged_in()) + + def test_valid_token(self): + subject = ApiToken(TEST_TOKEN) + self.assertTrue(subject.is_logged_in()) + + def test_invalid_token(self): + subject = ApiToken("INVALID_TOKEN") + self.assertFalse(subject.is_logged_in()) + \ No newline at end of file diff --git a/node/package.json b/node/package.json index a0ae8c06..385a8d0a 100644 --- a/node/package.json +++ b/node/package.json @@ -10,7 +10,7 @@ }, "scripts": { "lint": "eslint src/**/*.js test/**/*.js", - "preinstall": "cd src && npm i && cd ../lambdas && npm i && cd ../", + "preinstall": "cd src && npm i && cd - && cd ../lambdas && npm i && cd -", "prettier": "prettier -c src test", "prettier:fix": "prettier -cw src test", "test": "mocha",