|
| 1 | +from time import sleep |
| 2 | +from uuid import uuid4 |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | +from schema import Schema |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def list_schema(): |
| 11 | + """Response schema""" |
| 12 | + return Schema( |
| 13 | + { |
| 14 | + "lid": str, |
| 15 | + "title": str, |
| 16 | + "host": str, |
| 17 | + "public": bool, |
| 18 | + }, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def lists_schema(list_schema): |
| 24 | + """Response schema""" |
| 25 | + return Schema([list_schema]) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("payload", (None, "", " ", {}, [])) |
| 29 | +def test_lists_put_empty_payload(payload, zod): |
| 30 | + url = "/apps/maat/api/lists" |
| 31 | + response = zod.put(url, json=payload) |
| 32 | + assert response.status_code in (418, 500) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("title", (None, "", " ")) |
| 36 | +def test_lists_put_invalid_title(title, zod): |
| 37 | + list_ = { |
| 38 | + "lid": str(uuid4()), |
| 39 | + "title": title, |
| 40 | + "public": False, |
| 41 | + } |
| 42 | + url = "/apps/maat/api/lists" |
| 43 | + |
| 44 | + # PUT /lists |
| 45 | + response = zod.put(url, json=list_) |
| 46 | + assert response.status_code in (422, 500) |
| 47 | + |
| 48 | + # GET /lists |
| 49 | + response = zod.get(url) |
| 50 | + assert response.status_code == 200 |
| 51 | + result = response.json() |
| 52 | + assert list_ not in result |
| 53 | + |
| 54 | + |
| 55 | +def test_lists_get_all(zod, list_, lists_schema): |
| 56 | + # GET /lists |
| 57 | + url = "/apps/maat/api/lists" |
| 58 | + response = zod.get(url) |
| 59 | + assert response.status_code == 200 |
| 60 | + result = response.json() |
| 61 | + assert lists_schema.is_valid(result) |
| 62 | + assert list_ in result |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.usefixtures("list_") |
| 66 | +def test_lists_get_all_unauthorized(): |
| 67 | + # GET /lists |
| 68 | + url = "http://localhost:8080/apps/maat/api/lists" |
| 69 | + response = requests.get(url) |
| 70 | + assert response.status_code == 401 |
| 71 | + |
| 72 | + |
| 73 | +def test_lists_get_single(zod, gid, list_, list_schema): |
| 74 | + # GET /lists/{uuid} |
| 75 | + url = f"/apps/maat/api/lists/{gid}" |
| 76 | + response = zod.get(url) |
| 77 | + assert response.status_code == 200 |
| 78 | + result = response.json() |
| 79 | + assert list_schema.is_valid(result) |
| 80 | + assert result == list_ |
| 81 | + |
| 82 | + |
| 83 | +def test_lists_get_single_not_found(zod): |
| 84 | + # GET /lists/{uuid} |
| 85 | + url = f"/apps/maat/api/lists/{str(uuid4())}" |
| 86 | + response = zod.get(url) |
| 87 | + # TODO: should be 4xx |
| 88 | + assert response.status_code == 500 |
| 89 | + |
| 90 | + |
| 91 | +def test_lists_delete(zod, gid, list_): |
| 92 | + # PUT |
| 93 | + url = f"/apps/maat/api/lists/{gid}" |
| 94 | + response = zod.delete(url) |
| 95 | + assert response.status_code == 200 |
| 96 | + |
| 97 | + # GET /lists |
| 98 | + url = "/apps/maat/api/lists" |
| 99 | + response = zod.get(url) |
| 100 | + assert response.status_code == 200 |
| 101 | + result = response.json() |
| 102 | + assert list_ not in result |
| 103 | + |
| 104 | + |
| 105 | +# @pytest.mark.usefixtures("member") |
| 106 | +# def test_lists_delete_unauthorized(zod, nus, gid, list): |
| 107 | +# # wait for join |
| 108 | +# sleep(0.5) |
| 109 | + |
| 110 | +# # PUT |
| 111 | +# url = f"/apps/maat/api/lists/{gid}" |
| 112 | +# response = nus.delete(url) |
| 113 | +# assert response.status_code == 403 |
| 114 | + |
| 115 | +# # GET /lists |
| 116 | +# url = "/apps/maat/api/lists" |
| 117 | +# response = zod.get(url) |
| 118 | +# assert response.status_code == 200 |
| 119 | +# result = response.json() |
| 120 | +# assert list in result |
| 121 | + |
| 122 | + |
| 123 | +# @pytest.mark.usefixtures("list") |
| 124 | +# def test_lists_get_private(gid): |
| 125 | +# # GET /lists/{uuid} |
| 126 | +# url = f"http://localhost:8080/apps/maat/api/lists/{gid}" |
| 127 | +# response = requests.get(url) |
| 128 | +# assert response.status_code == 401 |
| 129 | + |
| 130 | + |
| 131 | +# def test_lists_get_public(gid, list_public, list_schema): |
| 132 | +# # GET /lists/{uuid} |
| 133 | +# url = f"http://localhost:8080/apps/maat/api/lists/{gid}" |
| 134 | +# response = requests.get(url) |
| 135 | +# assert response.status_code == 200 |
| 136 | +# result = response.json() |
| 137 | +# assert list_schema.is_valid(result) |
| 138 | +# assert result == list_public |
| 139 | + |
| 140 | + |
| 141 | +# @pytest.mark.usefixtures("list") |
| 142 | +# def test_lists_delete_private(gid): |
| 143 | +# # PUT |
| 144 | +# url = f"http://localhost:8080/apps/maat/api/lists/{gid}" |
| 145 | +# response = requests.delete(url) |
| 146 | +# assert response.status_code == 401 |
| 147 | + |
| 148 | + |
| 149 | +# @pytest.mark.usefixtures("list_public") |
| 150 | +# def test_lists_delete_public(gid): |
| 151 | +# # PUT |
| 152 | +# url = f"http://localhost:8080/apps/maat/api/lists/{gid}" |
| 153 | +# response = requests.delete(url) |
| 154 | +# assert response.status_code == 401 |
0 commit comments