Skip to content

Commit

Permalink
Linter refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaOCR committed Nov 19, 2024
1 parent 76224ee commit 05bf57d
Showing 1 changed file with 27 additions and 54 deletions.
81 changes: 27 additions & 54 deletions oscarapi/tests/unit/testbasket.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def test_basket_api_create_not_possible(self):

# authenticated
self.login("nobody", "nobody")
data = {"owner": "http://testserver%s" %
reverse("user-detail", args=[2])}
data = {"owner": "http://testserver%s" % reverse("user-detail", args=[2])}

self.response = self.client.post(
url, json.dumps(data), content_type="application/json"
Expand All @@ -64,8 +63,7 @@ def test_basket_api_create_not_possible(self):
# admin
self.login("admin", "admin")

data = {"owner": "http://testserver%s" %
reverse("user-detail", args=[1])}
data = {"owner": "http://testserver%s" % reverse("user-detail", args=[1])}
self.response = self.client.post(
url, json.dumps(data), content_type="application/json"
)
Expand Down Expand Up @@ -128,27 +126,23 @@ def test_retrieve_basket_header(self):

# authenticated
self.hlogin("nobody", "nobody", session_id="nobody")
self.response = self.get(
"api-basket", session_id="nobody", authenticated=True)
self.response = self.get("api-basket", session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)
self.response.assertObjectIdEqual("owner", 2)
basket_id = self.response["id"]

self.response = self.get(
"api-basket", session_id="nobody", authenticated=True)
self.response = self.get("api-basket", session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)
self.response.assertValueEqual("id", basket_id)

# admin
self.hlogin("admin", "admin", session_id="admin")
self.response = self.get(
"api-basket", session_id="admin", authenticated=True)
self.response = self.get("api-basket", session_id="admin", authenticated=True)
self.response.assertStatusEqual(200)
self.response.assertObjectIdEqual("owner", 1)
basket_id = self.response["id"]

self.response = self.get(
"api-basket", session_id="admin", authenticated=True)
self.response = self.get("api-basket", session_id="admin", authenticated=True)
self.response.assertStatusEqual(200)
self.response.assertValueEqual("id", basket_id)

Expand Down Expand Up @@ -242,8 +236,7 @@ def test_basket_read_permissions(self):

url = reverse("basket-lines-list", args=(1,))
self.response = self.client.get(url)
self.response.assertStatusEqual(
403, "Users not access other peoples baskets.")
self.response.assertStatusEqual(403, "Users not access other peoples baskets.")

self.assertEqual(
Basket.objects.count(),
Expand Down Expand Up @@ -295,8 +288,7 @@ def test_basket_read_permissions_header(self):

# now try for authenticated user.
self.hlogin("nobody", "nobody", session_id="nobody")
self.response = self.get(
"api-basket", session_id="nobody", authenticated=True)
self.response = self.get("api-basket", session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)

# try to access the urls in the response.
Expand Down Expand Up @@ -331,8 +323,7 @@ def test_basket_read_permissions_header(self):
)

self.hlogin("admin", "admin", session_id="admin")
self.response = self.get(
"api-basket", session_id="admin", authenticated=True)
self.response = self.get("api-basket", session_id="admin", authenticated=True)
self.response.assertStatusEqual(200)

# try to access the urls in the response.
Expand Down Expand Up @@ -571,8 +562,7 @@ def test_basket_write_permissions_header_authenticated(self):

# now try for authenticated user.
self.hlogin("nobody", "nobody", session_id="nobody")
self.response = self.get(
"api-basket", session_id="nobody", authenticated=True)
self.response = self.get("api-basket", session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)

# try to access the urls in the response.
Expand Down Expand Up @@ -618,13 +608,11 @@ def test_basket_write_permissions_header_authenticated(self):
self.response.assertStatusEqual(201)

# throw the basket away
self.response = self.delete(
url, session_id="nobody", authenticated=True)
self.response = self.delete(url, session_id="nobody", authenticated=True)
self.response.assertStatusEqual(204)

# now lets start messing around
self.response = self.get(
"api-basket", session_id="nobody", authenticated=True)
self.response = self.get("api-basket", session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)
basket_id = self.response["id"]

Expand Down Expand Up @@ -664,8 +652,7 @@ def test_basket_write_permissions_header_authenticated(self):
self.response.assertStatusEqual(403)

# try to delete someone else's basket
self.response = self.delete(
url, session_id="nobody", authenticated=True)
self.response = self.delete(url, session_id="nobody", authenticated=True)
self.response.assertStatusEqual(403)

# try adding lines to someone else's basket
Expand Down Expand Up @@ -754,8 +741,7 @@ def test_basket_write_permissions_admin(self):

# try to write to someone else's basket by sending the primary key
# along.
self.response = self.put(
url, status="Saved", id=somebody_basket_id)
self.response = self.put(url, status="Saved", id=somebody_basket_id)
self.response.assertStatusEqual(200)
self.response.assertValueEqual(
"id", basket_id, "Primary key value can not be changed."
Expand Down Expand Up @@ -791,8 +777,7 @@ def test_basket_write_permissions_header_admin(self):

# now try for authenticated user.
self.hlogin("admin", "admin", session_id="admin")
self.response = self.get(
"api-basket", session_id="admin", authenticated=True)
self.response = self.get("api-basket", session_id="admin", authenticated=True)
self.response.assertStatusEqual(200)

# try to access the urls in the response.
Expand Down Expand Up @@ -838,13 +823,11 @@ def test_basket_write_permissions_header_admin(self):
self.response.assertStatusEqual(201)

# throw the basket away
self.response = self.delete(
url, session_id="admin", authenticated=True)
self.response = self.delete(url, session_id="admin", authenticated=True)
self.response.assertStatusEqual(204)

# now lets start messing around
self.response = self.get(
"api-basket", session_id="admin", authenticated=True)
self.response = self.get("api-basket", session_id="admin", authenticated=True)
self.response.assertStatusEqual(200)
basket_id = self.response["id"]

Expand Down Expand Up @@ -901,8 +884,7 @@ def test_basket_write_permissions_header_admin(self):
self.response.assertStatusEqual(403)

# try to delete someone else's basket
self.response = self.delete(
url, session_id="admin", authenticated=True)
self.response = self.delete(url, session_id="admin", authenticated=True)
self.response.assertStatusEqual(403)

def test_add_product_anonymous(self):
Expand Down Expand Up @@ -949,8 +931,7 @@ def test_add_product_basket_addition_signal_send(self, mock):
self.assertTrue(mock.called)

signal_arguments = mock.call_args[1]
self.assertEqual(
signal_arguments["product"], Product.objects.get(pk=1))
self.assertEqual(signal_arguments["product"], Product.objects.get(pk=1))
self.assertEqual(signal_arguments["user"].username, "nobody")

# see if we can get the basket from the request
Expand All @@ -974,8 +955,7 @@ def test_basket_line_permissions(self):

self.response = self.get(line0url)
self.response.assertStatusEqual(200)
self.response.assertValueEqual(
"product", "http://testserver/api/products/1/")
self.response.assertValueEqual("product", "http://testserver/api/products/1/")
self.response.assertValueEqual("quantity", 5)

# now let's try to cheat
Expand All @@ -986,8 +966,7 @@ def test_basket_line_permissions(self):
def test_basket_line_permissions_header(self):
"A user's Basket lines can not be viewed by another user in any way, even with header authentication"
self.hlogin("nobody", "nobody", session_id="nobody")
self.response = self.get(
"api-basket", session_id="nobody", authenticated=True)
self.response = self.get("api-basket", session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)

self.response = self.post(
Expand All @@ -1004,17 +983,14 @@ def test_basket_line_permissions_header(self):
line0 = self.response.body[0]
line0url = line0["url"]

self.response = self.get(
line0url, session_id="nobody", authenticated=True)
self.response = self.get(line0url, session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)
self.response.assertValueEqual(
"product", "http://testserver/api/products/1/")
self.response.assertValueEqual("product", "http://testserver/api/products/1/")
self.response.assertValueEqual("quantity", 5)

# now let's try to cheat
self.hlogin("somebody", "somebody", session_id="somebody")
self.response = self.get(
line0url, session_id="somebody", authenticated=True)
self.response = self.get(line0url, session_id="somebody", authenticated=True)
self.response.assertStatusEqual(404)

def test_frozen_basket_can_not_be_accessed(self):
Expand All @@ -1035,8 +1011,7 @@ def test_frozen_basket_can_not_be_accessed(self):
def test_frozen_basket_can_not_be_accessed_header(self):
"Prove that frozen baskets can no longer be accessed by the user, even with header authentication"
self.hlogin("nobody", "nobody", session_id="nobody")
self.response = self.get(
"api-basket", session_id="nobody", authenticated=True)
self.response = self.get("api-basket", session_id="nobody", authenticated=True)
self.response.assertStatusEqual(200)
self.response.assertValueEqual("status", "Open")

Expand Down Expand Up @@ -1377,17 +1352,15 @@ def test_basket_login_logout(self):
self.client.cookies,
"An basket cookie should have been created",
)
self.assertStartsWith(
self.client.cookies["oscar_open_basket"].value, "1")
self.assertStartsWith(self.client.cookies["oscar_open_basket"].value, "1")

# retrieve the basket with oscarapi.
self.response = self.get("api-basket")
self.response.assertValueEqual(
"owner", None, "The basket should not have an owner"
)
self.response.assertValueEqual("id", 1)
self.assertStartsWith(
self.client.cookies["oscar_open_basket"].value, "1")
self.assertStartsWith(self.client.cookies["oscar_open_basket"].value, "1")

# now lets log in with oscarapi
response = self.post("api-login", username="nobody", password="nobody")
Expand Down

0 comments on commit 05bf57d

Please sign in to comment.