Skip to content

Commit 4584f67

Browse files
committed
restore tests/*
1 parent d520e1e commit 4584f67

26 files changed

Lines changed: 1255 additions & 0 deletions

tests/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9

tests/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# To run the tests locally
2+
Install uv : https://docs.astral.sh/uv/getting-started/installation/
3+
4+
Run the following commands:
5+
```bash
6+
uv sync --frozen
7+
uv run playwright install
8+
docker compose exec -e DJANGO_SUPERUSER_PASSWORD=codabench django python manage.py createsuperuser --username codabench --email codabench@test.mail --no-input
9+
uv run pytest test_auth.py test_account_creation.py test_competition.py test_submission.py
10+
```
11+
12+
# Adding Tests
13+
First, read the [documentation](https://playwright.dev/python/docs/writing-tests) on Playwright if you haven't used the tool before.
14+
Since we are using pytest, you should also try to get more familiar with it by reading some of its [documentation](https://docs.pytest.org/en/stable/getting-started.html).
15+
16+
17+
Once you are done, you can start adding tests. Playwright allows us to generate code with the following command :
18+
```bash
19+
uv run playwright codegen -o test.py
20+
```
21+
22+
This will open two windows:
23+
- A window containing the generated code
24+
- A browser that is used by playwright to generate the code. Every action you take there will generate new lines of the code.
25+
26+
Once you are done, close the browser and open the file that playwright created containing the code it generated. Make sure to test it to make it sure it works.
27+
28+
Since we are passing custom commands to pytest, we need to remove some of the generated lines. Spawning a new browser and/or context will make them not take into the commands we have added in the `pytest.ini` file :
29+
```python title="Original file created by codegen"
30+
from playwright.sync_api import Playwright, sync_playwright, expect
31+
32+
33+
def run(playwright: Playwright) -> None:
34+
browser = playwright.chromium.launch(headless=False)
35+
context = browser.new_context()
36+
page = context.new_page()
37+
page.goto("http://localhost/")
38+
page.get_by_text("Benchmarks/Competitions Datasets Login Sign-up").click()
39+
page.get_by_role("link", name="Login").click()
40+
41+
page.close()
42+
43+
# ---------------------
44+
context.close()
45+
browser.close()
46+
47+
48+
with sync_playwright() as playwright:
49+
run(playwright)
50+
```
51+
52+
The previous file becomes :
53+
```python title="Modified file"
54+
from playwright.sync_api import Page, expect
55+
56+
57+
def test_run(page: Page) -> None:
58+
page.goto("http://localhost/")
59+
page.get_by_text("Benchmarks/Competitions Datasets Login Sign-up").click()
60+
page.get_by_role("link", name="Login").click()
61+
```
62+

tests/config/config.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[database]
2+
host="localhost"
3+
name="postgres"
4+
user="postgres"
5+
password="postgres"
6+
port=5432
7+
8+
9+
[default_user]
10+
username="codabench"
11+
password="codabench"
12+
13+
[test_user]
14+
username="testname"
15+
password="testPassword"
16+
17+
[test_failed_user]
18+
username="test.name"

tests/main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def main():
2+
print("Hello from playwrightpython!")
3+
4+
5+
if __name__ == "__main__":
6+
main()

tests/pyproject.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[project]
2+
name = "tests"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.9"
7+
dependencies = [
8+
"loguru~=0.7.3",
9+
"psycopg[binary]~=3.2.13",
10+
"pytest~=8.4.2",
11+
"pytest-playwright~=0.7.1",
12+
"pytest-xdist~=3.8.0",
13+
"toml~=0.10.2",
14+
]

tests/pytest.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# content of pytest.ini
2+
[pytest]
3+
# Use localhost as default host
4+
# addopts = --base-url=localhost --headed --browser webkit --browser firefox --browser chromium --numprocesses 2
5+
addopts = --base-url=http://localhost:8000 --browser firefox --screenshot only-on-failure --full-page-screenshot
6+
log_cli = true
7+
log_cli_level = INFO

tests/test_account_creation.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import psycopg
2+
import toml
3+
from loguru import logger
4+
from playwright.sync_api import expect, Page
5+
import random
6+
7+
8+
def randomNumber():
9+
return str(random.randint(0, 1000))
10+
11+
12+
data = toml.load("config/config.toml")
13+
test_user = data["test_user"]["username"] + randomNumber()
14+
test_password = data["test_user"]["password"] + randomNumber()
15+
test_email = test_user + "@email.com"
16+
test_failed_user = data["test_failed_user"]["username"]
17+
db_host = data["database"]["host"]
18+
db_user = data["database"]["user"]
19+
db_name = data["database"]["name"]
20+
db_password = data["database"]["password"]
21+
22+
23+
def test_FailedAccountCreationUsername(page: Page):
24+
page.goto("/")
25+
page.get_by_role("link", name="Sign-up").click()
26+
page.get_by_role("textbox", name="username").click()
27+
page.get_by_role("textbox", name="username").fill(test_failed_user)
28+
page.get_by_role("textbox", name="email").click()
29+
page.get_by_role("textbox", name="email").fill(test_failed_user + test_email)
30+
page.get_by_role("textbox", name="password", exact=True).click()
31+
page.get_by_role("textbox", name="password", exact=True).fill(test_password)
32+
page.get_by_role("textbox", name="confirm password").click()
33+
page.get_by_role("textbox", name="confirm password").fill(test_password)
34+
page.get_by_role("checkbox", name="I accept the terms and").check()
35+
page.get_by_role("button", name="Sign Up").click()
36+
expect(
37+
page.get_by_text("Username can only contain lowercase letters,")
38+
).to_be_visible()
39+
40+
41+
def test_account_creation(page: Page):
42+
page.goto("/")
43+
page.get_by_role("link", name="Sign-up").click()
44+
page.get_by_role("textbox", name="username").click()
45+
page.get_by_role("textbox", name="username").fill(test_user)
46+
page.get_by_role("textbox", name="email").click()
47+
page.get_by_role("textbox", name="email").fill(test_email)
48+
page.get_by_role("textbox", name="password", exact=True).click()
49+
page.get_by_role("textbox", name="password", exact=True).fill(test_password)
50+
page.get_by_role("textbox", name="confirm password").click()
51+
page.get_by_role("textbox", name="confirm password").fill(test_password)
52+
page.get_by_role("checkbox", name="I accept the terms and").check()
53+
page.get_by_role("button", name="Sign Up").click()
54+
expect(page.get_by_text(f"Dear {test_user}, please go to")).to_be_visible()
55+
56+
found = "false"
57+
with psycopg.connect(
58+
f"dbname={db_name} user={db_user} host={db_host} password={db_password}"
59+
) as conn:
60+
# Open a cursor to perform database operations
61+
with conn.cursor() as cur:
62+
# Execute a command: this creates a new table
63+
cur.execute("SELECT username FROM profiles_user;")
64+
for row in cur:
65+
logger.debug(row[0])
66+
if row[0] == test_user:
67+
assert row[0] == test_user
68+
found = "true"
69+
break
70+
if found != "true":
71+
assert 0, "Username not found in database"
72+
73+
74+
def activate_account():
75+
with psycopg.connect(
76+
f"dbname={db_name} user={db_user} host={db_host} password={db_password}"
77+
) as conn:
78+
# Open a cursor to perform database operations
79+
with conn.cursor() as cur:
80+
# Execute a command: this creates a new table
81+
cur.execute(
82+
f"UPDATE profiles_user SET is_active = 't' WHERE username = '{test_user}';"
83+
)
84+
cur.execute(
85+
f"SELECT is_active FROM profiles_user WHERE username = '{test_user}';"
86+
)
87+
for row in cur:
88+
logger.debug(str(row))
89+
if str(row) not in "(True,)":
90+
assert 0, "Activation unsuccesful"
91+
92+
93+
def test_loginIntoActivatedAccount(page: Page):
94+
activate_account()
95+
page.goto("/")
96+
page.get_by_role("link", name="Login").click()
97+
page.get_by_role("textbox", name="username or email").click()
98+
page.get_by_role("textbox", name="username or email").fill(test_user)
99+
page.get_by_role("textbox", name="password").click()
100+
page.get_by_role("textbox", name="password").fill(test_password)
101+
page.get_by_role("button", name="Log In").click()
102+
expect(page.get_by_text(test_user)).to_be_visible()

tests/test_auth.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from playwright.sync_api import Browser
2+
import toml
3+
import pytest
4+
5+
data = toml.load("config/config.toml")
6+
7+
8+
@pytest.mark.only_browser("firefox")
9+
def test_auth(browser: Browser) -> None:
10+
context = browser.new_context()
11+
page = context.new_page()
12+
page.goto("http://localhost:8000")
13+
page.get_by_role("link", name="Login").click()
14+
page.get_by_role("textbox", name="username or email").click()
15+
page.get_by_role("textbox", name="username or email").fill(
16+
data["default_user"]["username"]
17+
)
18+
page.get_by_role("textbox", name="password").click()
19+
page.get_by_role("textbox", name="password").fill(data["default_user"]["password"])
20+
page.get_by_role("button", name="Log In").click()
21+
context.storage_state(path="config/state.json")
22+
# ---------------------
23+
context.close()

0 commit comments

Comments
 (0)