|
1 | 1 | import json
|
2 | 2 | import urllib
|
3 | 3 | from typing import Any
|
| 4 | +from unittest.mock import Mock |
4 | 5 |
|
| 6 | +import pytest |
5 | 7 | from boto3.dynamodb.conditions import Key
|
6 | 8 | from django.urls import reverse
|
7 | 9 | from mypy_boto3_dynamodb.service_resource import Table
|
|
18 | 20 | )
|
19 | 21 | from environments.models import Environment
|
20 | 22 |
|
| 23 | +_invalid_identifier_error_message = "Identifier can only contain unicode letters, numbers, and the symbols: ! # $ % & * + / = ? ^ _ ` { } | ~ @ . -" |
| 24 | + |
21 | 25 |
|
22 | 26 | def test_get_identities_returns_bad_request_if_dynamo_is_not_enabled( # type: ignore[no-untyped-def]
|
23 | 27 | admin_client, environment, environment_api_key
|
@@ -116,6 +120,78 @@ def test_create_identity( # type: ignore[no-untyped-def]
|
116 | 120 | assert response.json()["identity_uuid"] is not None
|
117 | 121 |
|
118 | 122 |
|
| 123 | +@pytest.mark.parametrize( |
| 124 | + "given_identifier", |
| 125 | + [ |
| 126 | + "bond...jamesbond", |
| 127 | + "ゴジラ", |
| 128 | + "ElChapulínColorado", |
| 129 | + |
| 130 | + "agáta={^_^}=", |
| 131 | + "_ツ_/-handless-shrug", |
| 132 | + "who+am+i?", |
| 133 | + "i_100%_dont_know!", |
| 134 | + "~neo|simulation`0065192*75`", |
| 135 | + "KacperGustyr$Flagsmat", |
| 136 | + ], |
| 137 | +) |
| 138 | +@pytest.mark.usefixtures( |
| 139 | + "dynamo_enabled_environment", |
| 140 | +) |
| 141 | +def test_create_identity_accepts_valid_identifiers( |
| 142 | + admin_client: APIClient, |
| 143 | + environment_api_key: str, |
| 144 | + given_identifier: str, |
| 145 | + edge_identity_dynamo_wrapper_mock: Mock, |
| 146 | +) -> None: |
| 147 | + # Given |
| 148 | + edge_identity_dynamo_wrapper_mock.get_item.return_value = None |
| 149 | + |
| 150 | + # When |
| 151 | + response = admin_client.post( |
| 152 | + f"/api/v1/environments/{environment_api_key}/edge-identities/", |
| 153 | + data={"identifier": given_identifier}, |
| 154 | + ) |
| 155 | + |
| 156 | + # Then |
| 157 | + assert response.status_code == status.HTTP_201_CREATED |
| 158 | + assert response.json()["identifier"] == given_identifier |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.parametrize( |
| 162 | + ["given_identifier", "error_message"], |
| 163 | + [ |
| 164 | + ("", "This field may not be blank."), |
| 165 | + (" ", "This field may not be blank."), |
| 166 | + ("or really anything with a whitespace", _invalid_identifier_error_message), |
| 167 | + ("<script>alert(1)</script>", _invalid_identifier_error_message), |
| 168 | + ("'; DROP TABLE users;--", _invalid_identifier_error_message), |
| 169 | + ("'single-quotes'", _invalid_identifier_error_message), |
| 170 | + ('"double-quotes"', _invalid_identifier_error_message), |
| 171 | + ("figaro" * 334, "Ensure this field has no more than 2000 characters."), |
| 172 | + ], |
| 173 | +) |
| 174 | +@pytest.mark.usefixtures( |
| 175 | + "dynamo_enabled_environment", |
| 176 | + "edge_identity_dynamo_wrapper_mock", |
| 177 | +) |
| 178 | +def test_create_identity_responds_400_if_identifier_is_invalid( |
| 179 | + admin_client: APIClient, |
| 180 | + environment_api_key: str, |
| 181 | + error_message: str, |
| 182 | + given_identifier: str, |
| 183 | +) -> None: |
| 184 | + # When |
| 185 | + response = admin_client.post( |
| 186 | + f"/api/v1/environments/{environment_api_key}/edge-identities/", |
| 187 | + data={"identifier": given_identifier}, |
| 188 | + ) |
| 189 | + |
| 190 | + # Then |
| 191 | + assert response.status_code == status.HTTP_400_BAD_REQUEST |
| 192 | + assert response.json() == {"identifier": [error_message]} |
| 193 | + |
| 194 | + |
119 | 195 | def test_create_identity_returns_400_if_identity_already_exists( # type: ignore[no-untyped-def]
|
120 | 196 | admin_client,
|
121 | 197 | dynamo_enabled_environment,
|
|
0 commit comments