Commit 63108493 authored by Erlend Ydse's avatar Erlend Ydse
Browse files

Add tests for comments and offers

parent 94bb94a7
Pipeline #128407 failed with stage
in 1 minute and 2 seconds
from rest_framework.test import APIClient, APITestCase
from django.utils import timezone
class CommentAPITests(APITestCase):
def setUp(self):
self.path = "/api/workouts/"
self.client = APIClient()
now = timezone.now()
# Creating users
self.alice_r_data = {
"username": "alice",
"email": "aa@aa.aa",
"password": "aliceisok",
"password1": "aliceisok"}
response = self.client.post(path="/api/users/", data=self.alice_r_data)
self.alice_data = response.data
self.bob_r_data = {
"username": "bob",
"email": "aa@aa.aa",
"password": "aliceisok",
"password1": "aliceisok"}
response = self.client.post(path="/api/users/", data=self.bob_r_data)
self.bob_data = response.data
# Logging in as bob
self.login_data = {"username": self.bob_r_data["username"], "password": self.bob_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
# Creating an exercise
response = self.client.post(path="/api/exercises/", data={"description": "Test Exercise", "name": "Test Exercise", "unit": "test unit"})
self.exerciseA = { "exercise": response.data["url"], "sets": 1, "number": 1}
# Creating a private workout on bob
self.data = {
"name": "wBR",
"date": now,
"notes": "Good workout",
"visibility": "PR",
"exercise_instances": [self.exerciseA]}
self.wbr_data = self.client.post(path=self.path, data=self.data, format="json")
# Logging in as alice
self.login_data = {"username": self.alice_r_data["username"], "password": self.alice_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
# Creating a private workout as alice
self.data["name"] = "wAR"
self.war_data = self.client.post(path=self.path, data=self.data, format="json")
def test_can_comment_on_private_workout(self):
data = {
"workout": self.wbr_data.data["url"],
"content": self.login_data["username"]
}
response = self.client.post(path="/api/comments/", data=data)
self.assertEqual(response.status_code, 400)
data = {
"workout": self.war_data.data["url"],
"content": self.login_data["username"]
}
response = self.client.post(path="/api/comments/", data=data)
self.assertEqual(response.status_code, 201)
from django.utils import timezone
from rest_framework.test import APIClient, APITestCase
def pretty_print_response(response):
print(response.data)
print(response.status_code)
class OffersAPITests(APITestCase):
def setUp(self):
self.path = "/api/offers/"
self.client = APIClient()
# Creating users
self.alice_r_data = {
"username": "alice",
"email": "aa@aa.aa",
"password": "aliceisok",
"password1": "aliceisok"}
response = self.client.post(path="/api/users/", data=self.alice_r_data)
self.alice_data = response.data
self.bob_r_data = {
"username": "bob",
"email": "aa@aa.aa",
"password": "aliceisok",
"password1": "aliceisok"}
response = self.client.post(path="/api/users/", data=self.bob_r_data)
self.bob_data = response.data
self.clair_r_data = {
"username": "clair",
"email": "aa@aa.aa",
"password": "aliceisok",
"password1": "aliceisok"}
response = self.client.post(path="/api/users/", data=self.clair_r_data)
self.clair_data = response.data
# Logging in as bob
self.login_data = {"username": self.bob_r_data["username"], "password": self.bob_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
# Making offer to clair
response = self.client.post(path=self.path, data={"status": "p", "recipient": self.clair_data["url"]})
self.b_to_c_offer = response.data
# Logging in as alice
self.login_data = {"username": self.alice_r_data["username"], "password": self.alice_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
## GET
def test_can_not_view_other_user_offer(self):
response = self.client.get(path=f"{self.path}{self.b_to_c_offer['id']}/")
self.assertEqual(response.status_code, 400)
def test_can_view_own_offer_recipient(self):
# Logging in as clair
self.login_data = {"username": self.clair_r_data["username"], "password": self.clair_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
response = self.client.get(path=f"{self.path}{self.b_to_c_offer['id']}/")
self.assertEqual(response.status_code, 200)
def test_can_view_own_offer_owner(self):
# Logging in as bob
self.login_data = {"username": self.bob_r_data["username"], "password": self.bob_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
response = self.client.get(path=f"{self.path}{self.b_to_c_offer['id']}/")
self.assertEqual(response.status_code, 200)
## PATCH/PUT
def test_can_not_accept_other_offer(self):
data = {"status": "a"}
response = self.client.patch(path=f"{self.path}{self.b_to_c_offer['id']}/", data=data)
self.assertEqual(response.status_code, 400)
data = {
'url': 'http://testserver/api/offers/1/',
'id': 1,
'owner': 'bob',
'recipient': 'http://testserver/api/users/3/',
'status': 'a',
'timestamp': '2021-03-22T10:06:24.034403Z'}
response = self.client.put(path=f"{self.path}{self.b_to_c_offer['id']}/", data=data)
self.assertEqual(response.status_code, 400)
def test_can_accept_own_offer_recipient(self):
# Logging in as clair
self.login_data = {"username": self.clair_r_data["username"], "password": self.clair_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
data = {"status": "a"}
response = self.client.patch(path=f"{self.path}{self.b_to_c_offer['id']}/", data=data)
self.assertEqual(response.status_code, 200)
## DELETE
def test_can_not_delete_other_user_offer(self):
response = self.client.delete(path=f"{self.path}{self.b_to_c_offer['id']}/")
self.assertEqual(response.status_code, 400)
def test_can_delete_own_user_offer_owner(self):
# Logging in as bob
self.login_data = {"username": self.bob_r_data["username"], "password": self.bob_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
response = self.client.delete(path=f"{self.path}{self.b_to_c_offer['id']}/")
self.assertEqual(response.status_code, 204)
def test_can_not_delete_own_user_offer_recipient(self):
# Logging in as clair
self.login_data = {"username": self.clair_r_data["username"], "password": self.clair_r_data["password"]}
response = self.client.post(path="/api/token/", data=self.login_data, format="json")
self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
response = self.client.delete(path=f"{self.path}{self.b_to_c_offer['id']}/")
self.assertEqual(response.status_code, 400)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment