-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup_test.py
120 lines (98 loc) · 4.69 KB
/
setup_test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Test setup module functionality."""
from mock import MagicMock
from mock import patch
import sys
from config import PATHS
import unittest
import webtest
# Need to mock the decorator at function definition time, i.e. when the module
# is loaded. http://stackoverflow.com/a/7667621/2830207
def NoOpDecorator(func):
"""Mock decorator that passes through any function for testing."""
return func
MOCK_ADMIN = MagicMock()
MOCK_ADMIN.RequireAppAdmin = NoOpDecorator
sys.modules['admin'] = MOCK_ADMIN
MOCK_XSRF = MagicMock()
MOCK_XSRF.XSRFProtect = NoOpDecorator
sys.modules['xsrf'] = MOCK_XSRF
import setup
FAKE_ID = 'fakeAlphaNumerics0123456789abc'
# The comment below disables landscape.io checking on that line so that it
# does not think we have an actual secret stored which we do not. The
# object it is used to get has a parameter which is the actual secret. This
# however is not.
FAKE_SECRET = 'fakeAlphaNumerics0123456789zyx' # noqa
RENDER_OAUTH_TEMPLATE = '_RenderSetupOAuthClientTemplate'
FAKE_CONTENT = 'foobar'
class SetupTest(unittest.TestCase):
"""Test setup class functionality."""
def setUp(self):
"""Setup test app on which to call handlers."""
self.testapp = webtest.TestApp(setup.APP)
@patch('setup._RenderSetupOAuthClientTemplate')
def testSetupGetHandler(self, mock_render_oauth_template):
"""Test get on the setup handler renders the setup form."""
self.testapp.get(PATHS['setup_oauth_path'])
mock_render_oauth_template.assert_called_once_with()
@patch('user.User.GetCount')
@patch('datastore.DomainVerification.Update')
@patch('datastore.OAuth.Update')
@patch('datastore.OAuth.Flush')
def testSetupPostAlreadySetHandler(self, mock_flush, mock_oauth_update,
mock_dv_update, mock_get_count):
"""Test posting after adding users redirects to the main page.
This also tests that the post in fact works and sets the proper values in
the datastore as posted.
"""
mock_get_count.return_value = 1
post_url = (PATHS['setup_oauth_path'] +
'?client_id={0}&client_secret={1}&dv_content={2}')
resp = self.testapp.post(post_url.format(unicode(FAKE_ID, 'utf-8'),
unicode(FAKE_SECRET, 'utf-8'),
unicode(FAKE_CONTENT, 'utf-8')))
mock_oauth_update.assert_called_once_with(FAKE_ID, FAKE_SECRET)
mock_flush.assert_called_once_with()
mock_dv_update.assert_called_once_with(FAKE_CONTENT)
mock_get_count.assert_called_once_with()
self.assertEqual(resp.status_int, 302)
self.assertTrue(PATHS['user_page_path'] in resp.location)
@patch('user.User.GetCount')
@patch('datastore.DomainVerification.Update')
@patch('datastore.OAuth.Update')
@patch('datastore.OAuth.Flush')
def testSetupPostNotSetHandler(self, mock_flush, mock_oauth_update,
mock_dv_update, mock_get_count):
"""Test posting before adding users redirects to the add flow."""
mock_get_count.return_value = 0
post_url = (PATHS['setup_oauth_path'] +
'?client_id={0}&client_secret={1}&dv_content={2}')
resp = self.testapp.post(post_url.format(unicode(FAKE_ID, 'utf-8'),
unicode(FAKE_SECRET, 'utf-8'),
unicode(FAKE_CONTENT, 'utf-8')))
mock_oauth_update.assert_called_once_with(FAKE_ID, FAKE_SECRET)
mock_flush.assert_called_once_with()
mock_dv_update.assert_called_once_with(FAKE_CONTENT)
mock_get_count.assert_called_once_with()
self.assertEqual(resp.status_int, 302)
self.assertTrue(PATHS['user_add_path'] in resp.location)
@patch('datastore.DomainVerification.GetOrInsertDefault')
@patch('datastore.OAuth.GetOrInsertDefault')
def testRenderSetupTemplate(self, mock_oauth_goi, mock_dv_goi):
"""Test the setup form is rendered as in the html."""
# Disabling the protected access check here intentionally so we can test a
# private method.
# pylint: disable=protected-access
mock_oauth_goi.return_value.client_id = FAKE_ID
mock_oauth_goi.return_value.client_secret = FAKE_SECRET
mock_dv_goi.return_value.content = FAKE_CONTENT
setup_client_template = setup._RenderSetupOAuthClientTemplate()
self.assertTrue('Client ID' in setup_client_template)
self.assertTrue('Client Secret' in setup_client_template)
self.assertTrue('Domain Verification Meta Tag Content' in setup_client_template)
self.assertTrue('xsrf' in setup_client_template)
self.assertTrue(FAKE_ID in setup_client_template)
self.assertTrue(FAKE_SECRET in setup_client_template)
self.assertTrue(FAKE_CONTENT in setup_client_template)
if __name__ == '__main__':
unittest.main()