-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_flask_share.py
92 lines (71 loc) · 3.2 KB
/
test_flask_share.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
test_flask_share
~~~~~~~~~~~~~~~~
Create social share component in Jinja2 template based on share.js.
:author: Grey Li <[email protected]>
:copyright: (c) 2017 by Grey Li.
:license: MIT, see LICENSE for more details.
"""
import unittest
from flask import Flask, render_template_string, current_app
from flask_share import Share
class ShareTestCase(unittest.TestCase):
def setUp(self):
self.mobile_agent = {'HTTP_USER_AGENT': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) \
AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'}
app = Flask(__name__)
app.testing = True
self.share = Share(app)
@app.route('/')
def index():
return render_template_string('{{ share.load() }}\n{{ share.create() }}')
self.context = app.app_context()
self.context.push()
self.client = app.test_client()
def tearDown(self):
self.context.pop()
def test_config(self):
self.assertIn('SHARE_SITES', current_app.config)
self.assertIn('SHARE_MOBILE_SITES', current_app.config)
self.assertIn('SHARE_HIDE_ON_MOBILE', current_app.config)
self.assertIn('SHARE_SERVE_LOCAL', current_app.config)
self.assertEqual(current_app.config['SHARE_HIDE_ON_MOBILE'], False)
def test_load(self):
rv = self.share.load()
self.assertIn('https://cdn.bootcss.com', rv)
self.assertIn('social-share.min.js', rv)
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('social-share.min.js', data)
def test_create(self):
rv = self.share.create()
self.assertIn('<div class="social-share', rv)
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('<div class="social-share', data)
def test_custom_sites(self):
current_app.config['SHARE_SITES'] = 'twiter, facebook'
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('data-sites="twiter, facebook"', data)
def test_custom_mobile_sites(self):
current_app.config['SHARE_MOBILE_SITES'] = 'twitter'
response = self.client.get('/', environ_base=self.mobile_agent)
data = response.get_data(as_text=True)
self.assertIn('data-mobile-sites="twitter"', data)
def test_hide_on_mobile_config(self):
current_app.config['SHARE_HIDE_ON_MOBILE'] = True
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('social-share.min.js', data)
self.assertIn('<div class="social-share', data)
def test_create_on_mobile(self):
current_app.config['SHARE_HIDE_ON_MOBILE'] = True
response = self.client.get('/', environ_base=self.mobile_agent)
data = response.get_data(as_text=True)
self.assertIn('social-share.min.js', data)
self.assertNotIn('<div class="social-share', data)
def test_local_resources(self):
current_app.config['SHARE_SERVE_LOCAL'] = True
response = self.client.get('/share/static/css/share.min.css')
self.assertNotEqual(response.status_code, 404)