Skip to content

Commit

Permalink
Restore python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Oct 17, 2023
1 parent 54decee commit fcff40d
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 6 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added chat/test/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions chat/test/fixtures/apitoken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TEST_SECRET = "TEST_SECRET"
TEST_TOKEN_NAME = "dcTestToken"
TEST_TOKEN = ('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NDM1ODY2MDYxNjUs'
'ImlhdCI6MTY4Nzg5MTM2OSwiZW50aXRsZW1lbnRzIjpbXSwiaXNMb2dnZWRJbiI6d'
'HJ1ZSwic3ViIjoidGVzdFVzZXIifQ.vIZag1pHE1YyrxsKKlakXX_44ckAvkg7xWOoA_w4x58')
56 changes: 56 additions & 0 deletions chat/test/fixtures/events.py
Original file line number Diff line number Diff line change
@@ -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}")
Empty file added chat/test/handlers/__init__.py
Empty file.
Empty file added chat/test/helpers/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions chat/test/helpers/test_apitoken.py
Original file line number Diff line number Diff line change
@@ -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())

2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit fcff40d

Please sign in to comment.