Created
April 27, 2015 22:42
-
-
Save bsmithgall/ae2e0299c62ccaa09d68 to your computer and use it in GitHub Desktop.
Testing w/Mocks, Persona
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase | |
from mock import Mock, patch | |
class BaseTestCase(TestCase) | |
@patch('urllib2.urlopen') | |
def login_user(self, user, urlopen): | |
_email = user.email if user else '[email protected]' | |
mock_open = Mock() | |
mock_open.read.side_effect = ['{"status": "okay", "email": "' + _email + '"}'] | |
urlopen.return_value = mock_open | |
self.client.post('/users/auth', data=dict( | |
assertion='test' | |
)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from base_test import BaseTestCase | |
class TestAccess(BaseTestCase): | |
def test_no_role_access(self): | |
# test that it properly redirects to the login page for anonymous users | |
request = self.client.get('/admin/') | |
self.assertTrue(request.status_code, 302) | |
self.assertEquals(request.location, 'http://localhost/users/login?next=%2Fadmin%2F') | |
def test_admin_role_access(self): | |
# test that it works properly for admin users | |
self.login_user(self.admin_user) | |
request = self.client.get('/admin/') | |
self.assert200(request) | |
def test_superadmin_role_access(self): | |
# test that it works properly for superadmin users | |
self.login_user(self.superadmin_user) | |
request = self.client.get('/admin/') | |
self.assert200(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment