clipboard
This commit is contained in:
@@ -197,6 +197,88 @@ class MainAppTest(unittest.TestCase):
|
||||
self.assertEqual(hourly.status_code, 200)
|
||||
self.assertEqual(len(hourly.get_json()), 1)
|
||||
|
||||
def test_clipboard_requires_login(self):
|
||||
response = self.client.get("/clipboard")
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertIn("/login", response.headers["Location"])
|
||||
|
||||
def test_clipboard_page_shows_upload_form(self):
|
||||
self.login()
|
||||
response = self.client.get("/clipboard")
|
||||
html = response.get_data(as_text=True)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn('action="/clipboard/upload"', html)
|
||||
self.assertIn('name="file"', html)
|
||||
|
||||
def test_clipboard_upload_download_delete_flow(self):
|
||||
import io
|
||||
from sqlalchemy import func
|
||||
from user_models import ClipboardFile, User, user_db
|
||||
|
||||
self.login()
|
||||
upload = self.client.post(
|
||||
"/clipboard/upload",
|
||||
data={
|
||||
"file": (io.BytesIO(b"clipboard payload"), "hello.txt", "text/plain")
|
||||
},
|
||||
content_type="multipart/form-data",
|
||||
follow_redirects=False,
|
||||
)
|
||||
self.assertEqual(upload.status_code, 302)
|
||||
|
||||
with self.main_app.app_context():
|
||||
user = User.query.filter_by(username="tester").one()
|
||||
stored = (
|
||||
ClipboardFile.query.filter_by(user_id=user.id)
|
||||
.order_by(ClipboardFile.id.desc())
|
||||
.first()
|
||||
)
|
||||
self.assertIsNotNone(stored)
|
||||
file_id = stored.id
|
||||
original_name = stored.original_name
|
||||
size = stored.size
|
||||
total = (
|
||||
user_db.session.query(func.coalesce(func.sum(ClipboardFile.size), 0))
|
||||
.filter(ClipboardFile.user_id == user.id)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
self.assertEqual(original_name, "hello.txt")
|
||||
self.assertEqual(size, len(b"clipboard payload"))
|
||||
self.assertEqual(total, len(b"clipboard payload"))
|
||||
|
||||
download = self.client.get(f"/clipboard/{file_id}/download")
|
||||
self.assertEqual(download.status_code, 200)
|
||||
self.assertEqual(download.data, b"clipboard payload")
|
||||
|
||||
delete = self.client.post(f"/clipboard/{file_id}/delete")
|
||||
self.assertEqual(delete.status_code, 302)
|
||||
|
||||
with self.main_app.app_context():
|
||||
user_db.session.expire_all()
|
||||
self.assertEqual(ClipboardFile.query.filter_by(id=file_id).count(), 0)
|
||||
|
||||
def test_notifications_can_be_deleted(self):
|
||||
from user_models import Notification, User, user_db
|
||||
|
||||
self.login()
|
||||
with self.main_app.app_context():
|
||||
user = User.query.filter_by(username="tester").one()
|
||||
first = Notification(user_id=user.id, message="first notification")
|
||||
second = Notification(user_id=user.id, message="second notification")
|
||||
user_db.session.add_all([first, second])
|
||||
user_db.session.commit()
|
||||
first_id = first.id
|
||||
second_id = second.id
|
||||
|
||||
delete = self.client.post(f"/notifications/{first_id}/delete")
|
||||
self.assertEqual(delete.status_code, 302)
|
||||
|
||||
with self.main_app.app_context():
|
||||
user_db.session.expire_all()
|
||||
self.assertEqual(Notification.query.filter_by(id=first_id).count(), 0)
|
||||
self.assertEqual(Notification.query.filter_by(id=second_id).count(), 1)
|
||||
|
||||
def test_notes_require_login(self):
|
||||
response = self.client.get("/notes")
|
||||
self.assertEqual(response.status_code, 302)
|
||||
@@ -332,11 +414,12 @@ class MainAppTest(unittest.TestCase):
|
||||
'href="/home"',
|
||||
'href="/sensors"',
|
||||
'href="/notes"',
|
||||
'href="/clipboard"',
|
||||
'href="/notifications"',
|
||||
'href="/api/docs"',
|
||||
'href="/logout"',
|
||||
)
|
||||
for path in ("/home", "/sensors", "/notes", "/notifications", "/api/docs"):
|
||||
for path in ("/home", "/sensors", "/notes", "/clipboard", "/notifications", "/api/docs"):
|
||||
html = self.client.get(path).get_data(as_text=True)
|
||||
for link in links:
|
||||
self.assertIn(link, html, f"{link} missing on {path}")
|
||||
|
||||
Reference in New Issue
Block a user