Skip to content

Commit

Permalink
test: added scenarios for the file upload feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nekhvoya authored and supakeen committed Aug 2, 2023
1 parent 1f3d45a commit 8ed2c3c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 3 deletions.
12 changes: 10 additions & 2 deletions test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from test.e2e.env_config import PORT
from test.e2e.pageobjects.create_paste_page import CreatePastePage
from typing import Generator

import pytest
from playwright.sync_api import Page

Expand All @@ -16,7 +15,16 @@ def application() -> Generator[None, None, None]:
# Before All
log.info(f"Starting Pinnwand application on port {PORT}")
proc = subprocess.Popen(
[sys.executable, "-m", "pinnwand", "http", "--port", str(PORT)]
[
sys.executable,
"-m",
"pinnwand",
"--configuration-path",
"test/e2e/pinnwand.toml",
"http",
"--port",
str(PORT),
]
)
yield
# After All
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/pageobjects/create_paste_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, page: Page) -> None:
self.paste_input = page.locator(".file-part textarea")
self.submit_button = page.locator(".paste-submit button[type=submit]")
self.add_another_paste_button = page.locator("button.add")
self.file_input = page.locator("#file-input")

def open(self):
log.info(f"Opening Pinnwand at {self.url}")
Expand All @@ -35,6 +36,10 @@ def click_add_another_file_button(self):
log.info("Clicking Add Another Paste Button")
self.add_another_paste_button.click()

def add_file_to_file_input(self, file_paths):
log.info("Adding file for uploading")
self.file_input.set_input_files(file_paths)

# Step sequences
def paste_random_text(self, paste_number=0):
paste_text = string.ascii_letters + string.digits
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/pinnwand.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ratelimit.read.capacity = 200
ratelimit.read.consume = 1
ratelimit.read.refill = 10

ratelimit.create.capacity = 200
ratelimit.create.consume = 1
ratelimit.create.refill = 10

ratelimit.delete.capacity = 200
ratelimit.delete.consume = 1
ratelimit.delete.refill = 10
1 change: 0 additions & 1 deletion test/e2e/testscenarios/test_delete_paste.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from test.e2e.conftest import create_paste_page
from test.e2e.pageobjects.create_paste_page import CreatePastePage
from test.e2e.pageobjects.error_page import ErrorPage
from test.e2e.pageobjects.view_paste_page import ViewPastePage
Expand Down
40 changes: 40 additions & 0 deletions test/e2e/testscenarios/test_upload_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from test.e2e.pageobjects.create_paste_page import CreatePastePage
from test.e2e.pageobjects.view_paste_page import ViewPastePage
from test.e2e.utils.file_utils import create_random_file
import pytest
from playwright.sync_api import Page


@pytest.mark.e2e
def test_upload_single_file(page: Page, create_paste_page: CreatePastePage):
with create_random_file() as temp:
file_content = temp.read().decode()
create_paste_page.add_file_to_file_input(temp.name)
create_paste_page.should_have_value_in_paste_input(file_content)
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()
view_paste_page.should_have_pasted_text(file_content)


@pytest.mark.e2e
def test_upload_multiple_files(page: Page, create_paste_page: CreatePastePage):
with create_random_file() as first_temp, create_random_file() as second_temp:
first_file_content = first_temp.read().decode()
second_file_content = second_temp.read().decode()
create_paste_page.add_file_to_file_input(
[first_temp.name, second_temp.name]
)
create_paste_page.should_have_value_in_paste_input(
first_file_content, paste_number=0
)
create_paste_page.should_have_value_in_paste_input(
second_file_content, paste_number=1
)
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()
view_paste_page.should_have_pasted_text(first_file_content, paste_number=0)
view_paste_page.should_have_pasted_text(second_file_content, paste_number=1)
16 changes: 16 additions & 0 deletions test/e2e/utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from test.e2e.utils.string_utils import random_string
import tempfile
import os
from contextlib import contextmanager


@contextmanager
def create_random_file():
temp = tempfile.NamedTemporaryFile(suffix=".txt", delete=False)
temp.write(random_string().encode())
temp.seek(0)
try:
yield temp
finally:
temp.close()
os.remove(temp.name)
11 changes: 11 additions & 0 deletions test/e2e/utils/string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import random
import string


def random_string(size=1000) -> str:
return "".join(
[
random.choice(string.ascii_letters + string.digits)
for i in range(size)
]
)

0 comments on commit 8ed2c3c

Please sign in to comment.