Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel committed Jul 30, 2024
1 parent 5e4928d commit 7109d30
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 66 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ build-dev:
build-tests:
docker build . -f Dockerfile.tests --tag openslides-media-tests

build-dummy-presenter:
docker build . -f tests/dummy_presenter/Dockerfile.dummy_presenter --tag openslides-media-dummy-presenter
build-dummy-autoupdate:
docker build . -f tests/dummy_autoupdate/Dockerfile.dummy_autoupdate --tag openslides-media-dummy-autoupdate

start-test-setup: | build-dev build-tests build-dummy-presenter
start-test-setup: | build-dev build-tests build-dummy-autoupdate
docker compose -f docker-compose.test.yml up -d
docker compose -f docker-compose.test.yml exec -T tests wait-for-it "media:9006"

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Delivers media files for OpenSlides. It stores the data in the database.
password (default: `/run/secrets/postgres_password`; in dev mode the password is always assumed to be `openslides`)
- `MEDIA_BLOCK_SIZE`: The size of the blocks, the file is chunked into (default: `4096`)
- `MEDIA_CLIENT_CACHE_DURATION`: The duration in seconds a file should be cached by a client (default: `86400`; disabled when: `0`)
- `AUTOUPDATE_HOST`: Host of the presenter service (default: `autoupdate`)
- `AUTOUPDATE_PORT`: Port of the presenter service (default: `9012`)
- `AUTOUPDATE_HOST`: Host of the autoupdate service (default: `autoupdate`)
- `AUTOUPDATE_PORT`: Port of the autoupdate service (default: `9012`)

## Production setup
Use the provided Dockerfile. It creates the tables in Postgresql, if they don't
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ services:
- ./src:/app/src
ports:
- 9006:9006
dummy_presenter:
image: openslides-media-dummy-presenter
dummy_autoupdate:
image: openslides-media-dummy-autoupdate
volumes:
- ./tests/dummy_presenter:/app/dummy_presenter
- ./tests/dummy_autoupdate:/app/dummy_autoupdate
tests:
image: openslides-media-tests
environment:
- OPENSLIDES_DEVELOPMENT=1
depends_on:
- media
- postgres
- dummy_presenter
- dummy_autoupdate
volumes:
- ./tests:/app/tests
postgres:
Expand Down
8 changes: 4 additions & 4 deletions src/mediaserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def serve(file_id):
return redirect("/")

# get file id
presenter_headers = dict(request.headers)
del_keys = [key for key in presenter_headers if "content" in key]
autoupdate_headers = dict(request.headers)
del_keys = [key for key in autoupdate_headers if "content" in key]
for key in del_keys:
del presenter_headers[key]
ok, filename, auth_header = check_file_id(file_id, presenter_headers)
del autoupdate_headers[key]
ok, filename, auth_header = check_file_id(file_id, autoupdate_headers)
if not ok:
raise NotFoundError()

Expand Down
10 changes: 10 additions & 0 deletions tests/dummy_autoupdate/Dockerfile.dummy_autoupdate
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.10.13-slim-bookworm

WORKDIR /app

RUN pip install flask

ENV FLASK_APP ./dummy_autoupdate/dummy_autoupdate.py
ENV FLASK_ENV development

CMD ["flask", "run", "--host", "0.0.0.0", "--port", "9012"]
62 changes: 62 additions & 0 deletions tests/dummy_autoupdate/dummy_autoupdate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from flask import Flask, jsonify, request

app = Flask(__name__)

app.logger.info("Started Dummy-Autoupdate")


# for testing
@app.route("/internal/autoupdate", methods=["POST"])
def dummy_autoupdate():
app.logger.debug(f"dummy_autoupdate gets: {request.json}")
file_id = request.json[0]["ids"][0]

# Valid response from autoupdate, but not found in DB
if file_id == 1:
return jsonify(
{
f"mediafile/{file_id}/id": file_id,
f"mediafile/{file_id}/filename": "Does not exist",
}
)

# OK-cases for dummy data
if file_id == 2:
return jsonify(
{
f"mediafile/{file_id}/id": file_id,
f"mediafile/{file_id}/filename": "A.txt",
}
)
if file_id == 3:
return jsonify(
{
f"mediafile/{file_id}/id": file_id,
f"mediafile/{file_id}/filename": "in.jpg",
}
)

# OK-cases for uploaded data
if file_id in (4, 5, 6, 7):
return jsonify(
{
f"mediafile/{file_id}/id": file_id,
f"mediafile/{file_id}/filename": str(file_id),
}
)

# invalid responses
if file_id == 10:
return jsonify([None])
if file_id == 11:
return "some text"
if file_id == 12:
return "An error", 500
if file_id == 13:
return []
if file_id == 14:
return jsonify({f"mediafile/{file_id}/id": file_id})

# not found or no perms
if file_id == 20:
return jsonify({})
10 changes: 0 additions & 10 deletions tests/dummy_presenter/Dockerfile.dummy_presenter

This file was deleted.

42 changes: 0 additions & 42 deletions tests/dummy_presenter/dummy_presenter.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_invalid_responses():
assert "message" in response.json()


def test_not_ok_from_presenter():
def test_not_ok_from_autoupdate():
response = get_mediafile(20)
assert response.status_code == 404
assert "message" in response.json()
Expand Down

0 comments on commit 7109d30

Please sign in to comment.