Skip to content

Commit

Permalink
Merge pull request #9 from synw/root_path_url
Browse files Browse the repository at this point in the history
Ninja api.root_path seems doesn't exist anymore. Use get_root_path
  • Loading branch information
synw authored Mar 17, 2024
2 parents 99028cb + b0c9454 commit d3e3c4c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
34 changes: 17 additions & 17 deletions tests/account/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ def test_user_creation():


def test_admin_account_state(admin_client):
response = admin_client.get(f"{api.root_path}account/state")
response = admin_client.get(f"{api.get_root_path([])}account/state")
assert response.status_code == 200
assert response.content == b'{"is_connected": true, "username": "admin"}'


class TestAccount(NinjaTestCase):
def test_anonymous_account_state(self):
response = self.client.get(f"{api.root_path}account/state")
response = self.client.get(f"{api.get_root_path([])}account/state")
assert response.status_code == 200
self.assertJSONEqual(
response.content, {"is_connected": False, "username": "anonymous"}
)

def test_user_account_state(self):
response = self.user_client.get(f"{api.root_path}account/state")
response = self.user_client.get(f"{api.get_root_path([])}account/state")
assert response.status_code == 200
self.assertJSONEqual(
response.content, {"is_connected": True, "username": "user"}
)

def test_admin_account_state(self):
response = self.admin_client.get(f"{api.root_path}account/state")
response = self.admin_client.get(f"{api.get_root_path([])}account/state")
assert response.status_code == 200
assert response.content == b'{"is_connected": true, "username": "admin"}'

def test_accout_register(self):
# TODO: name is not used, may change username django settings
response = self.client.post(
f"{api.root_path}account/register",
f"{api.get_root_path([])}account/register",
data=json.dumps(
{
"name": "johndoe",
Expand All @@ -60,7 +60,7 @@ def test_accout_register(self):

def test_accout_register_fail(self):
response = self.client.post(
f"{api.root_path}account/register",
f"{api.get_root_path([])}account/register",
data=json.dumps(
{
"name": "johndoe",
Expand All @@ -85,7 +85,7 @@ def test_accout_register_fail(self):

def test_account_activate_token(self):
self.client.post(
f"{api.root_path}account/register",
f"{api.get_root_path([])}account/register",
data=json.dumps(
{
"name": "johndoe",
Expand All @@ -101,40 +101,40 @@ def test_account_activate_token(self):
token = encode_token(user.email)
# Activate
assert user.is_active is False
response = self.client.get(f"{api.root_path}account/activate/{token}")
response = self.client.get(f"{api.get_root_path([])}account/activate/{token}")
assert response.status_code == 204
assert response.reason_phrase == "No Content"
user.refresh_from_db()
assert user.is_active

def test_account_activate_token_fail(self):
response = self.client.get(f"{api.root_path}account/activate/invalidtoken")
response = self.client.get(f"{api.get_root_path([])}account/activate/invalidtoken")
assert response.status_code == 401
assert response.json() == {"message": "Account activation refused"}

def test_account_login(self):
# Check state before login
state_response = self.client.get(f"{api.root_path}account/state")
state_response = self.client.get(f"{api.get_root_path([])}account/state")
assert state_response.status_code == 200
assert (
state_response.content
== b'{"is_connected": false, "username": "anonymous"}'
)
# Login with admin
response = self.client.post(
f"{api.root_path}account/login",
f"{api.get_root_path([])}account/login",
data=json.dumps({"username": "admin", "password": "admin"}),
content_type="application/json",
)
assert response.status_code == 200
# Check state after login
state_response = self.client.get(f"{api.root_path}account/state")
state_response = self.client.get(f"{api.get_root_path([])}account/state")
assert state_response.status_code == 200
assert state_response.content == b'{"is_connected": true, "username": "admin"}'

def test_account_login_fail(self):
response = self.client.post(
f"{api.root_path}account/login",
f"{api.get_root_path([])}account/login",
data=json.dumps({"username": "admin", "password": "wrongpassword"}),
content_type="application/json",
)
Expand All @@ -155,21 +155,21 @@ def test_account_login_fail(self):
def test_account_logout(self):
# Login with admin
response = self.client.post(
f"{api.root_path}account/login",
f"{api.get_root_path([])}account/login",
data=json.dumps({"username": "admin", "password": "admin"}),
content_type="application/json",
)
assert response.status_code == 200
# Check state after login
state_response = self.client.get(f"{api.root_path}account/state")
state_response = self.client.get(f"{api.get_root_path([])}account/state")
assert state_response.status_code == 200
assert state_response.content == b'{"is_connected": true, "username": "admin"}'
# Logout
response = self.client.get(f"{api.root_path}account/logout")
response = self.client.get(f"{api.get_root_path([])}account/logout")
assert response.status_code == 200
assert response.reason_phrase == "OK"
# Check state after logout
state_response = self.client.get(f"{api.root_path}account/state")
state_response = self.client.get(f"{api.get_root_path([])}account/state")
assert state_response.status_code == 200
assert (
state_response.content
Expand Down
8 changes: 4 additions & 4 deletions tests/test_ninja_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

def test_api_json_openapi(admin_client):
"""Test endpoint which provide autogenerated openapi.json"""
response = admin_client.get(f"{api.root_path}{api.openapi_url[1:]}")
response = admin_client.get(f"{api.get_root_path([])}{api.openapi_url[1:]}")
assert response.status_code == 200
assert response.json()["info"]["title"] == "NinjaAPI"


def test_api_docs(admin_client):
"""Test the /api/docs endpoint"""
response = admin_client.get(f"{api.root_path}{api.docs_url[1:]}")
response = admin_client.get(f"{api.get_root_path([])}{api.docs_url[1:]}")
assert response.status_code == 200


def test_visitor_cannot_access_api(client):
"""Test that visitor cannot access API"""
response = client.get(f"{api.root_path}{api.docs_url[1:]}")
response = client.get(f"{api.get_root_path([])}{api.docs_url[1:]}")
assert response.status_code == 302
assert response.url == f"/admin/login/?next={api.root_path}{api.docs_url[1:]}"
assert response.url == f"/admin/login/?next={api.get_root_path([])}{api.docs_url[1:]}"
2 changes: 1 addition & 1 deletion tests/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_client_helper(self):
def link_api_helper(self):
"""Can override this method to link another project api to the test case."""
self.api = api
self.root_path = self.api.root_path
self.root_path = self.api.get_root_path([])
self.docs_url = self.api.docs_url

def setUp(self):
Expand Down

0 comments on commit d3e3c4c

Please sign in to comment.