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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
import six
import random
import unittest
from sure import expect
from social.strategies.base import BaseStrategy
from social.storage.base import UserMixin, NonceMixin, AssociationMixin, \
CodeMixin, BaseStorage
from social.tests.models import User
class BrokenUser(UserMixin):
pass
class BrokenAssociation(AssociationMixin):
pass
class BrokenNonce(NonceMixin):
pass
class BrokenCode(CodeMixin):
pass
class BrokenStrategy(BaseStrategy):
pass
class BrokenStrategyWithSettings(BrokenStrategy):
def get_setting(self, name):
raise AttributeError()
class BrokenStorage(BaseStorage):
pass
class BrokenUserTests(unittest.TestCase):
def setUp(self):
self.user = BrokenUser
def tearDown(self):
self.user = None
def test_get_username(self):
self.user.get_username.when.called_with(User('foobar')).should.throw(
NotImplementedError, 'Implement in subclass'
)
def test_user_model(self):
self.user.user_model.when.called_with().should.throw(
NotImplementedError, 'Implement in subclass'
)
def test_username_max_length(self):
self.user.username_max_length.when.called_with().should.throw(
NotImplementedError, 'Implement in subclass'
)
def test_get_user(self):
self.user.get_user.when.called_with(1).should.throw(
NotImplementedError, 'Implement in subclass'
)
def test_get_social_auth(self):
self.user.get_social_auth.when.called_with('foo', 1).should.throw(
NotImplementedError, 'Implement in subclass'
)
def test_get_social_auth_for_user(self):
self.user.get_social_auth_for_user.when.called_with(User('foobar')) \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_create_social_auth(self):
self.user.create_social_auth.when \
.called_with(User('foobar'), 1, 'foo') \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_disconnect(self):
self.user.disconnect\
.when.called_with(BrokenUser())\
.should.throw(NotImplementedError, 'Implement in subclass')
class BrokenAssociationTests(unittest.TestCase):
def setUp(self):
self.association = BrokenAssociation
def tearDown(self):
self.association = None
def test_store(self):
self.association.store.when \
.called_with('http://foobar.com', BrokenAssociation()) \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_get(self):
self.association.get.when.called_with() \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_remove(self):
self.association.remove.when.called_with([1, 2, 3]) \
.should.throw(NotImplementedError, 'Implement in subclass')
class BrokenNonceTests(unittest.TestCase):
def setUp(self):
self.nonce = BrokenNonce
def tearDown(self):
self.nonce = None
def test_use(self):
self.nonce.use.when \
.called_with('http://foobar.com', 1364951922, 'foobar123') \
.should.throw(NotImplementedError, 'Implement in subclass')
class BrokenCodeTest(unittest.TestCase):
def setUp(self):
self.code = BrokenCode
def tearDown(self):
self.code = None
def test_get_code(self):
self.code.get_code.when \
.called_with('foobar') \
.should.throw(NotImplementedError, 'Implement in subclass')
class BrokenStrategyTests(unittest.TestCase):
def setUp(self):
self.strategy = BrokenStrategy(storage=BrokenStorage)
def tearDown(self):
self.strategy = None
def test_redirect(self):
self.strategy.redirect.when \
.called_with('http://foobar.com') \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_get_setting(self):
self.strategy.get_setting.when \
.called_with('foobar') \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_html(self):
self.strategy.html.when \
.called_with('<p>foobar</p>') \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_request_data(self):
self.strategy.request_data.when \
.called_with() \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_request_host(self):
self.strategy.request_host.when \
.called_with() \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_session_get(self):
self.strategy.session_get.when \
.called_with('foobar') \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_session_set(self):
self.strategy.session_set.when \
.called_with('foobar', 123) \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_session_pop(self):
self.strategy.session_pop.when \
.called_with('foobar') \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_build_absolute_uri(self):
self.strategy.build_absolute_uri.when \
.called_with('/foobar') \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_render_html_with_tpl(self):
self.strategy.render_html.when \
.called_with('foobar.html', context={}) \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_render_html_with_html(self):
self.strategy.render_html.when \
.called_with(html='<p>foobar</p>', context={}) \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_render_html_with_none(self):
self.strategy.render_html.when \
.called_with() \
.should.throw(ValueError, 'Missing template or html parameters')
def test_is_integrity_error(self):
self.strategy.storage.is_integrity_error.when \
.called_with(None) \
.should.throw(NotImplementedError, 'Implement in subclass')
def test_random_string(self):
expect(isinstance(self.strategy.random_string(), six.string_types)) \
.to.equal(True)
def test_random_string_without_systemrandom(self):
def SystemRandom():
raise NotImplementedError()
orig_random = getattr(random, 'SystemRandom', None)
random.SystemRandom = SystemRandom
strategy = BrokenStrategyWithSettings(storage=BrokenStorage)
expect(isinstance(strategy.random_string(), six.string_types)) \
.to.equal(True)
random.SystemRandom = orig_random
|