37 lines
1.9 KiB
Python
37 lines
1.9 KiB
Python
"""No Google account is needed to validate safe link rendering."""
|
|
import json
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
from unittest import TestCase
|
|
from .google_shortcuts import google_shortcut_url, render_google_shortcut
|
|
|
|
class GoogleShortcutTests(TestCase):
|
|
def check(self, ext, url, accepted):
|
|
with TemporaryDirectory() as temp:
|
|
path = Path(temp) / f'example{ext}'
|
|
path.write_text(json.dumps({'url': url}), encoding='utf-8')
|
|
if accepted:
|
|
self.assertEqual(google_shortcut_url(path),url)
|
|
html = render_google_shortcut(path)
|
|
self.assertIn('Ouvrir sur Google', html)
|
|
self.assertIn('rel="noopener noreferrer"',html)
|
|
else:
|
|
with self.assertRaises(ValueError): google_shortcut_url(path)
|
|
|
|
def test_google_shortcuts(self):
|
|
self.check('.gdoc', 'https://docs.google.com/document/d/AbcDefGhijkLMN123/edit', True)
|
|
self.check('.gsheet', 'https://docs.google.com/spreadsheets/d/AbcDefGhijkLMN123/edit', True)
|
|
self.check('.gslides', 'https://docs.google.com/presentation/d/AbcDefGhijkLMN123/edit', True)
|
|
self.check('.gdoc', 'https://docs.google.com/spreadsheets/d/AbcDefGhijkLMN123/edit', False)
|
|
self.check('.gdoc', 'https://docs.google.com.evil.test/document/d/AbcDefGhijkLMN123/edit', False)
|
|
self.check('.gdoc', 'http://docs.google.com/document/d/AbcDefGhijkLMN123/edit', False)
|
|
self.check('.gdoc', 'https://drive.google.com/open?id=AbcDefGhijkLMN123', True)
|
|
|
|
def test_reject_invalid_contents(self):
|
|
with TemporaryDirectory() as temp:
|
|
path = Path(temp) / 'broken.gdoc'
|
|
path.write_text('{broken',encoding='utf-8')
|
|
with self.assertRaises(ValueError): google_shortcut_url(path)
|
|
path.write_text('x'*70000,encoding='utf-8')
|
|
with self.assertRaises(ValueError): google_shortcut_url(path)
|