def test_confirm_u2f_success(self): with mocker_context() as mocker: mocker.patch('django.http.request.HttpRequest.is_secure') self.client.post('/control/settings/2fa/add', { 'devicetype': 'u2f', 'name': 'Foo' }, follow=True) m = self.monkeypatch m.setattr("u2flib_server.u2f.complete_register", lambda *args, **kwargs: (JSONDict({}), None)) d = U2FDevice.objects.first() r = self.client.post('/control/settings/2fa/u2f/{}/confirm'.format( d.pk), { 'token': 'FOO', 'activate': 'on' }, follow=True) d.refresh_from_db() assert d.confirmed assert 'alert-success' in r.content.decode() self.user.refresh_from_db() assert self.user.require_2fa m.undo()
def test_u2f_valid(self): m = self.monkeypatch m.setattr("u2flib_server.u2f.verify_authenticate", lambda *args, **kwargs: True) m.setattr("u2flib_server.u2f.start_authenticate", lambda *args, **kwargs: JSONDict({'authenticateRequests': []})) d = U2FDevice.objects.create(user=self.user, name='test', json_data="{}") response = self.client.get('/control/login/2fa') assert 'token' in response.rendered_content response = self.client.post('/control/login/2fa'.format(d.pk), { 'token': '{"response": "true"}' }) self.assertEqual(response.status_code, 302) self.assertIn('/control/', response['Location']) m.undo()
def test_wrap(self): self.assertTrue(isinstance(JSONDict.wrap({}), JSONDict))
def test_getattr(self): self.assertEqual(1, JSONDict(a=1).a)
def test_getattr_unknown(self): self.assertRaises(AttributeError, lambda: JSONDict().foo)
def test_json(self): self.assertEqual('{}', JSONDict().json) self.assertEqual('{"a": 1}', JSONDict(a=1).json)
def test_create_from_kwargs(self): self.assertEqual({'a': 1, 'b': 2}, JSONDict(a=1, b=2))
def test_create_from_dict(self): self.assertEqual({'a': 1, 'b': 2}, JSONDict({'a': 1, 'b': 2}))
def test_create_from_unicode(self): self.assertEqual({'a': 1, 'b': 2}, JSONDict(u'{"a":1,"b":2}'))
def test_create_from_bytes(self): self.assertEqual({'a': 1, 'b': 2}, JSONDict(b'{"a":1,"b":2}'))
def test_create(self): self.assertEqual({}, JSONDict())