|
| 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() |
0 commit comments