common.py 3.46 KB
Newer Older
Einar Uvsløkk's avatar
Einar Uvsløkk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import enum
import os
from dataclasses import dataclass
from urllib.parse import urljoin

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

backend_port = os.environ.get("BACKEND_PORT", 8000)
frontend_port = os.environ.get("FRONTEND_PORT", 8001)
frontend_address = f"http://localhost:{frontend_port}"
webdriver_path = os.environ.get("WEBDRIVER", "chromedriver")


class Visibility(str, enum.Enum):
and's avatar
and committed
17
18
    """Enum representing workout visibility."""

Einar Uvsløkk's avatar
Einar Uvsløkk committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    PUBLIC = "PU"
    COACH = "CO"
    PRIVATE = "PR"


@dataclass
class LoginData(dict):
    username: str
    password: str


login_data = {
    "athlete": LoginData("arya", "dOnOtCaLlMeMiLaDy"),
    "coach": LoginData("syrio", "wHaTdOwEsAyToThEgOdOfDeAtH"),
}


class BlackBoxTestServer(LiveServerTestCase):
    """Base class for running black box tests with Selenium.

    The frontend needs be started before running the tests. The frontend port
    can be specified using the environment variable FRONTEND_PORT. If this
    environment variable is not present, the test case expect the frontend to
    be running on port 8001.

    Likewise, the port for the backend test server can be specified using the
    BACKEND_PORT environment variable, which defaults to 8000.

    Examples:

    The following examples assumes you stand in the root folder of repo.

    Run tests with default values:

        (cd frontend && cordova run browser --port=8001)
        (cd backend/secfit && python manage.py test tests)

    Run tests with backend listening on port 12345:

        (
            cd frontend &&
            BACKEND_HOST=http://localhost:12345 \
                cordova run browser --port=8001
        )
        (
            cd backend/secfit &&
            BACKEND_PORT=12345 python manage.py test tests
        )
    """

    port = backend_port
    timeout = 5
    headless = False

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        options = webdriver.ChromeOptions()
        options.headless = cls.headless
        cls.driver = webdriver.Chrome(webdriver_path, options=options)
        cls.wait = WebDriverWait(cls.driver, cls.timeout)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        super().tearDownClass()

    @property
    def address(self):
        return frontend_address

    def get(self, page: str, **params):
        """Navigate to a given web page.

        Args:
            page (str): The web page to navigate to.
            **params: Arbitrary keyword arguments used to create and attach a
                query string to the web page URL.
        """
        url = urljoin(self.address, page)
        if params:
            query_str = "&".join([f"{k}={v}" for k, v in params.items()])
            url = f"{url}?{query_str}"
        self.driver.get(url)

    def login(self, user: LoginData):
        self.get("login.html")
        self.assertEqual("Login", self.driver.title)
        username_input = self.driver.find_element_by_name("username")
        username_input.send_keys(user.username)
        password_input = self.driver.find_element_by_name("password")
        password_input.send_keys(user.password)
        self.driver.find_element_by_id("btn-login").click()
        self.wait.until(lambda driver: driver.title == "Workouts")

    def logout(self):
        self.get("logout.html")
        self.assertEqual("Logout", self.driver.title)
        self.wait.until(lambda driver: driver.title == "Home")