diff --git a/backend/secfit/tests/data/workouts.json b/backend/secfit/tests/data/workouts.json index 3dbcbdfa6a6be90137b36e7aa07890b49ad2b6c4..b3f2d64bbd60ce7cdbf1b5f771a5dbfd09bc22af 100644 --- a/backend/secfit/tests/data/workouts.json +++ b/backend/secfit/tests/data/workouts.json @@ -41,5 +41,56 @@ "visibility": "PR", "owner": 2 } + }, + { + "model": "workouts.workout", + "pk": 5, + "fields": { + "name": "Valar Morghulis", + "date": "2011-06-11T20:01:00.00000Z", + "notes": "Practicing fighting while blind", + "visibility": "PU", + "owner": 2 + } + }, + { + "model": "workouts.exerciseinstance", + "pk": 1, + "fields": { + "workout": 1, + "exercise": 1, + "sets": 3, + "number": 8 + } + }, + { + "model": "workouts.exerciseinstance", + "pk": 2, + "fields": { + "workout": 2, + "exercise": 1, + "sets": 3, + "number": 8 + } + }, + { + "model": "workouts.exerciseinstance", + "pk": 3, + "fields": { + "workout": 3, + "exercise": 2, + "sets": 3, + "number": 8 + } + }, + { + "model": "workouts.exerciseinstance", + "pk": 4, + "fields": { + "workout": 4, + "exercise": 2, + "sets": 3, + "number": 8 + } } ] diff --git a/backend/secfit/tests/test_filter_workouts.py b/backend/secfit/tests/test_filter_workouts.py new file mode 100644 index 0000000000000000000000000000000000000000..efd4b8d5479ad90e20f0a8d371cdebef013b846e --- /dev/null +++ b/backend/secfit/tests/test_filter_workouts.py @@ -0,0 +1,63 @@ +import os +import pathlib +import time + + +from selenium.webdriver.support.ui import Select + +from selenium.webdriver.common.by import By + +from tests.common import BlackBoxTestServer +from tests.common import login_data + +here = pathlib.Path(__file__).parent.absolute() +data_dir = here / "data" +backend_port = os.environ.get("BACKEND_PORT", 8000) +frontend_port = os.environ.get("FRONTEND_PORT", 8001) +frontend_address = f"http://localhost:{frontend_port}" + + +class FilterWorkoutsTest(BlackBoxTestServer): + + headless = True + fixtures = [ + data_dir / "users.json", + data_dir / "exercises.json", + data_dir / "workouts.json", + ] + + def wait_until_value_not_empty(self, by: By, locator: str): + self.wait.until(lambda _: self.get_input_value(by, locator) != "") + + def get_input_value(self, by: By, locator: str) -> str: + element = self.driver.find_element(by, locator) + self.assertIsNotNone(element) + return element.get_attribute("value") + + def test_exercise_filter_is_populated(self): + user = login_data["athlete"] + self.login(user) + self.get("workouts.html") + self.assertEqual("Workouts", self.driver.title) + time.sleep(2) # :) + self.get_input_value(By.ID, "filter-exercise") + options = self.driver.find_elements_by_tag_name('option') + self.assertEqual(len(options), 3) + for option in options: + self.assertIn(option.text, ['', 'Fencing', 'Stick fighting']) + + def test_user_can_filter_workouts(self): + user = login_data["athlete"] + self.login(user) + self.get("workouts.html") + self.assertEqual("Workouts", self.driver.title) + time.sleep(2) # :) + options = self.driver.find_elements_by_class_name('workout') + self.assertEqual(len(options), 5) + select = Select((self.driver.find_element_by_id("filter-exercise"))) + select.select_by_visible_text("Fencing") + time.sleep(2) + filtered_workouts = self.driver.find_elements_by_css_selector( + '.workout:not(.hide)' + ) + self.assertEqual(len(filtered_workouts), 2) diff --git a/backend/secfit/tests/test_highscore.py b/backend/secfit/tests/test_highscore.py new file mode 100644 index 0000000000000000000000000000000000000000..3dd23cc05b7d054386b3d2e47f1e441ea0286b83 --- /dev/null +++ b/backend/secfit/tests/test_highscore.py @@ -0,0 +1,71 @@ +import os +import pathlib +import time + + +from selenium.webdriver.support.ui import Select + +from selenium.webdriver.common.by import By + +from tests.common import BlackBoxTestServer +from tests.common import login_data + +here = pathlib.Path(__file__).parent.absolute() +data_dir = here / "data" +backend_port = os.environ.get("BACKEND_PORT", 8000) +frontend_port = os.environ.get("FRONTEND_PORT", 8001) +frontend_address = f"http://localhost:{frontend_port}" + + +class FilterWorkoutsTest(BlackBoxTestServer): + + headless = True + fixtures = [ + data_dir / "users.json", + data_dir / "exercises.json", + data_dir / "workouts.json", + ] + + def wait_until_value_not_empty(self, by: By, locator: str): + self.wait.until(lambda _: self.get_input_value(by, locator) != "") + + def get_input_value(self, by: By, locator: str) -> str: + element = self.driver.find_element(by, locator) + self.assertIsNotNone(element) + return element.get_attribute("value") + + def test_user_can_view_highscores(self): + user = login_data["athlete"] + self.login(user) + self.get("highscores.html") + self.assertEqual("Highscores", self.driver.title) + time.sleep(2) # :) + highscores = self.driver.find_elements_by_css_selector( + '#highscore-content>tr') + self.assertEqual(len(highscores), 2) + + def test_highscore_ranking(self): + user = login_data["athlete"] + self.login(user) + self.get("highscores.html") + self.assertEqual("Highscores", self.driver.title) + time.sleep(2) # :) + highscores = self.driver.find_elements_by_css_selector( + '#highscore-content>tr') + self.assertEqual(highscores[0].text, '1 arya 2') + self.assertEqual(highscores[1].text, '2 syrio 1') + + def test_filter_highscore_on_exercise(self): + user = login_data["athlete"] + self.login(user) + self.get("highscores.html") + self.assertEqual("Highscores", self.driver.title) + time.sleep(2) # :) + select = Select((self.driver.find_element_by_id("filter-exercise"))) + select.select_by_visible_text("Fencing") + time.sleep(2) + highscores = self.driver.find_elements_by_css_selector( + '#highscore-content>tr' + ) + self.assertEqual(len(highscores), 1) + self.assertEqual(highscores[0].text, '1 arya 1 24') diff --git a/backend/secfit/tests/test_profile.py b/backend/secfit/tests/test_profile.py new file mode 100644 index 0000000000000000000000000000000000000000..8ce1f42a90051f5752d92568a18623caf7b91efe --- /dev/null +++ b/backend/secfit/tests/test_profile.py @@ -0,0 +1,55 @@ +import os +import pathlib + +from selenium.webdriver.common.by import By + +from tests.common import BlackBoxTestServer +from tests.common import login_data + +here = pathlib.Path(__file__).parent.absolute() +data_dir = here / "data" +backend_port = os.environ.get("BACKEND_PORT", 8000) +frontend_port = os.environ.get("FRONTEND_PORT", 8001) +frontend_address = f"http://localhost:{frontend_port}" + + +class ViewProfileTest(BlackBoxTestServer): + + headless = True + fixtures = [ + data_dir / "users.json", + ] + + def wait_until_value_not_empty(self, by: By, locator: str): + self.wait.until(lambda _: self.get_input_value(by, locator) != "") + + def get_input_value(self, by: By, locator: str) -> str: + element = self.driver.find_element(by, locator) + self.assertIsNotNone(element) + return element.get_attribute("value") + + def test_non_user_can_not_view_own_profile(self): + """Test if a user is able to view their own public workout.""" + self.get("profile.html") + self.assertEqual("My Profile", self.driver.title) + self.wait_until_value_not_empty(By.ID, "username") + username_el = self.driver.find_element_by_id('username') + email_el = self.driver.find_element_by_id('email') + username = username_el.text + email = email_el.text + self.assertEqual(username, '') + self.assertEqual(email, '') + + def test_user_can_view_own_profile(self): + """Test if a user is able to view their own public workout.""" + user = login_data["athlete"] + self.login(user) + self.get("profile.html") + self.assertEqual("My Profile", self.driver.title) + self.wait_until_value_not_empty(By.ID, "username") + username_el = self.driver.find_element_by_id('username') + email_el = self.driver.find_element_by_id('email') + username = username_el.text + email = email_el.text + self.assertEqual(username, 'Username: arya') + self.assertEqual(email, 'Email: arya@stark.io')