-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-4: Register API when API key configured
- Loading branch information
1 parent
1309d08
commit 11c61da
Showing
3 changed files
with
47 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,76 +7,68 @@ | |
from schemes.users import User, UserRepository | ||
|
||
|
||
@pytest.fixture(name="config") | ||
def config_fixture(config: Mapping[str, Any]) -> Mapping[str, Any]: | ||
return config | {"API_KEY": "boardman"} | ||
|
||
|
||
@pytest.fixture(name="users") | ||
def users_fixture() -> UserRepository: | ||
return inject.instance(UserRepository) | ||
|
||
|
||
def test_add_users(users: UserRepository, client: FlaskClient) -> None: | ||
response = client.post( | ||
"/users", | ||
headers={"Authorization": "API-Key boardman"}, | ||
json=[{"email": "[email protected]"}, {"email": "[email protected]"}], | ||
) | ||
|
||
assert response.status_code == 201 | ||
assert users.get_all() == [User("[email protected]"), User("[email protected]")] | ||
|
||
|
||
def test_cannot_add_users_when_no_credentials(users: UserRepository, client: FlaskClient) -> None: | ||
response = client.post("/users", json=[{"email": "[email protected]"}]) | ||
|
||
assert response.status_code == 401 | ||
assert not users.get_all() | ||
class TestApiEnabled: | ||
@pytest.fixture(name="config") | ||
def config_fixture(self, config: Mapping[str, Any]) -> Mapping[str, Any]: | ||
return config | {"API_KEY": "boardman"} | ||
|
||
@pytest.fixture(name="users") | ||
def users_fixture(self) -> UserRepository: | ||
return inject.instance(UserRepository) | ||
|
||
def test_cannot_add_users_when_incorrect_credentials(users: UserRepository, client: FlaskClient) -> None: | ||
response = client.post( | ||
"/users", headers={"Authorization": "API-Key obree"}, json=[{"email": "[email protected]"}] | ||
) | ||
def test_add_users(self, users: UserRepository, client: FlaskClient) -> None: | ||
response = client.post( | ||
"/users", | ||
headers={"Authorization": "API-Key boardman"}, | ||
json=[{"email": "[email protected]"}, {"email": "[email protected]"}], | ||
) | ||
|
||
assert response.status_code == 401 | ||
assert not users.get_all() | ||
assert response.status_code == 201 | ||
assert users.get_all() == [User("[email protected]"), User("[email protected]")] | ||
|
||
def test_cannot_add_users_when_no_credentials(self, users: UserRepository, client: FlaskClient) -> None: | ||
response = client.post("/users", json=[{"email": "[email protected]"}]) | ||
|
||
def test_clear_users(users: UserRepository, client: FlaskClient) -> None: | ||
users.add(User("[email protected]")) | ||
assert response.status_code == 401 | ||
assert not users.get_all() | ||
|
||
response = client.delete("/users", headers={"Authorization": "API-Key boardman"}) | ||
def test_cannot_add_users_when_incorrect_credentials(self, users: UserRepository, client: FlaskClient) -> None: | ||
response = client.post( | ||
"/users", headers={"Authorization": "API-Key obree"}, json=[{"email": "[email protected]"}] | ||
) | ||
|
||
assert response.status_code == 204 | ||
assert not users.get_all() | ||
assert response.status_code == 401 | ||
assert not users.get_all() | ||
|
||
def test_clear_users(self, users: UserRepository, client: FlaskClient) -> None: | ||
users.add(User("[email protected]")) | ||
|
||
def test_cannot_clear_users_when_no_credentials(users: UserRepository, client: FlaskClient) -> None: | ||
users.add(User("[email protected]")) | ||
response = client.delete("/users", headers={"Authorization": "API-Key boardman"}) | ||
|
||
response = client.delete("/users") | ||
assert response.status_code == 204 | ||
assert not users.get_all() | ||
|
||
assert response.status_code == 401 | ||
assert users.get_all() == [User("[email protected]")] | ||
def test_cannot_clear_users_when_no_credentials(self, users: UserRepository, client: FlaskClient) -> None: | ||
users.add(User("[email protected]")) | ||
|
||
response = client.delete("/users") | ||
|
||
def test_cannot_clear_users_when_incorrect_credentials(users: UserRepository, client: FlaskClient) -> None: | ||
users.add(User("[email protected]")) | ||
assert response.status_code == 401 | ||
assert users.get_all() == [User("[email protected]")] | ||
|
||
response = client.delete("/users", headers={"Authorization": "API-Key obree"}) | ||
def test_cannot_clear_users_when_incorrect_credentials(self, users: UserRepository, client: FlaskClient) -> None: | ||
users.add(User("[email protected]")) | ||
|
||
assert response.status_code == 401 | ||
assert users.get_all() == [User("[email protected]")] | ||
response = client.delete("/users", headers={"Authorization": "API-Key obree"}) | ||
|
||
assert response.status_code == 401 | ||
assert users.get_all() == [User("[email protected]")] | ||
|
||
class TestProduction: | ||
@pytest.fixture(name="config") | ||
def config_fixture(self, config: Mapping[str, Any]) -> Mapping[str, Any]: | ||
return config | {"TESTING": False} | ||
|
||
class TestApiDisabled: | ||
def test_cannot_add_user(self, client: FlaskClient) -> None: | ||
response = client.post("/users", json={"email": "[email protected]"}) | ||
response = client.post( | ||
"/users", headers={"Authorization": "API-Key boardman"}, json={"email": "[email protected]"} | ||
) | ||
|
||
assert response.status_code == 404 | ||
assert response.status_code == 401 |