Settings, Themes and Migration Route Tests (#100)

* dev-bug: fixed vscode freezes

* test: refactor database init to support tests

* mealplan CRUD testing

* restructure test folder

* git attributes

* tests: migration, settings, theme routes testing

Co-authored-by: Hayden <hay-kot@pm.me>
This commit is contained in:
Hayden
2021-01-19 15:51:36 -09:00
committed by GitHub
parent 22a517b9c0
commit 29db7f8a67
40 changed files with 367 additions and 93 deletions

View File

@@ -1,67 +1,78 @@
import json
import pytest
from tests.test_routes.utils.routes_data import recipe_test_data
def cleanup(api_client):
api_client.delete(f"/api/recipe/{recipe_test_data[0].expected_slug}/delete/")
api_client.delete(f"/api/recipe/{recipe_test_data[1].expected_slug}/delete/")
def get_meal_plan_template(first=None, second=None):
return {
"startDate": "2021-01-18",
"endDate": "2021-01-19",
"meals": [
{
"slug": first,
"date": "2021-1-17",
"dateText": "Monday, January 18, 2021",
},
{
"slug": second,
"date": "2021-1-18",
"dateText": "Tueday, January 19, 2021",
},
],
}
meal_plan = {
"startDate": "2021-01-18",
"endDate": "2021-01-19",
"meals": [
{
"slug": None,
"date": "2021-1-17",
"dateText": "Monday, January 18, 2021",
},
{
"slug": None,
"date": "2021-1-18",
"dateText": "Tueday, January 19, 2021",
},
],
}
def test_create_mealplan(api_client):
@pytest.fixture
def slug_1(api_client):
# Slug 1
slug_1 = api_client.post(
"/api/recipe/create-url/", json={"url": recipe_test_data[0].url}
)
slug_1 = json.loads(slug_1.content)
yield slug_1
api_client.delete(f"/api/recipe/{recipe_test_data[1].expected_slug}/delete/")
@pytest.fixture
def slug_2(api_client):
# Slug 2
slug_2 = api_client.post(
"/api/recipe/create-url/", json={"url": recipe_test_data[1].url}
)
slug_2 = json.loads(slug_2.content)
meal_plan["meals"][0]["slug"] = json.loads(slug_1.content)
meal_plan["meals"][1]["slug"] = json.loads(slug_2.content)
yield slug_2
api_client.delete(f"/api/recipe/{recipe_test_data[0].expected_slug}/delete/")
def test_create_mealplan(api_client, slug_1, slug_2):
meal_plan = get_meal_plan_template()
meal_plan["meals"][0]["slug"] = slug_1
meal_plan["meals"][1]["slug"] = slug_2
response = api_client.post("/api/meal-plan/create/", json=meal_plan)
assert response.status_code == 200
def test_read_mealplan(api_client):
def test_read_mealplan(api_client, slug_1, slug_2):
response = api_client.get("/api/meal-plan/all/")
assert response.status_code == 200
meal_plan = get_meal_plan_template(slug_1, slug_2)
new_meal_plan = json.loads(response.text)
meals = new_meal_plan[0]["meals"]
assert meals[0]["slug"] == meal_plan["meals"][0]["slug"]
assert meals[1]["slug"] == meal_plan["meals"][1]["slug"]
cleanup(api_client)
def test_update_mealplan(api_client):
slug_1 = api_client.post(
"/api/recipe/create-url/", json={"url": recipe_test_data[0].url}
)
slug_2 = api_client.post(
"/api/recipe/create-url/", json={"url": recipe_test_data[1].url}
)
def test_update_mealplan(api_client, slug_1, slug_2):
response = api_client.get("/api/meal-plan/all/")
@@ -70,8 +81,8 @@ def test_update_mealplan(api_client):
## Swap
plan_uid = existing_mealplan.get("uid")
existing_mealplan["meals"][0]["slug"] = json.loads(slug_2.content)
existing_mealplan["meals"][1]["slug"] = json.loads(slug_1.content)
existing_mealplan["meals"][0]["slug"] = slug_2
existing_mealplan["meals"][1]["slug"] = slug_1
response = api_client.post(
f"/api/meal-plan/{plan_uid}/update/", json=existing_mealplan
@@ -83,10 +94,8 @@ def test_update_mealplan(api_client):
existing_mealplan = json.loads(response.text)
existing_mealplan = existing_mealplan[0]
assert existing_mealplan["meals"][0]["slug"] == json.loads(slug_2.content)
assert existing_mealplan["meals"][1]["slug"] == json.loads(slug_1.content)
cleanup(api_client)
assert existing_mealplan["meals"][0]["slug"] == slug_2
assert existing_mealplan["meals"][1]["slug"] == slug_1
def test_delete_mealplan(api_client):

View File

@@ -0,0 +1,74 @@
import json
import shutil
from pathlib import Path
from app_config import BASE_DIR, MIGRATION_DIR
from tests.test_services.test_migrations.test_nextcloud import NEXTCLOUD_DIR
TEST_DIR = BASE_DIR.joinpath("tests")
import pytest
#! Broken
# def test_import_chowdown_recipes(api_client):
# response = api_client.post(
# "/api/migration/chowdown/repo/",
# json={"url": "https://github.com/hay-kot/chowdown"},
# )
# assert response.status_code == 200
# test_slug = "banana-bread"
# response = api_client.get(f"/api/recipe/{test_slug}/")
# assert response.status_code == 200
# recipe = json.loads(response.content)
# assert recipe["slug"] == test_slug
@pytest.fixture(scope="session")
def nextcloud_zip():
zip = TEST_DIR.joinpath(
"test_services/test_migrations/data/nextcloud_recipes/nextcloud.zip"
)
zip_copy = TEST_DIR.joinpath(
"test_services/test_migrations/data/nextcloud_recipes/new_nextcloud.zip"
)
shutil.copy(zip, zip_copy)
return zip_copy
def test_upload_nextcloud_zip(api_client, nextcloud_zip):
response = api_client.post(
"/api/migration/upload/", files={"archive": nextcloud_zip.open("rb")}
)
assert response.status_code == 200
assert MIGRATION_DIR.joinpath(nextcloud_zip.name).is_file()
def test_import_nextcloud_directory(api_client, nextcloud_zip):
selection = nextcloud_zip.name
response = api_client.post(f"/api/migration/nextcloud/{selection}/import/")
assert response.status_code == 200
report = json.loads(response.content)
assert report["failed"] == []
expected_slug = "air-fryer-shrimp"
response = api_client.get(f"/api/recipe/{expected_slug}/")
assert response.status_code == 200
def test_delete_migration_data(api_client, nextcloud_zip):
selection = nextcloud_zip.name
response = api_client.delete(f"/api/migration/{selection}/delete/")
assert response.status_code == 200
assert not MIGRATION_DIR.joinpath(nextcloud_zip.name).is_file()

View File

@@ -0,0 +1,108 @@
import json
import pytest
@pytest.fixture(scope="function")
def default_settings():
return {
"name": "main",
"webhooks": {"webhookTime": "00:00", "webhookURLs": [], "enabled": False},
}
@pytest.fixture(scope="session")
def default_theme(api_client):
default_theme = {
"name": "default",
"colors": {
"primary": "#E58325",
"accent": "#00457A",
"secondary": "#973542",
"success": "#5AB1BB",
"info": "#4990BA",
"warning": "#FF4081",
"error": "#EF5350",
},
}
api_client.post("/api/site-settings/themes/create/", json=default_theme)
return default_theme
@pytest.fixture(scope="session")
def new_theme():
return {
"name": "myTestTheme",
"colors": {
"primary": "#E58325",
"accent": "#00457A",
"secondary": "#973542",
"success": "#5AB1BB",
"info": "#4990BA",
"warning": "#FF4081",
"error": "#EF5350",
},
}
def test_default_settings(api_client, default_settings):
response = api_client.get("/api/site-settings/")
assert response.status_code == 200
assert json.loads(response.content) == default_settings
def test_update_settings(api_client, default_settings):
default_settings["webhooks"]["webhookURLs"] = [
"https://test1.url.com",
"https://test2.url.com",
"https://test3.url.com",
]
response = api_client.post("/api/site-settings/update/", json=default_settings)
assert response.status_code == 200
response = api_client.get("/api/site-settings/")
assert json.loads(response.content) == default_settings
def test_default_theme(api_client, default_theme):
response = api_client.get("/api/site-settings/themes/default/")
assert response.status_code == 200
assert json.loads(response.content) == default_theme
def test_create_theme(api_client, new_theme):
response = api_client.post("/api/site-settings/themes/create/", json=new_theme)
assert response.status_code == 200
response = api_client.get(f"/api/site-settings/themes/{new_theme.get('name')}/")
assert response.status_code == 200
assert json.loads(response.content) == new_theme
def test_read_all_themes(api_client, default_theme, new_theme):
response = api_client.get("/api/site-settings/themes/")
assert response.status_code == 200
assert json.loads(response.content) == [default_theme, new_theme]
def test_read_theme(api_client, default_theme, new_theme):
for theme in [default_theme, new_theme]:
response = api_client.get(f"/api/site-settings/themes/{theme.get('name')}/")
assert response.status_code == 200
assert json.loads(response.content) == theme
def test_delete_theme(api_client, default_theme, new_theme):
for theme in [default_theme, new_theme]:
response = api_client.delete(
f"/api/site-settings/themes/{theme.get('name')}/delete/"
)
assert response.status_code == 200